Context manager for transactional reference updates. A transaction allows multiple reference updates to be performed atomically. All updates are applied when the transaction is committed, or none are applied if the transaction is rolled back. Example: with repo.transaction(
| 38 | |
| 39 | |
| 40 | class ReferenceTransaction: |
| 41 | """Context manager for transactional reference updates. |
| 42 | |
| 43 | A transaction allows multiple reference updates to be performed atomically. |
| 44 | All updates are applied when the transaction is committed, or none are applied |
| 45 | if the transaction is rolled back. |
| 46 | |
| 47 | Example: |
| 48 | with repo.transaction() as txn: |
| 49 | txn.lock_ref('refs/heads/master') |
| 50 | txn.set_target('refs/heads/master', new_oid, message='Update master') |
| 51 | # Changes committed automatically on context exit |
| 52 | """ |
| 53 | |
| 54 | def __init__(self, repository: BaseRepository) -> None: |
| 55 | self._repository = repository |
| 56 | self._transaction = ffi.new('git_transaction **') |
| 57 | self._tx = None |
| 58 | self._thread_id = threading.get_ident() |
| 59 | |
| 60 | err = C.git_transaction_new(self._transaction, repository._repo) |
| 61 | check_error(err) |
| 62 | self._tx = self._transaction[0] |
| 63 | |
| 64 | def _check_thread(self) -> None: |
| 65 | """Verify transaction is being used from the same thread that created it.""" |
| 66 | current_thread = threading.get_ident() |
| 67 | if current_thread != self._thread_id: |
| 68 | raise RuntimeError( |
| 69 | f'Transaction created in thread {self._thread_id} ' |
| 70 | f'but used in thread {current_thread}. ' |
| 71 | 'Transactions must be used from the thread that created them.' |
| 72 | ) |
| 73 | |
| 74 | def lock_ref(self, refname: str) -> None: |
| 75 | """Lock a reference in preparation for updating it. |
| 76 | |
| 77 | Args: |
| 78 | refname: Name of the reference to lock (e.g., 'refs/heads/master') |
| 79 | """ |
| 80 | self._check_thread() |
| 81 | if self._tx is None: |
| 82 | raise ValueError('Transaction already closed') |
| 83 | |
| 84 | c_refname = ffi.new('char[]', to_bytes(refname)) |
| 85 | err = C.git_transaction_lock_ref(self._tx, c_refname) |
| 86 | check_error(err) |
| 87 | |
| 88 | def set_target( |
| 89 | self, |
| 90 | refname: str, |
| 91 | target: Oid | str, |
| 92 | signature: Signature | None = None, |
| 93 | message: str | None = None, |
| 94 | ) -> None: |
| 95 | """Set the target of a direct reference. |
| 96 | |
| 97 | The reference must be locked first via lock_ref(). |
no outgoing calls
no test coverage detected
searching dependent graphs…