Skip to content

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

fix(list, prune): degrade gracefully on unborn worktrees (#2937)

Worktrunk Botf41614322b12
diff --git a/src/commands/list/collect/execution.rs b/src/commands/list/collect/execution.rs
index 7539d04f0..8f27d62c7 100644
--- a/src/commands/list/collect/execution.rs
+++ b/src/commands/list/collect/execution.rs
@@ -37,6 +37,13 @@ use super::types::{TaskError, TaskKind, TaskResult};
/// Tasks that require a valid commit SHA. Skipped for unborn branches (no commits yet).
/// Without this, these tasks would fail on the null OID and show as errors in the table.
+///
+/// `WorkingTreeDiff` runs `git diff HEAD` and `WorkingTreeConflicts` runs
+/// `git merge-tree` against the worktree's HEAD SHA — both fail on a null OID
+/// with `fatal: ambiguous argument 'HEAD'` / `fatal: 0000… is not a valid
+/// object`. Their seed defaults (`None` for the diff, `Some(None)` for the
+/// conflict probe) render as `·` placeholders, matching how the other
+/// commit-dependent columns degrade on unborn worktrees.
const COMMIT_TASKS: &[TaskKind] = &[
TaskKind::AheadBehind,
TaskKind::CommittedTreesMatch,
@@ -47,6 +54,8 @@ const COMMIT_TASKS: &[TaskKind] = &[
TaskKind::WouldMergeAdd,
TaskKind::CiStatus,
TaskKind::Upstream,
+ TaskKind::WorkingTreeDiff,
+ TaskKind::WorkingTreeConflicts,
];
// ============================================================================
diff --git a/src/commands/step/prune.rs b/src/commands/step/prune.rs
index 46708680b..03c7904be 100644
--- a/src/commands/step/prune.rs
+++ b/src/commands/step/prune.rs
@@ -418,6 +418,15 @@ fn gather_check_items(
continue;
}
+ // Unborn worktrees (`git worktree add --orphan`, HEAD = null OID)
+ // have no commits to integrate, so `integration_reason` would abort
+ // the whole prune scan with `fatal: Needed a single revision` from
+ // `git rev-parse` on the unborn branch. Skip them — they're never
+ // auto-prunable.
+ if !wt.has_commits() {
+ continue;
+ }
+
if wt.is_prunable() {
let integration_ref = wt.branch.clone().unwrap_or_else(|| wt.head.clone());
check_items.push(CheckItem {
diff --git a/tests/integration_tests/diagnostic.rs b/tests/integration_tests/diagnostic.rs
index f56659f7e..ff98dbf72 100644
--- a/tests/integration_tests/diagnostic.rs
+++ b/tests/integration_tests/diagnostic.rs
@@ -24,6 +24,10 @@ use rstest::rstest;
use crate::common::{TestRepo, repo, setup_snapshot_settings};
/// Helper to corrupt a worktree's HEAD file to trigger git errors.
+///
+/// Writes a non-null invalid SHA so the worktree is treated as "valid but
+/// broken" (errors surface) rather than the NULL_OID-keyed unborn path,
+/// which now skips commit-dependent tasks silently (see #2936).
fn corrupt_worktree_head(repo: &TestRepo, worktree_name: &str) -> PathBuf {
let feature_path = repo.worktrees.get(worktree_name).unwrap();
let git_dir = feature_path.join(".git");
@@ -34,7 +38,7 @@ fn corrupt_worktree_head(repo: &TestRepo, worktree_name: &str) -> PathBuf {
.trim()
.to_string();
let head_path = PathBuf::from(&actual_git_dir).join("HEAD");
- fs::write(&head_path, "invalid").unwrap();
+ fs::write(&head_path, "0000000000000000000000000000000000000001\n").unwrap();
head_path
}
diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rs
index bb4cca791..1896738c3 100644
--- a/tests/integration_tests/list.rs
+++ b/tests/integration_tests/list.rs
@@ -3000,6 +3000,12 @@ fn test_list_warns_when_default_branch_missing_worktree(repo: TestRepo) {
/// Corrupts a branch ref to point to a non-existent commit, which causes
/// ahead_behind and other git operations to fail. Verifies the warning
/// message appears after the table.
+///
+/// The dummy SHA is `…001` (non-null) on purpose: a NULL_OID branch ref
+/// reads as an unborn worktree and is handled by the commit-task skip path
+/// (no warning, by design — see #2936). A non-null invalid SHA still flows
+/// through the regular task path and surfaces the warning this test
+/// validates.
#[rstest]
fn test_list_shows_warning_on_git_error(mut repo: TestRepo) {
repo.add_worktree("feature");
@@ -3009,8 +3015,8 @@ fn test_list_shows_warning_on_git_error(mut repo: TestRepo) {
let git_dir = repo.root_path().join(".git");
let ref_path = git_dir.join("refs/heads/feature");
- // Write an invalid SHA that doesn't exist in the repo
- std::fs::write(&ref_path, "0000000000000000000000000000000000000000\n").unwrap();
+ // Non-null invalid SHA so the worktree is not treated as unborn.
+ std::fs::write(&ref_path, "0000000000000000000000000000000000000001\n").unwrap();
assert_cmd_snapshot!(list_snapshots::command(&repo, repo.root_path()));
}
@@ -3255,6 +3261,62 @@ fn test_list_empty_repo_json() {
assert_eq!(item["commit"]["short_sha"], "");
}
+/// Regression for #2936: a linked worktree on an unborn branch (`git worktree
+/// add --orphan`) reports `HEAD 0000…` in `worktree list --porcelain`. The
+/// working-tree-diff and working-tree-conflicts tasks both reference HEAD /
+/// the null OID and fail with `fatal: ambiguous argument 'HEAD'` and
+/// `fatal: 0000… is not a valid object`. Adding untracked content to the
+/// orphan worktree forces both code paths to run.
+#[rstest]
+fn test_list_unborn_worktree_no_task_failures(repo: TestRepo) {
+ repo.commit("initial");
+
+ let orphan_path = repo.root_path().parent().unwrap().join("repo.orphan");
+ let out = repo
+ .git_command()
+ .args([
+ "worktree",
+ "add",
+ "--orphan",
+ "-b",
+ "orphan",
+ orphan_path.to_str().unwrap(),
+ ])
+ .run()
+ .unwrap();
+ assert!(
+ out.status.success(),
+ "git worktree add --orphan failed: {}\n{}",
+ String::from_utf8_lossy(&out.stdout),
+ String::from_utf8_lossy(&out.stderr)
+ );
+ // Untracked content forces WorkingTreeConflictsTask to run merge-tree
+ // against the null OID rather than short-circuiting on a clean tree.
+ std::fs::write(orphan_path.join("untracked.txt"), "content\n").unwrap();
+
+ let output = repo
+ .wt_command()
+ .args(["list"])
+ .current_dir(repo.root_path())
+ .output()
+ .unwrap();
+
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ !stderr.contains("working-tree-diff") && !stderr.contains("working-tree-conflicts"),
+ "wt list should not surface working-tree-* task failures for unborn worktrees, got stderr:\n{stderr}"
+ );
+ assert!(
+ !stderr.contains("ambiguous argument 'HEAD'")
+ && !stderr.contains("0000000000000000000000000000000000000000"),
+ "wt list should not run HEAD/null-OID commands against unborn worktrees, got stderr:\n{stderr}"
+ );
+ assert!(
+ output.status.success(),
+ "wt list should succeed; stderr:\n{stderr}"
+ );
+}
+
/// Regression: the `wt list` integration column ORs over local AND upstream,
/// matching `Repository::integration_reason`. A branch whose content lives in
/// LOCAL `main` (via a merge commit) must still render as integrated when
diff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rs
index 6e99da442..1e587a8e1 100644
--- a/tests/integration_tests/step_prune.rs
+++ b/tests/integration_tests/step_prune.rs
@@ -310,6 +310,66 @@ fn test_prune_skips_locked(mut repo: TestRepo) {
);
}
+/// Regression for #2936: an unborn worktree (`git worktree add --orphan`) has
+/// HEAD = `0000…`, so its branch name doesn't resolve via `git rev-parse`.
+/// Prune used to abort with `fatal: Needed a single revision` while checking
+/// branch integration. The conservative fix is to skip such worktrees from
+/// the prune candidate set — they have no commits to integrate.
+#[rstest]
+fn test_prune_skips_unborn_worktree(mut repo: TestRepo) {
+ repo.commit("initial");
+
+ // Merged worktree — should still be pruned alongside the unborn one.
+ repo.add_worktree("merged-branch");
+
+ let orphan_path = repo.root_path().parent().unwrap().join("repo.orphan");
+ let out = repo
+ .git_command()
+ .args([
+ "worktree",
+ "add",
+ "--orphan",
+ "-b",
+ "orphan",
+ orphan_path.to_str().unwrap(),
+ ])
+ .run()
+ .unwrap();
+ assert!(
+ out.status.success(),
+ "git worktree add --orphan failed: {}\n{}",
+ String::from_utf8_lossy(&out.stdout),
+ String::from_utf8_lossy(&out.stderr)
+ );
+
+ let output = repo
+ .wt_command()
+ .args(["step", "prune", "--yes", "--min-age=0s"])
+ .current_dir(repo.root_path())
+ .output()
+ .unwrap();
+
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ !stderr.contains("Needed a single revision"),
+ "wt step prune should not bail on unborn worktrees, got stderr:\n{stderr}"
+ );
+ assert!(
+ output.status.success(),
+ "wt step prune should succeed; stderr:\n{stderr}"
+ );
+
+ let parent = repo.root_path().parent().unwrap();
+ assert!(
+ !parent.join("repo.merged-branch").exists(),
+ "merged worktree should still be pruned"
+ );
+ assert!(
+ orphan_path.exists(),
+ "unborn worktree is skipped (not a prune candidate), so it should remain"
+ );
+}
+
/// Prune deletes orphan branches (integrated branches without worktrees).
///
/// Two orphan branches exercise the "N branches" plural path in the summary.
diff --git a/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_file_format.snap b/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_file_format.snap
index 1b35fe28a..e984646d2 100644
--- a/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_file_format.snap
+++ b/tests/snapshots/integration__integration_tests__diagnostic__diagnostic_file_format.snap
@@ -28,6 +28,7 @@ branch refs/heads/main
worktree [PATH]
HEAD [COMMIT]
+detached
worktree [PATH]
HEAD [COMMIT]
diff --git a/tests/snapshots/integration__integration_tests__list__list_empty_repo.snap b/tests/snapshots/integration__integration_tests__list__list_empty_repo.snap
index 369260b6a..9f2d49a69 100644
--- a/tests/snapshots/integration__integration_tests__list__list_empty_repo.snap
+++ b/tests/snapshots/integration__integration_tests__list__list_empty_repo.snap
@@ -10,37 +10,48 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
NO_COLOR: ""
OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
PSModulePath: ""
- RUST_LOG: warn
SHELL: ""
TERM: alacritty
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_CODEX_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
+ WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
exit_code: 0
----- stdout -----
Branch Status HEAD± main↕ Remote⇅ Path Commit Age Message
-@ main ^ . · ·
+@ main · ·^ · . · ·
○ Showing 1 worktree
diff --git a/tests/snapshots/integration__integration_tests__list__list_shows_warning_on_git_error.snap b/tests/snapshots/integration__integration_tests__list__list_shows_warning_on_git_error.snap
index 899d2728e..6c4597342 100644
--- a/tests/snapshots/integration__integration_tests__list__list_shows_warning_on_git_error.snap
+++ b/tests/snapshots/integration__integration_tests__list__list_shows_warning_on_git_error.snap
@@ -10,48 +10,67 @@ info:
COLUMNS: "500"
GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMON_DIR: ""
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
+ GIT_DIR: ""
GIT_EDITOR: ""
+ GIT_INDEX_FILE: ""
+ GIT_OBJECT_DIRECTORY: ""
GIT_TERMINAL_PROMPT: "0"
+ GIT_WORK_TREE: ""
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
NO_COLOR: ""
OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
PATH: "[PATH]"
PSModulePath: ""
- RUST_LOG: warn
SHELL: ""
TERM: alacritty
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_CODEX_INSTALLED: "0"
WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
+ WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
WORKTRUNK_TEST_NUSHELL_ENV: "0"
WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: true
exit_code: 0
----- stdout -----
Branch Status HEAD± main↕ Remote⇅ Path Commit Age Message
-@ main ^| | . 05a4a45d 16h Initial commit
-+ feature-a ↑ ↑1 ../repo.feature-a 1b87d473 16h Add feature-a file
-+ feature-b ↑ ↑1 ../repo.feature-b f62940fc 16h Add feature-b file
-+ feature-c ↑ ↑1 ../repo.feature-c 345c7c93 16h Add feature-c file
-+ feature · · · · ../repo.feature · ·
+@ main ^· · . 05a4a45d · ·
++ feature · ··· · · · ../repo.feature 00000000 · ·
++ feature-a ↑· ↑1 · ../repo.feature-a 1b87d473 · ·
++ feature-b ↑· ↑1 · ../repo.feature-b f62940fc · ·
++ feature-c ↑· ↑1 · ../repo.feature-c 345c7c93 · ·
-○ Showing 5 worktrees, 3 ahead. 2 tasks failed
+○ Showing 5 worktrees, 3 ahead. 9 tasks failed
----- stderr -----
+▲ Failed to batch-fetch commit details: fatal: bad object 0000000000000000000000000000000000000001
▲ Some git operations failed:
+  main: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)
+  feature: ahead-behind (git merge-base failed for 05a4a45d0b981dad5c27db59dca482836d59f89e 0000000000000000000000000000000000000001: fatal: Not a valid commit name 0000000000000000000000000000000000000001)
  feature: working-tree-diff (fatal: bad object HEAD)
-  feature: working-tree-conflicts (fatal: bad object HEAD)
+  feature: merge-tree-conflicts (fatal: bad object HEAD)
+  feature: working-tree-conflicts (fatal: bad object HEAD)
+  feature: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)
+  feature-a: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)
+  feature-b: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)
+  feature-c: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)
↳ To create a diagnostic file, run with -vv