Skip to content

Commit 00a7928

Browse files
jpgraysonclaude
andcommitted
fix(push): run git mergetool for stgit.autoimerge
stgit.autoimerge ran `git merge-tool`, which is not a git command, so the merge tool was never run and conflicted pushes always stopped with conflicts. Run `git mergetool` instead, with the terminal's stdin, stdout, and stderr since merge tools are commonly interactive. The push succeeds only if no unmerged paths remain after the merge tool exits, which matches how the Python implementation of StGit decided the outcome. The last t1213 case drove the conflict report through a fake `git-merge-tool` that resolved every conflict yet exited 1. That path is no longer reachable, so the case is removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N5CRg3qhgS4cj1Ki6vh82L
1 parent 7c7a6b5 commit 00a7928

3 files changed

Lines changed: 119 additions & 40 deletions

File tree

‎src/stupid/context.rs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ impl StupidContext<'_, '_> {
860860
}
861861

862862
/// Perform three-way merge, with optional auto-resolution of conflicts with
863-
/// `git merge-tool`.
863+
/// `git mergetool`.
864864
pub(crate) fn merge_recursive_or_mergetool(
865865
&self,
866866
base_tree_id: gix::ObjectId,
@@ -877,15 +877,25 @@ impl StupidContext<'_, '_> {
877877
}
878878
}
879879

880-
/// Attempt to resolve outstanding merge conflicts with `git merge-tool`.
880+
/// Attempt to resolve outstanding merge conflicts with `git mergetool`.
881+
///
882+
/// The merge tool may be interactive, so it is run with the terminal's standard
883+
/// streams. Returns true if no unmerged paths remain afterwards.
881884
pub(crate) fn mergetool(&self) -> Result<bool> {
882-
let output = self.git().arg("merge-tool").output_git()?;
883-
if output.status.success() {
884-
Ok(true)
885-
} else if output.status.code() == Some(1) {
886-
Ok(false)
885+
let status = self
886+
.git()
887+
.arg("mergetool")
888+
.stdin(Stdio::inherit())
889+
.stdout(Stdio::inherit())
890+
.stderr(Stdio::inherit())
891+
.status()
892+
.context("could not execute `git mergetool`")?;
893+
if status.success() || status.code() == Some(1) {
894+
Ok(self.diff_unmerged_names()?.is_empty())
895+
} else if let Some(code) = status.code() {
896+
Err(anyhow!("`git mergetool` exited with code {code}"))
887897
} else {
888-
Err(git_command_error("mergetool", &output.stderr))
898+
Err(anyhow!("`git mergetool` failed"))
889899
}
890900
}
891901

‎t/t1213-push-conflict-report.sh‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -773,36 +773,4 @@ test_expect_success 'Conflict report with sparse index' '
773773
)
774774
'
775775

776-
# When the conflicts flag is set but the index has no unmerged entries, no
777-
# pathspecs are passed to `git status` and the full short status, including
778-
# untracked files, is reported. This is reachable with stgit.autoimerge when
779-
# the merge tool (stg runs `git merge-tool`) resolves all conflicts but exits
780-
# with status 1.
781-
test_expect_success 'Conflict report with no unmerged index entries' '
782-
make_repo no-unmerged &&
783-
mkdir bin &&
784-
write_script bin/git-merge-tool <<-\EOF &&
785-
git checkout -q --theirs -- :/a.txt :/d/b.txt :/d/e/c.txt &&
786-
git add -u -- :/a.txt :/d/b.txt :/d/e/c.txt &&
787-
exit 1
788-
EOF
789-
(
790-
PATH="$(pwd)/bin:$PATH" &&
791-
cd no-unmerged/d &&
792-
git config stgit.autoimerge true &&
793-
conflict stg push --keep p1 >../actual &&
794-
cat >../expected <<-\EOF &&
795-
> p1 (conflict)
796-
M ../a.txt
797-
M b.txt
798-
M e/c.txt
799-
M e/x.txt
800-
M ../y.txt
801-
?? e/untracked.txt
802-
?? ../untracked.txt
803-
EOF
804-
test_cmp ../expected ../actual
805-
)
806-
'
807-
808776
test_done

‎t/t1214-push-autoimerge.sh‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/sh
2+
3+
test_description='Test stgit.autoimerge
4+
5+
When stgit.autoimerge is true and pushing a patch conflicts, git mergetool is
6+
run to resolve the conflicts. The push succeeds if no unmerged paths remain
7+
afterwards.'
8+
9+
. ./test-lib.sh
10+
11+
test_expect_success 'Setup conflicting patches' '
12+
test_commit_bulk --message="base" --filename=a.txt --contents=base 1 &&
13+
echo base >b.txt &&
14+
git add b.txt &&
15+
git commit -m "add b" &&
16+
stg init &&
17+
stg new -m p1 p1 &&
18+
echo p1-a >a.txt &&
19+
echo p1-b >b.txt &&
20+
stg refresh &&
21+
stg pop &&
22+
stg new -m p2 p2 &&
23+
echo p2-a >a.txt &&
24+
echo p2-b >b.txt &&
25+
stg refresh &&
26+
git config merge.tool fake &&
27+
git config mergetool.fake.trustExitCode true &&
28+
git config mergetool.prompt false &&
29+
git config mergetool.keepBackup false
30+
'
31+
32+
test_expect_success 'Push conflicts without stgit.autoimerge' '
33+
test_when_finished "stg undo --hard" &&
34+
git config mergetool.fake.cmd "exit 99" &&
35+
conflict stg push p1 &&
36+
test "$(stg top)" = "p1" &&
37+
git diff --name-only --diff-filter=U >unmerged &&
38+
test_line_count = 2 unmerged
39+
'
40+
41+
test_expect_success 'Merge tool resolves all conflicts' '
42+
test_when_finished "stg undo --hard" &&
43+
test_config stgit.autoimerge true &&
44+
git config mergetool.fake.cmd "cp \"\$REMOTE\" \"\$MERGED\"" &&
45+
stg push p1 &&
46+
test "$(stg top)" = "p1" &&
47+
git diff --name-only --diff-filter=U >unmerged &&
48+
test_must_be_empty unmerged &&
49+
git diff-index --quiet HEAD -- &&
50+
echo p1-a >expected &&
51+
test_cmp expected a.txt &&
52+
echo p1-b >expected &&
53+
test_cmp expected b.txt &&
54+
test "$(stg series --applied --noprefix)" = "$(printf "p2\np1")"
55+
'
56+
57+
test_expect_success 'Merge tool resolves some conflicts' '
58+
test_when_finished "stg undo --hard" &&
59+
test_config stgit.autoimerge true &&
60+
git config mergetool.fake.cmd "case \"\$MERGED\" in *b.txt) exit 1;; esac; cp \"\$REMOTE\" \"\$MERGED\"" &&
61+
conflict stg push p1 >out &&
62+
test "$(stg top)" = "p1" &&
63+
git diff --name-only --diff-filter=U >unmerged &&
64+
echo b.txt >expected &&
65+
test_cmp expected unmerged &&
66+
grep -e "^UU b.txt" out &&
67+
! grep -e "^[ADU][ADU] a.txt" out &&
68+
echo p1-a >expected &&
69+
test_cmp expected a.txt
70+
'
71+
72+
test_expect_success 'Merge tool resolves nothing' '
73+
test_when_finished "stg undo --hard" &&
74+
test_config stgit.autoimerge true &&
75+
git config mergetool.fake.cmd "exit 1" &&
76+
conflict stg push p1 &&
77+
git diff --name-only --diff-filter=U >unmerged &&
78+
test_line_count = 2 unmerged
79+
'
80+
81+
test_expect_success 'Merge tool is run with stdin and stdout' '
82+
test_when_finished "stg undo --hard" &&
83+
test_config stgit.autoimerge true &&
84+
git config mergetool.fake.cmd "read answer && echo \"\$answer\" >\"\$MERGED\" && echo tool-ran-on-\"\$MERGED\"" &&
85+
printf "answer-a\nanswer-b\n" | stg push p1 >out &&
86+
grep -e "^tool-ran-on-a.txt" out &&
87+
grep -e "^tool-ran-on-b.txt" out &&
88+
echo answer-a >expected &&
89+
test_cmp expected a.txt &&
90+
echo answer-b >expected &&
91+
test_cmp expected b.txt
92+
'
93+
94+
test_expect_success 'stgit.autoimerge has no effect when conflicts are disallowed' '
95+
test_config stgit.autoimerge true &&
96+
git config mergetool.fake.cmd "cp \"\$REMOTE\" \"\$MERGED\"" &&
97+
conflict stg push --conflicts=disallow p1 &&
98+
test "$(stg top)" = "p2"
99+
'
100+
101+
test_done

0 commit comments

Comments
 (0)