Skip to content

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

perf(commit): skip the second staging pass when no pre-commit hook ran (#4540)

Worktrunk Botbe5c53205f14
diff --git a/src/commands/commit.rs b/src/commands/commit.rs
index 4ffcdcbf9..ba200b05d 100644
--- a/src/commands/commit.rs
+++ b/src/commands/commit.rs
@@ -251,13 +251,14 @@ impl CommitOptions<'_> {
// project); `auto_stage` says why. Each stage re-checks for unmerged
// paths, since a hook is free to leave them behind.
auto_stage(&wt, self.stage_mode)?;
- if self.hooks.run() {
- execute_hook(
+ if self.hooks.run()
+ && execute_hook(
self.ctx,
HookType::PreCommit,
&template_vars.as_extra_vars(),
FailureStrategy::FailFast,
- )?;
+ )?
+ {
auto_stage(&wt, self.stage_mode)?;
}
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index f4ff4d828..46de2b291 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -629,9 +629,9 @@ pub(crate) fn check_not_default_branch(
///
/// Commit paths call this on both sides of the `pre-commit` hooks: before them
/// so a hook that reads the index (`pre-commit run`, lint-staged,
-/// `git diff --cached`) sees the changes about to be committed, and after them
-/// so a formatter hook's edits land in the commit. The second call warns only
-/// about untracked paths the hooks created.
+/// `git diff --cached`) sees the changes about to be committed, and, when a
+/// hook ran, after them so a formatter hook's edits land in the commit. The
+/// second call warns only about untracked paths the hooks created.
pub(crate) fn auto_stage(wt: &WorkingTree, mode: StageMode) -> anyhow::Result<()> {
if mode == StageMode::All {
warn_about_untracked_files(wt)?;
diff --git a/src/commands/step/squash.rs b/src/commands/step/squash.rs
index afb664c84..57cd51085 100644
--- a/src/commands/step/squash.rs
+++ b/src/commands/step/squash.rs
@@ -183,13 +183,14 @@ pub fn handle_squash_in(
// Stage on both sides of the pre-commit hooks (user first, then project);
// `auto_stage` says why.
auto_stage(&wt, stage_mode)?;
- if hooks.run() {
- execute_hook(
+ if hooks.run()
+ && execute_hook(
&ctx,
HookType::PreCommit,
&template_vars.as_extra_vars(),
FailureStrategy::FailFast,
- )?;
+ )?
+ {
auto_stage(&wt, stage_mode)?;
}
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index af98ffc55..93e9aca8a 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -1380,6 +1380,46 @@ fn test_pre_commit_hook_sees_staged_changes(
assert_eq!(repo.git_output(&["rev-parse", "main"]), main_head);
}
+/// With no pre-commit hook configured, a commit stages once. The second
+/// staging pass only picks up edits a hook made, so running it with no hook
+/// would repeat a full `git status` and `git add` for nothing.
+#[rstest]
+#[case::merge(&["-vv", "merge", "main", "--yes"])]
+#[case::step_commit(&["-vv", "step", "commit", "--yes"])]
+fn test_commit_stages_once_without_pre_commit_hook(
+ repo_with_multi_commit_feature: TestRepo,
+ #[case] args: &[&str],
+) {
+ let repo = &repo_with_multi_commit_feature;
+ let feature_wt = repo.worktrees["feature"].clone();
+ fs::write(feature_wt.join("new.txt"), "content\n").unwrap();
+
+ let output = repo
+ .wt_command()
+ .args(args)
+ .current_dir(&feature_wt)
+ .env(
+ "WORKTRUNK_COMMIT__GENERATION__COMMAND",
+ "cat >/dev/null && echo 'feat: combined'",
+ )
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "commit failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ let trace_log =
+ crate::common::resolve_git_common_dir(repo.root_path()).join("wt/logs/trace.log");
+ let trace = fs::read_to_string(&trace_log).unwrap();
+ let untracked_scans = trace
+ .lines()
+ .filter(|line| line.contains("$ git status --porcelain -z -unormal"))
+ .count();
+ assert_eq!(untracked_scans, 1, "trace.log: {trace}");
+}
+
// 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