Skip to content

Commit

Permalink
view: extract method that iterates local branches only
Browse files Browse the repository at this point in the history
I'll probably reorganize the local/remote branches structure, so let's
minimize call sites which rely on the BranchTarget struct.
  • Loading branch information
yuja committed Sep 30, 2023
1 parent 520f692 commit c5474ff
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
10 changes: 6 additions & 4 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
10 changes: 2 additions & 8 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = &CommitId> {
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()),
)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,8 @@ impl MutableRepo {
commit: &Commit,
) -> Result<(), EditCommitError> {
fn local_branch_target_ids(view: &View) -> impl Iterator<Item = &CommitId> {
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
Expand Down
6 changes: 3 additions & 3 deletions lib/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = (&str, &RefTarget)> {
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
Expand Down

0 comments on commit c5474ff

Please sign in to comment.