Skip to content

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

Extract remote branch filtering to Repository method

Maximilian Roose668bdd45e01
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index c089e14ff..086f47182 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -626,22 +626,6 @@ fn worktree_branch_set(worktrees: &[Worktree]) -> std::collections::HashSet<&str
.collect()
}
-/// Get remote branches that aren't tracked by any local branch.
-///
-/// Returns (branch_name, commit_sha) pairs for remote branches.
-/// 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()?;
-
- let remote_branches: Vec<_> = all_remote_branches
- .into_iter()
- .filter(|(remote_branch_name, _)| !tracked_upstreams.contains(remote_branch_name))
- .collect();
-
- Ok(remote_branches)
-}
-
/// Collect worktree data with optional progressive rendering.
///
/// When `show_progress` is true, renders a skeleton immediately and updates as data arrives.
@@ -695,7 +679,7 @@ pub fn collect(
},
|| {
if show_remotes {
- get_remote_branches(repo)
+ repo.list_untracked_remote_branches()
} else {
Ok(Vec::new())
}
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 33b1ea3c5..632435713 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -1018,6 +1018,46 @@ impl Repository {
Ok(upstreams)
}
+ /// List remote branches that aren't tracked by any local branch.
+ ///
+ /// Returns (branch_name, commit_sha) pairs for remote branches that have no
+ /// corresponding local tracking branch.
+ pub fn list_untracked_remote_branches(&self) -> anyhow::Result<Vec<(String, String)>> {
+ let all_remote_branches = self.list_remote_branches()?;
+ let tracked_upstreams = self.list_tracked_upstreams()?;
+
+ let remote_branches: Vec<_> = all_remote_branches
+ .into_iter()
+ .filter(|(remote_branch_name, _)| !tracked_upstreams.contains(remote_branch_name))
+ .collect();
+
+ Ok(remote_branches)
+ }
+
+ /// Get recent commit subjects for style reference.
+ ///
+ /// Returns up to `count` commit subjects (first line of message), excluding merges.
+ /// If `start_ref` is provided, gets commits starting from that ref.
+ /// Returns `None` if no commits are found or the command fails.
+ pub fn recent_commit_subjects(
+ &self,
+ start_ref: Option<&str>,
+ count: usize,
+ ) -> Option<Vec<String>> {
+ let count_str = count.to_string();
+ let mut args = vec!["log", "--pretty=format:%s", "-n", &count_str, "--no-merges"];
+ if let Some(ref_name) = start_ref {
+ args.push(ref_name);
+ }
+ self.run_command(&args).ok().and_then(|output| {
+ if output.trim().is_empty() {
+ None
+ } else {
+ Some(output.lines().map(String::from).collect())
+ }
+ })
+ }
+
/// 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 files
diff --git a/src/llm.rs b/src/llm.rs
index a47857630..0ce518c38 100644
--- a/src/llm.rs
+++ b/src/llm.rs
@@ -192,24 +192,6 @@ fn format_command_display(command: &str, args: &[String]) -> String {
}
}
-/// Get recent commit subjects for style reference.
-///
-/// If `start_ref` is provided, gets commits starting from that ref (exclusive).
-/// Otherwise gets commits from HEAD.
-fn get_recent_commits(repo: &Repository, start_ref: Option<&str>) -> Option<Vec<String>> {
- let mut args = vec!["log", "--pretty=format:%s", "-n", "5", "--no-merges"];
- if let Some(ref_name) = start_ref {
- args.push(ref_name);
- }
- repo.run_command(&args).ok().and_then(|output| {
- if output.trim().is_empty() {
- None
- } else {
- Some(output.lines().map(String::from).collect())
- }
- })
-}
-
/// Default template for commit message prompts
///
/// Synced to dev/config.example.toml by `cargo test readme_sync`
@@ -523,7 +505,7 @@ pub fn build_commit_prompt(config: &CommitGenerationConfig) -> anyhow::Result<St
.and_then(|n| n.to_str())
.unwrap_or("repo");
- let recent_commits = get_recent_commits(&repo, None);
+ let recent_commits = repo.recent_commit_subjects(None, 5);
let context = TemplateContext {
git_diff: &prepared.diff,
@@ -611,7 +593,7 @@ pub fn build_squash_prompt(
// Prepare diff (may filter if too large)
let prepared = prepare_diff(diff_output, diff_stat);
- let recent_commits = get_recent_commits(&repo, Some(merge_base));
+ let recent_commits = repo.recent_commit_subjects(Some(merge_base), 5);
let context = TemplateContext {
git_diff: &prepared.diff,
git_diff_stat: &prepared.stat,