match-trees: stop using `the_repository`

Stop using `the_repository` in the "match-trees" subsystem by passing down the already-available repository parameters to internal functions as required. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 17, 2024 at 07:44 UTC d4cd757051d1c779f1d95557b7ac523a6e1803fc
1 file changed +27 -23
match-trees.c
+27 -23
@@ -1,4 +1,3 @@
1 -#define USE_THE_REPOSITORY_VARIABLE
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
@@ -8,6 +7,7 @@
7 #include "tree.h"
8 #include "tree-walk.h"
9 #include "object-store-ll.h"
10 +#include "repository.h"
11
12 static int score_missing(unsigned mode)
13 {
@@ -54,14 +54,15 @@ static int score_matches(unsigned mode1, unsigned mode2)
54 return score;
55 }
56
57 -static void *fill_tree_desc_strict(struct tree_desc *desc,
57 +static void *fill_tree_desc_strict(struct repository *r,
58 + struct tree_desc *desc,
59 const struct object_id *hash)
60 {
61 void *buffer;
62 enum object_type type;
63 unsigned long size;
64
64 - buffer = repo_read_object_file(the_repository, hash, &type, &size);
65 + buffer = repo_read_object_file(r, hash, &type, &size);
66 if (!buffer)
67 die("unable to read tree (%s)", oid_to_hex(hash));
68 if (type != OBJ_TREE)
@@ -80,12 +81,13 @@ static int base_name_entries_compare(const struct name_entry *a,
81 /*
82 * Inspect two trees, and give a score that tells how similar they are.
83 */
83 -static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
84 +static int score_trees(struct repository *r,
85 + const struct object_id *hash1, const struct object_id *hash2)
86 {
87 struct tree_desc one;
88 struct tree_desc two;
87 - void *one_buf = fill_tree_desc_strict(&one, hash1);
88 - void *two_buf = fill_tree_desc_strict(&two, hash2);
89 + void *one_buf = fill_tree_desc_strict(r, &one, hash1);
90 + void *two_buf = fill_tree_desc_strict(r, &two, hash2);
91 int score = 0;
92
93 for (;;) {
@@ -133,7 +135,8 @@ static int score_trees(const struct object_id *hash1, const struct object_id *ha
135 /*
136 * Match one itself and its subtrees with two and pick the best match.
137 */
136 -static void match_trees(const struct object_id *hash1,
138 +static void match_trees(struct repository *r,
139 + const struct object_id *hash1,
140 const struct object_id *hash2,
141 int *best_score,
142 char **best_match,
@@ -141,7 +144,7 @@ static void match_trees(const struct object_id *hash1,
144 int recurse_limit)
145 {
146 struct tree_desc one;
144 - void *one_buf = fill_tree_desc_strict(&one, hash1);
147 + void *one_buf = fill_tree_desc_strict(r, &one, hash1);
148
149 while (one.size) {
150 const char *path;
@@ -152,7 +155,7 @@ static void match_trees(const struct object_id *hash1,
155 elem = tree_entry_extract(&one, &path, &mode);
156 if (!S_ISDIR(mode))
157 goto next;
155 - score = score_trees(elem, hash2);
158 + score = score_trees(r, elem, hash2);
159 if (*best_score < score) {
160 free(*best_match);
161 *best_match = xstrfmt("%s%s", base, path);
@@ -160,7 +163,7 @@ static void match_trees(const struct object_id *hash1,
163 }
164 if (recurse_limit) {
165 char *newbase = xstrfmt("%s%s/", base, path);
163 - match_trees(elem, hash2, best_score, best_match,
166 + match_trees(r, elem, hash2, best_score, best_match,
167 newbase, recurse_limit - 1);
168 free(newbase);
169 }
@@ -175,7 +178,8 @@ static void match_trees(const struct object_id *hash1,
178 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
179 * replacing it with another tree "oid2".
180 */
178 -static int splice_tree(const struct object_id *oid1, const char *prefix,
181 +static int splice_tree(struct repository *r,
182 + const struct object_id *oid1, const char *prefix,
183 const struct object_id *oid2, struct object_id *result)
184 {
185 char *subpath;
@@ -194,7 +198,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
198 if (*subpath)
199 subpath++;
200
197 - buf = repo_read_object_file(the_repository, oid1, &type, &sz);
201 + buf = repo_read_object_file(r, oid1, &type, &sz);
202 if (!buf)
203 die("cannot read tree %s", oid_to_hex(oid1));
204 init_tree_desc(&desc, oid1, buf, sz);
@@ -232,15 +236,15 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
236 oid_to_hex(oid1));
237 if (*subpath) {
238 struct object_id tree_oid;
235 - oidread(&tree_oid, rewrite_here, the_repository->hash_algo);
236 - status = splice_tree(&tree_oid, subpath, oid2, &subtree);
239 + oidread(&tree_oid, rewrite_here, r->hash_algo);
240 + status = splice_tree(r, &tree_oid, subpath, oid2, &subtree);
241 if (status)
242 return status;
243 rewrite_with = &subtree;
244 } else {
245 rewrite_with = oid2;
246 }
243 - hashcpy(rewrite_here, rewrite_with->hash, the_repository->hash_algo);
247 + hashcpy(rewrite_here, rewrite_with->hash, r->hash_algo);
248 status = write_object_file(buf, sz, OBJ_TREE, result);
249 free(buf);
250 return status;
@@ -271,7 +275,7 @@ void shift_tree(struct repository *r,
275 if (!depth_limit)
276 depth_limit = 2;
277
274 - add_score = del_score = score_trees(hash1, hash2);
278 + add_score = del_score = score_trees(r, hash1, hash2);
279 add_prefix = xcalloc(1, 1);
280 del_prefix = xcalloc(1, 1);
281
@@ -279,13 +283,13 @@ void shift_tree(struct repository *r,
283 * See if one's subtree resembles two; if so we need to prefix
284 * two with a few fake trees to match the prefix.
285 */
282 - match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
286 + match_trees(r, hash1, hash2, &add_score, &add_prefix, "", depth_limit);
287
288 /*
289 * See if two's subtree resembles one; if so we need to
290 * pick only subtree of two.
291 */
288 - match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
292 + match_trees(r, hash2, hash1, &del_score, &del_prefix, "", depth_limit);
293
294 /* Assume we do not have to do any shifting */
295 oidcpy(shifted, hash2);
@@ -306,7 +310,7 @@ void shift_tree(struct repository *r,
310 if (!*add_prefix)
311 goto out;
312
309 - splice_tree(hash1, add_prefix, hash2, shifted);
313 + splice_tree(r, hash1, add_prefix, hash2, shifted);
314
315 out:
316 free(add_prefix);
@@ -340,16 +344,16 @@ void shift_tree_by(struct repository *r,
344
345 if (candidate == 3) {
346 /* Both are plausible -- we need to evaluate the score */
343 - int best_score = score_trees(hash1, hash2);
347 + int best_score = score_trees(r, hash1, hash2);
348 int score;
349
350 candidate = 0;
347 - score = score_trees(&sub1, hash2);
351 + score = score_trees(r, &sub1, hash2);
352 if (score > best_score) {
353 candidate = 1;
354 best_score = score;
355 }
352 - score = score_trees(&sub2, hash1);
356 + score = score_trees(r, &sub2, hash1);
357 if (score > best_score)
358 candidate = 2;
359 }
@@ -365,7 +369,7 @@ void shift_tree_by(struct repository *r,
369 * shift tree2 down by adding shift_prefix above it
370 * to match tree1.
371 */
368 - splice_tree(hash1, shift_prefix, hash2, shifted);
372 + splice_tree(r, hash1, shift_prefix, hash2, shifted);
373 else
374 /*
375 * shift tree2 up by removing shift_prefix from it