Skip to content

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

fix(squash): run pre-commit hooks before staging so their edits reach the commit (#4321)

Worktrunk Bot3949c21124a9
diff --git a/src/commands/step/squash.rs b/src/commands/step/squash.rs
index 55203a274..0859b3c4c 100644
--- a/src/commands/step/squash.rs
+++ b/src/commands/step/squash.rs
@@ -158,13 +158,9 @@ pub fn handle_squash(
.unwrap_or_else(|| integration_target.clone());
let template_vars = TemplateVars::new().with_target(&integration_target);
- // Auto-stage changes before running pre-commit hooks so both beta and merge paths behave identically
- if stage_mode == StageMode::All {
- warn_about_untracked_files(&wt)?;
- }
- wt.stage(stage_mode)?;
-
- // Run pre-commit hooks (user first, then project).
+ // Run pre-commit hooks (user first, then project) before staging, as
+ // `wt step commit` does, so the edits a formatter hook makes are staged
+ // into the squash commit rather than left in the working tree.
if hooks.run() {
execute_hook(
&ctx,
@@ -174,6 +170,11 @@ pub fn handle_squash(
)?;
}
+ if stage_mode == StageMode::All {
+ warn_about_untracked_files(&wt)?;
+ }
+ wt.stage(stage_mode)?;
+
// Resolve HEAD once, so the span, the message's commit list, and the
// compare-and-swap that finally moves the branch all describe one tip.
let head_sha = wt.run_command(&["rev-parse", "HEAD"])?.trim().to_string();
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index ecd0cc89e..492f1cb4b 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -1289,6 +1289,50 @@ fn test_merge_pre_commit_collected_for_squash_clean_worktree(
));
}
+/// A pre-commit hook's edits land in the squash commit, as they do in the
+/// commit `wt step commit` makes: the hook runs before staging, not after.
+#[rstest]
+fn test_step_squash_includes_pre_commit_hook_edits(repo_with_multi_commit_feature: TestRepo) {
+ let repo = &repo_with_multi_commit_feature;
+ let feature_wt = repo.worktrees["feature"].clone();
+
+ let config_dir = feature_wt.join(".config");
+ fs::create_dir_all(&config_dir).unwrap();
+ fs::write(
+ config_dir.join("wt.toml"),
+ "pre-commit = \"echo formatted > fmt.txt\"",
+ )
+ .unwrap();
+ repo.run_git_in(&feature_wt, &["add", ".config/wt.toml"]);
+ repo.run_git_in(&feature_wt, &["commit", "-m", "Add config"]);
+
+ let output = repo
+ .wt_command()
+ .args(["step", "squash", "--yes"])
+ .current_dir(&feature_wt)
+ .env(
+ "WORKTRUNK_COMMIT__GENERATION__COMMAND",
+ "cat >/dev/null && echo 'feat: combined'",
+ )
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "step squash failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ assert_eq!(
+ repo.git_output(&["rev-parse", "feature^"]),
+ repo.git_output(&["rev-parse", "main"])
+ );
+ assert_eq!(repo.git_output(&["show", "feature:fmt.txt"]), "formatted");
+ assert_eq!(
+ repo.git_output(&["-C", feature_wt.to_str().unwrap(), "status", "--porcelain"]),
+ ""
+ );
+}
+
// README EXAMPLE GENERATION TESTS
// These tests are specifically designed to generate realistic output examples for the README.
// The snapshots from these tests are manually copied into README.md to show users what
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
index 691b8598f..0ea520567 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_failure.snap
@@ -60,8 +60,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-▲ Auto-staging 1 untracked path:
-  feature.txt
◎ Running pre-commit project hook
  exit 1
✗ pre-commit command failed: exit status: 1
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
index 4d56ebf2f..160457f4b 100644
--- a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_command_success.snap
@@ -60,11 +60,11 @@ exit_code: 0
----- stdout -----
----- stderr -----
-▲ Auto-staging 1 untracked path:
-  feature.txt
◎ Running pre-commit project hook
  echo 'Pre-commit check passed'
Pre-commit check passed
+▲ Auto-staging 1 untracked path:
+  feature.txt
◎ Committing changes with default message... (1 file, +1, no squashing needed)
↳ Using fallback commit message. For LLM setup guide, run wt config --help
  Changes to feature.txt
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_executes.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_executes.snap
index 150da3734..2a601210f 100644
--- a/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_executes.snap
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_executes.snap
@@ -61,17 +61,19 @@ exit_code: 0
----- stdout -----
----- stderr -----
-▲ Auto-staging 1 untracked path:
-  uncommitted.txt
◎ Running pre-commit user:lint
  echo 'USER_PRE_COMMIT_RAN' > user_precommit.txt
-◎ Committing changes with default message... (1 file, +1, no squashing needed)
+▲ Auto-staging 2 untracked paths:
+  uncommitted.txt
+  user_precommit.txt
+◎ Committing changes with default message... (2 files, +2, no squashing needed)
↳ Using fallback commit message. For LLM setup guide, run wt config --help
-  Changes to uncommitted.txt
+  Changes to uncommitted.txt & user_precommit.txt
✓ Committed changes @ [HASH]
◎ Merging 1 commit to main @ [HASH] (no rebase needed)
-  * [HASH] Changes to uncommitted.txt
-  uncommitted.txt | 1 +
-  1 file changed, 1 insertion(+)
-✓ Merged to main (1 commit, 1 file, +1)
+  * [HASH] Changes to uncommitted.txt & user_precommit.txt
+  uncommitted.txt | 1 +
+  user_precommit.txt | 1 +
+  2 files changed, 2 insertions(+)
+✓ Merged to main (1 commit, 2 files, +2)
○ Worktree preserved (--no-remove)
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_failure.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_failure.snap
index 8511abb04..c2502137e 100644
--- a/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_failure.snap
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_pre_commit_failure.snap
@@ -61,8 +61,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-▲ Auto-staging 1 untracked path:
-  uncommitted.txt
◎ Running pre-commit user:lint
  exit 1
✗ pre-commit command failed: lint: exit status: 1