Skip to content

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

Feat: Differentiate squash results for user feedback

Maximilian Roos7e4a6d162e2f
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index b156a0d9d..b6644c34f 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -151,7 +151,16 @@ pub fn handle_merge(
// Squash commits if enabled - track whether squashing occurred
let squashed = if squash_enabled {
- super::standalone::handle_squash(Some(&target_branch), force, !verify, true, stage_mode)?
+ matches!(
+ super::standalone::handle_squash(
+ Some(&target_branch),
+ force,
+ !verify,
+ true,
+ stage_mode
+ )?,
+ super::standalone::SquashResult::Squashed
+ )
} else {
false
};
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index 14e0ae165..fe620638f 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -28,7 +28,7 @@ pub use merge::handle_merge;
#[cfg(unix)]
pub use select::handle_select;
pub use standalone::{
- RebaseResult, handle_rebase, handle_squash, handle_standalone_add_approvals,
+ RebaseResult, SquashResult, handle_rebase, handle_squash, handle_standalone_add_approvals,
handle_standalone_clear_approvals, handle_standalone_commit, handle_standalone_run_hook,
};
pub use worktree::{handle_remove, handle_switch};
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 82fd708c5..dc2e504bd 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -78,20 +78,31 @@ pub fn handle_standalone_commit(
options.commit()
}
+/// Result of a squash operation
+#[derive(Debug, Clone)]
+pub enum SquashResult {
+ /// Squash or commit occurred
+ Squashed,
+ /// Nothing to squash: no commits ahead of target branch
+ NoCommitsAhead(String),
+ /// Nothing to squash: already a single commit
+ AlreadySingleCommit,
+ /// Squash attempted but resulted in no net changes (commits canceled out)
+ NoNetChanges,
+}
+
/// Handle shared squash workflow (used by `wt step squash` and `wt merge`)
///
/// # Arguments
/// * `auto_trust` - If true, skip approval prompts for pre-commit commands (already approved in batch)
/// * `stage_mode` - What to stage before committing (All or Tracked; None not supported for squash)
-///
-/// Returns true if a commit or squash operation occurred, false if nothing needed to be done
pub fn handle_squash(
target: Option<&str>,
force: bool,
skip_pre_commit: bool,
auto_trust: bool,
stage_mode: super::commit::StageMode,
-) -> anyhow::Result<bool> {
+) -> anyhow::Result<SquashResult> {
use super::commit::StageMode;
let env = CommandEnv::for_action("squash")?;
@@ -144,18 +155,18 @@ pub fn handle_squash(
// Handle different scenarios
if commit_count == 0 && !has_staged {
// No commits and no staged changes - nothing to squash
- return Ok(false);
+ return Ok(SquashResult::NoCommitsAhead(target_branch));
}
if commit_count == 0 && has_staged {
// Just staged changes, no commits - commit them directly (no squashing needed)
generator.commit_staged_changes(true, stage_mode)?;
- return Ok(true);
+ return Ok(SquashResult::Squashed);
}
if commit_count == 1 && !has_staged {
- // Single commit, no staged changes - nothing to do
- return Ok(false);
+ // Single commit, no staged changes - already squashed
+ return Ok(SquashResult::AlreadySingleCommit);
}
// Either multiple commits OR single commit with staged changes - squash them
@@ -246,7 +257,7 @@ pub fn handle_squash(
crate::output::info(format!(
"{dim}No changes after squashing {commit_count} {commit_text}{dim:#}"
))?;
- return Ok(false);
+ return Ok(SquashResult::NoNetChanges);
}
// Commit with the generated message
@@ -266,7 +277,7 @@ pub fn handle_squash(
"{GREEN}Squashed @ {green_dim}{commit_hash}{green_dim:#}{GREEN:#}"
))?;
- Ok(true)
+ Ok(SquashResult::Squashed)
}
/// Result of a rebase operation
diff --git a/src/main.rs b/src/main.rs
index 1b24ad89c..4233ca228 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,7 +24,7 @@ use commands::command_executor::CommandContext;
use commands::handle_select;
use commands::worktree::{SwitchResult, handle_push};
use commands::{
- ConfigAction, RebaseResult, handle_config_create, handle_config_refresh_cache,
+ ConfigAction, RebaseResult, SquashResult, handle_config_create, handle_config_refresh_cache,
handle_config_show, handle_config_status_set, handle_config_status_unset,
handle_configure_shell, handle_init, handle_list, handle_merge, handle_rebase, handle_remove,
handle_squash, handle_standalone_add_approvals, handle_standalone_clear_approvals,
@@ -504,10 +504,16 @@ fn main() {
let stage_final = stage
.or_else(|| config.commit.and_then(|c| c.stage))
.unwrap_or_default();
- let did_work =
- handle_squash(target.as_deref(), force, !verify, false, stage_final)?;
- if !did_work {
- crate::output::info("Nothing to squash")?;
+ match handle_squash(target.as_deref(), force, !verify, false, stage_final)? {
+ SquashResult::Squashed | SquashResult::NoNetChanges => {}
+ SquashResult::NoCommitsAhead(branch) => {
+ crate::output::info(format!(
+ "Nothing to squash; no commits ahead of {branch}"
+ ))?;
+ }
+ SquashResult::AlreadySingleCommit => {
+ crate::output::info("Nothing to squash; already a single commit")?;
+ }
}
Ok(())
}),
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index 7e7c3444b..51403b520 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -3234,6 +3234,52 @@ fn test_step_squash_with_both_flags() {
);
}
+#[test]
+fn test_step_squash_no_commits() {
+ // Test "nothing to squash; no commits ahead" message
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create a feature worktree but don't add any commits
+ let feature_wt = repo.add_worktree("feature", "feature");
+
+ snapshot_step_squash_with_env("step_squash_no_commits", &repo, &[], Some(&feature_wt), &[]);
+}
+
+#[test]
+fn test_step_squash_single_commit() {
+ // Test "nothing to squash; already a single commit" message
+ let mut repo = TestRepo::new();
+ repo.commit("Initial commit");
+ repo.setup_remote("main");
+
+ // Create a feature worktree with exactly one commit
+ let feature_wt = repo.add_worktree("feature", "feature");
+
+ fs::write(feature_wt.join("file1.txt"), "content 1").expect("Failed to write file");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["add", "file1.txt"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to add file");
+ let mut cmd = Command::new("git");
+ repo.configure_git_cmd(&mut cmd);
+ cmd.args(["commit", "-m", "feat: single commit"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("Failed to commit");
+
+ snapshot_step_squash_with_env(
+ "step_squash_single_commit",
+ &repo,
+ &[],
+ Some(&feature_wt),
+ &[],
+ );
+}
+
#[test]
fn test_step_commit_with_no_verify_flag() {
let repo = TestRepo::new();
diff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_no_commits.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_no_commits.snap
new file mode 100644
index 000000000..61dd6d9f9
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_no_commits.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - step
+ - squash
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ 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: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+⚪ Nothing to squash; no commits ahead of main
+
+----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_single_commit.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_single_commit.snap
new file mode 100644
index 000000000..91be80ee6
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_single_commit.snap
@@ -0,0 +1,27 @@
+---
+source: tests/integration_tests/merge.rs
+info:
+ program: wt
+ args:
+ - step
+ - squash
+ env:
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ 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: ""
+ LANG: C
+ LC_ALL: C
+ SOURCE_DATE_EPOCH: "1761609600"
+ WORKTRUNK_BIN: ""
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+⚪ Nothing to squash; already a single commit
+
+----- stderr -----