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

[PATCH] commit-tree: disallow an empty single-parent commit.

From
Junio C Hamano <junkio@cox.net>
Date
Feb 24, 2006, 12:20 UTC
Message-ID
<7vslq99cd5.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<7vy8019d44.fsf@assigned-by-dhcp.cox.net>

Allow "git-commit-tree v2.6.15^{tree} -p HEAD", instead of requiring "git-commit-tree `git rev-parse v2.6.15^{tree}` -p HEAD". The parent parameter that follows -p uses get_sha1() to understand the extended notation, and there is little reason not to allow it for the tree object name parameter.

Also make the check_valid() function simpler. This function which predates sha1_object_info() so it had to do things by hand but there is no reason to read the data just to discard anymore.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 * Replacement patch.  Very lightly tested.
 commit-tree.c |   27 ++++++++++++++++++++-------
 1 files changed, 20 insertions(+), 7 deletions(-)
8f97300b2580b43905ce11eef21e77c5e8e809a6
diff --git a/commit-tree.c b/commit-tree.c
index 88871b0..b1d816c 100644
--- a/commit-tree.c
+++ b/commit-tree.c
@@ -43,14 +43,11 @@ static void add_buffer(char **bufp, unsi
 
 static void check_valid(unsigned char *sha1, const char *expect)
 {
-	void *buf;
 	char type[20];
-	unsigned long size;
 
-	buf = read_sha1_file(sha1, type, &size);
-	if (!buf || strcmp(type, expect))
-		die("%s is not a valid '%s' object", sha1_to_hex(sha1), expect);
-	free(buf);
+	if (sha1_object_info(sha1, type, NULL) || strcmp(type, expect))
+		die("%s is not a valid '%s' object",
+		    sha1_to_hex(sha1), expect);
 }
 
 /*
@@ -75,6 +72,19 @@ static int new_parent(int idx)
 	return 1;
 }
 
+static int check_empty_commit(const unsigned char *tree_sha1,
+			      const unsigned char *parent_sha1)
+{
+	unsigned char sha1[20];
+	char refit[50];
+	sprintf(refit, "%s^{tree}", sha1_to_hex(parent_sha1));
+	if (get_sha1(refit, sha1))
+		die("cannot determine tree in parent commit.");
+	if (!memcmp(sha1, tree_sha1, 20))
+		return error ("empty commit?  aborting.");
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int i;
@@ -90,7 +100,7 @@ int main(int argc, char **argv)
 
 	git_config(git_default_config);
 
-	if (argc < 2 || get_sha1_hex(argv[1], tree_sha1) < 0)
+	if (argc < 2 || get_sha1(argv[1], tree_sha1) < 0)
 		usage(commit_tree_usage);
 
 	check_valid(tree_sha1, "tree");
@@ -105,6 +115,9 @@ int main(int argc, char **argv)
 	}
 	if (!parents)
 		fprintf(stderr, "Committing initial tree %s\n", argv[1]);
+	if (parents == 1)
+		if (check_empty_commit(tree_sha1, parent_sha1[0]))
+			exit(1);
 
 	init_buffer(&buffer, &size);
 	add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
-- 
1.2.3.g7465
Previous: Junio C HamanoNext: Eric W. Biederman
Message 5 of 8 in “FYI: git-am allows creation of empty commits.”
  1. Eric W. BiedermanFeb 23, 2006
  2. Junio C HamanoFeb 24, 2006
  3. Eric W. BiedermanFeb 24, 2006
  4. Junio C HamanoFeb 24, 2006
  5. commit-tree: disallow an empty single-parent commit.Junio C Hamano, Feb 24, 2006
  6. Eric W. BiedermanFeb 24, 2006
  7. Eric WongFeb 24, 2006
  8. Junio C HamanoFeb 25, 2006

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.