Journal one shim-owned Git ref move before it becomes visible, perform the move, then record its leased receipt and verify the operation. Journal failure returns an error before the write is performed; an unexpected ref value after the write appends a durable rejection receipt and fails closed.
(
repo: &Repository,
working_copy: WorkingCopyId,
git: &GitRepository,
target_ref: &str,
commit: Oid,
message: &str,
)
| 49 | /// unexpected ref value after the write appends a durable rejection receipt |
| 50 | /// and fails closed. |
| 51 | pub(crate) fn journal_and_move_branch( |
| 52 | repo: &Repository, |
| 53 | working_copy: WorkingCopyId, |
| 54 | git: &GitRepository, |
| 55 | target_ref: &str, |
| 56 | commit: Oid, |
| 57 | message: &str, |
| 58 | ) -> CliResult<()> { |
| 59 | let observed_old = git |
| 60 | .find_reference(target_ref) |
| 61 | .ok() |
| 62 | .and_then(|reference| reference.target()) |
| 63 | .and_then(canonical_ref_target); |
| 64 | let intended_new = GitRefTarget::Direct( |
| 65 | GitObjectId::new(GitHashAlgorithm::Sha1, commit.as_bytes().to_vec()) |
| 66 | .map_err(|error| git_error(error.to_string()))?, |
| 67 | ); |
| 68 | // An already-satisfied move is a no-op: there is no transition to journal |
| 69 | // and no effect to lease (the journal refuses identical old/new values by |
| 70 | // construction). |
| 71 | if observed_old == Some(intended_new.clone()) { |
| 72 | return Ok(()); |
| 73 | } |
| 74 | let evidence = |
| 75 | atomic_core::Hash::of(format!("atomic:bridge-ref:v1\0{target_ref}\0{commit}").as_bytes()); |
| 76 | // Journal-before-visibility: the operation (ID + intended effect) is |
| 77 | // durable before the ref write. Any failure here prevents the write. |
| 78 | let prepared = repo |
| 79 | .prepare_shim_git_ref_write( |
| 80 | working_copy, |
| 81 | target_ref, |
| 82 | observed_old.clone(), |
| 83 | intended_new.clone(), |
| 84 | evidence, |
| 85 | ) |
| 86 | .map_err(|error| { |
| 87 | git_error(format!( |
| 88 | "cannot journal '{target_ref}' before writing: {error}" |
| 89 | )) |
| 90 | })?; |
| 91 | |
| 92 | // Review R3: the move runs under a REAL Git reference transaction — the |
| 93 | // ref is locked, the observed old value is re-compared under Git's own |
| 94 | // ref lock, and a symbolic ref (or any moved target) fails the |
| 95 | // transaction instead of being observed-then-set. |
| 96 | { |
| 97 | let mut transaction = git.transaction().map_err(|error| { |
| 98 | git_error(format!("cannot start a Git reference transaction: {error}")) |
| 99 | })?; |
| 100 | transaction.lock_ref(target_ref).map_err(|error| { |
| 101 | git_error(format!( |
| 102 | "cannot lock '{target_ref}' for the transaction: {error}" |
| 103 | )) |
| 104 | })?; |
| 105 | let observed_under_lock = git |
| 106 | .find_reference(target_ref) |
| 107 | .ok() |
| 108 | .and_then(|reference| { |
no test coverage detected