Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat: Improve CLI messaging and define list ordering rules

Maximilian Roos4e2422035fc8
diff --git a/src/commands/list/render.rs b/src/commands/list/render.rs
index 25e77ce09..0ff24079d 100644
--- a/src/commands/list/render.rs
+++ b/src/commands/list/render.rs
@@ -114,62 +114,67 @@ fn diff_render_config(variant: DiffVariant) -> DiffRenderConfig {
}
}
-fn format_diff_like_column(
- positive: usize,
- negative: usize,
+struct DiffColumnConfig {
digits: DiffDigits,
total_width: usize,
variant: DiffVariant,
positive_style: Style,
negative_style: Style,
+ always_show_zeros: bool,
+}
+
+fn format_diff_like_column(
+ positive: usize,
+ negative: usize,
+ config: DiffColumnConfig,
) -> StyledLine {
- let config = diff_render_config(variant);
+ let render_config = diff_render_config(config.variant);
let mut segment = StyledLine::new();
- if positive == 0 && negative == 0 {
- segment.push_raw(" ".repeat(total_width));
+ if positive == 0 && negative == 0 && !config.always_show_zeros {
+ segment.push_raw(" ".repeat(config.total_width));
return segment;
}
- let positive_width = 1 + digits.added;
- let negative_width = 1 + digits.deleted;
+ let positive_width = 1 + config.digits.added;
+ let negative_width = 1 + config.digits.deleted;
let content_width = positive_width + 1 + negative_width;
- let extra_padding = total_width.saturating_sub(content_width);
+ let extra_padding = config.total_width.saturating_sub(content_width);
- if matches!(config.align, ValueAlign::Right) && extra_padding > 0 {
+ if matches!(render_config.align, ValueAlign::Right) && extra_padding > 0 {
segment.push_raw(" ".repeat(extra_padding));
}
- if positive > 0 {
- let value = format!("{}{}", config.positive_symbol, positive);
- let formatted = match config.align {
+ if positive > 0 || (positive == 0 && config.always_show_zeros) {
+ let value = format!("{}{}", render_config.positive_symbol, positive);
+ let formatted = match render_config.align {
ValueAlign::Right => format!("{:>width$}", value, width = positive_width),
ValueAlign::Left => format!("{:<width$}", value, width = positive_width),
};
- segment.push_styled(formatted, positive_style);
+ segment.push_styled(formatted, config.positive_style);
} else {
segment.push_raw(" ".repeat(positive_width));
}
segment.push_raw(" ");
- if negative > 0 {
- let value = format!("{}{}", config.negative_symbol, negative);
- let formatted = match config.align {
+ if negative > 0 || (negative == 0 && config.always_show_zeros) {
+ let value = format!("{}{}", render_config.negative_symbol, negative);
+ let formatted = match render_config.align {
ValueAlign::Right => format!("{:>width$}", value, width = negative_width),
ValueAlign::Left => format!("{:<width$}", value, width = negative_width),
};
- segment.push_styled(formatted, negative_style);
+ segment.push_styled(formatted, config.negative_style);
} else {
segment.push_raw(" ".repeat(negative_width));
}
- if matches!(config.align, ValueAlign::Left) && extra_padding > 0 {
+ if matches!(render_config.align, ValueAlign::Left) && extra_padding > 0 {
segment.pad_to(segment.width() + extra_padding);
}
- if segment.width() < total_width {
- segment.pad_to(total_width);
+ if segment.width() < config.total_width {
+ segment.pad_to(config.total_width);
}
segment
@@ -356,11 +361,14 @@ pub fn format_list_item_line(
let segment = format_diff_like_column(
wt_added,
wt_deleted,
- digits,
- column.width,
- variant,
- ADDITION,
- DELETION,
+ DiffColumnConfig {
+ digits,
+ total_width: column.width,
+ variant,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
);
append_line(&mut line, segment);
} else if !is_last {
@@ -373,11 +381,14 @@ pub fn format_list_item_line(
let segment = format_diff_like_column(
counts.ahead,
counts.behind,
- digits,
- column.width,
- variant,
- ADDITION,
- dim_deletion,
+ DiffColumnConfig {
+ digits,
+ total_width: column.width,
+ variant,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: false,
+ },
);
append_line(&mut line, segment);
} else if !is_last {
@@ -389,11 +400,14 @@ pub fn format_list_item_line(
let segment = format_diff_like_column(
branch_diff.0,
branch_diff.1,
- digits,
- column.width,
- variant,
- ADDITION,
- DELETION,
+ DiffColumnConfig {
+ digits,
+ total_width: column.width,
+ variant,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
);
append_line(&mut line, segment);
} else if !is_last {
@@ -440,11 +454,14 @@ pub fn format_list_item_line(
let segment = format_diff_like_column(
upstream_ahead,
upstream_behind,
- digits,
- column.width,
- variant,
- ADDITION,
- dim_deletion,
+ DiffColumnConfig {
+ digits,
+ total_width: column.width,
+ variant,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: true,
+ },
);
append_line(&mut line, segment);
} else if !is_last {
@@ -517,8 +534,18 @@ mod tests {
deleted: 1,
};
let total = 6;
- let result =
- format_diff_like_column(1, 1, digits, total, DiffVariant::Signs, ADDITION, DELETION);
+ let result = format_diff_like_column(
+ 1,
+ 1,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Signs,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
+ );
assert_eq!(
result.width(),
total,
@@ -534,11 +561,14 @@ mod tests {
let result = format_diff_like_column(
10,
50,
- digits,
- total,
- DiffVariant::Signs,
- ADDITION,
- DELETION,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Signs,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
);
assert_eq!(
result.width(),
@@ -555,11 +585,14 @@ mod tests {
let result = format_diff_like_column(
100,
50,
- digits,
- total,
- DiffVariant::Signs,
- ADDITION,
- DELETION,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Signs,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
);
assert_eq!(
result.width(),
@@ -573,8 +606,18 @@ mod tests {
deleted: 1,
};
let total = 6;
- let result =
- format_diff_like_column(0, 0, digits, total, DiffVariant::Signs, ADDITION, DELETION);
+ let result = format_diff_like_column(
+ 0,
+ 0,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Signs,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
+ );
assert_eq!(result.width(), total, "Empty diff should be 6 spaces");
}
@@ -589,8 +632,18 @@ mod tests {
};
let total = 6;
- let result =
- format_diff_like_column(1, 1, digits, total, DiffVariant::Signs, ADDITION, DELETION);
+ let result = format_diff_like_column(
+ 1,
+ 1,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Signs,
+ positive_style: ADDITION,
+ negative_style: DELETION,
+ always_show_zeros: false,
+ },
+ );
let rendered = result.render();
// Strip ANSI codes to check alignment
@@ -728,11 +781,14 @@ mod tests {
let result = format_diff_like_column(
ahead,
behind,
- digits,
- total,
- DiffVariant::Arrows,
- ADDITION,
- dim_deletion,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Arrows,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: false,
+ },
);
assert_eq!(result.width(), total);
}
@@ -754,23 +810,84 @@ mod tests {
let empty = format_diff_like_column(
0,
0,
- digits,
- total,
- DiffVariant::Arrows,
- ADDITION,
- dim_deletion,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Arrows,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: false,
+ },
);
assert_eq!(empty.width(), total);
let behind_only = format_diff_like_column(
0,
50,
- digits,
- total,
- DiffVariant::Arrows,
- ADDITION,
- dim_deletion,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Arrows,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: false,
+ },
);
assert_eq!(behind_only.width(), total);
}
+
+ #[test]
+ fn test_always_show_zeros_renders_zero_values() {
+ use super::super::columns::DiffVariant;
+ use worktrunk::styling::{ADDITION, DELETION};
+
+ let digits = DiffDigits {
+ added: 1,
+ deleted: 1,
+ };
+ let total = 7;
+
+ let dim_deletion = DELETION.dimmed();
+
+ // With always_show_zeros=false, (0, 0) renders as blank
+ let without = format_diff_like_column(
+ 0,
+ 0,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Arrows,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: false,
+ },
+ );
+ assert_eq!(without.width(), total);
+ let rendered_without = without.render();
+ let ansi_escape = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
+ let clean_without = ansi_escape.replace_all(&rendered_without, "");
+ assert_eq!(clean_without.as_ref(), " ", "Should render as blank");
+
+ // With always_show_zeros=true, (0, 0) renders as "↑0 ↓0"
+ let with = format_diff_like_column(
+ 0,
+ 0,
+ DiffColumnConfig {
+ digits,
+ total_width: total,
+ variant: DiffVariant::Arrows,
+ positive_style: ADDITION,
+ negative_style: dim_deletion,
+ always_show_zeros: true,
+ },
+ );
+ assert_eq!(with.width(), total);
+ let rendered_with = with.render();
+ let clean_with = ansi_escape.replace_all(&rendered_with, "");
+ assert_eq!(
+ clean_with.as_ref(),
+ "↑0 ↓0 ",
+ "Should render ↑0 ↓0 with padding"
+ );
+ }
}
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index 094017fef..4eea58440 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -442,3 +442,158 @@ fn test_list_ordering_rules() {
// Run from feature-current worktree to test "current worktree" logic
snapshot_list_from_dir("list_ordering_rules", &repo, &current_path);
}
+
+#[test]
+fn test_list_with_upstream_tracking() {
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit on main");
+
+ // Set up remote - this already pushes main
+ repo.setup_remote("main");
+
+ // Scenario 1: Branch in sync with remote (should show ↑0 ↓0)
+ let in_sync_wt = repo.add_worktree("in-sync", "in-sync");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["push", "-u", "origin", "in-sync"])
+ .current_dir(&in_sync_wt)
+ .output()
+ .expect("Failed to push in-sync");
+
+ // Scenario 2: Branch ahead of remote (should show ↑2)
+ let ahead_wt = repo.add_worktree("ahead", "ahead");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["push", "-u", "origin", "ahead"])
+ .current_dir(&ahead_wt)
+ .output()
+ .expect("Failed to push ahead");
+
+ // Make 2 commits ahead
+ std::fs::write(ahead_wt.join("ahead1.txt"), "ahead 1").expect("Failed to write");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&ahead_wt)
+ .output()
+ .expect("Failed to add");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Ahead commit 1"])
+ .current_dir(&ahead_wt)
+ .output()
+ .expect("Failed to commit");
+
+ std::fs::write(ahead_wt.join("ahead2.txt"), "ahead 2").expect("Failed to write");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&ahead_wt)
+ .output()
+ .expect("Failed to add");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Ahead commit 2"])
+ .current_dir(&ahead_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Scenario 3: Branch behind remote (should show ↓1)
+ let behind_wt = repo.add_worktree("behind", "behind");
+ std::fs::write(behind_wt.join("behind.txt"), "behind").expect("Failed to write");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&behind_wt)
+ .output()
+ .expect("Failed to add");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Behind commit"])
+ .current_dir(&behind_wt)
+ .output()
+ .expect("Failed to commit");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["push", "-u", "origin", "behind"])
+ .current_dir(&behind_wt)
+ .output()
+ .expect("Failed to push");
+ // Reset local to one commit behind
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["reset", "--hard", "HEAD~1"])
+ .current_dir(&behind_wt)
+ .output()
+ .expect("Failed to reset");
+
+ // Scenario 4: Branch both ahead and behind (should show ↑1 ↓1)
+ let diverged_wt = repo.add_worktree("diverged", "diverged");
+ std::fs::write(diverged_wt.join("diverged.txt"), "diverged").expect("Failed to write");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to add");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Diverged remote commit"])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to commit");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["push", "-u", "origin", "diverged"])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to push");
+ // Reset and make different commit
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["reset", "--hard", "HEAD~1"])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to reset");
+ std::fs::write(diverged_wt.join("different.txt"), "different").expect("Failed to write");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "."])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to add");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "Diverged local commit"])
+ .current_dir(&diverged_wt)
+ .output()
+ .expect("Failed to commit");
+
+ // Scenario 5: Branch without upstream (should show blank)
+ repo.add_worktree("no-upstream", "no-upstream");
+
+ // Run list --branches --full to show all columns including Remote
+ let mut settings = Settings::clone_current();
+ settings.set_snapshot_path("../snapshots");
+
+ // Normalize paths
+ settings.add_filter(repo.root_path().to_str().unwrap(), "[REPO]");
+ for (name, path) in &repo.worktrees {
+ settings.add_filter(
+ path.to_str().unwrap(),
+ format!("[WORKTREE_{}]", name.to_uppercase().replace('-', "_")),
+ );
+ }
+ settings.add_filter(r"\b[0-9a-f]{7,40}\b", "[SHA] ");
+ settings.add_filter(r"\\", "/");
+
+ settings.bind(|| {
+ let mut cmd = Command::new(get_cargo_bin("wt"));
+ repo.clean_cli_env(&mut cmd);
+ cmd.arg("list")
+ .arg("--branches")
+ .arg("--full")
+ .current_dir(repo.root_path());
+ assert_cmd_snapshot!("with_upstream_tracking", cmd);
+ });
+}
diff --git a/tests/snapshots/integration__integration_tests__list__with_upstream_tracking.snap b/tests/snapshots/integration__integration_tests__list__with_upstream_tracking.snap
new file mode 100644
index 000000000..c2b47f7b1
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__list__with_upstream_tracking.snap
@@ -0,0 +1,35 @@
+---
+source: tests/integration_tests/list.rs
+info:
+ program: wt
+ args:
+ - list
+ - "--branches"
+ - "--full"
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: /dev/null
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpdwCP3X/test-config.toml
+---
+success: true
+exit_code: 0
+----- stdout -----
+Branch Main ↕ Main ± State Path Remote ↕ CI Commit Age Message
+main  ./test-repo  ↑0 ↓0 6c3da842 10 months ago Initial commit on main
+ahead ↑2 +2 ./ahead ↑2 ↓0 8d8f4673 10 months ago Ahead commit 2
+behind  (matches main) ./behind  ↑0 ↓1 6c3da842 10 months ago Initial commit on main
+diverged ↑1 +1 ./diverged ↑1 ↓1 ca7cb696 10 months ago Diverged local commit
+in-sync  (matches main) ./in-sync  ↑0 ↓0 6c3da842 10 months ago Initial commit on main
+no-upstream (matches main) ./no-upstream 6c3da842 10 months ago Initial commit on main
+
+Showing 6 worktrees, 2 ahead
+
+----- stderr -----