Skip to content

Commit 4ae3543

Browse files
committed
Improve performance of list-local using git2.
1 parent 36f23d5 commit 4ae3543

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

crates/gitbutler-branch-actions/src/remote.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,30 @@ pub fn list_local_branches(ctx: &CommandContext) -> Result<Vec<RemoteBranch>> {
6868
let default_target = default_target(&ctx.project().gb_dir())?;
6969

7070
let mut remote_branches = vec![];
71+
let remotes = ctx.repository().remotes()?;
7172
for (branch, _) in ctx
7273
.repository()
7374
.branches(None)
7475
.context("failed to list remote branches")?
7576
.flatten()
7677
{
77-
let branch = branch_to_remote_branch(ctx, &branch);
78+
let branch = match branch_to_remote_branch(&branch, &remotes) {
79+
Ok(Some(b)) => b,
80+
Ok(None) => continue,
81+
Err(err) => {
82+
tracing::warn!(?err, "Ignoring branch");
83+
continue;
84+
}
85+
};
7886

79-
if let Some(branch) = branch {
80-
let branch_is_trunk = branch.name.branch() == Some(default_target.branch.branch())
81-
&& branch.name.remote() == Some(default_target.branch.remote());
87+
let branch_is_trunk = branch.name.branch() == Some(default_target.branch.branch())
88+
&& branch.name.remote() == Some(default_target.branch.remote());
8289

83-
if !branch_is_trunk
84-
&& branch.name.branch() != Some("gitbutler/integration")
85-
&& branch.name.branch() != Some("gitbutler/target")
86-
{
87-
remote_branches.push(branch);
88-
}
90+
if !branch_is_trunk
91+
&& branch.name.branch() != Some("gitbutler/integration")
92+
&& branch.name.branch() != Some("gitbutler/target")
93+
{
94+
remote_branches.push(branch);
8995
}
9096
}
9197
Ok(remote_branches)
@@ -104,30 +110,15 @@ pub(crate) fn get_branch_data(ctx: &CommandContext, refname: &Refname) -> Result
104110
}
105111

106112
pub(crate) fn branch_to_remote_branch(
107-
ctx: &CommandContext,
108-
branch: &git2::Branch,
109-
) -> Option<RemoteBranch> {
110-
let commit = match branch.get().peel_to_commit() {
111-
Ok(c) => c,
112-
Err(err) => {
113-
tracing::warn!(
114-
?err,
115-
"ignoring branch {:?} as peeling failed",
116-
branch.name()
117-
);
118-
return None;
119-
}
120-
};
121-
let name = Refname::try_from(branch)
122-
.context("could not get branch name")
123-
.ok()?;
113+
branch: &git2::Branch<'_>,
114+
remotes: &git2::string_array::StringArray,
115+
) -> Result<Option<RemoteBranch>> {
116+
let commit = branch.get().peel_to_commit()?;
117+
let name = Refname::try_from(branch).context("could not get branch name")?;
124118

125-
let given_name = branch
126-
.get()
127-
.given_name(&ctx.repository().remotes().ok()?)
128-
.ok()?;
119+
let given_name = branch.get().given_name(remotes)?;
129120

130-
branch.get().target().map(|sha| RemoteBranch {
121+
Ok(branch.get().target().map(|sha| RemoteBranch {
131122
sha,
132123
upstream: if let Refname::Local(local_name) = &name {
133124
local_name.remote().cloned()
@@ -144,7 +135,7 @@ pub(crate) fn branch_to_remote_branch(
144135
.ok(),
145136
last_commit_author: commit.author().name().map(std::string::ToString::to_string),
146137
is_remote: branch.get().is_remote(),
147-
})
138+
}))
148139
}
149140

150141
pub(crate) fn branch_to_remote_branch_data(

crates/gitbutler-branch-actions/src/virtual.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,10 @@ pub fn list_virtual_branches_cached(
354354
.context("failed to find merge base")?;
355355
let base_current = true;
356356

357-
let upstream = upstream_branch
358-
.and_then(|upstream_branch| branch_to_remote_branch(ctx, &upstream_branch));
357+
let upstream = upstream_branch.and_then(|upstream_branch| {
358+
let remotes = repo.remotes().ok()?;
359+
branch_to_remote_branch(&upstream_branch, &remotes).ok()?
360+
});
359361

360362
let path_claim_positions: HashMap<&PathBuf, usize> = branch
361363
.ownership

0 commit comments

Comments
 (0)