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.rsindex 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.rsindex 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 whatdiff --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.snapindex 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 ------[33m▲[39m [33mAuto-staging 1 untracked path:[39m-[107m [0m feature.txt[36m◎[39m [36mRunning pre-commit project hook[39m[107m [0m [2m[0m[2m[34mexit[0m[2m 1[0m[0m[31m✗[39m [31mpre-commit command failed: exit status: 1[39mdiff --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.snapindex 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 ------[33m▲[39m [33mAuto-staging 1 untracked path:[39m-[107m [0m feature.txt[36m◎[39m [36mRunning pre-commit project hook[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'Pre-commit check passed'[0m[0mPre-commit check passed+[33m▲[39m [33mAuto-staging 1 untracked path:[39m+[107m [0m feature.txt[36m◎[39m [36mCommitting changes with default message... [90m(1 file, [32m+1[39m, no squashing needed[39m[90m)[39m[39m[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [4mwt config --help[24m[22m[107m [0m [1mChanges to feature.txt[22mdiff --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.snapindex 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 ------[33m▲[39m [33mAuto-staging 1 untracked path:[39m-[107m [0m uncommitted.txt[36m◎[39m [36mRunning pre-commit [1muser:lint[22m[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'USER_PRE_COMMIT_RAN'[0m[2m [0m[2m[36m>[0m[2m user_precommit.txt[0m-[0m[36m◎[39m [36mCommitting changes with default message... [90m(1 file, [32m+1[39m, no squashing needed[39m[90m)[39m[39m+[0m[33m▲[39m [33mAuto-staging 2 untracked paths:[39m+[107m [0m uncommitted.txt+[107m [0m user_precommit.txt+[36m◎[39m [36mCommitting changes with default message... [90m(2 files, [32m+2[39m, no squashing needed[39m[90m)[39m[39m[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [4mwt config --help[24m[22m-[107m [0m [1mChanges to uncommitted.txt[22m+[107m [0m [1mChanges to uncommitted.txt & user_precommit.txt[22m[32m✓[39m [32mCommitted changes @ [2m[HASH][22m[39m[36m◎[39m [36mMerging 1 commit to [1mmain[22m @ [2m[HASH][22m (no rebase needed)[39m-[107m [0m * [33m[HASH][m Changes to uncommitted.txt-[107m [0m uncommitted.txt | 1 [32m+[m-[107m [0m 1 file changed, 1 insertion(+)-[32m✓[39m [32mMerged to [1mmain[22m [90m(1 commit, 1 file, [32m+1[39m[39m[90m)[39m[39m+[107m [0m * [33m[HASH][m Changes to uncommitted.txt & user_precommit.txt+[107m [0m uncommitted.txt | 1 [32m+[m+[107m [0m user_precommit.txt | 1 [32m+[m+[107m [0m 2 files changed, 2 insertions(+)+[32m✓[39m [32mMerged to [1mmain[22m [90m(1 commit, 2 files, [32m+2[39m[39m[90m)[39m[39m[2m○[22m 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.snapindex 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 ------[33m▲[39m [33mAuto-staging 1 untracked path:[39m-[107m [0m uncommitted.txt[36m◎[39m [36mRunning pre-commit [1muser:lint[22m[39m[107m [0m [2m[0m[2m[34mexit[0m[2m 1[0m[0m[31m✗[39m [31mpre-commit command failed: [1mlint[22m: exit status: 1[39m