Journal an expected-absent ref creation, perform it through a real Git reference transaction, then record its leased receipt and verify. CB-10A review R3: the ref is locked with libgit2's reference transaction and re-compared under Git's own ref lock, so a concurrent write to the target fails the transaction instead of being lost or rewound by a post-write observation. The expected old is *absenc
(
repo: &Repository,
working_copy: WorkingCopyId,
git: &GitRepository,
target_ref: &str,
commit: Oid,
message: &str,
)
| 185 | /// post-write observation. The expected old is *absence*: publication is |
| 186 | /// create-only. |
| 187 | pub(crate) fn journal_and_create_branch_cas( |
| 188 | repo: &Repository, |
| 189 | working_copy: WorkingCopyId, |
| 190 | git: &GitRepository, |
| 191 | target_ref: &str, |
| 192 | commit: Oid, |
| 193 | message: &str, |
| 194 | ) -> CliResult<()> { |
| 195 | let observed_old = git |
| 196 | .find_reference(target_ref) |
| 197 | .ok() |
| 198 | .and_then(|reference| reference.target()) |
| 199 | .and_then(canonical_ref_target); |
| 200 | if observed_old.is_some() { |
| 201 | return Err(git_error(format!( |
| 202 | "refusing to create '{target_ref}': it already exists; publication is create-only" |
| 203 | ))); |
| 204 | } |
| 205 | let intended_new = GitRefTarget::Direct( |
| 206 | GitObjectId::new(GitHashAlgorithm::Sha1, commit.as_bytes().to_vec()) |
| 207 | .map_err(|error| git_error(error.to_string()))?, |
| 208 | ); |
| 209 | let evidence = atomic_core::Hash::of( |
| 210 | format!("atomic:bridge-ref-create:v1\0{target_ref}\0{commit}").as_bytes(), |
| 211 | ); |
| 212 | // Journal-before-visibility: the operation (ID + intended effect) is |
| 213 | // durable before the ref write. Any failure here prevents the write. |
| 214 | let prepared = repo |
| 215 | .prepare_shim_git_ref_write(working_copy, target_ref, None, intended_new, evidence) |
| 216 | .map_err(|error| { |
| 217 | git_error(format!( |
| 218 | "cannot journal '{target_ref}' before writing: {error}" |
| 219 | )) |
| 220 | })?; |
| 221 | |
| 222 | // Real Git reference transaction: lock the ref (reserving it), re-compare |
| 223 | // under the lock, then commit the single-ref transaction. |
| 224 | let mut transaction = git |
| 225 | .transaction() |
| 226 | .map_err(|error| git_error(format!("cannot start a Git reference transaction: {error}")))?; |
| 227 | transaction.lock_ref(target_ref).map_err(|error| { |
| 228 | git_error(format!( |
| 229 | "cannot lock '{target_ref}' for the transaction: {error}" |
| 230 | )) |
| 231 | })?; |
| 232 | // Review R3: presence is refused in ANY form. `reference.target()` is |
| 233 | // `None` for a symbolic reference, so the target-only read would treat a |
| 234 | // symbolic ref as absence and overwrite it; the kind is checked instead. |
| 235 | if let Ok(reference) = git.find_reference(target_ref) { |
| 236 | let shape = match reference.kind() { |
| 237 | Some(git2::ReferenceType::Symbolic) => "a symbolic reference", |
| 238 | Some(git2::ReferenceType::Direct) => "a direct reference", |
| 239 | _ => "a reference", |
| 240 | }; |
| 241 | return Err(git_error(format!( |
| 242 | "refusing to create '{target_ref}': it appeared as {shape} while the publication \ |
| 243 | transaction was being journaled; nothing was written" |
| 244 | ))); |
nothing calls this directly
no test coverage detected