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.rsindex 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.rsindex 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.rsindex 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.rsindex 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 whendiff --git a/tests/integration_tests/step_prune.rs b/tests/integration_tests/step_prune.rsindex 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.snapindex 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/mainworktree [PATH]HEAD [COMMIT]+detachedworktree [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.snapindex 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: CLC_ALL: C+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"NO_COLOR: ""OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"PSModulePath: ""- RUST_LOG: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[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: trueexit_code: 0----- stdout -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mRemote⇅[0m [1mPath[0m [1mCommit[0m [1mAge[0m [1mMessage-@ main [2m^[22m . [2m·[0m [2m·+@ main [2m·[0m [2m·[0m[2m^[22m [2m·[0m . [2m·[0m [2m·[2m○[22m [2mShowing 1 worktreediff --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.snapindex 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: CLC_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: warnSHELL: ""TERM: alacrittyUSERPROFILE: "[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: trueexit_code: 0----- stdout -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mRemote⇅[0m [1mPath[0m [1mCommit[0m [1mAge[0m [1mMessage-@ main [2m^[22m[2m|[22m [2m|[0m . [2m05a4a45d[0m [2m16h[0m [2mInitial commit-+ feature-a [2m↑[22m [32m↑1[0m ../repo.feature-a [2m1b87d473[0m [2m16h[0m [2mAdd feature-a file-+ feature-b [2m↑[22m [32m↑1[0m ../repo.feature-b [2mf62940fc[0m [2m16h[0m [2mAdd feature-b file-+ feature-c [2m↑[22m [32m↑1[0m ../repo.feature-c [2m345c7c93[0m [2m16h[0m [2mAdd feature-c file-+ feature [2m·[0m [2m·[0m [2m·[0m [2m·[0m ../repo.feature [2m·[0m [2m·+@ main [2m^[22m[2m·[0m [2m·[0m . [2m05a4a45d[0m [2m·[0m [2m·++ feature [2m·[0m [2m·[0m[2m·[0m[2m·[0m [2m·[0m [2m·[0m [2m·[0m ../repo.feature [2m00000000[0m [2m·[0m [2m·++ feature-a [2m↑[22m[2m·[0m [32m↑1[0m [2m·[0m ../repo.feature-a [2m1b87d473[0m [2m·[0m [2m·++ feature-b [2m↑[22m[2m·[0m [32m↑1[0m [2m·[0m ../repo.feature-b [2mf62940fc[0m [2m·[0m [2m·++ feature-c [2m↑[22m[2m·[0m [32m↑1[0m [2m·[0m ../repo.feature-c [2m345c7c93[0m [2m·[0m [2m·-[2m○[22m [2mShowing 5 worktrees, 3 ahead. 2 tasks failed+[2m○[22m [2mShowing 5 worktrees, 3 ahead. 9 tasks failed----- stderr -----+[33m▲[39m [33mFailed to batch-fetch commit details: fatal: bad object 0000000000000000000000000000000000000001[39m[33m▲[39m [33mSome git operations failed:+[107m [0m [1mmain[22m: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)+[107m [0m [1mfeature[22m: ahead-behind (git merge-base failed for 05a4a45d0b981dad5c27db59dca482836d59f89e 0000000000000000000000000000000000000001: fatal: Not a valid commit name 0000000000000000000000000000000000000001)[107m [0m [1mfeature[22m: working-tree-diff (fatal: bad object HEAD)-[107m [0m [1mfeature[22m: working-tree-conflicts (fatal: bad object HEAD)[39m+[107m [0m [1mfeature[22m: merge-tree-conflicts (fatal: bad object HEAD)+[107m [0m [1mfeature[22m: working-tree-conflicts (fatal: bad object HEAD)+[107m [0m [1mfeature[22m: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)+[107m [0m [1mfeature-a[22m: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)+[107m [0m [1mfeature-b[22m: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)+[107m [0m [1mfeature-c[22m: upstream (fatal: missing object 0000000000000000000000000000000000000001 for refs/heads/feature)[39m[2m↳[22m [2mTo create a diagnostic file, run with [4m-vv[24m[22m