Skip to content

Commit c36e465

Browse files
committed
Merge branch 'pt/am-abort-fix' into maint
Various fixes around "git am" that applies a patch to a history that is not there yet. * pt/am-abort-fix: am --abort: keep unrelated commits on unborn branch am --abort: support aborting to unborn branch am --abort: revert changes introduced by failed 3way merge am --skip: support skipping while on unborn branch am -3: support 3way merge on unborn branch am --skip: revert changes introduced by failed 3way merge
2 parents 0533a9b + 6ea3b67 commit c36e465

File tree

2 files changed

+104
-8
lines changed

2 files changed

+104
-8
lines changed

git-am.sh

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ then
6969
cmdline="$cmdline -3"
7070
fi
7171

72+
empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
73+
7274
sq () {
7375
git rev-parse --sq-quote "$@"
7476
}
@@ -85,7 +87,7 @@ safe_to_abort () {
8587
return 1
8688
fi
8789

88-
if ! test -s "$dotest/abort-safety"
90+
if ! test -f "$dotest/abort-safety"
8991
then
9092
return 0
9193
fi
@@ -177,7 +179,8 @@ It does not apply to blobs recorded in its index.")"
177179
then
178180
GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
179181
fi
180-
git-merge-recursive $orig_tree -- HEAD $his_tree || {
182+
our_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree)
183+
git-merge-recursive $orig_tree -- $our_tree $his_tree || {
181184
git rerere $allow_rerere_autoupdate
182185
die "$(gettext "Failed to merge in the changes.")"
183186
}
@@ -502,10 +505,11 @@ then
502505
;;
503506
t,)
504507
git rerere clear
505-
git read-tree --reset -u HEAD HEAD
506-
orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
507-
git reset HEAD
508-
git update-ref ORIG_HEAD $orig_head
508+
head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
509+
git read-tree --reset -u $head_tree $head_tree &&
510+
index_tree=$(git write-tree) &&
511+
git read-tree -m -u $index_tree $head_tree
512+
git read-tree $head_tree
509513
;;
510514
,t)
511515
if test -f "$dotest/rebasing"
@@ -515,8 +519,19 @@ then
515519
git rerere clear
516520
if safe_to_abort
517521
then
518-
git read-tree --reset -u HEAD ORIG_HEAD
519-
git reset ORIG_HEAD
522+
head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
523+
git read-tree --reset -u $head_tree $head_tree &&
524+
index_tree=$(git write-tree) &&
525+
orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
526+
git read-tree -m -u $index_tree $orig_head
527+
if git rev-parse --verify -q ORIG_HEAD >/dev/null 2>&1
528+
then
529+
git reset ORIG_HEAD
530+
else
531+
git read-tree $empty_tree
532+
curr_branch=$(git symbolic-ref HEAD 2>/dev/null) &&
533+
git update-ref -d $curr_branch
534+
fi
520535
fi
521536
rm -fr "$dotest"
522537
exit ;;

t/t4151-am-abort.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test_expect_success setup '
1414
git add file-1 file-2 &&
1515
git commit -m initial &&
1616
git tag initial &&
17+
git format-patch --stdout --root initial >initial.patch &&
1718
for i in 2 3 4 5 6
1819
do
1920
echo $i >>file-1 &&
@@ -63,6 +64,28 @@ do
6364

6465
done
6566

67+
test_expect_success 'am -3 --skip removes otherfile-4' '
68+
git reset --hard initial &&
69+
test_must_fail git am -3 0003-*.patch &&
70+
test 3 -eq $(git ls-files -u | wc -l) &&
71+
test 4 = "$(cat otherfile-4)" &&
72+
git am --skip &&
73+
test_cmp_rev initial HEAD &&
74+
test -z "$(git ls-files -u)" &&
75+
test_path_is_missing otherfile-4
76+
'
77+
78+
test_expect_success 'am -3 --abort removes otherfile-4' '
79+
git reset --hard initial &&
80+
test_must_fail git am -3 0003-*.patch &&
81+
test 3 -eq $(git ls-files -u | wc -l) &&
82+
test 4 = "$(cat otherfile-4)" &&
83+
git am --abort &&
84+
test_cmp_rev initial HEAD &&
85+
test -z $(git ls-files -u) &&
86+
test_path_is_missing otherfile-4
87+
'
88+
6689
test_expect_success 'am --abort will keep the local commits intact' '
6790
test_must_fail git am 0004-*.patch &&
6891
test_commit unrelated &&
@@ -72,4 +95,62 @@ test_expect_success 'am --abort will keep the local commits intact' '
7295
test_cmp expect actual
7396
'
7497

98+
test_expect_success 'am -3 stops on conflict on unborn branch' '
99+
git checkout -f --orphan orphan &&
100+
git reset &&
101+
rm -f otherfile-4 &&
102+
test_must_fail git am -3 0003-*.patch &&
103+
test 2 -eq $(git ls-files -u | wc -l) &&
104+
test 4 = "$(cat otherfile-4)"
105+
'
106+
107+
test_expect_success 'am -3 --skip clears index on unborn branch' '
108+
test_path_is_dir .git/rebase-apply &&
109+
echo tmpfile >tmpfile &&
110+
git add tmpfile &&
111+
git am --skip &&
112+
test -z "$(git ls-files)" &&
113+
test_path_is_missing otherfile-4 &&
114+
test_path_is_missing tmpfile
115+
'
116+
117+
test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' '
118+
git checkout -f --orphan orphan &&
119+
git reset &&
120+
rm -f otherfile-4 file-1 &&
121+
test_must_fail git am -3 0003-*.patch &&
122+
test 2 -eq $(git ls-files -u | wc -l) &&
123+
test 4 = "$(cat otherfile-4)" &&
124+
git am --abort &&
125+
test -z "$(git ls-files -u)" &&
126+
test_path_is_missing otherfile-4
127+
'
128+
129+
test_expect_success 'am -3 --abort on unborn branch removes applied commits' '
130+
git checkout -f --orphan orphan &&
131+
git reset &&
132+
rm -f otherfile-4 otherfile-2 file-1 file-2 &&
133+
test_must_fail git am -3 initial.patch 0003-*.patch &&
134+
test 3 -eq $(git ls-files -u | wc -l) &&
135+
test 4 = "$(cat otherfile-4)" &&
136+
git am --abort &&
137+
test -z "$(git ls-files -u)" &&
138+
test_path_is_missing otherfile-4 &&
139+
test_path_is_missing file-1 &&
140+
test_path_is_missing file-2 &&
141+
test 0 -eq $(git log --oneline 2>/dev/null | wc -l) &&
142+
test refs/heads/orphan = "$(git symbolic-ref HEAD)"
143+
'
144+
145+
test_expect_success 'am --abort on unborn branch will keep local commits intact' '
146+
git checkout -f --orphan orphan &&
147+
git reset &&
148+
test_must_fail git am 0004-*.patch &&
149+
test_commit unrelated2 &&
150+
git rev-parse HEAD >expect &&
151+
git am --abort &&
152+
git rev-parse HEAD >actual &&
153+
test_cmp expect actual
154+
'
155+
75156
test_done

0 commit comments

Comments
 (0)