Skip to content

Commit a6c612b

Browse files
Genki Skygitster
authored andcommitted
rebase: add --allow-empty-message option
This option allows commits with empty commit messages to be rebased, matching the same option in git-commit and git-cherry-pick. While empty log messages are frowned upon, sometimes one finds them in older repositories (e.g. translated from another VCS [0]), or have other reasons for desiring them. The option is available in git-commit and git-cherry-pick, so it is natural to make other git tools play nicely with them. Adding this as an option allows the default to be "give the user a chance to fix", while not interrupting the user's workflow otherwise [1]. [0]: https://stackoverflow.com/q/8542304 [1]: https://public-inbox.org/git/[email protected]/ To implement this, add a new --allow-empty-message flag. Then propagate it to all calls of 'git commit', 'git cherry-pick', and 'git rebase--helper' within the rebase scripts. Signed-off-by: Genki Sky <[email protected]> Reviewed-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5be1f00 commit a6c612b

File tree

7 files changed

+53
-11
lines changed

7 files changed

+53
-11
lines changed

Documentation/git-rebase.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ leave out at most one of A and B, in which case it defaults to HEAD.
244244
Keep the commits that do not change anything from its
245245
parents in the result.
246246

247+
--allow-empty-message::
248+
By default, rebasing commits with an empty message will fail.
249+
This option overrides that behavior, allowing commits with empty
250+
messages to be rebased.
251+
247252
--skip::
248253
Restart the rebasing process by skipping the current patch.
249254

builtin/rebase--helper.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2222
struct option options[] = {
2323
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
2424
OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
25+
OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
26+
N_("allow commits with empty messages")),
2527
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
2628
CONTINUE),
2729
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),

git-rebase--am.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ then
4646
# makes this easy
4747
git cherry-pick ${gpg_sign_opt:+"$gpg_sign_opt"} --allow-empty \
4848
$allow_rerere_autoupdate --right-only "$revisions" \
49+
$allow_empty_message \
4950
${restrict_revision+^$restrict_revision}
5051
ret=$?
5152
else

git-rebase--interactive.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ pick_one () {
281281

282282
test -d "$rewritten" &&
283283
pick_one_preserving_merges "$@" && return
284-
output eval git cherry-pick $allow_rerere_autoupdate \
284+
output eval git cherry-pick $allow_rerere_autoupdate $allow_empty_message \
285285
${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
286286
"$strategy_args" $empty_args $ff "$@"
287287

@@ -406,6 +406,7 @@ pick_one_preserving_merges () {
406406
;;
407407
*)
408408
output eval git cherry-pick $allow_rerere_autoupdate \
409+
$allow_empty_message \
409410
${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
410411
"$strategy_args" "$@" ||
411412
die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
@@ -559,7 +560,8 @@ do_next () {
559560

560561
mark_action_done
561562
do_pick $sha1 "$rest"
562-
git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
563+
git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} \
564+
$allow_empty_message || {
563565
warn "$(eval_gettext "\
564566
Could not amend commit after successfully picking \$sha1... \$rest
565567
This is most likely due to an empty commit message, or the pre-commit hook
@@ -607,21 +609,21 @@ you are able to reword the commit.")"
607609
# This is an intermediate commit; its message will only be
608610
# used in case of trouble. So use the long version:
609611
do_with_author output git commit --amend --no-verify -F "$squash_msg" \
610-
${gpg_sign_opt:+"$gpg_sign_opt"} ||
612+
${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
611613
die_failed_squash $sha1 "$rest"
612614
;;
613615
*)
614616
# This is the final command of this squash/fixup group
615617
if test -f "$fixup_msg"
616618
then
617619
do_with_author git commit --amend --no-verify -F "$fixup_msg" \
618-
${gpg_sign_opt:+"$gpg_sign_opt"} ||
620+
${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
619621
die_failed_squash $sha1 "$rest"
620622
else
621623
cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
622624
rm -f "$GIT_DIR"/MERGE_MSG
623625
do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
624-
${gpg_sign_opt:+"$gpg_sign_opt"} ||
626+
${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
625627
die_failed_squash $sha1 "$rest"
626628
fi
627629
rm -f "$squash_msg" "$fixup_msg"
@@ -754,7 +756,8 @@ case "$action" in
754756
continue)
755757
if test ! -d "$rewritten"
756758
then
757-
exec git rebase--helper ${force_rebase:+--no-ff} --continue
759+
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
760+
--continue
758761
fi
759762
# do we have anything to commit?
760763
if git diff-index --cached --quiet HEAD --
@@ -794,11 +797,11 @@ In both cases, once you're done, continue with:
794797
You have uncommitted changes in your working tree. Please commit them
795798
first and then run 'git rebase --continue' again.")"
796799
do_with_author git commit --amend --no-verify -F "$msg" -e \
797-
${gpg_sign_opt:+"$gpg_sign_opt"} ||
800+
${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
798801
die "$(gettext "Could not commit staged changes.")"
799802
else
800803
do_with_author git commit --no-verify -F "$msg" -e \
801-
${gpg_sign_opt:+"$gpg_sign_opt"} ||
804+
${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
802805
die "$(gettext "Could not commit staged changes.")"
803806
fi
804807
fi
@@ -817,7 +820,8 @@ skip)
817820

818821
if test ! -d "$rewritten"
819822
then
820-
exec git rebase--helper ${force_rebase:+--no-ff} --continue
823+
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
824+
--continue
821825
fi
822826
do_rest
823827
return 0
@@ -1016,7 +1020,8 @@ checkout_onto
10161020
if test -z "$rebase_root" && test ! -d "$rewritten"
10171021
then
10181022
require_clean_work_tree "rebase"
1019-
exec git rebase--helper ${force_rebase:+--no-ff} --continue
1023+
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
1024+
--continue
10201025
fi
10211026
do_rest
10221027

git-rebase--merge.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ continue_merge () {
2727
cmt=$(cat "$state_dir/current")
2828
if ! git diff-index --quiet --ignore-submodules HEAD --
2929
then
30-
if ! git commit ${gpg_sign_opt:+"$gpg_sign_opt"} --no-verify -C "$cmt"
30+
if ! git commit ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message \
31+
--no-verify -C "$cmt"
3132
then
3233
echo "Commit failed, please do not call \"git commit\""
3334
echo "directly, but instead do one of the following: "

git-rebase.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ m,merge! use merging strategies to rebase
2424
i,interactive! let the user edit the list of commits to rebase
2525
x,exec=! add exec lines after each commit of the editable list
2626
k,keep-empty preserve empty commits during rebase
27+
allow-empty-message allow rebasing commits with empty messages
2728
f,force-rebase! force rebase even if branch is up to date
2829
X,strategy-option=! pass the argument through to the merge strategy
2930
stat! display a diffstat of what changed upstream
@@ -89,6 +90,7 @@ action=
8990
preserve_merges=
9091
autosquash=
9192
keep_empty=
93+
allow_empty_message=
9294
test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
9395
case "$(git config --bool commit.gpgsign)" in
9496
true) gpg_sign_opt=-S ;;
@@ -262,6 +264,9 @@ do
262264
--keep-empty)
263265
keep_empty=yes
264266
;;
267+
--allow-empty-message)
268+
allow_empty_message=--allow-empty-message
269+
;;
265270
--preserve-merges)
266271
preserve_merges=t
267272
test -z "$interactive_rebase" && interactive_rebase=implied

t/t3405-rebase-malformed.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
test_description='rebase should handle arbitrary git message'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-rebase.sh
67

78
cat >F <<\EOF
89
This is an example of a commit log message
@@ -25,6 +26,7 @@ test_expect_success setup '
2526
test_tick &&
2627
git commit -m "Initial commit" &&
2728
git branch diff-in-message &&
29+
git branch empty-message-merge &&
2830
2931
git checkout -b multi-line-subject &&
3032
cat F >file2 &&
@@ -45,6 +47,11 @@ test_expect_success setup '
4547
4648
git cat-file commit HEAD | sed -e "1,/^\$/d" >G0 &&
4749
50+
git checkout empty-message-merge &&
51+
echo file3 >file3 &&
52+
git add file3 &&
53+
git commit --allow-empty-message -m "" &&
54+
4855
git checkout master &&
4956
5057
echo One >file1 &&
@@ -69,4 +76,20 @@ test_expect_success 'rebase commit with diff in message' '
6976
test_cmp G G0
7077
'
7178

79+
test_expect_success 'rebase -m commit with empty message' '
80+
test_must_fail git rebase -m master empty-message-merge &&
81+
git rebase --abort &&
82+
git rebase -m --allow-empty-message master empty-message-merge
83+
'
84+
85+
test_expect_success 'rebase -i commit with empty message' '
86+
git checkout diff-in-message &&
87+
set_fake_editor &&
88+
test_must_fail env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
89+
git rebase -i HEAD^ &&
90+
git rebase --abort &&
91+
FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
92+
git rebase -i --allow-empty-message HEAD^
93+
'
94+
7295
test_done

0 commit comments

Comments
 (0)