Skip to content

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

Merge: Include staged changes in squash commit

Maximilian Roos995002a48eb8
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 6d3058dc8..fd00b6e9f 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -52,15 +52,23 @@ pub fn handle_merge(
)?;
}
- // Auto-commit uncommitted changes if they exist
+ // Handle uncommitted changes depending on whether we're squashing
// Only do this after pre-merge checks pass
if repo.is_dirty()? {
- handle_commit_changes(message, &config.commit_generation)?;
+ if squash {
+ // Just stage - squash will handle committing
+ crate::output::progress(format!("πŸ”„ {CYAN}Staging uncommitted changes...{CYAN:#}"))?;
+ repo.run_command(&["add", "-A"])
+ .git_context("Failed to stage changes")?;
+ } else {
+ // Commit immediately when not squashing
+ handle_commit_changes(message, &config.commit_generation)?;
+ }
}
// Squash commits if requested
if squash {
- handle_squash(&target_branch)?;
+ handle_squash(&target_branch, message, &config.commit_generation)?;
}
// Rebase onto target
@@ -198,32 +206,22 @@ fn format_commit_message_for_display(message: &str) -> String {
result
}
-/// Commit uncommitted changes with LLM-generated message
-fn handle_commit_changes(
+/// Commit already-staged changes with an LLM-generated message
+fn commit_with_generated_message(
+ progress_msg: &str,
custom_instruction: Option<&str>,
commit_generation_config: &worktrunk::config::CommitGenerationConfig,
) -> Result<(), GitError> {
let repo = Repository::current();
- crate::output::progress(format!(
- "πŸ”„ {CYAN}Committing uncommitted changes...{CYAN:#}"
- ))?;
-
- // Stage all changes including untracked files
- repo.run_command(&["add", "-A"])
- .git_context("Failed to stage changes")?;
-
- // Generate commit message
+ crate::output::progress(format!("πŸ”„ {CYAN}{progress_msg}{CYAN:#}"))?;
crate::output::progress(format!("πŸ”„ {CYAN}Generating commit message...{CYAN:#}"))?;
let commit_message =
crate::llm::generate_commit_message(custom_instruction, commit_generation_config)?;
-
- // Display the generated commit message
let formatted_message = format_commit_message_for_display(&commit_message);
crate::output::progress(format_with_gutter(&formatted_message, "", None))?;
- // Commit
repo.run_command(&["commit", "-m", &commit_message])
.git_context("Failed to commit")?;
@@ -233,7 +231,29 @@ fn handle_commit_changes(
Ok(())
}
-fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
+/// Commit uncommitted changes with LLM-generated message
+fn handle_commit_changes(
+ custom_instruction: Option<&str>,
+ commit_generation_config: &worktrunk::config::CommitGenerationConfig,
+) -> Result<(), GitError> {
+ let repo = Repository::current();
+
+ // Stage all changes including untracked files
+ repo.run_command(&["add", "-A"])
+ .git_context("Failed to stage changes")?;
+
+ commit_with_generated_message(
+ "Committing uncommitted changes...",
+ custom_instruction,
+ commit_generation_config,
+ )
+}
+
+fn handle_squash(
+ target_branch: &str,
+ custom_instruction: Option<&str>,
+ commit_generation_config: &worktrunk::config::CommitGenerationConfig,
+) -> Result<Option<usize>, GitError> {
let repo = Repository::current();
// Get merge base with target branch
@@ -256,8 +276,13 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
}
if commit_count == 0 && has_staged {
- // Just staged changes, no commits - would need to commit but this shouldn't happen in merge flow
- return Err(GitError::StagedChangesWithoutCommits);
+ // Just staged changes, no commits - commit them directly (no squashing needed)
+ commit_with_generated_message(
+ "Committing staged changes...",
+ custom_instruction,
+ commit_generation_config,
+ )?;
+ return Ok(None);
}
if commit_count == 1 && !has_staged {
@@ -268,7 +293,7 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
return Ok(None);
}
- // One or more commits (possibly with staged changes) - squash them
+ // Either multiple commits OR single commit with staged changes - squash them
crate::output::progress(format!(
"πŸ”„ {CYAN}Squashing {commit_count} commits into one...{CYAN:#}"
))?;
@@ -277,21 +302,20 @@ fn handle_squash(target_branch: &str) -> Result<Option<usize>, GitError> {
let range = format!("{}..HEAD", merge_base);
let subjects = repo.commit_subjects(&range)?;
- // Load config and generate commit message
+ // Generate squash commit message
crate::output::progress(format!(
"πŸ”„ {CYAN}Generating squash commit message...{CYAN:#}"
))?;
- let config = WorktrunkConfig::load().git_context("Failed to load config")?;
let commit_message =
- crate::llm::generate_squash_message(target_branch, &subjects, &config.commit_generation)
+ crate::llm::generate_squash_message(target_branch, &subjects, commit_generation_config)
.git_context("Failed to generate commit message")?;
// Display the generated commit message
let formatted_message = format_commit_message_for_display(&commit_message);
crate::output::progress(format_with_gutter(&formatted_message, "", None))?;
- // Reset to merge base (soft reset stages all changes)
+ // Reset to merge base (soft reset stages all changes, including any already-staged uncommitted changes)
repo.run_command(&["reset", "--soft", &merge_base])
.git_context("Failed to reset to merge base")?;
diff --git a/src/git.rs b/src/git.rs
index 113c9f49b..f283a6d40 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -22,8 +22,6 @@ pub enum GitError {
DetachedHead,
/// Working tree has untracked files
UntrackedFiles,
- /// Working tree has staged changes but no commits
- StagedChangesWithoutCommits,
/// Pre-merge command failed
PreMergeCommandFailed { command_name: String, error: String },
/// Working tree has uncommitted changes
@@ -83,14 +81,6 @@ impl std::fmt::Display for GitError {
)
}
- // Staged changes without commits
- GitError::StagedChangesWithoutCommits => {
- write!(
- f,
- "{ERROR_EMOJI} {ERROR}Staged changes without commits{ERROR:#}\n\n{HINT_EMOJI} {HINT}Please commit them first{HINT:#}"
- )
- }
-
// Pre-merge command failed
GitError::PreMergeCommandFailed {
command_name,
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 953405c33..73a59ee7f 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -783,7 +783,7 @@ fn test_merge_auto_commit_and_squash() {
std::fs::write(feature_wt.join("file1.txt"), "updated content 1")
.expect("Failed to write file");
- // Merge with --squash - should auto-commit first, then squash all commits including the new one
+ // Merge with --squash - should stage uncommitted changes, then squash all commits including the staged changes
snapshot_merge_with_env(
"merge_auto_commit_and_squash",
&repo,
@@ -791,7 +791,7 @@ fn test_merge_auto_commit_and_squash() {
Some(&feature_wt),
&[
("WORKTRUNK_COMMIT_GENERATION__COMMAND", "echo"),
- // First message is for auto-commit
+ // Message is for the final squash commit
(
"WORKTRUNK_COMMIT_GENERATION__ARGS",
"fix: update file 1 content",
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap
index 96cd67a72..5a7898f9b 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap
@@ -19,26 +19,21 @@ info:
SOURCE_DATE_EPOCH: "1761609600"
WORKTRUNK_COMMIT_GENERATION__ARGS: "fix: update file 1 content"
WORKTRUNK_COMMIT_GENERATION__COMMAND: echo
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmplU2QSs/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpb02yof/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
-πŸ”„ Committing uncommitted changes...
-πŸ”„ Generating commit message...
-  WIP: Auto-commit before merge
-
-βœ… Committed changes
-πŸ”„ Squashing 3 commits into one...
+πŸ”„ Staging uncommitted changes...
+πŸ”„ Squashing 2 commits into one...
πŸ”„ Generating squash commit message...
  Squash commits from main
 
  Combined commits:
  - feat: add file 1
  - feat: add file 2
-  - WIP: Auto-commit before merge
-βœ… Squashed 3 commits into one
+βœ… Squashed 2 commits into one
πŸ”„ Rebasing onto main...
πŸ”„ Merging 1 commit to main @ [SHA]