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

[PATCH/RFC 01/12] commit-tree: allow indirect tree references

From
Mark Lodato <lodatom@gmail.com>
Date
Mar 13, 2010, 04:52 UTC
Message-ID
<1268455984-19061-2-git-send-email-lodatom@gmail.com>
In-Reply-To
<1268455984-19061-1-git-send-email-lodatom@gmail.com>

'git commit-tree' was the last remaining built-in to accept a <tree> but not a <tree-ish> (an indirect tree reference through a commit or tag object.)

Signed-off-by: Mark Lodato <lodatom@gmail.com>
---
A similar thing should be done for the parent commits.  As far as I can 
tell, this is the only command that requires a commit object, not a tag.
 Documentation/git-commit-tree.txt |    4 +-
 builtin/commit-tree.c             |   13 ++++++----
 t/t1101-commit-tree-indirect.sh   |   49 +++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 7 deletions(-)
 create mode 100755 t/t1101-commit-tree-indirect.sh
diff --git a/Documentation/git-commit-tree.txt b/Documentation/git-commit-tree.txt
index 6188854..67b6bc0 100644
--- a/Documentation/git-commit-tree.txt
+++ b/Documentation/git-commit-tree.txt
@@ -8,7 +8,7 @@ git-commit-tree - Create a new commit object
 
 SYNOPSIS
 --------
-'git commit-tree' <tree> [-p <parent commit>]\* < changelog
+'git commit-tree' <tree-ish> [-p <parent commit>]\* < changelog
 
 DESCRIPTION
 -----------
@@ -35,7 +35,7 @@ state was.
 
 OPTIONS
 -------
-<tree>::
+<tree-ish>::
 	An existing tree object
 
 -p <parent commit>::
diff --git a/builtin/commit-tree.c b/builtin/commit-tree.c
index 90dac34..96a421e 100644
--- a/builtin/commit-tree.c
+++ b/builtin/commit-tree.c
@@ -97,16 +97,19 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
 {
 	int i;
 	struct commit_list *parents = NULL;
-	unsigned char tree_sha1[20];
-	unsigned char commit_sha1[20];
+	unsigned char sha1[20];
 	struct strbuf buffer = STRBUF_INIT;
+	struct tree *tree;
 
 	git_config(git_default_config, NULL);
 
 	if (argc < 2 || !strcmp(argv[1], "-h"))
 		usage(commit_tree_usage);
-	if (get_sha1(argv[1], tree_sha1))
+	if (get_sha1(argv[1], sha1))
 		die("Not a valid object name %s", argv[1]);
+	tree = parse_tree_indirect(sha1);
+	if (!tree)
+		die("%s is not a valid 'tree' object", argv[1]);
 
 	for (i = 2; i < argc; i += 2) {
 		unsigned char sha1[20];
@@ -124,8 +127,8 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
 	if (strbuf_read(&buffer, 0, 0) < 0)
 		die_errno("git commit-tree: failed to read");
 
-	if (!commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
-		printf("%s\n", sha1_to_hex(commit_sha1));
+	if (!commit_tree(buffer.buf, tree->object.sha1, parents, sha1, NULL)) {
+		printf("%s\n", sha1_to_hex(sha1));
 		return 0;
 	}
 	else
diff --git a/t/t1101-commit-tree-indirect.sh b/t/t1101-commit-tree-indirect.sh
new file mode 100755
index 0000000..905bad7
--- /dev/null
+++ b/t/t1101-commit-tree-indirect.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Mark Lodato
+#
+
+test_description='git commit-tree works with indirect references'
+
+. ./test-lib.sh
+
+get_tree() {
+	git cat-file commit $1 | sed -n -e 's/^tree //p'
+}
+
+test_expect_success 'prepare the repository' '
+	touch a &&
+	git add a &&
+	git commit -m x &&
+	git tag -a -m x tag-commit &&
+	git tag -a -m x tag-tree HEAD^{tree} &&
+	git tag -a -m x tag-blob HEAD:a &&
+	git tag -a -m x tag-tag-commit tag-commit
+'
+
+expected_tree="$(get_tree HEAD)"
+
+test_success() {
+	test_expect_success "commit-tree succeeds with $2" '
+		id="$(echo x | git commit-tree '"$1"')" &&
+		tree="$(get_tree $id)" &&
+		test "x$tree" = "x$expected_tree"
+	'
+}
+test_failure() {
+	test_expect_code 128 "commit-tree fails with $2" '
+		echo x | git commit-tree '"$1"' 2>/dev/null
+	'
+}
+
+test_failure HEAD:a "a blob"
+test_success HEAD^{tree} "a tree"
+test_success HEAD "HEAD"
+test_success master "a branch"
+test_failure foo "an invalid object name"
+test_failure tag-blob "a tag pointing to a blob"
+test_success tag-tree "a tag pointing to a tree"
+test_success tag-commit "a tag pointing to a commit"
+test_success tag-tag-commit "a tag pointing to a tag (to a commit)"
+
+test_done
-- 
1.7.0.2
Previous: Mark LodatoNext: Junio C Hamano
Message 2 of 20 in “docs: use metavariables consistently”
  1. 00/12 docs: use metavariables consistentlyMark Lodato, Mar 13, 2010
  2. 01/12 commit-tree: allow indirect tree referencesMark Lodato, Mar 13, 2010
  3. Junio C HamanoMar 13, 2010
  4. Mark LodatoMar 16, 2010
  5. 02/12 grep docs: grep accepts a <tree-ish>, not a <tree>Mark Lodato, Mar 13, 2010
  6. Junio C HamanoMar 13, 2010
  7. Mark LodatoMar 16, 2010
  8. 03/12 fsck docs: remove outdated and useless diagnosticMark Lodato, Mar 13, 2010
  9. 04/12 docs: use <sha1> to mean unabbreviated IDMark Lodato, Mar 13, 2010
  10. 05/12 docs: differentiate between <tag> and <tagname>Mark Lodato, Mar 13, 2010
  11. Junio C HamanoMar 13, 2010
  12. 06/12 docs: clarify <object>, <commit>, <tree-ish>, etcMark Lodato, Mar 13, 2010
  13. 07/12 docs: use <tree> instead of <tree-ish>Mark Lodato, Mar 13, 2010
  14. 08/12 http-fetch docs: use <commit-id> consistentlyMark Lodato, Mar 13, 2010
  15. 09/12 docs: use <commit> instead of <commit-ish>Mark Lodato, Mar 13, 2010
  16. Junio C HamanoMar 13, 2010
  17. Mark LodatoMar 16, 2010
  18. 10/12 diff: use brackets for optional args in usageMark Lodato, Mar 13, 2010
  19. 11/12 docs: use ... instead of * for multiplicityMark Lodato, Mar 13, 2010
  20. 12/12 diff docs: remove <rev>{0,2} notationMark Lodato, Mar 13, 2010

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.