Skip to content

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.rs
index 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 allowed
diff --git a/src/git/error.rs b/src/git/error.rs
index 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::Er
anyhow::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 error
pub fn merge_commits_found() -> anyhow::Error {
anyhow::anyhow!(
diff --git a/src/git/mod.rs b/src/git/mod.rs
index 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.rs
index 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.snap
new file mode 100644
index 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 -----
+❌ Can't push to local main branch: it has newer commits
+  * [SHA] Add main file
+
+πŸ’‘ Run 'wt merge' again to incorporate these changes
+
+----- 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.snap
index 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
❌ Can't push to local main branch: it has newer commits
  * [SHA] Main commit
-πŸ’‘ Use 'wt merge' to rebase onto main
+πŸ’‘ Use 'wt step rebase' or 'wt merge' to rebase onto main
----- stderr -----