match-trees: convert shift_tree() and shift_tree_by() to use object_id

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Apr 17, 2016 at 23:10 UTC 82db3d44e7a9d9219dd8e19607026bae9e435012
4 files changed +27 -27
cache.h
+2 -2
@@ -1738,8 +1738,8 @@ int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int
1738 extern int diff_auto_refresh_index;
1739
1740 /* match-trees.c */
1741 -void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
1742 -void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *, const char *);
1741 +void shift_tree(const struct object_id *, const struct object_id *, struct object_id *, int);
1742 +void shift_tree_by(const struct object_id *, const struct object_id *, struct object_id *, const char *);
1743
1744 /*
1745 * whitespace rules.
match-trees.c
+22 -22
@@ -229,9 +229,9 @@ static int splice_tree(const unsigned char *hash1,
229 * other hand, it could cover tree one and we might need to pick a
230 * subtree of it.
231 */
232 -void shift_tree(const unsigned char *hash1,
233 - const unsigned char *hash2,
234 - unsigned char *shifted,
232 +void shift_tree(const struct object_id *hash1,
233 + const struct object_id *hash2,
234 + struct object_id *shifted,
235 int depth_limit)
236 {
237 char *add_prefix;
@@ -245,7 +245,7 @@ void shift_tree(const unsigned char *hash1,
245 if (!depth_limit)
246 depth_limit = 2;
247
248 - add_score = del_score = score_trees(hash1, hash2);
248 + add_score = del_score = score_trees(hash1->hash, hash2->hash);
249 add_prefix = xcalloc(1, 1);
250 del_prefix = xcalloc(1, 1);
251
@@ -253,16 +253,16 @@ void shift_tree(const unsigned char *hash1,
253 * See if one's subtree resembles two; if so we need to prefix
254 * two with a few fake trees to match the prefix.
255 */
256 - match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
256 + match_trees(hash1->hash, hash2->hash, &add_score, &add_prefix, "", depth_limit);
257
258 /*
259 * See if two's subtree resembles one; if so we need to
260 * pick only subtree of two.
261 */
262 - match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
262 + match_trees(hash2->hash, hash1->hash, &del_score, &del_prefix, "", depth_limit);
263
264 /* Assume we do not have to do any shifting */
265 - hashcpy(shifted, hash2);
265 + oidcpy(shifted, hash2);
266
267 if (add_score < del_score) {
268 /* We need to pick a subtree of two */
@@ -271,16 +271,16 @@ void shift_tree(const unsigned char *hash1,
271 if (!*del_prefix)
272 return;
273
274 - if (get_tree_entry(hash2, del_prefix, shifted, &mode))
274 + if (get_tree_entry(hash2->hash, del_prefix, shifted->hash, &mode))
275 die("cannot find path %s in tree %s",
276 - del_prefix, sha1_to_hex(hash2));
276 + del_prefix, oid_to_hex(hash2));
277 return;
278 }
279
280 if (!*add_prefix)
281 return;
282
283 - splice_tree(hash1, add_prefix, hash2, shifted);
283 + splice_tree(hash1->hash, add_prefix, hash2->hash, shifted->hash);
284 }
285
286 /*
@@ -288,44 +288,44 @@ void shift_tree(const unsigned char *hash1,
288 * Unfortunately we cannot fundamentally tell which one to
289 * be prefixed, as recursive merge can work in either direction.
290 */
291 -void shift_tree_by(const unsigned char *hash1,
292 - const unsigned char *hash2,
293 - unsigned char *shifted,
291 +void shift_tree_by(const struct object_id *hash1,
292 + const struct object_id *hash2,
293 + struct object_id *shifted,
294 const char *shift_prefix)
295 {
296 - unsigned char sub1[20], sub2[20];
296 + struct object_id sub1, sub2;
297 unsigned mode1, mode2;
298 unsigned candidate = 0;
299
300 /* Can hash2 be a tree at shift_prefix in tree hash1? */
301 - if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
301 + if (!get_tree_entry(hash1->hash, shift_prefix, sub1.hash, &mode1) &&
302 S_ISDIR(mode1))
303 candidate |= 1;
304
305 /* Can hash1 be a tree at shift_prefix in tree hash2? */
306 - if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
306 + if (!get_tree_entry(hash2->hash, shift_prefix, sub2.hash, &mode2) &&
307 S_ISDIR(mode2))
308 candidate |= 2;
309
310 if (candidate == 3) {
311 /* Both are plausible -- we need to evaluate the score */
312 - int best_score = score_trees(hash1, hash2);
312 + int best_score = score_trees(hash1->hash, hash2->hash);
313 int score;
314
315 candidate = 0;
316 - score = score_trees(sub1, hash2);
316 + score = score_trees(sub1.hash, hash2->hash);
317 if (score > best_score) {
318 candidate = 1;
319 best_score = score;
320 }
321 - score = score_trees(sub2, hash1);
321 + score = score_trees(sub2.hash, hash1->hash);
322 if (score > best_score)
323 candidate = 2;
324 }
325
326 if (!candidate) {
327 /* Neither is plausible -- do not shift */
328 - hashcpy(shifted, hash2);
328 + oidcpy(shifted, hash2);
329 return;
330 }
331
@@ -334,11 +334,11 @@ void shift_tree_by(const unsigned char *hash1,
334 * shift tree2 down by adding shift_prefix above it
335 * to match tree1.
336 */
337 - splice_tree(hash1, shift_prefix, hash2, shifted);
337 + splice_tree(hash1->hash, shift_prefix, hash2->hash, shifted->hash);
338 else
339 /*
340 * shift tree2 up by removing shift_prefix from it
341 * to match tree1.
342 */
343 - hashcpy(shifted, sub2);
343 + oidcpy(shifted, &sub2);
344 }
merge-recursive.c
+2 -2
@@ -29,9 +29,9 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
29 struct object_id shifted;
30
31 if (!*subtree_shift) {
32 - shift_tree(one->object.oid.hash, two->object.oid.hash, shifted.hash, 0);
32 + shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
33 } else {
34 - shift_tree_by(one->object.oid.hash, two->object.oid.hash, shifted.hash,
34 + shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
35 subtree_shift);
36 }
37 if (!oidcmp(&two->object.oid, &shifted))
test-match-trees.c
+1 -1
@@ -17,7 +17,7 @@ int main(int ac, char **av)
17 if (!two)
18 die("not a tree-ish %s", av[2]);
19
20 - shift_tree(one->object.oid.hash, two->object.oid.hash, shifted.hash, -1);
20 + shift_tree(&one->object.oid, &two->object.oid, &shifted, -1);
21 printf("shifted: %s\n", oid_to_hex(&shifted));
22
23 exit(0);