Skip to content

Commit

Permalink
git: extract a function for abandoning unreachable commits
Browse files Browse the repository at this point in the history
This motivation for this is so we can easily skip calling the function
if the user has opted out of the propagation of abandoned commits we
usually do (#2504). However, it seems like a good piece of code to
extract regardless of that feature.
  • Loading branch information
martinvonz committed Nov 5, 2023
1 parent d9fbf21 commit 7bf8906
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,24 @@ pub fn import_some_refs(
}
}

// Find commits that are no longer referenced in the git repo and abandon them
// in jj as well.
let abandoned_commits = abandon_unreachable_commits(mut_repo, &changed_remote_refs);
let stats = GitImportStats { abandoned_commits };
Ok(stats)
}

/// Finds commits that used to be reachable in git that no longer are reachable.
/// Those commits will be recorded as abandoned in the `MutableRepo`.
fn abandon_unreachable_commits(
mut_repo: &mut MutableRepo,
changed_remote_refs: &BTreeMap<RefName, (RemoteRef, RefTarget)>,
) -> Vec<CommitId> {
let hidable_git_heads = changed_remote_refs
.values()
.flat_map(|(old_remote_ref, _)| old_remote_ref.target.added_ids())
.cloned()
.collect_vec();
if hidable_git_heads.is_empty() {
let stats = GitImportStats {
abandoned_commits: vec![],
};
return Ok(stats);
return vec![];
}
let pinned_heads = itertools::chain!(
changed_remote_refs
Expand All @@ -335,10 +341,6 @@ pub fn import_some_refs(
)
.cloned()
.collect_vec();
// We could use mut_repo.record_rewrites() here but we know we only need to care
// about abandoned commits for now. We may want to change this if we ever
// add a way of preserving change IDs across rewrites by `git` (e.g. by
// putting them in the commit message).
let abandoned_expression = RevsetExpression::commits(pinned_heads)
.range(&RevsetExpression::commits(hidable_git_heads))
.intersection(&RevsetExpression::visible_heads().ancestors());
Expand All @@ -352,9 +354,7 @@ pub fn import_some_refs(
for abandoned_commit in &abandoned_commits {
mut_repo.record_abandoned_commit(abandoned_commit.clone());
}

let stats = GitImportStats { abandoned_commits };
Ok(stats)
abandoned_commits
}

/// Calculates diff of git refs to be imported.
Expand Down

0 comments on commit 7bf8906

Please sign in to comment.