Skip to content

Commit cbcf0a6

Browse files
committed
Merge branch 'kb/maint-rebase-autosquash'
* kb/maint-rebase-autosquash: rebase: teach --autosquash to match on sha1 in addition to message rebase: better rearranging of fixup!/squash! lines with --autosquash
2 parents 786f174 + 68d5d03 commit cbcf0a6

File tree

2 files changed

+111
-7
lines changed

2 files changed

+111
-7
lines changed

git-rebase--interactive.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,27 @@ get_saved_options () {
675675
# comes immediately after the former, and change "pick" to
676676
# "fixup"/"squash".
677677
rearrange_squash () {
678-
sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
679-
-e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
680-
"$1" >"$1.sq"
678+
# extract fixup!/squash! lines and resolve any referenced sha1's
679+
while read -r pick sha1 message
680+
do
681+
case "$message" in
682+
"squash! "*|"fixup! "*)
683+
action="${message%%!*}"
684+
rest="${message#*! }"
685+
echo "$sha1 $action $rest"
686+
# if it's a single word, try to resolve to a full sha1 and
687+
# emit a second copy. This allows us to match on both message
688+
# and on sha1 prefix
689+
if test "${rest#* }" = "$rest"; then
690+
fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
691+
if test -n "$fullsha"; then
692+
# prefix the action to uniquely identify this line as
693+
# intended for full sha1 match
694+
echo "$sha1 +$action $fullsha"
695+
fi
696+
fi
697+
esac
698+
done >"$1.sq" <"$1"
681699
test -s "$1.sq" || return
682700

683701
used=
@@ -687,14 +705,26 @@ rearrange_squash () {
687705
*" $sha1 "*) continue ;;
688706
esac
689707
printf '%s\n' "$pick $sha1 $message"
708+
used="$used$sha1 "
690709
while read -r squash action msg
691710
do
692-
case "$message" in
693-
"$msg"*)
711+
case " $used" in
712+
*" $squash "*) continue ;;
713+
esac
714+
emit=0
715+
case "$action" in
716+
+*)
717+
action="${action#+}"
718+
# full sha1 prefix test
719+
case "$msg" in "$sha1"*) emit=1;; esac ;;
720+
*)
721+
# message prefix test
722+
case "$message" in "$msg"*) emit=1;; esac ;;
723+
esac
724+
if test $emit = 1; then
694725
printf '%s\n' "$action $squash $action! $msg"
695726
used="$used$squash "
696-
;;
697-
esac
727+
fi
698728
done <"$1.sq"
699729
done >"$1.rearranged" <"$1"
700730
cat "$1.rearranged" >"$1"

t/t3415-rebase-autosquash.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,78 @@ test_expect_success 'misspelled auto squash' '
9494
test 0 = $(git rev-list final-missquash...HEAD | wc -l)
9595
'
9696

97+
test_expect_success 'auto squash that matches 2 commits' '
98+
git reset --hard base &&
99+
echo 4 >file4 &&
100+
git add file4 &&
101+
test_tick &&
102+
git commit -m "first new commit" &&
103+
echo 1 >file1 &&
104+
git add -u &&
105+
test_tick &&
106+
git commit -m "squash! first" &&
107+
git tag final-multisquash &&
108+
test_tick &&
109+
git rebase --autosquash -i HEAD~4 &&
110+
git log --oneline >actual &&
111+
test 4 = $(wc -l <actual) &&
112+
git diff --exit-code final-multisquash &&
113+
test 1 = "$(git cat-file blob HEAD^^:file1)" &&
114+
test 2 = $(git cat-file commit HEAD^^ | grep first | wc -l) &&
115+
test 1 = $(git cat-file commit HEAD | grep first | wc -l)
116+
'
117+
118+
test_expect_success 'auto squash that matches a commit after the squash' '
119+
git reset --hard base &&
120+
echo 1 >file1 &&
121+
git add -u &&
122+
test_tick &&
123+
git commit -m "squash! third" &&
124+
echo 4 >file4 &&
125+
git add file4 &&
126+
test_tick &&
127+
git commit -m "third commit" &&
128+
git tag final-presquash &&
129+
test_tick &&
130+
git rebase --autosquash -i HEAD~4 &&
131+
git log --oneline >actual &&
132+
test 5 = $(wc -l <actual) &&
133+
git diff --exit-code final-presquash &&
134+
test 0 = "$(git cat-file blob HEAD^^:file1)" &&
135+
test 1 = "$(git cat-file blob HEAD^:file1)" &&
136+
test 1 = $(git cat-file commit HEAD | grep third | wc -l) &&
137+
test 1 = $(git cat-file commit HEAD^ | grep third | wc -l)
138+
'
139+
test_expect_success 'auto squash that matches a sha1' '
140+
git reset --hard base &&
141+
echo 1 >file1 &&
142+
git add -u &&
143+
test_tick &&
144+
git commit -m "squash! $(git rev-parse --short HEAD^)" &&
145+
git tag final-shasquash &&
146+
test_tick &&
147+
git rebase --autosquash -i HEAD^^^ &&
148+
git log --oneline >actual &&
149+
test 3 = $(wc -l <actual) &&
150+
git diff --exit-code final-shasquash &&
151+
test 1 = "$(git cat-file blob HEAD^:file1)" &&
152+
test 1 = $(git cat-file commit HEAD^ | grep squash | wc -l)
153+
'
154+
155+
test_expect_success 'auto squash that matches longer sha1' '
156+
git reset --hard base &&
157+
echo 1 >file1 &&
158+
git add -u &&
159+
test_tick &&
160+
git commit -m "squash! $(git rev-parse --short=11 HEAD^)" &&
161+
git tag final-longshasquash &&
162+
test_tick &&
163+
git rebase --autosquash -i HEAD^^^ &&
164+
git log --oneline >actual &&
165+
test 3 = $(wc -l <actual) &&
166+
git diff --exit-code final-longshasquash &&
167+
test 1 = "$(git cat-file blob HEAD^:file1)" &&
168+
test 1 = $(git cat-file commit HEAD^ | grep squash | wc -l)
169+
'
170+
97171
test_done

0 commit comments

Comments
 (0)