rebase -i: comment out squash!/fixup! subjects from squash message

When squashing commit messages the squash!/fixup! subjects are not of
interest so comment them out to stop them becoming part of the final
message.

This change breaks a bunch of --autosquash tests which rely on the
"squash! <subject>" line appearing in the final commit message. This is
addressed by adding a second line to the commit message of the "squash!
..." commits and testing for that.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Reviewed-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Charvi Mendiratta <charvi077@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Phillip Wood 2021-01-29 23:50:44 +05:30 committed by Junio C Hamano
parent 498bb5b82e
commit 7cdb968254
3 changed files with 36 additions and 19 deletions

View File

@ -1718,15 +1718,34 @@ static int is_pick_or_similar(enum todo_command command)
}
}
static size_t subject_length(const char *body)
{
const char *p = body;
while (*p) {
const char *next = skip_blank_lines(p);
if (next != p)
break;
p = strchrnul(p, '\n');
if (*p)
p++;
}
return p - body;
}
static void append_squash_message(struct strbuf *buf, const char *body,
struct replay_opts *opts)
{
size_t commented_len = 0;
unlink(rebase_path_fixup_msg());
if (starts_with(body, "squash!") || starts_with(body, "fixup!"))
commented_len = subject_length(body);
strbuf_addf(buf, "\n%c ", comment_line_char);
strbuf_addf(buf, _("This is the commit message #%d:"),
++opts->current_fixup_count + 1);
strbuf_addstr(buf, "\n\n");
strbuf_addstr(buf, body);
strbuf_add_commented_lines(buf, body, commented_len);
strbuf_addstr(buf, body + commented_len);
}
static int update_squash_messages(struct repository *r,

View File

@ -81,8 +81,7 @@ test_auto_squash () {
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! first" &&
git commit -m "squash! first" -m "extra para for first" &&
git tag $1 &&
test_tick &&
git rebase $2 -i HEAD^^^ &&
@ -139,7 +138,7 @@ test_expect_success 'auto squash that matches 2 commits' '
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "squash! first" &&
git commit -m "squash! first" -m "extra para for first" &&
git tag final-multisquash &&
test_tick &&
git rebase --autosquash -i HEAD~4 &&
@ -192,7 +191,7 @@ test_expect_success 'auto squash that matches a sha1' '
git add -u &&
test_tick &&
oid=$(git rev-parse --short HEAD^) &&
git commit -m "squash! $oid" &&
git commit -m "squash! $oid" -m "extra para" &&
git tag final-shasquash &&
test_tick &&
git rebase --autosquash -i HEAD^^^ &&
@ -203,7 +202,8 @@ test_expect_success 'auto squash that matches a sha1' '
git cat-file blob HEAD^:file1 >actual &&
test_cmp expect actual &&
git cat-file commit HEAD^ >commit &&
grep squash commit >actual &&
! grep "squash" commit &&
grep "^extra para" commit >actual &&
test_line_count = 1 actual
'
@ -213,7 +213,7 @@ test_expect_success 'auto squash that matches longer sha1' '
git add -u &&
test_tick &&
oid=$(git rev-parse --short=11 HEAD^) &&
git commit -m "squash! $oid" &&
git commit -m "squash! $oid" -m "extra para" &&
git tag final-longshasquash &&
test_tick &&
git rebase --autosquash -i HEAD^^^ &&
@ -224,7 +224,8 @@ test_expect_success 'auto squash that matches longer sha1' '
git cat-file blob HEAD^:file1 >actual &&
test_cmp expect actual &&
git cat-file commit HEAD^ >commit &&
grep squash commit >actual &&
! grep "squash" commit &&
grep "^extra para" commit >actual &&
test_line_count = 1 actual
'
@ -233,7 +234,7 @@ test_auto_commit_flags () {
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit --$1 first-commit &&
git commit --$1 first-commit -m "extra para for first" &&
git tag final-commit-$1 &&
test_tick &&
git rebase --autosquash -i HEAD^^^ &&
@ -261,11 +262,11 @@ test_auto_fixup_fixup () {
echo 1 >file1 &&
git add -u &&
test_tick &&
git commit -m "$1! first" &&
git commit -m "$1! first" -m "extra para for first" &&
echo 2 >file1 &&
git add -u &&
test_tick &&
git commit -m "$1! $2! first" &&
git commit -m "$1! $2! first" -m "second extra para for first" &&
git tag "final-$1-$2" &&
test_tick &&
(
@ -326,12 +327,12 @@ test_expect_success C_LOCALE_OUTPUT 'autosquash with custom inst format' '
git add -u &&
test_tick &&
oid=$(git rev-parse --short HEAD^) &&
git commit -m "squash! $oid" &&
git commit -m "squash! $oid" -m "extra para for first" &&
echo 1 >file1 &&
git add -u &&
test_tick &&
subject=$(git log -n 1 --format=%s HEAD~2) &&
git commit -m "squash! $subject" &&
git commit -m "squash! $subject" -m "second extra para for first" &&
git tag final-squash-instFmt &&
test_tick &&
git rebase --autosquash -i HEAD~4 &&
@ -342,8 +343,9 @@ test_expect_success C_LOCALE_OUTPUT 'autosquash with custom inst format' '
git cat-file blob HEAD^:file1 >actual &&
test_cmp expect actual &&
git cat-file commit HEAD^ >commit &&
grep squash commit >actual &&
test_line_count = 2 actual
! grep "squash" commit &&
grep first commit >actual &&
test_line_count = 3 actual
'
test_expect_success 'autosquash with empty custom instructionFormat' '

View File

@ -226,10 +226,6 @@ test_commit_autosquash_multi_encoding () {
git rev-list HEAD >actual &&
test_line_count = 3 actual &&
iconv -f $old -t UTF-8 "$TEST_DIRECTORY"/t3900/$msg >expect &&
if test $flag = squash; then
subject="$(head -1 expect)" &&
printf "\nsquash! %s\n" "$subject" >>expect
fi &&
git cat-file commit HEAD^ >raw &&
(sed "1,/^$/d" raw | iconv -f $new -t utf-8) >actual &&
test_cmp expect actual