Skip to content

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

Move safety backup before squash message generation

Maximilian Roosd5bea3901d99
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 43dc66011..2d44ea59f 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -166,6 +166,15 @@ pub fn handle_squash(
};
crate::output::progress(squash_progress)?;
+ // Create safety backup before potentially destructive reset if there are working tree changes
+ if has_staged {
+ let backup_message = format!("{} → {} (squash)", current_branch, target_branch);
+ let (sha, _restore_cmd) = repo.create_safety_backup(&backup_message)?;
+ use worktrunk::styling::AnstyleStyle;
+ let dim = AnstyleStyle::new().dimmed();
+ crate::output::hint(format!("Backup created @ {dim}{sha}{dim:#}"))?;
+ }
+
// Get commit subjects for the squash message
let subjects = repo.commit_subjects(&range)?;
@@ -194,15 +203,6 @@ pub fn handle_squash(
let formatted_message = generator.format_message_for_display(&commit_message);
crate::output::gutter(format_with_gutter(&formatted_message, "", None))?;
- // Create safety backup before potentially destructive reset if there are working tree changes
- if has_staged {
- let backup_message = format!("{} → {} (squash)", current_branch, target_branch);
- let (sha, _restore_cmd) = repo.create_safety_backup(&backup_message)?;
- use worktrunk::styling::AnstyleStyle;
- let dim = AnstyleStyle::new().dimmed();
- crate::output::hint(format!("Backup created @ {dim}{sha}{dim:#}"))?;
- }
-
// Reset to merge base (soft reset stages all changes, including any already-staged uncommitted changes)
repo.run_command(&["reset", "--soft", &merge_base])
.context("Failed to reset to merge base")?;
diff --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.snap
index 5b0e2c523..ec1c6c080 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
@@ -24,6 +24,7 @@ success: true
exit_code: 0
----- stdout -----
🔄 Squashing 2 commits with working tree changes into 1 (2 files, +2)...
+💡 Backup created @ [SHA]
🔄 Generating squash commit message...
💡 Using fallback commit message. Run 'wt config --help' for LLM setup guide
  Squash commits from main
@@ -31,7 +32,6 @@ exit_code: 0
  Combined commits:
  - feat: add file 1
  - feat: add file 2
-💡 Backup created @ [SHA]
✅ Squashed @ [SHA]
🔄 Merging 1 commit to main @ [SHA] (no rebase needed)
  * [SHA] (HEAD -> feature) Squash commits from main
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snap b/tests/snapshots/integration__integration_tests__merge__merge_auto_commit_deterministic.snap
index 112dc7c28..6d4e75b96 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
@@ -22,13 +22,13 @@ success: true
exit_code: 0
----- stdout -----
🔄 Squashing 1 commit with working tree changes into 1 (1 file, +1)...
+💡 Backup created @ [SHA]
🔄 Generating squash commit message...
💡 Using fallback commit message. Run 'wt config --help' for LLM setup guide
  Squash commits from main
 
  Combined commits:
  - feat: initial feature
-💡 Backup created @ [SHA]
✅ Squashed @ [SHA]
🔄 Merging 1 commit to main @ [SHA] (no rebase needed)
  * [SHA] (HEAD -> feature) Squash commits from main
diff --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.snap
index fd5b332d4..000544ab2 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
@@ -24,13 +24,13 @@ success: true
exit_code: 0
----- stdout -----
🔄 Squashing 1 commit with working tree changes into 1 (1 file, +1)...
+💡 Backup created @ [SHA]
🔄 Generating squash commit message...
💡 Using fallback commit message. Run 'wt config --help' for LLM setup guide
  Squash commits from main
 
  Combined commits:
  - feat: add authentication
-💡 Backup created @ [SHA]
✅ Squashed @ [SHA]
🔄 Merging 1 commit to main @ [SHA] (no rebase needed)
  * [SHA] (HEAD -> feature) Squash commits from main
diff --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.snap
index d13fa15d6..dea55e3d8 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
@@ -24,6 +24,7 @@ success: true
exit_code: 0
----- stdout -----
🔄 Squashing 2 commits with working tree changes into 1 (2 files, +2)...
+💡 Backup created @ [SHA]
🔄 Generating squash commit message...
💡 Using fallback commit message. Run 'wt config --help' for LLM setup guide
  Squash commits from main
@@ -31,7 +32,6 @@ exit_code: 0
  Combined commits:
  - feat: add file 1
  - feat: add file 2
-💡 Backup created @ [SHA]
✅ Squashed @ [SHA]
🔄 Merging 1 commit to main @ [SHA] (no rebase needed)
  * [SHA] (HEAD -> feature) Squash commits from main
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap b/tests/snapshots/integration__integration_tests__merge__merge_with_untracked_files.snap
index 3a6d5266b..b3c039644 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
@@ -27,13 +27,13 @@ exit_code: 0
  untracked1.txt
  untracked2.txt
🔄 Squashing 1 commit with working tree changes into 1 (1 file, +1)...
+💡 Backup created @ [SHA]
🔄 Generating squash commit message...
💡 Using fallback commit message. Run 'wt config --help' for LLM setup guide
  Squash commits from main
 
  Combined commits:
  - feat: add file 1
-💡 Backup created @ [SHA]
✅ Squashed @ [SHA]
🔄 Merging 1 commit to main @ [SHA] (no rebase needed)
  * [SHA] (HEAD -> feature) Squash commits from main