From 98c537f5ec99d125a4462298d03565b36fff124f Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Wed, 16 Oct 2024 20:22:37 +0200 Subject: [PATCH] stack: improvve computing of upstream only commits It is necessary to compare against commits from either side of merge commits --- crates/gitbutler-stack-api/src/series.rs | 18 +++--------------- crates/gitbutler-stack-api/src/stack_ext.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/gitbutler-stack-api/src/series.rs b/crates/gitbutler-stack-api/src/series.rs index da4da0d82..5006b7364 100644 --- a/crates/gitbutler-stack-api/src/series.rs +++ b/crates/gitbutler-stack-api/src/series.rs @@ -20,6 +20,9 @@ pub struct Series { /// If the branch/series have never been pushed, this list will be empty. /// Topologically ordered, the first entry is the newest in the series. pub remote_commits: Vec, + /// The list of patches that are only in the upstream (remote) and not in the local commits, + /// as determined by the commit ID or change ID. + pub upstream_only_commits: Vec, /// The commit IDs of the remote commits that are part of this series, grouped by change id. /// Since we dont have a change_id to commit_id index, this is used to determine pub remote_commit_ids_by_change_id: HashMap, @@ -30,19 +33,4 @@ impl Series { pub fn remote(&self, patch: &CommitOrChangeId) -> bool { self.remote_commits.contains(patch) } - /// Returns a list of patches that are only in the upstream (remote) and not in the local commits, - /// as determined by the commit ID or change ID. - /// This comparison is peformed against the full stack of series. - pub fn upstream_only(&self, stack_series: &[Series]) -> Vec { - let mut upstream_only = vec![]; - for commit in &self.remote_commits { - if !stack_series - .iter() - .any(|s| s.local_commits.contains(commit)) - { - upstream_only.push(commit.clone()); - } - } - upstream_only - } } diff --git a/crates/gitbutler-stack-api/src/stack_ext.rs b/crates/gitbutler-stack-api/src/stack_ext.rs index 5f02c0602..535f65180 100644 --- a/crates/gitbutler-stack-api/src/stack_ext.rs +++ b/crates/gitbutler-stack-api/src/stack_ext.rs @@ -499,10 +499,29 @@ impl StackExt for Stack { }); } }; + + // compute the commits that are only in the upstream + let local_patches_including_merge = repo + .log(head_commit, LogUntil::Commit(previous_head), true)? + .iter() + .rev() // oldest commit first + .map(|c| match c.change_id() { + Some(change_id) => CommitOrChangeId::ChangeId(change_id.to_string()), + None => CommitOrChangeId::CommitId(c.id().to_string()), + }) + .collect_vec(); + let mut upstream_only = vec![]; + for patch in remote_patches.iter() { + if !local_patches_including_merge.contains(patch) { + upstream_only.push(patch.clone()); + } + } + all_series.push(Series { head: head.clone(), local_commits: local_patches, remote_commits: remote_patches, + upstream_only_commits: upstream_only, remote_commit_ids_by_change_id, }); previous_head = head_commit;