Skip to content

Commit

Permalink
git: handle lock error that could occur while adding GC-preventing refs
Browse files Browse the repository at this point in the history
If I spawned ~20 "jj status &" processes, some of them panicked there.
Spotted when debugging #924.
  • Loading branch information
yuja committed May 21, 2023
1 parent b01614b commit 1675aec
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ fn local_branch_name_to_ref_name(branch: &str) -> String {
format!("refs/heads/{branch}")
}

fn prevent_gc(git_repo: &git2::Repository, id: &CommitId) {
git_repo
.reference(
&format!("{}{}", NO_GC_REF_NAMESPACE, id.hex()),
Oid::from_bytes(id.as_bytes()).unwrap(),
true,
"used by jj",
)
.unwrap();
fn prevent_gc(git_repo: &git2::Repository, id: &CommitId) -> Result<(), git2::Error> {
// If multiple processes do git::import_refs() in parallel, this can fail to
// acquire a lock file even with force=true.
git_repo.reference(
&format!("{}{}", NO_GC_REF_NAMESPACE, id.hex()),
Oid::from_bytes(id.as_bytes()).unwrap(),
true,
"used by jj",
)?;
Ok(())
}

/// Reflect changes made in the underlying Git repo in the Jujutsu repo.
Expand Down Expand Up @@ -116,7 +117,7 @@ pub fn import_some_refs(
let head_commit_id = CommitId::from_bytes(head_git_commit.id().as_bytes());
let head_commit = store.get_commit(&head_commit_id).unwrap();
new_git_heads.insert("HEAD".to_string(), vec![head_commit_id.clone()]);
prevent_gc(git_repo, &head_commit_id);
prevent_gc(git_repo, &head_commit_id)?;
mut_repo.add_head(&head_commit);
mut_repo.set_git_head(RefTarget::Normal(head_commit_id));
} else {
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn import_some_refs(
let old_target = jj_view_git_refs.remove(&full_name);
let new_target = Some(RefTarget::Normal(id.clone()));
if new_target != old_target {
prevent_gc(git_repo, &id);
prevent_gc(git_repo, &id)?;
mut_repo.set_git_ref(full_name.clone(), RefTarget::Normal(id.clone()));
let commit = store.get_commit(&id).unwrap();
mut_repo.add_head(&commit);
Expand Down

0 comments on commit 1675aec

Please sign in to comment.