git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] git-commit --allow-empty

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 3, 2007, 08:53 UTC
Message-ID
<7vzlwsjh1q.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<7v63zgkw0x.fsf@gitster.siamese.dyndns.org>

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>
---
 * And this is for 'master'.
 Documentation/git-commit.txt |    8 +++++++-
 git-commit.sh                |   13 ++++++++++---
 t/t7501-commit.sh            |    7 +++++++
 3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-commit.txt b/Documentation/git-commit.txt
index d4bfd49..a7ef71f 100644
--- a/Documentation/git-commit.txt
+++ b/Documentation/git-commit.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 [verse]
 'git-commit' [-a | --interactive] [-s] [-v] [-u]
 	   [(-c | -C) <commit> | -F <file> | -m <msg> | --amend]
-	   [--no-verify] [-e] [--author <author>]
+	   [--allow-empty] [--no-verify] [-e] [--author <author>]
 	   [--] [[-i | -o ]<file>...]
 
 DESCRIPTION
@@ -89,6 +89,12 @@ OPTIONS
 	This option bypasses the pre-commit hook.
 	See also link:hooks.html[hooks].
 
+--allow-empty::
+	Usually recording a commit that has the exact same tree as its
+	sole parent commit and the command prevents you from making such
+	a mistake.  This option bypasses the safety, and is primarily
+	for use by foreign scm interface scripts.
+
 -e|--edit::
 	The message taken from file with `-F`, command line with
 	`-m`, and from file with `-C` are usually used as the
diff --git a/git-commit.sh b/git-commit.sh
index cef76a7..2c4a406 100755
--- a/git-commit.sh
+++ b/git-commit.sh
@@ -74,6 +74,7 @@ trap '
 
 all=
 also=
+allow_empty=f
 interactive=
 only=
 logfile=
@@ -114,6 +115,10 @@ do
 	-a|--a|--al|--all)
 		all=t
 		;;
+	--allo|--allow|--allow-|--allow-e|--allow-em|--allow-emp|\
+	--allow-empt|--allow-empty)
+		allow_empty=t
+		;;
 	--au=*|--aut=*|--auth=*|--autho=*|--author=*)
 		force_author="${1#*=}"
 		;;
@@ -515,9 +520,11 @@ else
 	# we need to check if there is anything to commit
 	run_status >/dev/null
 fi
-case "$?,$PARENTS" in
-0,* | *,-p' '?*-p' '?*)
-	# a merge commit can record the same tree as its parent.
+case "$allow_empty,$?,$PARENTS" in
+t,* | ?,0,* | ?,*,-p' '?*-p' '?*)
+	# an explicit --allow-empty, or a merge commit can record the
+	# same tree as its parent.  Otherwise having commitable paths
+	# is required.
 	;;
 *)
 	rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
diff --git a/t/t7501-commit.sh b/t/t7501-commit.sh
index 2e7bcb0..0316ecf 100755
--- a/t/t7501-commit.sh
+++ b/t/t7501-commit.sh
@@ -256,6 +256,13 @@ test_expect_success 'same tree (single parent)' '
 
 '
 
+test_expect_success 'same tree (single parent) --allow-empty' '
+
+	git commit --allow-empty -m "forced empty" &&
+	git cat-file commit HEAD | grep forced
+
+'
+
 test_expect_success 'same tree (merge and amend merge)' '
 
 	git checkout -b side HEAD^ &&
-- 
1.5.3.7-2077-ga07a
Previous: Junio C HamanoNext: Nicolas Pitre
Message 4 of 8 in “hg-to-git: handle an empty dir in hg by combining git commits”
  1. hg-to-git: handle an empty dir in hg by combining git commitsMark Drago, Dec 1, 2007
  2. Junio C HamanoDec 1, 2007
  3. git-commit --allow-emptyJunio C Hamano, Dec 3, 2007
  4. git-commit --allow-emptyJunio C Hamano, Dec 3, 2007
  5. Nicolas PitreDec 3, 2007
  6. Junio C HamanoDec 3, 2007
  7. Junio C HamanoDec 5, 2007
  8. Mark DragoDec 6, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.