diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index df48038ac1..15394375dc 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1610,12 +1610,14 @@ fn cmd_status( formatter.write_str("No working copy\n")?; } - let mut conflicted_local_branches = vec![]; + let conflicted_local_branches = repo + .view() + .local_branches() + .filter(|(_, target)| target.has_conflict()) + .map(|(branch_name, _)| branch_name) + .collect_vec(); let mut conflicted_remote_branches = vec![]; for (branch_name, branch_target) in repo.view().branches() { - if branch_target.local_target.has_conflict() { - conflicted_local_branches.push(branch_name.clone()); - } for (remote_name, remote_target) in &branch_target.remote_targets { if remote_target.has_conflict() { conflicted_remote_branches.push((branch_name.clone(), remote_name.clone())); diff --git a/lib/src/git.rs b/lib/src/git.rs index db2f823489..643f7c489a 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -362,12 +362,8 @@ fn diff_refs_to_import( /// `view.git_refs()`. Main difference is that local branches can be moved by /// tracking remotes, and such mutation isn't applied to `view.git_refs()` yet. fn pinned_commit_ids(view: &View) -> impl Iterator { - let branch_ref_targets = view - .branches() - .values() - .map(|branch_target| &branch_target.local_target); itertools::chain!( - branch_ref_targets, + view.local_branches().map(|(_, target)| target), view.tags().values(), iter::once(view.git_head()), ) @@ -852,9 +848,7 @@ pub fn fetch( // help branch forget`. let nonempty_branches: HashSet<_> = mut_repo .view() - .branches() - .iter() - .filter(|&(_branch, target)| target.local_target.is_present()) + .local_branches() .map(|(branch, _target)| branch.to_owned()) .collect(); // TODO: Inform the user if the export failed? In most cases, export is not diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 72009eec66..88880d0cb7 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -872,9 +872,8 @@ impl MutableRepo { commit: &Commit, ) -> Result<(), EditCommitError> { fn local_branch_target_ids(view: &View) -> impl Iterator { - view.branches() - .values() - .flat_map(|branch_target| branch_target.local_target.added_ids()) + view.local_branches() + .flat_map(|(_, target)| target.added_ids()) } let maybe_wc_commit_id = self diff --git a/lib/src/rewrite.rs b/lib/src/rewrite.rs index 042448aea2..07794ecde0 100644 --- a/lib/src/rewrite.rs +++ b/lib/src/rewrite.rs @@ -233,12 +233,12 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> { // Build a map from commit to branches pointing to it, so we don't need to scan // all branches each time we rebase a commit. let mut branches: HashMap<_, HashSet<_>> = HashMap::new(); - for (branch_name, branch_target) in mut_repo.view().branches() { - for commit in branch_target.local_target.added_ids() { + for (branch_name, target) in mut_repo.view().local_branches() { + for commit in target.added_ids() { branches .entry(commit.clone()) .or_default() - .insert(branch_name.clone()); + .insert(branch_name.to_owned()); } } diff --git a/lib/src/view.rs b/lib/src/view.rs index 8638886550..0311d68d13 100644 --- a/lib/src/view.rs +++ b/lib/src/view.rs @@ -160,6 +160,15 @@ impl View { self.data.branches.remove(name); } + /// Iterates local branch `(name, target)`s in lexicographical order. + pub fn local_branches(&self) -> impl Iterator { + self.data + .branches + .iter() + .map(|(name, branch_target)| (name.as_ref(), &branch_target.local_target)) + .filter(|(_, target)| target.is_present()) + } + pub fn get_local_branch(&self, name: &str) -> &RefTarget { if let Some(branch_target) = self.data.branches.get(name) { &branch_target.local_target