Skip to content

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.rs
index 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 request
let (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.rs
index 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 case
diff --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.snap
new file mode 100644
index 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 -----
+◎ Running pre-commit project hook:
+  echo 'Pre-commit from squash'
+Pre-commit from squash
+◎ Squashing 3 commits into a single commit (3 files, +3)...
+◎ Generating squash commit message...
+↳ Using fallback commit message. For LLM setup guide, run wt config --help
+  Squash commits from main
+ 
+  Combined commits:
+  - feat: add file 1
+  - feat: add file 2
+  - Add config
+✓ Squashed @ [HASH]
+◎ Merging 1 commit to main @ [HASH] (no rebase needed)
+  * [HASH] Squash commits from main
+  .config/wt.toml | 1 +
+  file1.txt | 1 +
+  file2.txt | 1 +
+  3 files changed, 3 insertions(+)
+✓ Merged to main (1 commit, 3 files, +3)
+◎ Removing feature worktree & branch in background (same commit as main, _)
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install