Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor list task error handling and add integration test
Maximilian Roos783b3eeb3a81
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex a17cc78f3..cbe507a60 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -57,7 +57,7 @@ struct StatusContext {#[strum_discriminants(name(TaskKind),vis(pub),- derive(Hash, strum::IntoStaticStr),+ derive(Hash, Ord, PartialOrd, strum::IntoStaticStr),strum(serialize_all = "kebab-case"))]pub(super) enum TaskResult {@@ -1005,11 +1005,15 @@ pub fn collect(// Display collection errors as warnings (after table rendering)if !errors.is_empty() {+ // Sort for deterministic output (tasks complete in arbitrary order)+ errors.sort_by_key(|e| (e.item_idx, e.kind));let mut warning = String::from("Some git operations failed:");for error in &errors {let name = all_items[error.item_idx].branch_name();let kind_str: &'static str = error.kind.into();- warning.push_str(&format!("\n - {}: {} ({})", name, kind_str, error.message));+ // Take first line only - git errors can be multi-line with usage hints+ let msg = error.message.lines().next().unwrap_or(&error.message);+ warning.push_str(&format!("\n - {}: {} ({})", name, kind_str, msg));}crate::output::print(warning_message(warning))?;}diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 75911aa22..a334f3501 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -2455,3 +2455,23 @@ fn test_list_warns_when_default_branch_missing_worktree(repo: TestRepo) {snapshot_list("default_branch_missing_worktree", &repo);}++/// Test that git errors during task execution are collected and displayed as warnings.+///+/// 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.+#[rstest]+fn test_list_shows_warning_on_git_error(mut repo: TestRepo) {+ repo.add_worktree("feature");++ // Corrupt the feature branch ref to point to a non-existent commit.+ // Branch refs are stored in the main repo's .git/refs/heads, not the worktree.+ 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();++ snapshot_list("git_error_warning", &repo);+}diff --git a/tests/snapshots/integration__integration_tests__list__git_error_warning.snap b/tests/snapshots/integration__integration_tests__list__git_error_warning.snapnew file mode 100644index 000000000..d809fc019--- /dev/null+++ b/tests/snapshots/integration__integration_tests__list__git_error_warning.snap@@ -0,0 +1,41 @@+---+source: tests/integration_tests/list.rs+info:+ program: wt+ args:+ - list+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "150"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ PATH: "[PATH]"+ SOURCE_DATE_EPOCH: "1735776000"+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+ [1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage+@ [1mmain[0m [2m^[22m [1m./repo[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ feature [33m✗[39m ./repo.feature [2m00000000[0m [2m55y[0m [2m++⚪ [2mShowing 2 worktrees+🟡 [33mSome git operations failed:+ - feature: commit-details (fatal: bad object 0000000000000000000000000000000000000000)+ - feature: ahead-behind (fatal: Invalid symmetric difference expression main...0000000000000000000000000000000000000000)+ - feature: committed-trees-match (fatal: ambiguous argument '0000000000000000000000000000000000000000^{tree}': unknown revision or path not in the working tree.)+ - feature: has-file-changes (fatal: Invalid symmetric difference expression main...feature)+ - feature: working-tree-diff (fatal: bad object HEAD)[39m