Skip to content

Commit 439dfda

Browse files
committed
Show non-main worktrees in but status
Note that this is just a first take, and more integrations are likely needed to get it right. For one, legacy APIs shouldn't be used.
1 parent a771d39 commit 439dfda

File tree

14 files changed

+276
-17
lines changed

14 files changed

+276
-17
lines changed

crates/but-graph/tests/fixtures/scenarios.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ mkdir ws
214214
(cd ws
215215
git init ambiguous-worktrees
216216
(cd ambiguous-worktrees
217-
set -x
218217
commit M1
219218
commit M-base
220219

crates/but-workspace/src/branch_details.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn branch_details(
8686

8787
Ok(ui::BranchDetails {
8888
name: branch_name.into(),
89+
linked_worktree_id: None, /* not implemented in legacy mode */
8990
remote_tracking_branch: upstream
9091
.as_ref()
9192
.and_then(|upstream| upstream.get().name())
@@ -228,6 +229,7 @@ pub fn branch_details_v3(
228229

229230
Ok(ui::BranchDetails {
230231
name: name.as_bstr().into(),
232+
linked_worktree_id: None, /* probably not needed here */
231233
remote_tracking_branch: remote_tracking_branch.map(|b| b.name().as_bstr().to_owned()),
232234
description: meta.description.clone(),
233235
pr_number: meta.review.pull_request,

crates/but-workspace/src/stacks.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ pub fn stack_details(
391391

392392
branch_details.push(ui::BranchDetails {
393393
name: branch.name().to_owned().into(),
394+
linked_worktree_id: None, /* not implemented in legacy mode */
394395
remote_tracking_branch: upstream_reference.map(Into::into),
395396
description: branch.description.clone(),
396397
pr_number: branch.pr_number,
@@ -549,10 +550,9 @@ impl ui::BranchDetails {
549550
base,
550551
}: &Segment,
551552
) -> anyhow::Result<Self> {
552-
let ref_name = ref_info
553+
let ref_info = ref_info
553554
.clone()
554-
.context("Can't handle a stack yet whose tip isn't pointed to by a ref")?
555-
.ref_name;
555+
.context("Can't handle a stack yet whose tip isn't pointed to by a ref")?;
556556
let (description, updated_at, review_id, pr_number) = metadata
557557
.clone()
558558
.map(|meta| {
@@ -566,10 +566,15 @@ impl ui::BranchDetails {
566566
.unwrap_or_default();
567567
let base_commit = base.unwrap_or(gix::hash::Kind::Sha1.null());
568568
Ok(ui::BranchDetails {
569-
is_remote_head: ref_name
569+
is_remote_head: ref_info
570+
.ref_name
570571
.category()
571572
.is_some_and(|c| matches!(c, gix::refs::Category::RemoteBranch)),
572-
name: ref_name.shorten().into(),
573+
name: ref_info.ref_name.shorten().into(),
574+
linked_worktree_id: ref_info.worktree.and_then(|ws| match ws {
575+
but_graph::Worktree::Main => None,
576+
but_graph::Worktree::LinkedId(id) => Some(id),
577+
}),
573578
remote_tracking_branch: remote_tracking_ref_name
574579
.as_ref()
575580
.map(|full_name| full_name.as_bstr().into()),

crates/but-workspace/src/ui/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ pub struct BranchDetails {
362362
/// The name of the branch.
363363
#[serde(with = "gitbutler_serde::bstring_lossy")]
364364
pub name: BString,
365+
/// The id of the linked worktree that has the reference of `name` checked out.
366+
/// Note that we don't list the main worktree here.
367+
#[serde(with = "gitbutler_serde::bstring_opt_lossy")]
368+
pub linked_worktree_id: Option<BString>,
365369
/// Upstream reference, e.g. `refs/remotes/origin/base-branch-improvements`
366370
#[serde(with = "gitbutler_serde::bstring_opt_lossy")]
367371
pub remote_tracking_branch: Option<BString>,

crates/but-workspace/tests/workspace/branch_details.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod with_workspace {
4141
@r#"
4242
BranchDetails {
4343
name: "refs/heads/A",
44+
linked_worktree_id: None,
4445
remote_tracking_branch: None,
4546
description: Some(
4647
"A: description",
@@ -89,6 +90,7 @@ mod with_workspace {
8990
insta::assert_debug_snapshot!(but_workspace::branch_details_v3(&repo, refname("A").as_ref(), &store).unwrap(), @r#"
9091
BranchDetails {
9192
name: "refs/heads/A",
93+
linked_worktree_id: None,
9294
remote_tracking_branch: Some(
9395
"refs/remotes/origin/A",
9496
),
@@ -141,6 +143,7 @@ mod with_workspace {
141143
insta::assert_debug_snapshot!(but_workspace::branch_details_v3(&repo, refname("A").as_ref(), &store).unwrap(), @r#"
142144
BranchDetails {
143145
name: "refs/heads/A",
146+
linked_worktree_id: None,
144147
remote_tracking_branch: Some(
145148
"refs/remotes/origin/A",
146149
),
@@ -178,6 +181,7 @@ mod with_workspace {
178181
insta::assert_debug_snapshot!(but_workspace::branch_details_v3(&repo, refname("origin/A").as_ref(), &store).unwrap(), @r#"
179182
BranchDetails {
180183
name: "refs/remotes/origin/A",
184+
linked_worktree_id: None,
181185
remote_tracking_branch: None,
182186
description: None,
183187
pr_number: None,
@@ -220,6 +224,7 @@ mod with_workspace {
220224
insta::assert_debug_snapshot!(but_workspace::branch_details_v3(&repo, refname("A").as_ref(), &store).unwrap(), @r#"
221225
BranchDetails {
222226
name: "refs/heads/A",
227+
linked_worktree_id: None,
223228
remote_tracking_branch: Some(
224229
"refs/remotes/origin/A",
225230
),

crates/but-workspace/tests/workspace/ref_info/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn unborn_untracked() -> anyhow::Result<()> {
8282
branch_details: [
8383
BranchDetails {
8484
name: "main",
85+
linked_worktree_id: None,
8586
remote_tracking_branch: None,
8687
description: None,
8788
pr_number: None,
@@ -232,6 +233,7 @@ fn conflicted_in_local_branch() -> anyhow::Result<()> {
232233
branch_details: [
233234
BranchDetails {
234235
name: "main",
236+
linked_worktree_id: None,
235237
remote_tracking_branch: None,
236238
description: None,
237239
pr_number: None,
@@ -348,6 +350,7 @@ fn single_branch() -> anyhow::Result<()> {
348350
branch_details: [
349351
BranchDetails {
350352
name: "main",
353+
linked_worktree_id: None,
351354
remote_tracking_branch: None,
352355
description: None,
353356
pr_number: None,
@@ -537,6 +540,7 @@ fn single_branch_multiple_segments() -> anyhow::Result<()> {
537540
branch_details: [
538541
BranchDetails {
539542
name: "main",
543+
linked_worktree_id: None,
540544
remote_tracking_branch: None,
541545
description: None,
542546
pr_number: None,
@@ -557,6 +561,7 @@ fn single_branch_multiple_segments() -> anyhow::Result<()> {
557561
},
558562
BranchDetails {
559563
name: "nine",
564+
linked_worktree_id: None,
560565
remote_tracking_branch: None,
561566
description: None,
562567
pr_number: None,
@@ -579,6 +584,7 @@ fn single_branch_multiple_segments() -> anyhow::Result<()> {
579584
},
580585
BranchDetails {
581586
name: "six",
587+
linked_worktree_id: None,
582588
remote_tracking_branch: None,
583589
description: None,
584590
pr_number: None,
@@ -601,6 +607,7 @@ fn single_branch_multiple_segments() -> anyhow::Result<()> {
601607
},
602608
BranchDetails {
603609
name: "three",
610+
linked_worktree_id: None,
604611
remote_tracking_branch: None,
605612
description: None,
606613
pr_number: None,
@@ -622,6 +629,7 @@ fn single_branch_multiple_segments() -> anyhow::Result<()> {
622629
},
623630
BranchDetails {
624631
name: "one",
632+
linked_worktree_id: None,
625633
remote_tracking_branch: None,
626634
description: None,
627635
pr_number: None,

crates/but-workspace/tests/workspace/ref_info/with_workspace_commit/branch_details.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn disjoint() -> anyhow::Result<()> {
1515
insta::assert_debug_snapshot!(actual, @r#"
1616
BranchDetails {
1717
name: "refs/heads/disjoint",
18+
linked_worktree_id: None,
1819
remote_tracking_branch: None,
1920
description: None,
2021
pr_number: None,
@@ -40,6 +41,7 @@ fn disjoint() -> anyhow::Result<()> {
4041
insta::assert_debug_snapshot!(actual, @r#"
4142
BranchDetails {
4243
name: "refs/heads/main",
44+
linked_worktree_id: None,
4345
remote_tracking_branch: None,
4446
description: None,
4547
pr_number: None,

crates/but-workspace/tests/workspace/ref_info/with_workspace_commit/legacy.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ mod stacks {
169169
branch_details: [
170170
BranchDetails {
171171
name: "C-on-A",
172+
linked_worktree_id: None,
172173
remote_tracking_branch: None,
173174
description: None,
174175
pr_number: None,
@@ -191,6 +192,7 @@ mod stacks {
191192
},
192193
BranchDetails {
193194
name: "A",
195+
linked_worktree_id: None,
194196
remote_tracking_branch: Some(
195197
"refs/remotes/origin/A",
196198
),
@@ -283,6 +285,7 @@ mod stack_details {
283285
branch_details: [
284286
BranchDetails {
285287
name: "dependant",
288+
linked_worktree_id: None,
286289
remote_tracking_branch: None,
287290
description: None,
288291
pr_number: None,
@@ -301,6 +304,7 @@ mod stack_details {
301304
},
302305
BranchDetails {
303306
name: "advanced-lane",
307+
linked_worktree_id: None,
304308
remote_tracking_branch: Some(
305309
"refs/remotes/origin/advanced-lane",
306310
),
@@ -356,6 +360,7 @@ mod stack_details {
356360
branch_details: [
357361
BranchDetails {
358362
name: "B-on-A",
363+
linked_worktree_id: None,
359364
remote_tracking_branch: None,
360365
description: None,
361366
pr_number: None,
@@ -378,6 +383,7 @@ mod stack_details {
378383
},
379384
BranchDetails {
380385
name: "A",
386+
linked_worktree_id: None,
381387
remote_tracking_branch: Some(
382388
"refs/remotes/origin/A",
383389
),
@@ -413,6 +419,7 @@ mod stack_details {
413419
branch_details: [
414420
BranchDetails {
415421
name: "C-on-A",
422+
linked_worktree_id: None,
416423
remote_tracking_branch: None,
417424
description: None,
418425
pr_number: None,
@@ -435,6 +442,7 @@ mod stack_details {
435442
},
436443
BranchDetails {
437444
name: "A",
445+
linked_worktree_id: None,
438446
remote_tracking_branch: Some(
439447
"refs/remotes/origin/A",
440448
),

crates/but/src/status/mod.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub fn print_group(
216216
review_map: &std::collections::HashMap<String, Vec<gitbutler_forge::review::ForgeReview>>,
217217
) -> anyhow::Result<()> {
218218
let mut stdout = std::io::stdout();
219+
let repo = project.open_isolated()?;
219220
if let Some(group) = &group {
220221
let mut first = true;
221222
for branch in &group.branch_details {
@@ -242,15 +243,24 @@ pub fn print_group(
242243
review_map,
243244
);
244245

246+
let workspace = branch
247+
.linked_worktree_id
248+
.as_ref()
249+
.and_then(|id| {
250+
let ws = repo.worktree_proxy_by_id(id.as_bstr())?;
251+
let base = ws.base().ok()?;
252+
let git_dir = gix::path::realpath(repo.git_dir()).ok();
253+
let base = git_dir
254+
.and_then(|git_dir| base.strip_prefix(git_dir).ok())
255+
.unwrap_or_else(|| &base);
256+
format!(" 📁 {base}", base = base.display()).into()
257+
})
258+
.unwrap_or_default();
245259
writeln!(
246260
stdout,
247-
"┊{}┄{} [{}]{} {} {}",
248-
notch,
249-
id,
250-
branch.name.to_string().green().bold(),
251-
reviews,
252-
no_commits,
253-
stack_mark.clone().unwrap_or_default()
261+
"┊{notch}┄{id} [{branch}{workspace}]{reviews} {no_commits} {stack_mark}",
262+
stack_mark = stack_mark.clone().unwrap_or_default(),
263+
branch = branch.name.to_string().green().bold(),
254264
)
255265
.ok();
256266
*stack_mark = None; // Only show the stack mark for the first branch
Lines changed: 59 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)