Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: collect pre-commit hooks when squashing on clean worktree (#695)
Maximilian Roose5e373d33b9a
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 05d54a574..017416670 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -45,6 +45,7 @@ fn collect_merge_commands(commit: bool,verify: bool,will_remove: bool,+ squash_enabled: bool,) -> anyhow::Result<(Vec<HookCommand>, String)> {let mut all_commands = Vec::new();let project_config = match repo.load_project_config()? {@@ -52,10 +53,11 @@ fn collect_merge_commands(None => return Ok((all_commands, repo.project_identifier()?.to_string())),};- // Collect pre-commit commands if we'll commit (direct or via squash)let mut hooks = Vec::new();- if commit && verify && repo.current_worktree().is_dirty()? {+ // Pre-commit hooks run when a commit will actually be created+ let will_create_commit = repo.current_worktree().is_dirty()? || squash_enabled;+ if commit && verify && will_create_commit {hooks.push(HookType::PreCommit);}@@ -146,7 +148,7 @@ pub fn handle_merge(opts: MergeOptions<'_>) -> anyhow::Result<()> {// Collect and approve all commands upfront for batch permission requestlet (all_commands, project_id) =- collect_merge_commands(repo, commit, verify, remove_effective)?;+ collect_merge_commands(repo, commit, verify, remove_effective, squash_enabled)?;// Approve all commands in a single batch (shows templates, not expanded values)let approved = approve_command_batch(&all_commands, &project_id, config, yes, false)?;diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 82d45c3d5..93b97aa54 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -919,6 +919,42 @@ fn test_merge_pre_squash_command_failure(mut repo: TestRepo) {));}+/// Bug #3: Pre-commit hooks should be collected for approval when squashing,+/// even if the worktree is clean (no uncommitted changes).+///+/// Scenario: Feature worktree has multiple commits to squash, but no dirty files.+/// Without the fix, pre-commit hooks would run during squash without approval.+/// With the fix, pre-commit hooks are collected upfront and approved.+#[rstest]+fn test_merge_pre_commit_collected_for_squash_clean_worktree(+ repo_with_multi_commit_feature: TestRepo,+) {+ let repo = &repo_with_multi_commit_feature;+ let feature_wt = repo.worktrees["feature"].clone();++ // Create project config in the FEATURE worktree (where merge runs)+ // This ensures the config is visible when loading project config+ let config_dir = feature_wt.join(".config");+ fs::create_dir_all(&config_dir).unwrap();+ fs::write(+ config_dir.join("wt.toml"),+ "pre-commit = \"echo 'Pre-commit from squash'\"",+ )+ .unwrap();+ // Commit the config in the feature worktree+ repo.run_git_in(&feature_wt, &["add", ".config/wt.toml"]);+ repo.run_git_in(&feature_wt, &["commit", "-m", "Add config"]);++ // Feature worktree is CLEAN (no uncommitted changes) but has 3 commits to squash.+ // Pre-commit should be collected and approved before squash runs.+ assert_cmd_snapshot!(make_snapshot_cmd(+ repo,+ "merge",+ &["main", "--yes"],+ Some(&feature_wt)+ ));+}+#[rstest]fn test_merge_no_remote(#[from(repo_with_feature_worktree)] repo: TestRepo) {// Deliberately NOT calling setup_remote to test the error casediff --git a/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_collected_for_squash_clean_worktree.snap b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_collected_for_squash_clean_worktree.snapnew file mode 100644index 000000000..0e474759a--- /dev/null+++ b/tests/snapshots/integration__integration_tests__merge__merge_pre_commit_collected_for_squash_clean_worktree.snap@@ -0,0 +1,60 @@+---+source: tests/integration_tests/merge.rs+info:+ program: wt+ args:+ - merge+ - main+ - "--yes"+ env:+ APPDATA: "[TEST_CONFIG_HOME]"+ CLICOLOR_FORCE: "1"+ COLUMNS: "500"+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ GIT_TERMINAL_PROMPT: "0"+ HOME: "[TEST_HOME]"+ LANG: C+ LC_ALL: C+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"+ PATH: "[PATH]"+ RUST_LOG: warn+ SHELL: ""+ SOURCE_DATE_EPOCH: "1735776000"+ TERM: alacritty+ USERPROFILE: "[TEST_HOME]"+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"+---+success: true+exit_code: 0+----- stdout -----++----- stderr -----+[36m◎[39m [36mRunning pre-commit project hook:[39m+[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'Pre-commit from squash'[0m[2m+[0mPre-commit from squash+[36m◎[39m [36mSquashing 3 commits into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m+[36m◎[39m [36mGenerating squash commit message...[39m+[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [90mwt config --help[39m[22m+[107m [0m [1mSquash commits from main[22m+[107m [0m+[107m [0m Combined commits:+[107m [0m - feat: add file 1+[107m [0m - feat: add file 2+[107m [0m - Add config+[32m✓[39m [32mSquashed @ [HASH][39m+[36m◎[39m [36mMerging 1 commit to [1mmain[22m @ [2m[HASH][22m (no rebase needed)[39m+[107m [0m * [33m[HASH][m Squash commits from main+[107m [0m .config/wt.toml | 1 [32m+[m+[107m [0m file1.txt | 1 [32m+[m+[107m [0m file2.txt | 1 [32m+[m+[107m [0m 3 files changed, 3 insertions(+)+[32m✓[39m [32mMerged to [1mmain[22m [90m(1 commit, 3 files, [32m+3[39m[39m[90m)[39m[39m+[36m◎ Removing [1mfeature[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39m+[33m▲[39m [33mCannot change directory — shell integration not installed[39m+[2m↳[22m [2mTo enable automatic cd, run [90mwt config shell install[39m[22m