diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 67d9fc072..8c6f226fa 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -893,13 +893,12 @@ fn cmd_git_push( // Check if there are conflicts in any commits we're about to push that haven't // already been pushed. - let mut old_heads = vec![]; - // TODO: get list of branches by remote name instead of using .branches() - for (_, branch_target) in repo.view().branches() { - if let Some(old_head) = branch_target.remote_targets.get(&remote) { - old_heads.extend(old_head.added_ids().cloned()); - } - } + let mut old_heads = repo + .view() + .remote_branches(&remote) + .flat_map(|(_, old_head)| old_head.added_ids()) + .cloned() + .collect_vec(); if old_heads.is_empty() { old_heads.push(repo.store().root_commit_id().clone()); } diff --git a/lib/src/view.rs b/lib/src/view.rs index d498b8a35..962c0db5f 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -197,6 +197,22 @@ impl View { op_store::flatten_remote_branches(&self.data.remote_views) } + /// Iterates branch `(name, target)`s of the specified remote in + /// lexicographical order. + pub fn remote_branches(&self, remote_name: &str) -> impl Iterator { + // TODO: maybe yield RemoteRef instead of RefTarget? + let maybe_remote_view = self.data.remote_views.get(remote_name); + maybe_remote_view + .map(|remote_view| { + remote_view + .branches + .iter() + .map(|(name, remote_ref)| (name.as_ref(), &remote_ref.target)) + }) + .into_iter() + .flatten() + } + pub fn get_remote_branch(&self, name: &str, remote_name: &str) -> &RefTarget { // TODO: maybe return RemoteRef instead of RefTarget? if let Some(remote_view) = self.data.remote_views.get(remote_name) { @@ -234,23 +250,13 @@ impl View { ) -> impl Iterator)> + 'a { // TODO: maybe untracked remote target can be translated to absent, and rename // the method accordingly. - let maybe_remote_view = self.data.remote_views.get(remote_name); - let remote_branches = maybe_remote_view - .map(|remote_view| { - remote_view - .branches - .iter() - .map(|(name, remote_ref)| (name, &remote_ref.target)) - }) - .into_iter() - .flatten(); - iter_named_ref_pairs(&self.data.local_branches, remote_branches).map( + iter_named_ref_pairs(self.local_branches(), self.remote_branches(remote_name)).map( |(name, (local_target, remote_target))| { let targets = TrackingRefPair { local_target, remote_target, }; - (name.as_ref(), targets) + (name, targets) }, ) }