Skip to content

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

fix(push): read the push range from the target branch, not a same-named tag (#4499)

Worktrunk Botf5dec9c4f9ea
diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rs
index 46de2b291..fbf96e56d 100644
--- a/src/commands/repository_ext.rs
+++ b/src/commands/repository_ext.rs
@@ -71,6 +71,9 @@ pub trait RepositoryCliExt {
/// so this only names the files that genuinely conflict, before anything
/// moves.
///
+ /// The push range is `target_tip..source_sha`; `target_branch` only names
+ /// the branch in the error.
+ ///
/// The caller has already established that `target_worktree` exists on disk
/// (`MergeContext::prepare` refuses a registered-but-missing worktree), so
/// the status read here is free to fail if the directory is gone.
@@ -83,6 +86,7 @@ pub trait RepositoryCliExt {
&self,
target_worktree: Option<&PathBuf>,
target_branch: &str,
+ target_tip: &str,
source_sha: &str,
) -> anyhow::Result<()>;
@@ -443,6 +447,7 @@ impl RepositoryCliExt for Repository {
&self,
target_worktree: Option<&PathBuf>,
target_branch: &str,
+ target_tip: &str,
source_sha: &str,
) -> anyhow::Result<()> {
let Some(wt_path) = target_worktree else {
@@ -461,7 +466,7 @@ impl RepositoryCliExt for Repository {
return Ok(());
}
- let push_files = self.changed_files(target_branch, source_sha)?;
+ let push_files = self.changed_files(target_tip, source_sha)?;
let wt_files = parse_porcelain_z(&wt_status_output);
let overlapping: Vec<String> = push_files
diff --git a/src/commands/worktree/push.rs b/src/commands/worktree/push.rs
index 10e161325..0c6dbd1c7 100644
--- a/src/commands/worktree/push.rs
+++ b/src/commands/worktree/push.rs
@@ -160,6 +160,8 @@ impl MergeContext {
// Snapshot target SHA early for TOCTOU safety (used by both strategies
// for the fast-forward check; --no-ff also uses it for update-ref).
+ // Every later range reads this SHA rather than the bare branch name,
+ // which git would resolve to a same-named tag first.
let target_ref = format!("refs/heads/{}", target_branch);
let target_tip = repo
.run_command(&["rev-parse", &target_ref])?
@@ -197,7 +199,12 @@ impl MergeContext {
// Refuse when uncommitted changes in the target worktree overlap the
// push range. Non-overlapping changes stay in place: the two-tree
// merge in `advance_target` carries them through untouched.
- repo.ensure_no_target_conflicts(target_worktree_path.as_ref(), &target_branch, &head_sha)?;
+ repo.ensure_no_target_conflicts(
+ target_worktree_path.as_ref(),
+ &target_branch,
+ &target_tip,
+ &head_sha,
+ )?;
// TODO(#3519 follow-up): when `target_branch` was behind its upstream
// (see `Repository::span_upstream`), this count mixes the carried
@@ -206,11 +213,11 @@ impl MergeContext {
// overstates what the branch itself contributed. Splitting them needs
// a carried-count threaded through `MergeContext`; deferred as
// cosmetic.
- let commit_count = repo.count_commits(&target_branch, &head_sha)?;
+ let commit_count = repo.count_commits(&target_tip, &head_sha)?;
let stats_summary = if commit_count > 0 {
repo.current_worktree()
- .prepare_commit_diff(&target_branch, &head_sha)
+ .prepare_commit_diff(&target_tip, &head_sha)
.stats_summary()
} else {
Vec::new()
@@ -267,12 +274,12 @@ impl MergeContext {
"--graph",
"--oneline",
"--end-of-options",
- &format!("{}..{}", self.target_branch, self.head_sha),
+ &format!("{}..{}", self.target_tip, self.head_sha),
])?;
eprintln!("{}", format_with_gutter(&log_output, None));
// Diff statistics
- crate::commands::show_diffstat(&self.repo, &self.target_branch, &self.head_sha)?;
+ crate::commands::show_diffstat(&self.repo, &self.target_tip, &self.head_sha)?;
Ok(())
}
diff --git a/tests/integration_tests/push.rs b/tests/integration_tests/push.rs
index a7f625cf9..af49c8940 100644
--- a/tests/integration_tests/push.rs
+++ b/tests/integration_tests/push.rs
@@ -65,6 +65,73 @@ fn test_push_to_default_branch(#[from(repo_with_feature_worktree)] repo: TestRep
snapshot_push("push_to_default", &repo, &[], Some(feature_wt));
}
+/// A tag named after the target branch must not stand in for the branch. Git
+/// resolves a bare `main` to `refs/tags/main` before `refs/heads/main`, so
+/// with such a tag at the feature commit, `main..HEAD` counted zero commits
+/// and the push reported "Already up to date" without moving the branch.
+#[rstest]
+fn test_push_ignores_tag_named_like_target(mut repo: TestRepo) {
+ repo.add_main_worktree();
+ let feature_wt =
+ repo.add_worktree_with_commit("feature", "test.txt", "test content", "Add test file");
+ let feature_sha = repo.head_sha_in(&feature_wt);
+ repo.run_git(&["tag", "main", &feature_sha]);
+
+ let output = repo
+ .wt_command()
+ .args(["step", "push", "main"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("failed to run command");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(output.status.success(), "push failed: {stderr}");
+ assert!(
+ !stderr.contains("Already up to date"),
+ "push should not compare against the tag: {stderr}"
+ );
+
+ let main_sha = repo
+ .git_command()
+ .args(["rev-parse", "refs/heads/main"])
+ .run()
+ .unwrap();
+ assert_eq!(
+ String::from_utf8_lossy(&main_sha.stdout).trim(),
+ feature_sha
+ );
+}
+
+/// The overlap check against the target worktree's uncommitted changes reads
+/// the push range from the branch, not a same-named tag. With the tag at the
+/// feature commit, the range looked empty and the overlap went unreported.
+#[rstest]
+fn test_push_dirty_target_overlap_ignores_tag_named_like_target(mut repo: TestRepo) {
+ std::fs::write(repo.root_path().join("conflict.txt"), "old content").unwrap();
+ let feature_wt = repo.add_worktree_with_commit(
+ "feature",
+ "conflict.txt",
+ "new content",
+ "Add conflict file",
+ );
+ let feature_sha = repo.head_sha_in(&feature_wt);
+ repo.run_git(&["tag", "main", &feature_sha]);
+
+ let output = repo
+ .wt_command()
+ .args(["step", "push", "main"])
+ .current_dir(&feature_wt)
+ .output()
+ .expect("failed to run command");
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(!output.status.success(), "push should refuse: {stderr}");
+ assert!(
+ stderr.contains("conflicting uncommitted changes") && stderr.contains("conflict.txt"),
+ "the overlap check should refuse and name the file: {stderr}"
+ );
+ let main_contents = std::fs::read_to_string(repo.root_path().join("conflict.txt")).unwrap();
+ assert_eq!(main_contents, "old content");
+}
+
#[rstest]
fn test_push_with_dirty_target(mut repo: TestRepo) {
// Make main worktree (repo root) dirty with a conflicting file