Skip to content

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

WIP: Auto-commit before merge

Maximilian Roosd2d55d646ee3
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 602fffc15..dd031de06 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -71,20 +71,22 @@ pub fn handle_merge(
// Squash commits if requested
if squash {
- squashed_count = handle_squash(&target_branch)?;
+ squashed_count = handle_squash(&target_branch, internal)?;
}
// Rebase onto target
- let cyan = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
- let cyan_bold = cyan.bold();
- println!("πŸ”„ {cyan}Rebasing onto {cyan_bold}{target_branch}{cyan_bold:#}...{cyan:#}");
+ if !internal {
+ let cyan = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
+ let cyan_bold = cyan.bold();
+ println!("πŸ”„ {cyan}Rebasing onto {cyan_bold}{target_branch}{cyan_bold:#}...{cyan:#}");
+ }
repo.run_command(&["rebase", &target_branch]).map_err(|e| {
GitError::CommandFailed(format!("Failed to rebase onto '{}': {}", target_branch, e))
})?;
// Fast-forward push to target branch (reuse handle_push logic)
- handle_push(Some(&target_branch), false)?;
+ handle_push(Some(&target_branch), false, internal)?;
// Finish worktree unless --keep was specified
if !keep {
@@ -242,7 +244,7 @@ fn handle_commit_changes(
Ok(())
}
-fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
+fn handle_squash(target_branch: &str, internal: bool) -> Result<Option<usize>, GitError> {
let repo = Repository::current();
// Get merge base with target branch
@@ -257,8 +259,10 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
// Handle different scenarios
if commit_count == 0 && !has_staged {
// No commits and no staged changes - nothing to squash
- let dim = AnstyleStyle::new().dimmed();
- println!("{dim}No commits to squash - already at merge base{dim:#}");
+ if !internal {
+ let dim = AnstyleStyle::new().dimmed();
+ println!("{dim}No commits to squash - already at merge base{dim:#}");
+ }
return Ok(None);
}
@@ -272,19 +276,23 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
if commit_count == 1 && !has_staged {
// Single commit, no staged changes - nothing to do
- let cyan_bold = AnstyleStyle::new()
- .fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
- .bold();
- let dim = AnstyleStyle::new().dimmed();
- println!(
- "{dim}Only 1 commit since {cyan_bold}{target_branch}{cyan_bold:#} - no squashing needed{dim:#}"
- );
+ if !internal {
+ let cyan_bold = AnstyleStyle::new()
+ .fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
+ .bold();
+ let dim = AnstyleStyle::new().dimmed();
+ println!(
+ "{dim}Only 1 commit since {cyan_bold}{target_branch}{cyan_bold:#} - no squashing needed{dim:#}"
+ );
+ }
return Ok(None);
}
// One or more commits (possibly with staged changes) - squash them
- let cyan = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
- println!("πŸ”„ {cyan}Squashing {commit_count} commits into one...{cyan:#}");
+ if !internal {
+ let cyan = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan)));
+ println!("πŸ”„ {cyan}Squashing {commit_count} commits into one...{cyan:#}");
+ }
// Get commit subjects for the squash message
let range = format!("{}..HEAD", merge_base);
@@ -303,8 +311,10 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
repo.run_command(&["commit", "-m", &commit_message])
.map_err(|e| GitError::CommandFailed(format!("Failed to create squash commit: {}", e)))?;
- let green = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
- println!("βœ… {green}Squashed {commit_count} commits into one{green:#}");
+ if !internal {
+ let green = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
+ println!("βœ… {green}Squashed {commit_count} commits into one{green:#}");
+ }
Ok(Some(commit_count))
}
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index 5f2cac56c..5f24a3341 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -508,7 +508,11 @@ fn spawn_post_start_commands(
Ok(())
}
-pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<(), GitError> {
+pub fn handle_push(
+ target: Option<&str>,
+ allow_merge_commits: bool,
+ internal: bool,
+) -> Result<(), GitError> {
use anstyle::{AnsiColor, Color};
let repo = Repository::current();
@@ -560,7 +564,7 @@ pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<()
String::new()
};
- if commit_count > 0 {
+ if commit_count > 0 && !internal {
let commit_text = if commit_count == 1 {
"commit"
} else {
@@ -615,42 +619,44 @@ pub fn handle_push(target: Option<&str>, allow_merge_commits: bool) -> Result<()
})?;
// Build success message with statistics
- let green = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
- let green_bold = green.bold();
-
- if commit_count > 0 {
- // Parse shortstat to extract files/insertions/deletions
- // Example: " 3 files changed, 45 insertions(+), 12 deletions(-)"
- let stats = parse_diff_shortstat(&diff_shortstat);
-
- let mut summary_parts = vec![format!(
- "{} commit{}",
- commit_count,
- if commit_count == 1 { "" } else { "s" }
- )];
-
- if let Some(files) = stats.files {
- summary_parts.push(format!(
- "{} file{}",
- files,
- if files == 1 { "" } else { "s" }
- ));
- }
- if let Some(insertions) = stats.insertions {
- let addition = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
- summary_parts.push(format!("{addition}+{insertions}{addition:#}"));
- }
- if let Some(deletions) = stats.deletions {
- let deletion = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Red)));
- summary_parts.push(format!("{deletion}-{deletions}{deletion:#}"));
- }
+ if !internal {
+ let green = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
+ let green_bold = green.bold();
+
+ if commit_count > 0 {
+ // Parse shortstat to extract files/insertions/deletions
+ // Example: " 3 files changed, 45 insertions(+), 12 deletions(-)"
+ let stats = parse_diff_shortstat(&diff_shortstat);
+
+ let mut summary_parts = vec![format!(
+ "{} commit{}",
+ commit_count,
+ if commit_count == 1 { "" } else { "s" }
+ )];
+
+ if let Some(files) = stats.files {
+ summary_parts.push(format!(
+ "{} file{}",
+ files,
+ if files == 1 { "" } else { "s" }
+ ));
+ }
+ if let Some(insertions) = stats.insertions {
+ let addition = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Green)));
+ summary_parts.push(format!("{addition}+{insertions}{addition:#}"));
+ }
+ if let Some(deletions) = stats.deletions {
+ let deletion = AnstyleStyle::new().fg_color(Some(Color::Ansi(AnsiColor::Red)));
+ summary_parts.push(format!("{deletion}-{deletions}{deletion:#}"));
+ }
- println!(
- "βœ… {green}Pushed to {green_bold}{target_branch}{green_bold:#} ({}) {green:#}",
- summary_parts.join(", ")
- );
- } else {
- println!("βœ… {green}Pushed to {green_bold}{target_branch}{green_bold:#}{green:#}");
+ println!(
+ "βœ… {green}Pushed to {green_bold}{target_branch}{green_bold:#} ({}) {green:#}",
+ summary_parts.join(", ")
+ );
+ } else {
+ println!("βœ… {green}Pushed to {green_bold}{target_branch}{green_bold:#}{green:#}");
+ }
}
Ok(())
diff --git a/src/main.rs b/src/main.rs
index adefe43fa..d2749c0f8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -332,7 +332,7 @@ fn main() {
Commands::Push {
target,
allow_merge_commits,
- } => handle_push(target.as_deref(), allow_merge_commits),
+ } => handle_push(target.as_deref(), allow_merge_commits, false),
Commands::Merge {
target,
squash,
diff --git a/templates/fish.fish b/templates/fish.fish
index 1f5ee7b7a..90dafbdd7 100644
--- a/templates/fish.fish
+++ b/templates/fish.fish
@@ -25,25 +25,52 @@ if type -q {{ cmd_prefix }}
return 1
end
+ # Debug mode: set WORKTRUNK_DEBUG=1 to see what's happening
+ set -l debug_mode 0
+ if set -q WORKTRUNK_DEBUG
+ set debug_mode 1
+ echo "[DEBUG] Starting _wt_exec with args: $argv" >&2
+ end
+
# Use psub (process substitution) to preserve NUL bytes
# Command substitution $(...) strips NUL bytes, but psub preserves them
# Redirect directly from psub output, and save exit code to temp file
+ set -l chunk_count 0
while read -z chunk
+ set chunk_count (math $chunk_count + 1)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Chunk $chunk_count length: "(string length -- $chunk) >&2
+ echo "[DEBUG] Chunk $chunk_count first 50 chars: "(string sub -l 50 -- $chunk) >&2
+ end
+
if string match -q '__WORKTRUNK_CD__*' -- $chunk
# CD directive - extract path and change directory
set -l path (string replace '__WORKTRUNK_CD__' '' -- $chunk)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] CD directive: $path" >&2
+ end
if not cd $path
echo "Error: Failed to change directory to $path" >&2
end
else if string match -q '__WORKTRUNK_EXEC__*' -- $chunk
# EXEC directive - extract command (may contain newlines)
set exec_cmd (string replace '__WORKTRUNK_EXEC__' '' -- $chunk)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] EXEC directive: $exec_cmd" >&2
+ end
else if test -n "$chunk"
# Regular output - print it (preserving newlines)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Message chunk" >&2
+ end
printf '%s' $chunk
end
end < (begin; command $_WORKTRUNK_CMD $argv 2>&1; echo $status > $exit_code_file; end | psub)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Finished reading, total chunks: $chunk_count" >&2
+ end
+
# Read exit code from temp file
set -l exit_code (cat $exit_code_file 2>/dev/null; or echo 0)
rm -f $exit_code_file
diff --git a/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap b/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap
index 8d5d2ba2d..1810e0d24 100644
Binary files a/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap and b/tests/snapshots/integration__integration_tests__directives__merge_internal_keep.snap differ
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 43e1e90ee..200ae95be 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -46,25 +46,52 @@ if type -q wt
return 1
end
+ # Debug mode: set WORKTRUNK_DEBUG=1 to see what's happening
+ set -l debug_mode 0
+ if set -q WORKTRUNK_DEBUG
+ set debug_mode 1
+ echo "[DEBUG] Starting _wt_exec with args: $argv" >&2
+ end
+
# Use psub (process substitution) to preserve NUL bytes
# Command substitution $(...) strips NUL bytes, but psub preserves them
# Redirect directly from psub output, and save exit code to temp file
+ set -l chunk_count 0
while read -z chunk
+ set chunk_count (math $chunk_count + 1)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Chunk $chunk_count length: "(string length -- $chunk) >&2
+ echo "[DEBUG] Chunk $chunk_count first 50 chars: "(string sub -l 50 -- $chunk) >&2
+ end
+
if string match -q '__WORKTRUNK_CD__*' -- $chunk
# CD directive - extract path and change directory
set -l path (string replace '__WORKTRUNK_CD__' '' -- $chunk)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] CD directive: $path" >&2
+ end
if not cd $path
echo "Error: Failed to change directory to $path" >&2
end
else if string match -q '__WORKTRUNK_EXEC__*' -- $chunk
# EXEC directive - extract command (may contain newlines)
set exec_cmd (string replace '__WORKTRUNK_EXEC__' '' -- $chunk)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] EXEC directive: $exec_cmd" >&2
+ end
else if test -n "$chunk"
# Regular output - print it (preserving newlines)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Message chunk" >&2
+ end
printf '%s' $chunk
end
end < (begin; command $_WORKTRUNK_CMD $argv 2>&1; echo $status > $exit_code_file; end | psub)
+ if test $debug_mode -eq 1
+ echo "[DEBUG] Finished reading, total chunks: $chunk_count" >&2
+ end
+
# Read exit code from temp file
set -l exit_code (cat $exit_code_file 2>/dev/null; or echo 0)
rm -f $exit_code_file