Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(squash): back up the index with plumbing so hidden submodule changes don't abort the squash (#4303)
Maximilian Roos12efb51591a9
diff --git a/src/git/repository/working_tree.rs b/src/git/repository/working_tree.rsindex d8bbd3012..f8dba95ac 100644--- a/src/git/repository/working_tree.rs+++ b/src/git/repository/working_tree.rs@@ -1144,13 +1144,21 @@ impl<'a> WorkingTree<'a> {Ok(has_initialized_submodules_from_status(&output))}- /// Create a safety backup of current working tree state without affecting the working tree.+ /// Back up the index, as a squash is about to commit it, to `refs/wt-backup/<branch>`.///- /// This creates a backup commit containing all changes (staged, unstaged, and untracked files)- /// and stores it in a custom ref (`refs/wt-backup/<branch>`). This creates a reflog entry- /// for recovery without polluting the stash list. The working tree remains unchanged.+ /// Writes the index as a commit whose parent is `HEAD`, so one commit holds+ /// both the branch tip being rewritten and the changes the squash sweeps in,+ /// and records it in the ref's reflog. Unstaged and untracked files are not+ /// captured; the squash leaves them in place. The index, the working tree and+ /// the stash list are untouched.///- /// Users can find safety backups with: `git reflog show refs/wt-backup/<branch>`+ /// Plumbing throughout, so no config hides part of the index: `git stash+ /// create` honors `submodule.<name>.ignore=all` and sees nothing in a staged+ /// gitlink bump. The backup commit is never signed.+ ///+ /// To recover, `git reflog show refs/wt-backup/<branch>` lists the backups;+ /// `git read-tree <sha>` restores that index, `git checkout <sha> -- .` also+ /// restores the files, and `<sha>^` is the branch tip before the squash.////// Returns the short SHA of the backup commit.///@@ -1165,20 +1173,21 @@ impl<'a> WorkingTree<'a> {/// # Ok::<(), anyhow::Error>(())/// ```pub fn create_safety_backup(&self, message: &str) -> anyhow::Result<String> {- // Create a backup commit using git stash create (without storing it in the stash list)+ let tree = self.run_command(&["write-tree"])?;let backup_sha = self- .run_command(&["stash", "create", "--include-untracked"])?+ .run_command(&[+ "commit-tree",+ "--no-gpg-sign",+ tree.trim(),+ "-p",+ "HEAD",+ "-m",+ message,+ ])+ .context("Failed to create backup commit")?.trim().to_string();- // Validate that we got a SHA back- if backup_sha.is_empty() {- return Err(GitError::Other {- message: "git stash create returned empty SHA - no changes to backup".into(),- }- .into());- }-// Get current branch name to use in the ref namelet stdout = self.run_command(&["rev-parse", "--symbolic-full-name", "HEAD"])?;let branch = stdout@@ -1728,11 +1737,8 @@ mod tests {for branch in ["a/b", "a-b"] {test.run_git(&["switch", "-c", branch]);- // Modify the tracked file so `git stash create` picks up changes.- std::fs::write(test.root_path().join("file.txt"), branch).unwrap();wt.create_safety_backup(&format!("{branch} (squash)")).unwrap();- test.run_git(&["checkout", "--", "file.txt"]);test.run_git(&["switch", "main"]);}diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex ab1df877d..ecd0cc89e 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -2639,6 +2639,95 @@ fn test_merge_squash_with_working_tree_creates_backup(mut repo_with_main_worktre);}+/// A staged submodule bump that `submodule.<name>.ignore = all` hides from+/// porcelain is still squashed in and backed up, whatever the branch's commits+/// add up to: the backup snapshots the index with plumbing, which that config+/// cannot hide. `None` restores the file's content from main, so the last case's+/// commits cancel out and the bump is all that remains.+#[rstest]+#[case::two_commits(&[("a.txt", Some("a")), ("b.txt", Some("b"))], "a.txt\nb.txt\nsub")]+#[case::one_commit(&[("a.txt", Some("a"))], "a.txt\nsub")]+#[case::commits_cancel_out(&[("file.txt", Some("changed")), ("file.txt", None)], "sub")]+fn test_step_squash_backs_up_submodule_bump_hidden_by_ignore(+ mut repo: TestRepo,+ #[case] commits: &[(&str, Option<&str>)],+ #[case] squashed_paths: &str,+) {+ use worktrunk::git::PlumbingDiff;++ // A gitlink `sub` on main; uninitialized, so no clone is needed.+ fs::write(+ repo.root_path().join(".gitmodules"),+ "[submodule \"sub\"]\n\tpath = sub\n\turl = ./sub\n",+ )+ .unwrap();+ let old_pointer = repo.git_output(&["rev-parse", "HEAD"]);+ repo.run_git(&["add", ".gitmodules"]);+ repo.run_git(&[+ "update-index",+ "--add",+ "--cacheinfo",+ &format!("160000,{old_pointer},sub"),+ ]);+ repo.run_git(&["commit", "-m", "add submodule"]);+ repo.run_git(&["config", "submodule.sub.ignore", "all"]);++ let feature_wt = repo.add_worktree("feature");+ for (i, (file, content)) in commits.iter().enumerate() {+ match content {+ Some(content) => fs::write(feature_wt.join(file), content).unwrap(),+ None => repo.run_git_in(&feature_wt, &["checkout", "main", "--", file]),+ }+ repo.run_git_in(&feature_wt, &["add", file]);+ repo.run_git_in(&feature_wt, &["commit", "-m", &format!("commit {i}")]);+ }+ let pre_squash_tip = repo.git_output(&["rev-parse", "feature"]);+ let new_pointer = repo.git_output(&["rev-parse", "main"]);+ repo.run_git_in(+ &feature_wt,+ &[+ "update-index",+ "--cacheinfo",+ &format!("160000,{new_pointer},sub"),+ ],+ );++ let output = repo+ .wt_command()+ .args(["step", "squash", "--no-hooks"])+ .current_dir(&feature_wt)+ .env(+ "WORKTRUNK_COMMIT__GENERATION__COMMAND",+ "cat >/dev/null && echo 'feat: combined'",+ )+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "step squash failed: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ assert_eq!(+ repo.git_output(&["rev-parse", "feature^"]),+ repo.git_output(&["rev-parse", "main"])+ );+ assert_eq!(repo.git_output(&["rev-parse", "feature:sub"]), new_pointer);+ assert_eq!(+ repo.git_output(&PlumbingDiff::Tree.args(&["--name-only", "-r", "main", "feature"])),+ squashed_paths+ );+ // The backup holds the squashed tree on top of the pre-squash tip.+ assert_eq!(+ repo.git_output(&["rev-parse", "refs/wt-backup/feature^{tree}"]),+ repo.git_output(&["rev-parse", "feature^{tree}"])+ );+ assert_eq!(+ repo.git_output(&["rev-parse", "refs/wt-backup/feature^"]),+ pre_squash_tip+ );+}+#[rstest]fn test_merge_when_default_branch_missing_worktree(repo: TestRepo) {// Move primary off default branch so no worktree holds itdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snapindex 3390e9c91..b1d63423d 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_and_squash.snap@@ -60,7 +60,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mSquashing 2 commits & working tree changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ d5452aa[22m+[2m↳[22m [2mBackup created @ 650fba7[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1mfix: update file 1 content[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snapindex 07432bf07..bbabe90b0 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snap@@ -60,7 +60,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mSquashing 1 commit & working tree changes into a single commit [90m(1 file, [32m+1[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ ba39431[22m+[2m↳[22m [2mBackup created @ 6cb8292[22m[36m◎[39m [36mGenerating squash commit message...[39m[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [4mwt config --help[24m[22m[107m [0m [1mSquash commits from feature[22mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snapindex f6891c01e..1115df280 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_with_llm.snap@@ -60,7 +60,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mSquashing 1 commit & working tree changes into a single commit [90m(1 file, [32m+1[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ bcc96d5[22m+[2m↳[22m [2mBackup created @ 96b35d4[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1mfix: improve auth validation logic[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snapindex 7033e2860..c50ce144a 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_squash_with_working_tree_creates_backup.snap@@ -61,7 +61,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mSquashing 2 commits & working tree changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ d5452aa[22m+[2m↳[22m [2mBackup created @ 650fba7[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1mfix: update files[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snapindex 8d609b1b2..c779d3c8e 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap@@ -64,7 +64,7 @@ exit_code: 0[107m [0m untracked1.txt[107m [0m untracked2.txt[36m◎[39m [36mSquashing 1 commit & working tree changes into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ a40b656[22m+[2m↳[22m [2mBackup created @ ce9d1c1[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1mfix: commit changes[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_with_both_flags.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_with_both_flags.snapindex 1c4931b06..64ecf8d05 100644--- a/tests/snapshots/integration__integration_tests__merge__step_squash_with_both_flags.snap+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_with_both_flags.snap@@ -64,7 +64,7 @@ exit_code: 0----- stderr -----[2m○[22m Skipping pre-commit hooks (--no-hooks)[36m◎[39m [36mSquashing 2 commits & tracked changes into a single commit [90m(3 files, [32m+3[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ 91a335e[22m+[2m↳[22m [2mBackup created @ 6f7dfd4[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1msquash: combined commits[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_squash_with_stage_tracked_flag.snap b/tests/snapshots/integration__integration_tests__merge__step_squash_with_stage_tracked_flag.snapindex 5efbaa3f9..6cf910651 100644--- a/tests/snapshots/integration__integration_tests__merge__step_squash_with_stage_tracked_flag.snap+++ b/tests/snapshots/integration__integration_tests__merge__step_squash_with_stage_tracked_flag.snap@@ -62,7 +62,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mSquashing 2 commits & tracked changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ d8f4370[22m+[2m↳[22m [2mBackup created @ 3f99f8e[22m[36m◎[39m [36mGenerating squash commit message...[39m[107m [0m [1msquash: combined commits[22m[32m✓[39m [32mSquashed @ [HASH][39mdiff --git a/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_kept.snap b/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_kept.snapindex 0bb410ae4..f3d30f476 100644--- a/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_kept.snap+++ b/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_kept.snap@@ -64,7 +64,7 @@ exit_code: 0[33m▲[39m [33mAuto-staging 1 untracked path:[39m[107m [0m dirty.txt[36m◎[39m [36mSquashing 1 commit & working tree changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ c866be4[22m+[2m↳[22m [2mBackup created @ 874aee4[22m[36m◎[39m [36mGenerating squash commit message...[39m[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [4mwt config --help[24m[22m[107m [0m [1mSquash commits from feature[22mdiff --git a/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_removed.snap b/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_removed.snapindex 5ac43b945..076319ade 100644--- a/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_removed.snap+++ b/tests/snapshots/integration__integration_tests__user_hooks__merge_post_commit_worktree_removed.snap@@ -63,7 +63,7 @@ exit_code: 0[33m▲[39m [33mAuto-staging 1 untracked path:[39m[107m [0m dirty.txt[36m◎[39m [36mSquashing 1 commit & working tree changes into a single commit [90m(2 files, [32m+2[39m[39m[90m)[39m...[39m-[2m↳[22m [2mBackup created @ c866be4[22m+[2m↳[22m [2mBackup created @ 874aee4[22m[36m◎[39m [36mGenerating squash commit message...[39m[2m↳[22m [2mUsing fallback commit message. For LLM setup guide, run [4mwt config --help[24m[22m[107m [0m [1mSquash commits from feature[22m