Skip to content

Commit

Permalink
op_store: make RefTarget return removes/adds as slice, .clone() as ne…
Browse files Browse the repository at this point in the history
…eded
  • Loading branch information
yuja committed Jul 2, 2023
1 parent d1453a0 commit 061fbeb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
5 changes: 3 additions & 2 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub fn import_some_refs(
mut_repo.remove_git_ref(&full_name);
changed_git_refs.insert(ref_name, (Some(target), None));
} else {
pinned_git_heads.insert(ref_name, target.adds());
pinned_git_heads.insert(ref_name, target.adds().to_vec());
}
}
for (ref_name, (old_git_target, new_git_target)) in &changed_git_refs {
Expand All @@ -244,7 +244,7 @@ pub fn import_some_refs(
None => pinned_git_heads.remove(&local_ref_name),
Some(target) => {
// Note that we are mostly *replacing*, not inserting
pinned_git_heads.insert(local_ref_name, target.adds())
pinned_git_heads.insert(local_ref_name, target.adds().to_vec())
}
};
}
Expand All @@ -256,6 +256,7 @@ pub fn import_some_refs(
.values()
.filter_map(|(old_git_target, _)| old_git_target.as_ref().map(|target| target.adds()))
.flatten()
.cloned()
.collect_vec();
if hidable_git_heads.is_empty() {
return Ok(());
Expand Down
17 changes: 7 additions & 10 deletions lib/src/op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::{Debug, Error, Formatter};
use std::slice;

use thiserror::Error;

Expand Down Expand Up @@ -144,21 +145,17 @@ impl RefTarget {
matches!(self, RefTarget::Conflict { .. })
}

pub fn removes(&self) -> Vec<CommitId> {
pub fn removes(&self) -> &[CommitId] {
match self {
RefTarget::Normal(_) => {
vec![]
}
RefTarget::Conflict { removes, adds: _ } => removes.clone(),
RefTarget::Normal(_) => &[],
RefTarget::Conflict { removes, adds: _ } => removes,
}
}

pub fn adds(&self) -> Vec<CommitId> {
pub fn adds(&self) -> &[CommitId] {
match self {
RefTarget::Normal(id) => {
vec![id.clone()]
}
RefTarget::Conflict { removes: _, adds } => adds.clone(),
RefTarget::Normal(id) => slice::from_ref(id),
RefTarget::Conflict { removes: _, adds } => adds,
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ pub fn merge_ref_targets(
let mut removes = vec![];
let mut adds = vec![];
if let Some(left) = left {
removes.extend(left.removes());
adds.extend(left.adds());
removes.extend_from_slice(left.removes());
adds.extend_from_slice(left.adds());
}
if let Some(base) = base {
// Note that these are backwards (because the base is subtracted).
removes.extend(base.adds());
adds.extend(base.removes());
removes.extend_from_slice(base.adds());
adds.extend_from_slice(base.removes());
}
if let Some(right) = right {
removes.extend(right.removes());
adds.extend(right.adds());
removes.extend_from_slice(right.removes());
adds.extend_from_slice(right.adds());
}

while let Some((maybe_remove_index, add_index)) = find_pair_to_remove(index, &removes, &adds) {
Expand Down
20 changes: 10 additions & 10 deletions lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ fn resolve_git_ref(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
// way to address local git repo's remote-tracking branches.
for git_ref_prefix in &["", "refs/", "refs/tags/", "refs/remotes/"] {
if let Some(ref_target) = view.git_refs().get(&(git_ref_prefix.to_string() + symbol)) {
return Some(ref_target.adds());
return Some(ref_target.adds().to_vec());
}
}
None
Expand All @@ -1646,20 +1646,20 @@ fn resolve_branch(repo: &dyn Repo, symbol: &str) -> Option<Vec<CommitId>> {
branch_target
.local_target
.as_ref()
.map(|target| target.adds())
.map(|target| target.adds().to_vec())
.unwrap_or_default(),
);
}
if let Some((name, remote_name)) = symbol.split_once('@') {
if let Some(branch_target) = repo.view().branches().get(name) {
if let Some(target) = branch_target.remote_targets.get(remote_name) {
return Some(target.adds());
return Some(target.adds().to_vec());
}
}
// A remote with name "git" will shadow local-git tracking branches
if remote_name == "git" {
if let Some(target) = get_git_tracking_branch(repo.view(), name) {
return Some(target.adds());
return Some(target.adds().to_vec());
}
}
}
Expand Down Expand Up @@ -1773,7 +1773,7 @@ impl SymbolResolver for DefaultSymbolResolver<'_> {
} else {
// Try to resolve as a tag
if let Some(target) = self.repo.view().tags().get(symbol) {
return Ok(target.adds());
return Ok(target.adds().to_vec());
}

// Try to resolve as a branch
Expand Down Expand Up @@ -1851,7 +1851,7 @@ fn resolve_commit_ref(
continue;
}
if let Some(local_target) = &branch_target.local_target {
commit_ids.extend(local_target.adds());
commit_ids.extend_from_slice(local_target.adds());
}
}
Ok(commit_ids)
Expand All @@ -1867,7 +1867,7 @@ fn resolve_commit_ref(
}
for (remote_name, remote_target) in branch_target.remote_targets.iter() {
if remote_name.contains(remote_needle) {
commit_ids.extend(remote_target.adds());
commit_ids.extend_from_slice(remote_target.adds());
}
}
}
Expand All @@ -1876,21 +1876,21 @@ fn resolve_commit_ref(
RevsetCommitRef::Tags => {
let mut commit_ids = vec![];
for ref_target in repo.view().tags().values() {
commit_ids.extend(ref_target.adds());
commit_ids.extend_from_slice(ref_target.adds());
}
Ok(commit_ids)
}
RevsetCommitRef::GitRefs => {
let mut commit_ids = vec![];
for ref_target in repo.view().git_refs().values() {
commit_ids.extend(ref_target.adds());
commit_ids.extend_from_slice(ref_target.adds());
}
Ok(commit_ids)
}
RevsetCommitRef::GitHead => {
let mut commit_ids = vec![];
if let Some(ref_target) = repo.view().git_head() {
commit_ids.extend(ref_target.adds());
commit_ids.extend_from_slice(ref_target.adds());
}
Ok(commit_ids)
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
if let Some(local_target) = &branch_target.local_target {
for commit in local_target.removes() {
branches
.entry(commit)
.entry(commit.clone())
.or_default()
.insert(branch_name.clone());
}
for commit in local_target.adds() {
branches
.entry(commit)
.entry(commit.clone())
.or_default()
.insert(branch_name.clone());
}
Expand Down Expand Up @@ -318,7 +318,7 @@ impl<'settings, 'repo> DescendantRebaser<'settings, 'repo> {
}
let local_target = self.mut_repo.get_local_branch(branch_name).unwrap();
for old_add in local_target.adds() {
if old_add == old_commit_id {
if *old_add == old_commit_id {
branch_updates.push(branch_name.clone());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ fn cmd_branch_list(
write!(formatter.labeled("branch"), "@{remote}")?;
if let Some(local_target) = branch_target.local_target.as_ref() {
let remote_ahead_count =
revset::walk_revs(repo.as_ref(), &remote_target.adds(), &local_target.adds())?
revset::walk_revs(repo.as_ref(), remote_target.adds(), local_target.adds())?
.count();
let local_ahead_count =
revset::walk_revs(repo.as_ref(), &local_target.adds(), &remote_target.adds())?
revset::walk_revs(repo.as_ref(), local_target.adds(), remote_target.adds())?
.count();
if remote_ahead_count != 0 && local_ahead_count == 0 {
write!(formatter, " (ahead by {remote_ahead_count} commits)")?;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ fn cmd_git_push(
let mut old_heads = vec![];
for branch_target in repo.view().branches().values() {
if let Some(old_head) = branch_target.remote_targets.get(&remote) {
old_heads.extend(old_head.adds());
old_heads.extend_from_slice(old_head.adds());
}
}
if old_heads.is_empty() {
Expand Down

0 comments on commit 061fbeb

Please sign in to comment.