Skip to content

Commit

Permalink
Improve performance of list-local using git2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Aug 29, 2024
1 parent 31d30e8 commit da1283e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
57 changes: 24 additions & 33 deletions crates/gitbutler-branch-actions/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,30 @@ pub fn list_local_branches(ctx: &CommandContext) -> Result<Vec<RemoteBranch>> {
let default_target = default_target(&ctx.project().gb_dir())?;

let mut remote_branches = vec![];
let remotes = ctx.repository().remotes()?;
for (branch, _) in ctx
.repository()
.branches(None)
.context("failed to list remote branches")?
.flatten()
{
let branch = branch_to_remote_branch(ctx, &branch);
let branch = match branch_to_remote_branch(&branch, &remotes) {
Ok(Some(b)) => b,
Ok(None) => continue,
Err(err) => {
tracing::warn!(?err, "Ignoring branch");
continue;
}
};

if let Some(branch) = branch {
let branch_is_trunk = branch.name.branch() == Some(default_target.branch.branch())
&& branch.name.remote() == Some(default_target.branch.remote());
let branch_is_trunk = branch.name.branch() == Some(default_target.branch.branch())
&& branch.name.remote() == Some(default_target.branch.remote());

if !branch_is_trunk
&& branch.name.branch() != Some("gitbutler/integration")
&& branch.name.branch() != Some("gitbutler/target")
{
remote_branches.push(branch);
}
if !branch_is_trunk
&& branch.name.branch() != Some("gitbutler/integration")
&& branch.name.branch() != Some("gitbutler/target")
{
remote_branches.push(branch);
}
}
Ok(remote_branches)
Expand All @@ -104,30 +110,15 @@ pub(crate) fn get_branch_data(ctx: &CommandContext, refname: &Refname) -> Result
}

pub(crate) fn branch_to_remote_branch(
ctx: &CommandContext,
branch: &git2::Branch,
) -> Option<RemoteBranch> {
let commit = match branch.get().peel_to_commit() {
Ok(c) => c,
Err(err) => {
tracing::warn!(
?err,
"ignoring branch {:?} as peeling failed",
branch.name()
);
return None;
}
};
let name = Refname::try_from(branch)
.context("could not get branch name")
.ok()?;
branch: &git2::Branch<'_>,
remotes: &git2::string_array::StringArray,
) -> Result<Option<RemoteBranch>> {
let commit = branch.get().peel_to_commit()?;
let name = Refname::try_from(branch).context("could not get branch name")?;

let given_name = branch
.get()
.given_name(&ctx.repository().remotes().ok()?)
.ok()?;
let given_name = branch.get().given_name(remotes)?;

branch.get().target().map(|sha| RemoteBranch {
Ok(branch.get().target().map(|sha| RemoteBranch {
sha,
upstream: if let Refname::Local(local_name) = &name {
local_name.remote().cloned()
Expand All @@ -144,7 +135,7 @@ pub(crate) fn branch_to_remote_branch(
.ok(),
last_commit_author: commit.author().name().map(std::string::ToString::to_string),
is_remote: branch.get().is_remote(),
})
}))
}

pub(crate) fn branch_to_remote_branch_data(
Expand Down
6 changes: 4 additions & 2 deletions crates/gitbutler-branch-actions/src/virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,10 @@ pub fn list_virtual_branches_cached(
.context("failed to find merge base")?;
let base_current = true;

let upstream = upstream_branch
.and_then(|upstream_branch| branch_to_remote_branch(ctx, &upstream_branch));
let upstream = upstream_branch.and_then(|upstream_branch| {
let remotes = repo.remotes().ok()?;
branch_to_remote_branch(&upstream_branch, &remotes).ok()?
});

let path_claim_positions: HashMap<&PathBuf, usize> = branch
.ownership
Expand Down

0 comments on commit da1283e

Please sign in to comment.