Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor push non-fast-forward error messaging
Maximilian Roos8d0795dcaa07
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rsindex d04f9a590..0906401a5 100644--- a/src/commands/worktree.rs+++ b/src/commands/worktree.rs@@ -113,12 +113,12 @@ use std::path::PathBuf;use worktrunk::HookType;use worktrunk::config::{CommandPhase, WorktrunkConfig};use worktrunk::git::{- Repository, branch_already_exists, is_command_not_approved, merge_commits_found,- not_fast_forward, push_failed, worktree_creation_failed, worktree_missing,- worktree_path_exists, worktree_path_occupied,+ Repository, branch_already_exists, is_command_not_approved, merge_commits_found, push_failed,+ worktree_creation_failed, worktree_missing, worktree_path_exists, worktree_path_occupied,};use worktrunk::styling::{- AnstyleStyle, CYAN, CYAN_BOLD, GREEN, GREEN_BOLD, WARNING, format_with_gutter,+ AnstyleStyle, CYAN, CYAN_BOLD, ERROR, ERROR_BOLD, ERROR_EMOJI, GREEN, GREEN_BOLD, HINT,+ HINT_EMOJI, WARNING, format_with_gutter,};use super::command_executor::CommandContext;@@ -512,7 +512,24 @@ pub fn handle_push(.trim().to_string();- bail!("{}", not_fast_forward(&target_branch, &commits_formatted));+ let mut msg = format!(+ "{ERROR_EMOJI} {ERROR}Can't push to local {ERROR_BOLD}{target_branch}{ERROR_BOLD:#}{ERROR} branch: it has newer commits{ERROR:#}"+ );++ if !commits_formatted.is_empty() {+ msg.push('\n');+ msg.push_str(&format_with_gutter(&commits_formatted, "", None));+ }++ // Context-appropriate hint: operations.is_some() means we're in merge context+ let hint = if operations.is_some() {+ "Run 'wt merge' again to incorporate these changes".to_string()+ } else {+ format!("Use 'wt step rebase' or 'wt merge' to rebase onto {target_branch}")+ };+ msg.push_str(&format!("\n{HINT_EMOJI} {HINT}{hint}{HINT:#}"));++ bail!("{}", msg);}// Check for merge commits unless alloweddiff --git a/src/git/error.rs b/src/git/error.rsindex 56708b0c3..de09626b1 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -175,23 +175,6 @@ pub fn conflicting_changes(files: &[String], worktree_path: &Path) -> anyhow::Eranyhow::anyhow!("{}", msg)}-/// Not fast-forward error-pub fn not_fast_forward(target_branch: &str, commits_formatted: &str) -> anyhow::Error {- let mut msg = format!(- "{ERROR_EMOJI} {ERROR}Can't push to local {ERROR_BOLD}{target_branch}{ERROR_BOLD:#}{ERROR} branch: it has newer commits{ERROR:#}"- );-- if !commits_formatted.is_empty() {- msg.push('\n');- msg.push_str(&format_with_gutter(commits_formatted, "", None));- }-- msg.push_str(&format!(- "\n{HINT_EMOJI} {HINT}Use 'wt merge' to rebase onto {target_branch}{HINT:#}"- ));- anyhow::anyhow!("{}", msg)-}-/// Merge commits found errorpub fn merge_commits_found() -> anyhow::Error {anyhow::anyhow!(diff --git a/src/git/mod.rs b/src/git/mod.rsindex 360c63500..f4b6ed911 100644--- a/src/git/mod.rs+++ b/src/git/mod.rs@@ -33,9 +33,9 @@ pub use diff::{DiffStats, LineDiff};pub use error::{WorktrunkError, branch_already_exists, branch_deletion_failed, conflicting_changes,detached_head, error_message, exit_code, is_command_not_approved, merge_commits_found,- no_worktree_found, not_fast_forward, not_interactive, parse_error, push_failed,- rebase_conflict, uncommitted_changes, worktree_creation_failed, worktree_missing,- worktree_path_exists, worktree_path_occupied, worktree_removal_failed,+ no_worktree_found, not_interactive, parse_error, push_failed, rebase_conflict,+ uncommitted_changes, worktree_creation_failed, worktree_missing, worktree_path_exists,+ worktree_path_occupied, worktree_removal_failed,};pub use repository::{Repository, set_base_path};diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rsindex 51403b520..caa82eb85 100644--- a/tests/integration_tests/merge.rs+++ b/tests/integration_tests/merge.rs@@ -266,6 +266,87 @@ fn test_merge_not_fast_forward() {);}+/// Test that `wt merge --no-commit` shows merge-context hint when main has newer commits.+/// The --no-commit flag skips the rebase step, so the push fails with not-fast-forward error.+/// The hint should say "Run 'wt merge' again" (not "Use 'wt merge'").+#[test]+fn test_merge_no_commit_not_fast_forward() {+ let mut repo = TestRepo::new();+ repo.commit("Initial commit");+ repo.setup_remote("main");++ // Get the initial commit SHA to create feature branch from there+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ let initial_sha = cmd+ .args(["rev-parse", "HEAD"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();+ let initial_sha = String::from_utf8_lossy(&initial_sha.stdout)+ .trim()+ .to_string();++ // Add commit to main (this advances main beyond the initial commit)+ std::fs::write(repo.root_path().join("main.txt"), "main content").unwrap();++ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "main.txt"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["commit", "-m", "Add main file"])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ // Create feature worktree from the INITIAL commit (before main advanced)+ let feature_path = repo.root_path().parent().unwrap().join("feature");+ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args([+ "worktree",+ "add",+ "-b",+ "feature",+ feature_path.to_str().unwrap(),+ &initial_sha,+ ])+ .current_dir(repo.root_path())+ .output()+ .unwrap();++ // Add a commit on feature branch+ std::fs::write(feature_path.join("feature.txt"), "feature content").unwrap();++ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["add", "feature.txt"])+ .current_dir(&feature_path)+ .output()+ .unwrap();++ let mut cmd = Command::new("git");+ repo.configure_git_cmd(&mut cmd);+ cmd.args(["commit", "-m", "Add feature file"])+ .current_dir(&feature_path)+ .output()+ .unwrap();++ // Try to merge with --no-commit --no-remove (skips rebase, so push fails with not-fast-forward)+ // Main has "Add main file" commit that feature doesn't have as ancestor+ snapshot_merge(+ "merge_no_commit_not_fast_forward",+ &repo,+ &["main", "--no-commit", "--no-remove"],+ Some(&feature_path),+ );+}+#[test]fn test_merge_rebase_conflict() {let mut repo = TestRepo::new();diff --git a/tests/snapshots/integration__integration_tests__merge__merge_no_commit_not_fast_forward.snap b/tests/snapshots/integration__integration_tests__merge__merge_no_commit_not_fast_forward.snapnew file mode 100644index 000000000..f9cd76a10--- /dev/null+++ b/tests/snapshots/integration__integration_tests__merge__merge_no_commit_not_fast_forward.snap@@ -0,0 +1,32 @@+---+source: tests/integration_tests/merge.rs+info:+ program: wt+ args:+ - merge+ - main+ - "--no-commit"+ - "--no-remove"+ 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: false+exit_code: 1+----- stdout -----+β [31mCan't push to local [1m[31mmain[0m[31m branch: it has newer commits[0m+[107m [0m * [SHA] Add main file++π‘ [2mRun 'wt merge' again to incorporate these changes[0m++----- stderr -----diff --git a/tests/snapshots/integration__integration_tests__push__push_error_not_fast_forward.snap b/tests/snapshots/integration__integration_tests__push__push_error_not_fast_forward.snapindex 3ac5bda34..70aa42b8e 100644--- a/tests/snapshots/integration__integration_tests__push__push_error_not_fast_forward.snap+++ b/tests/snapshots/integration__integration_tests__push__push_error_not_fast_forward.snap@@ -26,6 +26,6 @@ exit_code: 1β [31mCan't push to local [1m[31mmain[0m[31m branch: it has newer commits[0m[107m [0m * [SHA] Main commit-π‘ [2mUse 'wt merge' to rebase onto main[0m+π‘ [2mUse 'wt step rebase' or 'wt merge' to rebase onto main[0m----- stderr -----