Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Handle orphan branches in has_added_changes
Maximilian Roos7aa292a30b32
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 15ae470fc..b0e87ff00 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -689,12 +689,36 @@ impl Repository {/// Uses [three-dot diff][1] (`target...branch`) which shows files changed from/// merge-base to branch. Returns false when the diff is empty (no added changes).///+ /// For orphan branches (no common ancestor with target), returns true since all+ /// their changes are unique.+ ////// [1]: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgtltcommitgt--telepathhellippub fn has_added_changes(&self, branch: &str, target: &str) -> anyhow::Result<bool> {+ use crate::shell_exec::run;+// git diff --name-only target...branch shows files changed from merge-base to branchlet range = format!("{target}...{branch}");- let stdout = self.run_command(&["diff", "--name-only", &range])?;- Ok(!stdout.trim().is_empty())+ let mut cmd = std::process::Command::new("git");+ cmd.args(["diff", "--name-only", &range]);+ cmd.current_dir(&self.path);++ let output = run(&mut cmd, Some(&self.logging_context()))+ .with_context(|| format!("Failed to execute: git diff --name-only {range}"))?;++ if output.status.success() {+ let stdout = String::from_utf8_lossy(&output.stdout);+ Ok(!stdout.trim().is_empty())+ } else {+ let stderr = String::from_utf8_lossy(&output.stderr);+ // Orphan branches have no merge base with target - they definitely have unique changes.+ // Alternative: pre-check with `git merge-base`, but that adds a git call for every+ // branch. String matching here only costs extra for the rare orphan branch case.+ if stderr.contains("no merge base") {+ Ok(true)+ } else {+ bail!("{}", stderr.trim())+ }+ }}/// Count commits between base and head.diff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex c39a3e805..6459beff6 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -2601,3 +2601,38 @@ fn test_list_shows_warning_on_git_error(mut repo: TestRepo) {snapshot_list("git_error_warning", &repo);}++/// Test that orphan branches (no common ancestor with main) are handled gracefully.+///+/// Creates a true orphan branch using `git checkout --orphan` which has no merge base+/// with main. Verifies no error warning appears and the branch shows as unmerged.+#[rstest]+fn test_list_handles_orphan_branch(repo: TestRepo) {+ // Create an orphan branch (no common ancestor with main)+ repo.git_command(&["checkout", "--orphan", "assets"])+ .output()+ .unwrap();++ // Clear working tree and create new content+ repo.git_command(&["rm", "-rf", "."]).output().unwrap();+ std::fs::write(repo.root_path().join("asset.txt"), "asset content\n").unwrap();+ repo.git_command(&["add", "."]).output().unwrap();+ repo.git_command(&["commit", "-m", "Add asset"])+ .output()+ .unwrap();++ // Go back to main+ repo.git_command(&["checkout", "main"]).output().unwrap();++ // Verify no merge base exists (this confirms we have a true orphan branch)+ let output = repo+ .git_command(&["merge-base", "main", "assets"])+ .output()+ .unwrap();+ assert!(+ !output.status.success(),+ "Expected no merge base for orphan branch"+ );++ snapshot_list_with_branches("orphan_branch_no_error", &repo);+}diff --git a/tests/snapshots/integration__integration_tests__list__orphan_branch_no_error.snap b/tests/snapshots/integration__integration_tests__list__orphan_branch_no_error.snapnew file mode 100644index 000000000..5639875bd--- /dev/null+++ b/tests/snapshots/integration__integration_tests__list__orphan_branch_no_error.snap@@ -0,0 +1,37 @@+---+source: tests/integration_tests/list.rs+info:+ program: wt+ args:+ - list+ - "--branches"+ 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]"+ RUST_LOG: warn+ SOURCE_DATE_EPOCH: "1735776000"+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----+ [1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage+@ main [2m^[22m . [2ma1e809f5[0m [2m1d[0m [2mInitial commit+ assets [2m/[22m[33m✗[39m [32m↑1[0m [2m[31m↓1[0m [2m50209039[0m [2m1d[0m [2mAdd asset++[2m○[22m [2mShowing 1 worktrees, 1 branches, 1 ahead++----- stderr -----