Skip to content

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

refactor: Extract DiffStats to shared git/diff module

Maximilian Roos97af77d835c8
diff --git a/src/commands/dev.rs b/src/commands/dev.rs
index fbaacbe7b..7c98eceeb 100644
--- a/src/commands/dev.rs
+++ b/src/commands/dev.rs
@@ -1,6 +1,6 @@
use worktrunk::HookType;
use worktrunk::config::{ProjectConfig, WorktrunkConfig};
-use worktrunk::git::{GitError, GitResultExt, Repository};
+use worktrunk::git::{GitError, GitResultExt, Repository, parse_diff_shortstat};
use worktrunk::styling::{
AnstyleStyle, CYAN, CYAN_BOLD, HINT, HINT_EMOJI, eprintln, format_with_gutter,
};
@@ -9,9 +9,7 @@ use super::merge::{
commit_staged_changes, execute_post_merge_commands, format_commit_message_for_display,
run_pre_commit_commands, run_pre_merge_commands, show_llm_config_hint_if_needed,
};
-use super::worktree::{
- execute_post_create_commands, execute_post_start_commands_sequential, parse_diff_shortstat,
-};
+use super::worktree::{execute_post_create_commands, execute_post_start_commands_sequential};
/// Handle `wt dev run-hook` command
pub fn handle_dev_run_hook(hook_type: HookType, force: bool) -> Result<(), GitError> {
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index ba3a209cb..c70b558fc 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -1,6 +1,6 @@
use worktrunk::HookType;
use worktrunk::config::{Command, CommandPhase, ProjectConfig, WorktrunkConfig};
-use worktrunk::git::{GitError, GitResultExt, Repository};
+use worktrunk::git::{GitError, GitResultExt, Repository, parse_diff_shortstat};
use worktrunk::styling::{
AnstyleStyle, CYAN, CYAN_BOLD, ERROR, ERROR_EMOJI, HINT, HINT_EMOJI, WARNING,
format_bash_with_gutter, format_with_gutter,
@@ -8,7 +8,7 @@ use worktrunk::styling::{
use super::command_approval::approve_command_batch;
use super::command_executor::{CommandContext, prepare_project_commands};
-use super::worktree::{handle_push, parse_diff_shortstat};
+use super::worktree::handle_push;
use crate::output::execute_command_in_worktree;
/// Context for collecting merge commands
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index d00c4263f..4321b5a1e 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -102,10 +102,8 @@
use std::path::PathBuf;
use worktrunk::config::{CommandPhase, ProjectConfig, WorktrunkConfig};
-use worktrunk::git::{GitError, GitResultExt, Repository};
-use worktrunk::styling::{
- ADDITION, CYAN, CYAN_BOLD, DELETION, GREEN, GREEN_BOLD, WARNING, format_bash_with_gutter,
-};
+use worktrunk::git::{GitError, GitResultExt, Repository, parse_diff_shortstat};
+use worktrunk::styling::{CYAN, CYAN_BOLD, GREEN, GREEN_BOLD, WARNING, format_bash_with_gutter};
use super::command_executor::{CommandContext, prepare_project_commands};
use crate::commands::process::spawn_detached;
@@ -797,64 +795,3 @@ pub fn handle_push(
Ok(())
}
-
-/// Parse git diff --shortstat output
-pub struct DiffStats {
- pub files: Option<usize>,
- pub insertions: Option<usize>,
- pub deletions: Option<usize>,
-}
-
-impl DiffStats {
- /// Format stats as a summary string (e.g., "3 files, +45, -12")
- pub fn format_summary(&self) -> Vec<String> {
- let mut parts = Vec::new();
-
- if let Some(files) = self.files {
- parts.push(format!(
- "{} file{}",
- files,
- if files == 1 { "" } else { "s" }
- ));
- }
- if let Some(insertions) = self.insertions {
- parts.push(format!("{ADDITION}+{insertions}{ADDITION:#}"));
- }
- if let Some(deletions) = self.deletions {
- parts.push(format!("{DELETION}-{deletions}{DELETION:#}"));
- }
-
- parts
- }
-}
-
-pub fn parse_diff_shortstat(output: &str) -> DiffStats {
- let mut stats = DiffStats {
- files: None,
- insertions: None,
- deletions: None,
- };
-
- // Example: " 3 files changed, 45 insertions(+), 12 deletions(-)"
- let parts: Vec<&str> = output.split(',').collect();
-
- for part in parts {
- let part = part.trim();
-
- if part.contains("file") {
- if let Some(num_str) = part.split_whitespace().next() {
- stats.files = num_str.parse().ok();
- }
- } else if part.contains("insertion") {
- if let Some(num_str) = part.split_whitespace().next() {
- stats.insertions = num_str.parse().ok();
- }
- } else if part.contains("deletion")
- && let Some(num_str) = part.split_whitespace().next()
- {
- stats.deletions = num_str.parse().ok();
- }
- }
-
- stats
-}
diff --git a/src/git/diff.rs b/src/git/diff.rs
new file mode 100644
index 000000000..8d1913ea6
--- /dev/null
+++ b/src/git/diff.rs
@@ -0,0 +1,65 @@
+//! Git diff utilities for parsing and formatting diff statistics.
+
+use crate::styling::{ADDITION, DELETION};
+
+/// Parse git diff --shortstat output
+#[derive(Debug)]
+pub struct DiffStats {
+ pub files: Option<usize>,
+ pub insertions: Option<usize>,
+ pub deletions: Option<usize>,
+}
+
+impl DiffStats {
+ /// Format stats as a summary string (e.g., "3 files, +45, -12")
+ pub fn format_summary(&self) -> Vec<String> {
+ let mut parts = Vec::new();
+
+ if let Some(files) = self.files {
+ parts.push(format!(
+ "{} file{}",
+ files,
+ if files == 1 { "" } else { "s" }
+ ));
+ }
+ if let Some(insertions) = self.insertions {
+ parts.push(format!("{ADDITION}+{insertions}{ADDITION:#}"));
+ }
+ if let Some(deletions) = self.deletions {
+ parts.push(format!("{DELETION}-{deletions}{DELETION:#}"));
+ }
+
+ parts
+ }
+}
+
+pub fn parse_diff_shortstat(output: &str) -> DiffStats {
+ let mut stats = DiffStats {
+ files: None,
+ insertions: None,
+ deletions: None,
+ };
+
+ // Example: " 3 files changed, 45 insertions(+), 12 deletions(-)"
+ let parts: Vec<&str> = output.split(',').collect();
+
+ for part in parts {
+ let part = part.trim();
+
+ if part.contains("file") {
+ if let Some(num_str) = part.split_whitespace().next() {
+ stats.files = num_str.parse().ok();
+ }
+ } else if part.contains("insertion") {
+ if let Some(num_str) = part.split_whitespace().next() {
+ stats.insertions = num_str.parse().ok();
+ }
+ } else if part.contains("deletion")
+ && let Some(num_str) = part.split_whitespace().next()
+ {
+ stats.deletions = num_str.parse().ok();
+ }
+ }
+
+ stats
+}
diff --git a/src/git/mod.rs b/src/git/mod.rs
index 325a32f93..5ae07733a 100644
--- a/src/git/mod.rs
+++ b/src/git/mod.rs
@@ -3,6 +3,7 @@
use std::path::PathBuf;
// Submodules
+mod diff;
mod error;
mod parse;
mod repository;
@@ -11,6 +12,7 @@ mod repository;
mod test;
// Re-exports from submodules
+pub use diff::{DiffStats, parse_diff_shortstat};
pub use error::GitError;
pub use repository::{GitResultExt, Repository};