Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Use dedicated ref for safety backups
Maximilian Roos96f5be1c9753
diff --git a/src/cli.rs b/src/cli.rsindex 1ec350ef2..27d32ce99 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -560,6 +560,9 @@ The merge operation follows a strict order designed for fail-fast execution:commits exist, squashes them into one with LLM-generated message. Skip squashingwith --no-squash.+ A safety backup is created before squashing if there are working tree changes.+ Recover with: git reflog show refs/wt-backup/<branch>+4. Rebase onto targetRebases current branch onto target branch. Detects conflicts and aborts if found.This fails fast before running expensive checks.diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rsindex 9df1f0547..e76f8fd73 100644--- a/src/git/repository/mod.rs+++ b/src/git/repository/mod.rs@@ -681,10 +681,11 @@ impl Repository {/// Create a safety backup of current working tree state without affecting the working tree.///- /// This creates a stash commit containing all changes (staged, unstaged, and untracked files)- /// and adds it to the stash reflog for recovery. The working tree remains unchanged.+ /// 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.///- /// Users can find safety backups with: `git stash list`+ /// Users can find safety backups with: `git reflog show refs/wt-backup/<branch>`////// Returns the SHA of the backup commit and a restore command.///@@ -698,19 +699,45 @@ impl Repository {/// # Ok::<(), worktrunk::git::GitError>(())/// ```pub fn create_safety_backup(&self, message: &str) -> Result<(String, String), GitError> {- // Create a stash commit without modifying the working tree- let stash_sha = self+ // Create a backup commit using git stash create (without storing it in the stash list)+ let backup_sha = self.run_command(&["stash", "create", "--include-untracked"])?.trim().to_string();- // Store the stash commit in the reflog- // This makes it accessible via `git stash list` and prevents it from being garbage collected- self.run_command(&["stash", "store", "-m", message, &stash_sha])- .git_context("Failed to store backup in reflog")?;+ // Validate that we got a SHA back+ if backup_sha.is_empty() {+ return Err(GitError::CommandFailed(+ "git stash create returned empty SHA - no changes to backup".into(),+ ));+ }++ // Get current branch name to use in the ref name+ let branch = self+ .run_command(&["rev-parse", "--abbrev-ref", "HEAD"])?+ .trim()+ .to_string();++ // Sanitize branch name for use in ref path (replace / with -)+ let safe_branch = branch.replace('/', "-");++ // Update a custom ref to point to this commit+ // --create-reflog ensures the reflog is created for this custom ref+ // This creates a reflog entry but doesn't add to the stash list+ let ref_name = format!("refs/wt-backup/{}", safe_branch);+ self.run_command(&[+ "update-ref",+ "--create-reflog",+ "-m",+ message,+ &ref_name,+ &backup_sha,+ ])+ .git_context("Failed to create backup ref")?;// Return short SHA and restore command- let short_sha = &stash_sha[..7];+ // Use git stash apply because the backup is a merge commit (created by git stash create)+ let short_sha = &backup_sha[..7];let restore_cmd = format!("git stash apply --index {}", short_sha);Ok((short_sha.to_string(), restore_cmd))diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 96119c628..4ebe2c859 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -2867,20 +2867,20 @@ fn test_merge_squash_with_working_tree_creates_backup() {],);- // Verify that a stash backup was created+ // Verify that a backup was created in the reflog// Note: The worktree has been removed by the merge, so we check from the repo rootlet mut cmd = Command::new("git");repo.configure_git_cmd(&mut cmd);let output = cmd- .args(["stash", "list"])+ .args(["reflog", "show", "refs/wt-backup/feature"]).current_dir(repo.root_path()).output()- .expect("Failed to list stashes");+ .expect("Failed to show reflog");- let stash_list = String::from_utf8_lossy(&output.stdout);+ let reflog = String::from_utf8_lossy(&output.stdout);assert!(- stash_list.contains("feature → main (squash)"),- "Expected backup stash to be created, but stash list was: {}",- stash_list+ reflog.contains("feature → main (squash)"),+ "Expected backup in reflog, but reflog was: {}",+ reflog);}