Skip to content

Commit 36863af

Browse files
committed
git-commit --allow-empty
It does not usually make sense to record a commit that has the exact same tree as its sole parent commit and that is why git-commit prevents you from making such a mistake, but when data from foreign scm is involved, it is a different story. We are equipped to represent such an (perhaps insane, perhaps by mistake, or perhaps done on purpose) empty change, and it is better to represent it bypassing the safety valve for native use. This is primarily for use by foreign scm interface scripts. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 13aba1e commit 36863af

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Documentation/git-commit.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git-commit' [-a | --interactive] [-s] [-v] [-u]
1212
[(-c | -C) <commit> | -F <file> | -m <msg> | --amend]
13-
[--no-verify] [-e] [--author <author>]
13+
[--allow-empty] [--no-verify] [-e] [--author <author>]
1414
[--] [[-i | -o ]<file>...]
1515

1616
DESCRIPTION
@@ -89,6 +89,12 @@ OPTIONS
8989
This option bypasses the pre-commit hook.
9090
See also link:hooks.html[hooks].
9191

92+
--allow-empty::
93+
Usually recording a commit that has the exact same tree as its
94+
sole parent commit and the command prevents you from making such
95+
a mistake. This option bypasses the safety, and is primarily
96+
for use by foreign scm interface scripts.
97+
9298
-e|--edit::
9399
The message taken from file with `-F`, command line with
94100
`-m`, and from file with `-C` are usually used as the

git-commit.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ trap '
7474

7575
all=
7676
also=
77+
allow_empty=f
7778
interactive=
7879
only=
7980
logfile=
@@ -114,6 +115,10 @@ do
114115
-a|--a|--al|--all)
115116
all=t
116117
;;
118+
--allo|--allow|--allow-|--allow-e|--allow-em|--allow-emp|\
119+
--allow-empt|--allow-empty)
120+
allow_empty=t
121+
;;
117122
--au=*|--aut=*|--auth=*|--autho=*|--author=*)
118123
force_author="${1#*=}"
119124
;;
@@ -515,9 +520,11 @@ else
515520
# we need to check if there is anything to commit
516521
run_status >/dev/null
517522
fi
518-
case "$?,$PARENTS" in
519-
0,* | *,-p' '?*-p' '?*)
520-
# a merge commit can record the same tree as its parent.
523+
case "$allow_empty,$?,$PARENTS" in
524+
t,* | ?,0,* | ?,*,-p' '?*-p' '?*)
525+
# an explicit --allow-empty, or a merge commit can record the
526+
# same tree as its parent. Otherwise having commitable paths
527+
# is required.
521528
;;
522529
*)
523530
rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"

t/t7501-commit.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ test_expect_success 'same tree (single parent)' '
256256
257257
'
258258

259+
test_expect_success 'same tree (single parent) --allow-empty' '
260+
261+
git commit --allow-empty -m "forced empty" &&
262+
git cat-file commit HEAD | grep forced
263+
264+
'
265+
259266
test_expect_success 'same tree (merge and amend merge)' '
260267
261268
git checkout -b side HEAD^ &&

0 commit comments

Comments
 (0)