Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(merge): Prevent empty squash commits
Maximilian Roos61941e13cac0
diff --git a/src/commands/merge.rs b/src/commands/merge.rsindex 81211111b..8fbde2a2f 100644--- a/src/commands/merge.rs+++ b/src/commands/merge.rs@@ -417,6 +417,15 @@ fn handle_squash(repo.run_command(&["reset", "--soft", &merge_base]).git_context("Failed to reset to merge base")?;+ // Check if there are actually any changes to commit+ // This can happen if all commits result in no net changes (e.g., series of changes and reverts)+ if !repo.has_staged_changes()? {+ use worktrunk::styling::{ERROR, ERROR_EMOJI, HINT, HINT_EMOJI};+ return Err(GitError::CommandFailed(format!(+ "{ERROR_EMOJI} {ERROR}No changes to commit after squashing {commit_count} commits{ERROR:#}\n\n{HINT_EMOJI} {HINT}The commits resulted in no net changes (e.g., changes were reverted or already in {CYAN_BOLD}{target_branch}{CYAN_BOLD:#}){HINT:#}"+ )));+ }+// Commit with the generated messagerepo.run_command(&["commit", "-m", &commit_message]).git_context("Failed to create squash commit")?;diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 3c0dd3059..084c8806c 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -578,6 +578,82 @@ fn test_merge_squash_single_commit() {);}+#[test]+fn test_merge_squash_empty_changes() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Create a worktree for main+ let main_wt = repo.root_path().parent().unwrap().join("test-repo.main-wt");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["worktree", "add", main_wt.to_str().unwrap(), "main"])+ .current_dir(repo.root_path())+ .output()+ .expect("Failed to add worktree");++ // Create a feature worktree with commits that result in no net changes+ let feature_wt = repo.add_worktree("feature", "feature");++ // Get the initial content of file.txt (created by the initial commit)+ let file_path = feature_wt.join("file.txt");+ let initial_content = std::fs::read_to_string(&file_path).expect("Failed to read file.txt");++ // Commit 1: Modify file.txt+ std::fs::write(&file_path, "change1").expect("Failed to write file");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "file.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", "Change 1"])+ .current_dir(&feature_wt)+ .output()+ .expect("Failed to commit");++ // Commit 2: Modify file.txt again+ std::fs::write(&file_path, "change2").expect("Failed to write file");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "file.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", "Change 2"])+ .current_dir(&feature_wt)+ .output()+ .expect("Failed to commit");++ // Commit 3: Revert to original content+ std::fs::write(&file_path, initial_content).expect("Failed to write file");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "file.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", "Revert to initial"])+ .current_dir(&feature_wt)+ .output()+ .expect("Failed to commit");++ // Merge with squash - should fail with helpful error about no net changes+ snapshot_merge(+ "merge_squash_empty_changes",+ &repo,+ &["main", "--squash"],+ Some(&feature_wt),+ );+}+#[test]fn test_merge_auto_commit_deterministic() {let mut repo = TestRepo::new();diff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_empty_changes.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_empty_changes.snapnew file mode 100644index 000000000..6d2f565f5--- /dev/null+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_empty_changes.snap@@ -0,0 +1,39 @@+---+source: tests/integration_tests/merge.rs+info:+ program: wt+ args:+ - merge+ - main+ - "--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: /dev/null+ GIT_CONFIG_SYSTEM: /dev/null+ GIT_EDITOR: ""+ LANG: C+ LC_ALL: C+ SOURCE_DATE_EPOCH: "1761609600"+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpuiKLaJ/test-config.toml+---+success: false+exit_code: 1+----- stdout -----+π [36mSquashing 3 commits into 1...[0m+π [36mGenerating squash commit message...[0m+π‘ [2mUsing fallback commit message. Run 'wt config help' to configure LLM-generated messages[0m+[40m [0m [1mSquash commits from main[0m+[40m [0m+[40m [0m Combined commits:+[40m [0m - Change 1+[40m [0m - Change 2+[40m [0m - Revert to initial+++----- stderr -----+β [31mNo changes to commit after squashing 3 commits[0m++π‘ [2mThe commits resulted in no net changes (e.g., changes were reverted or already in [1m[36mmain[0m)[0m