Skip to content

Commit

Permalink
Merge branch 'kb/maint-rebase-autosquash'
Browse files Browse the repository at this point in the history
* kb/maint-rebase-autosquash:
  rebase: teach --autosquash to match on sha1 in addition to message
  rebase: better rearranging of fixup!/squash! lines with --autosquash
  • Loading branch information
Junio C Hamano committed Nov 24, 2010
2 parents 786f174 + 68d5d03 commit cbcf0a6
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
44 changes: 37 additions & 7 deletions git-rebase--interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,27 @@ get_saved_options () {
# comes immediately after the former, and change "pick" to
# "fixup"/"squash".
rearrange_squash () {
sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
-e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
"$1" >"$1.sq"
# extract fixup!/squash! lines and resolve any referenced sha1's
while read -r pick sha1 message
do
case "$message" in
"squash! "*|"fixup! "*)
action="${message%%!*}"
rest="${message#*! }"
echo "$sha1 $action $rest"
# if it's a single word, try to resolve to a full sha1 and
# emit a second copy. This allows us to match on both message
# and on sha1 prefix
if test "${rest#* }" = "$rest"; then
fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
if test -n "$fullsha"; then
# prefix the action to uniquely identify this line as
# intended for full sha1 match
echo "$sha1 +$action $fullsha"
fi
fi
esac
done >"$1.sq" <"$1"
test -s "$1.sq" || return

used=
Expand All @@ -687,14 +705,26 @@ rearrange_squash () {
*" $sha1 "*) continue ;;
esac
printf '%s\n' "$pick $sha1 $message"
used="$used$sha1 "
while read -r squash action msg
do
case "$message" in
"$msg"*)
case " $used" in
*" $squash "*) continue ;;
esac
emit=0
case "$action" in
+*)
action="${action#+}"
# full sha1 prefix test
case "$msg" in "$sha1"*) emit=1;; esac ;;
*)
# message prefix test
case "$message" in "$msg"*) emit=1;; esac ;;
esac
if test $emit = 1; then
printf '%s\n' "$action $squash $action! $msg"
used="$used$squash "
;;
esac
fi
done <"$1.sq"
done >"$1.rearranged" <"$1"
cat "$1.rearranged" >"$1"
Expand Down
74 changes: 74 additions & 0 deletions t/t3415-rebase-autosquash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,78 @@ test_expect_success 'misspelled auto squash' '
test 0 = $(git rev-list final-missquash...HEAD | wc -l)
'

test_expect_success 'auto squash that matches 2 commits' '
git reset --hard base &&
echo 4 >file4 &&
git add file4 &&
test_tick &&
git commit -m "first new commit" &&
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! first" &&
git tag final-multisquash &&
test_tick &&
git rebase --autosquash -i HEAD~4 &&
git log --oneline >actual &&
test 4 = $(wc -l <actual) &&
git diff --exit-code final-multisquash &&
test 1 = "$(git cat-file blob HEAD^^:file1)" &&
test 2 = $(git cat-file commit HEAD^^ | grep first | wc -l) &&
test 1 = $(git cat-file commit HEAD | grep first | wc -l)
'

test_expect_success 'auto squash that matches a commit after the squash' '
git reset --hard base &&
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! third" &&
echo 4 >file4 &&
git add file4 &&
test_tick &&
git commit -m "third commit" &&
git tag final-presquash &&
test_tick &&
git rebase --autosquash -i HEAD~4 &&
git log --oneline >actual &&
test 5 = $(wc -l <actual) &&
git diff --exit-code final-presquash &&
test 0 = "$(git cat-file blob HEAD^^:file1)" &&
test 1 = "$(git cat-file blob HEAD^:file1)" &&
test 1 = $(git cat-file commit HEAD | grep third | wc -l) &&
test 1 = $(git cat-file commit HEAD^ | grep third | wc -l)
'
test_expect_success 'auto squash that matches a sha1' '
git reset --hard base &&
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! $(git rev-parse --short HEAD^)" &&
git tag final-shasquash &&
test_tick &&
git rebase --autosquash -i HEAD^^^ &&
git log --oneline >actual &&
test 3 = $(wc -l <actual) &&
git diff --exit-code final-shasquash &&
test 1 = "$(git cat-file blob HEAD^:file1)" &&
test 1 = $(git cat-file commit HEAD^ | grep squash | wc -l)
'

test_expect_success 'auto squash that matches longer sha1' '
git reset --hard base &&
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! $(git rev-parse --short=11 HEAD^)" &&
git tag final-longshasquash &&
test_tick &&
git rebase --autosquash -i HEAD^^^ &&
git log --oneline >actual &&
test 3 = $(wc -l <actual) &&
git diff --exit-code final-longshasquash &&
test 1 = "$(git cat-file blob HEAD^:file1)" &&
test 1 = $(git cat-file commit HEAD^ | grep squash | wc -l)
'

test_done

0 comments on commit cbcf0a6

Please sign in to comment.