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

(fixed) [PATCH 1/4] Accept commit in some places when tree is needed.

From
Junio C Hamano <junkio@cox.net>
Date
Apr 20, 2005, 06:35 UTC
Message-ID
<7vy8bej6q3.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<7vis2ikmj0.fsf@assigned-by-dhcp.cox.net>

<cover-paragraph> _BLUSH_

The 1/4 in the series was a buggy one I sent by mistake. Please replace it with this fixed one. The other three are OK.

BTW, do you have a preferred patch-mail convention to mark the cover paragraph like this to be excluded from the commit log, like the three-dash one you mentioned to exclude the tail of the message? </cover-paragraph>

Similar to diff-cache which was introduced recently, when the intent is obvious we should accept commit ID when tree ID is required. This patch lifts the tree-from-tree-or-commit logic from diff-cache.c and moves it to sha1_file.c, which is a common library source for the SHA1 storage part.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 cache.h      |    1 +
 diff-cache.c |   19 ++-----------------
 sha1_file.c  |   29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 17 deletions(-)
--- a/cache.h
+++ b/cache.h
@@ -124,5 +124,6 @@ extern void die(const char *err, ...);
 extern int error(const char *err, ...);
 
 extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
+extern void *tree_from_tree_or_commit(const unsigned char *sha1, char *type, unsigned long *size);
 
 #endif /* CACHE_H */
--- a/diff-cache.c
+++ b/diff-cache.c
@@ -245,23 +245,8 @@ int main(int argc, char **argv)
 	if (argc != 2 || get_sha1_hex(argv[1], tree_sha1))
 		usage("diff-cache [-r] [-z] <tree sha1>");
 
-	tree = read_sha1_file(tree_sha1, type, &size);
+	tree = tree_from_tree_or_commit(tree_sha1, type, &size);
 	if (!tree)
-		die("bad tree object %s", argv[1]);
-
-	/* We allow people to feed us a commit object, just because we're nice */
-	if (!strcmp(type, "commit")) {
-		/* tree sha1 is always at offset 5 ("tree ") */
-		if (get_sha1_hex(tree + 5, tree_sha1))
-			die("bad commit object %s", argv[1]);
-		free(tree);
-		tree = read_sha1_file(tree_sha1, type, &size);       
-		if (!tree)
-			die("unable to read tree object %s", sha1_to_hex(tree_sha1));
-	}
-
-	if (strcmp(type, "tree"))
-		die("bad tree object %s (%s)", sha1_to_hex(tree_sha1), type);
-
+		die("cannot get tree object from %s", argv[1]);
 	return diff_cache(tree, size, active_cache, active_nr, "");
 }
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -245,3 +245,32 @@ int write_sha1_buffer(const unsigned cha
 	close(fd);
 	return 0;
 }
+
+void *tree_from_tree_or_commit(const unsigned char *sha1, char *type,
+			       unsigned long *size)
+{
+	void *tree = read_sha1_file(sha1, type, size);
+	if (!tree)
+		return tree;
+
+	/* We allow people to feed us a commit object,
+	 * just because we're nice.
+	 */
+	if (!strcmp(type, "commit")) {
+		/* tree sha1 is always at offset 5 ("tree ") */
+		char tree_sha1[20];
+		if (get_sha1_hex(tree + 5, tree_sha1)) {
+			free(tree);
+			return NULL;
+		}
+		free(tree);
+		tree = read_sha1_file(tree_sha1, type, size);
+		if (!tree)
+			return NULL;
+	}
+	if (strcmp(type , "tree")) {
+		free(tree);
+		return NULL;
+	}
+	return tree;
+}
Previous: Junio C HamanoNext: Linus Torvalds
Message 2 of 4 in “Accept commit in some places when tree is needed.”
  1. 1/4 Accept commit in some places when tree is needed.Junio C Hamano, Apr 20, 2005
  2. (fixed) [PATCH 1/4] Accept commit in some places when tree is needed.Junio C Hamano, Apr 20, 2005
  3. Linus TorvaldsApr 20, 2005
  4. (rework) [PATCH 1/5] Accept commit in some places when tree is needed.Junio C Hamano, Apr 21, 2005

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.