match-trees: factor out fill_tree_desc_strict

Deduplicate code by moving tree_desc initialization 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 13, 2013 at 20:19 UTC 10a3fb00ebbf19d5c22a0aa9ccd7803fe5367329
1 file changed +19 -25
match-trees.c
+19 -25
@@ -47,6 +47,22 @@ static int score_matches(unsigned mode1, unsigned mode2, const char *path)
47 return score;
48 }
49
50 +static void *fill_tree_desc_strict(struct tree_desc *desc,
51 + const unsigned char *hash)
52 +{
53 + void *buffer;
54 + enum object_type type;
55 + unsigned long size;
56 +
57 + buffer = read_sha1_file(hash, &type, &size);
58 + if (!buffer)
59 + die("unable to read tree (%s)", sha1_to_hex(hash));
60 + if (type != OBJ_TREE)
61 + die("%s is not a tree", sha1_to_hex(hash));
62 + init_tree_desc(desc, buffer, size);
63 + return buffer;
64 +}
65 +
66 static int base_name_entries_compare(const struct name_entry *a,
67 const struct name_entry *b)
68 {
@@ -61,23 +77,10 @@ static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
77 {
78 struct tree_desc one;
79 struct tree_desc two;
64 - void *one_buf, *two_buf;
80 + void *one_buf = fill_tree_desc_strict(&one, hash1);
81 + void *two_buf = fill_tree_desc_strict(&two, hash2);
82 int score = 0;
66 - enum object_type type;
67 - unsigned long size;
83
69 - one_buf = read_sha1_file(hash1, &type, &size);
70 - if (!one_buf)
71 - die("unable to read tree (%s)", sha1_to_hex(hash1));
72 - if (type != OBJ_TREE)
73 - die("%s is not a tree", sha1_to_hex(hash1));
74 - init_tree_desc(&one, one_buf, size);
75 - two_buf = read_sha1_file(hash2, &type, &size);
76 - if (!two_buf)
77 - die("unable to read tree (%s)", sha1_to_hex(hash2));
78 - if (type != OBJ_TREE)
79 - die("%s is not a tree", sha1_to_hex(hash2));
80 - init_tree_desc(&two, two_buf, size);
84 for (;;) {
85 struct name_entry e1, e2;
86 int got_entry_from_one = tree_entry(&one, &e1);
@@ -124,16 +127,7 @@ static void match_trees(const unsigned char *hash1,
127 int recurse_limit)
128 {
129 struct tree_desc one;
127 - void *one_buf;
128 - enum object_type type;
129 - unsigned long size;
130 -
131 - one_buf = read_sha1_file(hash1, &type, &size);
132 - if (!one_buf)
133 - die("unable to read tree (%s)", sha1_to_hex(hash1));
134 - if (type != OBJ_TREE)
135 - die("%s is not a tree", sha1_to_hex(hash1));
136 - init_tree_desc(&one, one_buf, size);
130 + void *one_buf = fill_tree_desc_strict(&one, hash1);
131
132 while (one.size) {
133 const char *path;