Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Filter remote branches by upstream tracking instead of worktrees
Maximilian Roos7789853188b3
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rsindex 29dfdd002..972e7230b 100644--- a/src/commands/list/collect.rs+++ b/src/commands/list/collect.rs@@ -626,34 +626,17 @@ fn worktree_branch_set(worktrees: &[Worktree]) -> std::collections::HashSet<&str.collect()}-/// Get remote branches from all remotes that don't have local worktrees.+/// Get remote branches that aren't tracked by any local branch.////// Returns (branch_name, commit_sha) pairs for remote branches.-/// Filters out branches that already have worktrees (whether the worktree is on the-/// local tracking branch or not).-fn get_remote_branches(- repo: &Repository,- worktrees: &[Worktree],-) -> anyhow::Result<Vec<(String, String)>> {- // Get all remote branches from all remotes+/// Filters out branches that are set as upstream for any local branch.+fn get_remote_branches(repo: &Repository) -> anyhow::Result<Vec<(String, String)>> {let all_remote_branches = repo.list_remote_branches()?;+ let tracked_upstreams = repo.list_tracked_upstreams()?;- // Build a set of branch names that have worktrees- let worktree_branches = worktree_branch_set(worktrees);-- // Filter to remote branches whose local equivalent doesn't have a worktreelet remote_branches: Vec<_> = all_remote_branches.into_iter()- .filter(|(remote_branch_name, _)| {- // First '/' separates remote from branch: "origin/feature/foo" → "feature/foo"- if let Some((_, local_name)) = remote_branch_name.split_once('/') {- // Include remote branch if local branch doesn't have a worktree- !worktree_branches.contains(local_name)- } else {- // Skip branches without a remote prefix- false- }- })+ .filter(|(remote_branch_name, _)| !tracked_upstreams.contains(remote_branch_name)).collect();Ok(remote_branches)@@ -712,7 +695,7 @@ pub fn collect(},|| {if show_remotes {- get_remote_branches(repo, &worktrees.worktrees)+ get_remote_branches(repo)} else {Ok(Vec::new())}diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex b0e87ff00..72685a160 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -993,6 +993,23 @@ impl Repository {Ok(branches)}+ /// List all upstream tracking refs that local branches are tracking.+ ///+ /// Returns a set of upstream refs like "origin/main", "origin/feature".+ /// Useful for filtering remote branches to only show those not tracked locally.+ pub fn list_tracked_upstreams(&self) -> anyhow::Result<std::collections::HashSet<String>> {+ let output =+ self.run_command(&["for-each-ref", "--format=%(upstream:short)", "refs/heads/"])?;++ let upstreams: std::collections::HashSet<String> = output+ .lines()+ .filter(|line| !line.is_empty())+ .map(|line| line.to_string())+ .collect();++ Ok(upstreams)+ }+/// Get line diff statistics for working tree changes (unstaged + staged).pub fn working_tree_diff_stats(&self) -> anyhow::Result<LineDiff> {// Limit concurrent diff operations to reduce mmap thrash on pack filesdiff --git a/tests/integration_tests/list.rs b/tests/integration_tests/list.rsindex 6459beff6..146a57f5e 100644--- a/tests/integration_tests/list.rs+++ b/tests/integration_tests/list.rs@@ -464,10 +464,16 @@ fn test_list_with_remotes_and_branches(#[from(repo_with_remote)] repo: TestRepo)}#[rstest]-fn test_list_with_remotes_filters_existing_worktrees(#[from(repo_with_remote)] mut repo: TestRepo) {- // Create a worktree and push the branch+fn test_list_with_remotes_filters_tracked_worktrees(#[from(repo_with_remote)] mut repo: TestRepo) {+ // Create a worktree and push with trackingrepo.add_worktree("feature-with-worktree");- repo.push_branch("feature-with-worktree");+ let feature_path = repo.worktree_path("feature-with-worktree");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["push", "-u", "origin", "feature-with-worktree"])+ .current_dir(feature_path)+ .output()+ .unwrap();// Create a branch, push it, delete it locally (remote-only)repo.create_branch("remote-only");@@ -482,9 +488,38 @@ fn test_list_with_remotes_filters_existing_worktrees(#[from(repo_with_remote)] m// Should show:// - main worktree// - feature-with-worktree worktree- // - origin/remote-only (remote branch without local worktree)- // Should NOT show origin/main or origin/feature-with-worktree (both have worktrees)- snapshot_list_with_remotes("with_remotes_filters_worktrees", &repo);+ // - origin/remote-only (remote branch not tracked by any local branch)+ // Should NOT show origin/main or origin/feature-with-worktree (both tracked)+ snapshot_list_with_remotes("with_remotes_filters_tracked_worktrees", &repo);+}++#[rstest]+fn test_list_with_remotes_filters_tracked_branches(#[from(repo_with_remote)] repo: TestRepo) {+ // Create a local branch (no worktree) and push with tracking+ repo.create_branch("tracked-branch");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["push", "-u", "origin", "tracked-branch"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ // Create a branch, push it, then delete locally (remote-only)+ repo.create_branch("remote-only");+ repo.push_branch("remote-only");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["branch", "-D", "remote-only"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ // Should show:+ // - main worktree+ // - origin/remote-only (remote branch not tracked by any local branch)+ // Should NOT show origin/main (tracked by main + has worktree)+ // Should NOT show origin/tracked-branch (tracked by local tracked-branch)+ snapshot_list_with_remotes("with_remotes_filters_tracked_branches", &repo);}#[rstest]diff --git a/tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_branches.snap b/tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_branches.snapnew file mode 100644index 000000000..6a5d51d24--- /dev/null+++ b/tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_branches.snap@@ -0,0 +1,37 @@+---+source: tests/integration_tests/list.rs+info:+ program: wt+ args:+ - list+ - "--remotes"+ 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[2m|[22m . [2m|[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit+ [2morigin/remote-only[0m [2m/[22m[2m_[22m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++[2m○[22m [2mShowing 1 worktrees, 1 remote branches++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__list__with_remotes_filters_worktrees.snap b/tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_worktrees.snapsimilarity index 87%rename from tests/snapshots/integration__integration_tests__list__with_remotes_filters_worktrees.snaprename to tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_worktrees.snapindex e1185f229..b11da5e4b 100644--- a/tests/snapshots/integration__integration_tests__list__with_remotes_filters_worktrees.snap+++ b/tests/snapshots/integration__integration_tests__list__with_remotes_filters_tracked_worktrees.snap@@ -30,7 +30,7 @@ exit_code: 0----- stdout -----[1mBranch[0m [1mStatus[0m [1mHEAD±[0m [1mmain↕[0m [1mPath[0m [1mRemote⇅[0m [1mCommit[0m [1mAge[0m [1mMessage@ main [2m^[22m[2m|[22m . [2m|[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit-+ [2mfeature-with-worktree[0m [2m_[22m [2m../repo.feature-with-worktree[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit++ [2mfeature-with-worktree[0m [2m_[22m[2m|[22m [2m../repo.feature-with-worktree[0m [2m|[0m [2ma1e809f5[0m [2m1d[0m [2mInitial commit[2morigin/remote-only[0m [2m/[22m[2m_[22m [2ma1e809f5[0m [2m1d[0m [2mInitial commit[2m○[22m [2mShowing 2 worktrees, 1 remote branches