/* * The user says the trees will be shifted by this much. * Unfortunately we cannot fundamentally tell which one to * be prefixed, as recursive merge can work in either direction. */ void shift_tree_by(const unsigned char *hash1, const unsigned char *hash2, unsigned char *shifted, const char *shift_prefix) { unsigned char sub1[20], sub2[20]; unsigned mode1, mode2; unsigned candidate = 0; /* Can hash2 be a tree at shift_prefix in tree hash1? */ if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) && S_ISDIR(mode1)) candidate |= 1; /* Can hash1 be a tree at shift_prefix in tree hash2? */ if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) && S_ISDIR(mode2)) candidate |= 2; if (candidate == 3) { /* Both are plausible -- we need to evaluate the score */ int best_score = score_trees(hash1, hash2); int score; candidate = 0; score = score_trees(sub1, hash2); if (score > best_score) { candidate = 1; best_score = score; } score = score_trees(sub2, hash1); if (score > best_score) candidate = 2; } if (!candidate) { /* Neither is plausible -- do not shift */ hashcpy(shifted, hash2); return; } if (candidate == 1) /* * shift tree2 down by adding shift_prefix above it * to match tree1. */ splice_tree(hash1, shift_prefix, hash2, shifted); else /* * shift tree2 up by removing shift_prefix from it * to match tree1. */ hashcpy(shifted, sub2); }
/* * We are trying to come up with a merge between one and two that * results in a tree shape similar to one. The tree two might * correspond to a subtree of one, in which case it needs to be * shifted down by prefixing otherwise empty directories. On the * other hand, it could cover tree one and we might need to pick a * subtree of it. */ void shift_tree(const unsigned char *hash1, const unsigned char *hash2, unsigned char *shifted, int depth_limit) { char *add_prefix; char *del_prefix; int add_score, del_score; /* * NEEDSWORK: this limits the recursion depth to hardcoded * value '2' to avoid excessive overhead. */ if (!depth_limit) depth_limit = 2; add_score = del_score = score_trees(hash1, hash2); add_prefix = xcalloc(1, 1); del_prefix = xcalloc(1, 1); /* * See if one's subtree resembles two; if so we need to prefix * two with a few fake trees to match the prefix. */ match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit); /* * See if two's subtree resembles one; if so we need to * pick only subtree of two. */ match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit); /* Assume we do not have to do any shifting */ hashcpy(shifted, hash2); if (add_score < del_score) { /* We need to pick a subtree of two */ unsigned mode; if (!*del_prefix) return; if (get_tree_entry(hash2, del_prefix, shifted, &mode)) die("cannot find path %s in tree %s", del_prefix, sha1_to_hex(hash2)); return; } if (!*add_prefix) return; splice_tree(hash1, add_prefix, hash2, shifted); }
/* * Match one itself and its subtrees with two and pick the best match. */ static void match_trees(const unsigned char *hash1, const unsigned char *hash2, int *best_score, char **best_match, const char *base, 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); while (one.size) { const char *path; const unsigned char *elem; unsigned mode; int score; elem = tree_entry_extract(&one, &path, &mode); if (!S_ISDIR(mode)) goto next; score = score_trees(elem, hash2); if (*best_score < score) { char *newpath; newpath = xmalloc(strlen(base) + strlen(path) + 1); sprintf(newpath, "%s%s", base, path); free(*best_match); *best_match = newpath; *best_score = score; } if (recurse_limit) { char *newbase; newbase = xmalloc(strlen(base) + strlen(path) + 2); sprintf(newbase, "%s%s/", base, path); match_trees(elem, hash2, best_score, best_match, newbase, recurse_limit - 1); free(newbase); } next: update_tree_entry(&one); } free(one_buf); }
/* * Match one itself and its subtrees with two and pick the best match. */ static void match_trees(const unsigned char *hash1, const unsigned char *hash2, int *best_score, char **best_match, const char *base, int recurse_limit) { struct tree_desc one; void *one_buf = fill_tree_desc_strict(&one, hash1); while (one.size) { const char *path; const unsigned char *elem; unsigned mode; int score; elem = tree_entry_extract(&one, &path, &mode); if (!S_ISDIR(mode)) goto next; score = score_trees(elem, hash2); if (*best_score < score) { char *newpath; newpath = xmalloc(strlen(base) + strlen(path) + 1); sprintf(newpath, "%s%s", base, path); free(*best_match); *best_match = newpath; *best_score = score; } if (recurse_limit) { char *newbase; newbase = xmalloc(strlen(base) + strlen(path) + 2); sprintf(newbase, "%s%s/", base, path); match_trees(elem, hash2, best_score, best_match, newbase, recurse_limit - 1); free(newbase); } next: update_tree_entry(&one); } free(one_buf); }