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

[PATCH] match-trees: factor out fill_tree_desc_strict

From
René Scharfe <rene.scharfe@lsrfire.ath.cx>
Date
Jun 13, 2013, 18:19 UTC
Message-ID
<1371147568-31348-1-git-send-email-rene.scharfe@lsrfire.ath.cx>

Deduplicate code by moving tree_desc initialtization into a helper function, fill_tree_desc_strict. It is like fill_tree_descriptor, except that it only accepts tree hashes and no tree references (tags, commits). No functional change.

Signed-off-by: René Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 match-trees.c | 44 +++++++++++++++++++-------------------------
 1 file changed, 19 insertions(+), 25 deletions(-)
diff --git a/match-trees.c b/match-trees.c
index 2bb734d..7873cde 100644
--- a/match-trees.c
+++ b/match-trees.c
@@ -47,6 +47,22 @@ static int score_matches(unsigned mode1, unsigned mode2, const char *path)
 	return score;
 }
 
+static void *fill_tree_desc_strict(struct tree_desc *desc,
+				   const unsigned char *hash)
+{
+	void *buffer;
+	enum object_type type;
+	unsigned long size;
+
+	buffer = read_sha1_file(hash, &type, &size);
+	if (!buffer)
+		die("unable to read tree (%s)", sha1_to_hex(hash));
+	if (type != OBJ_TREE)
+		die("%s is not a tree", sha1_to_hex(hash));
+	init_tree_desc(desc, buffer, size);
+	return buffer;
+}
+
 static int base_name_entries_compare(const struct name_entry *a,
 				     const struct name_entry *b)
 {
@@ -61,23 +77,10 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
 {
 	struct tree_desc one;
 	struct tree_desc two;
-	void *one_buf, *two_buf;
+	void *one_buf = fill_tree_desc_strict(&one, hash1);
+	void *two_buf = fill_tree_desc_strict(&two, hash2);
 	int score = 0;
-	enum object_type type;
-	unsigned long size;
 
-	one_buf = read_sha1_file(hash1, &type, &size);
-	if (!one_buf)
-		die("unable to read tree (%s)", sha1_to_hex(hash1));
-	if (type != OBJ_TREE)
-		die("%s is not a tree", sha1_to_hex(hash1));
-	init_tree_desc(&one, one_buf, size);
-	two_buf = read_sha1_file(hash2, &type, &size);
-	if (!two_buf)
-		die("unable to read tree (%s)", sha1_to_hex(hash2));
-	if (type != OBJ_TREE)
-		die("%s is not a tree", sha1_to_hex(hash2));
-	init_tree_desc(&two, two_buf, size);
 	for (;;) {
 		struct name_entry e1, e2;
 		int got_entry_from_one = tree_entry(&one, &e1);
@@ -124,16 +127,7 @@ static void match_trees(const unsigned char *hash1,
 			int recurse_limit)
 {
 	struct tree_desc one;
-	void *one_buf;
-	enum object_type type;
-	unsigned long size;
-
-	one_buf = read_sha1_file(hash1, &type, &size);
-	if (!one_buf)
-		die("unable to read tree (%s)", sha1_to_hex(hash1));
-	if (type != OBJ_TREE)
-		die("%s is not a tree", sha1_to_hex(hash1));
-	init_tree_desc(&one, one_buf, size);
+	void *one_buf = fill_tree_desc_strict(&one, hash1);
 
 	while (one.size) {
 		const char *path;
-- 
1.8.3
Next: Eric Sunshine
Message 1 of 2 in “match-trees: factor out fill_tree_desc_strict”
  1. match-trees: factor out fill_tree_desc_strictRené Scharfe, Jun 13, 2013
  2. Eric SunshineJun 13, 2013

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.