2007-02-16 01:32:45 +01:00
|
|
|
#include "cache.h"
|
|
|
|
#include "tree.h"
|
|
|
|
#include "tree-walk.h"
|
2018-05-16 01:42:15 +02:00
|
|
|
#include "object-store.h"
|
2007-02-16 01:32:45 +01:00
|
|
|
|
|
|
|
static int score_missing(unsigned mode, const char *path)
|
|
|
|
{
|
|
|
|
int score;
|
|
|
|
|
|
|
|
if (S_ISDIR(mode))
|
|
|
|
score = -1000;
|
|
|
|
else if (S_ISLNK(mode))
|
|
|
|
score = -500;
|
|
|
|
else
|
|
|
|
score = -50;
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int score_differs(unsigned mode1, unsigned mode2, const char *path)
|
|
|
|
{
|
|
|
|
int score;
|
|
|
|
|
|
|
|
if (S_ISDIR(mode1) != S_ISDIR(mode2))
|
|
|
|
score = -100;
|
|
|
|
else if (S_ISLNK(mode1) != S_ISLNK(mode2))
|
|
|
|
score = -50;
|
|
|
|
else
|
|
|
|
score = -5;
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int score_matches(unsigned mode1, unsigned mode2, const char *path)
|
|
|
|
{
|
|
|
|
int score;
|
|
|
|
|
|
|
|
/* Heh, we found SHA-1 collisions between different kind of objects */
|
|
|
|
if (S_ISDIR(mode1) != S_ISDIR(mode2))
|
|
|
|
score = -100;
|
|
|
|
else if (S_ISLNK(mode1) != S_ISLNK(mode2))
|
|
|
|
score = -50;
|
|
|
|
|
|
|
|
else if (S_ISDIR(mode1))
|
|
|
|
score = 1000;
|
|
|
|
else if (S_ISLNK(mode1))
|
|
|
|
score = 500;
|
|
|
|
else
|
|
|
|
score = 250;
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2013-06-13 20:19:28 +02:00
|
|
|
static void *fill_tree_desc_strict(struct tree_desc *desc,
|
2016-04-18 01:10:41 +02:00
|
|
|
const struct object_id *hash)
|
2013-06-13 20:19:28 +02:00
|
|
|
{
|
|
|
|
void *buffer;
|
|
|
|
enum object_type type;
|
|
|
|
unsigned long size;
|
|
|
|
|
sha1_file: convert read_sha1_file to struct object_id
Convert read_sha1_file to take a pointer to struct object_id and rename
it read_object_file. Do the same for read_sha1_file_extended.
Convert one use in grep.c to use the new function without any other code
change, since the pointer being passed is a void pointer that is already
initialized with a pointer to struct object_id. Update the declaration
and definitions of the modified functions, and apply the following
semantic patch to convert the remaining callers:
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1.hash, E2, E3)
+ read_object_file(&E1, E2, E3)
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1->hash, E2, E3)
+ read_object_file(E1, E2, E3)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1.hash, E2, E3, E4)
+ read_object_file_extended(&E1, E2, E3, E4)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1->hash, E2, E3, E4)
+ read_object_file_extended(E1, E2, E3, E4)
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-03-12 03:27:53 +01:00
|
|
|
buffer = read_object_file(hash, &type, &size);
|
2013-06-13 20:19:28 +02:00
|
|
|
if (!buffer)
|
2016-04-18 01:10:41 +02:00
|
|
|
die("unable to read tree (%s)", oid_to_hex(hash));
|
2013-06-13 20:19:28 +02:00
|
|
|
if (type != OBJ_TREE)
|
2016-04-18 01:10:41 +02:00
|
|
|
die("%s is not a tree", oid_to_hex(hash));
|
2013-06-13 20:19:28 +02:00
|
|
|
init_tree_desc(desc, buffer, size);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2013-03-24 23:46:28 +01:00
|
|
|
static int base_name_entries_compare(const struct name_entry *a,
|
|
|
|
const struct name_entry *b)
|
|
|
|
{
|
|
|
|
return base_name_compare(a->path, tree_entry_len(a), a->mode,
|
|
|
|
b->path, tree_entry_len(b), b->mode);
|
|
|
|
}
|
|
|
|
|
2007-02-16 01:32:45 +01:00
|
|
|
/*
|
|
|
|
* Inspect two trees, and give a score that tells how similar they are.
|
|
|
|
*/
|
2016-04-18 01:10:41 +02:00
|
|
|
static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
|
2007-02-16 01:32:45 +01:00
|
|
|
{
|
|
|
|
struct tree_desc one;
|
|
|
|
struct tree_desc two;
|
2013-06-13 20:19:28 +02:00
|
|
|
void *one_buf = fill_tree_desc_strict(&one, hash1);
|
|
|
|
void *two_buf = fill_tree_desc_strict(&two, hash2);
|
2007-02-16 01:32:45 +01:00
|
|
|
int score = 0;
|
|
|
|
|
2013-03-24 23:46:28 +01:00
|
|
|
for (;;) {
|
|
|
|
struct name_entry e1, e2;
|
|
|
|
int got_entry_from_one = tree_entry(&one, &e1);
|
|
|
|
int got_entry_from_two = tree_entry(&two, &e2);
|
2007-02-16 01:32:45 +01:00
|
|
|
int cmp;
|
|
|
|
|
2013-03-24 23:46:28 +01:00
|
|
|
if (got_entry_from_one && got_entry_from_two)
|
|
|
|
cmp = base_name_entries_compare(&e1, &e2);
|
|
|
|
else if (got_entry_from_one)
|
2007-02-16 01:32:45 +01:00
|
|
|
/* two lacks this entry */
|
2013-03-24 23:46:28 +01:00
|
|
|
cmp = -1;
|
|
|
|
else if (got_entry_from_two)
|
|
|
|
/* two has more entries */
|
|
|
|
cmp = 1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (cmp < 0)
|
2007-02-16 01:32:45 +01:00
|
|
|
/* path1 does not appear in two */
|
2013-03-24 23:46:28 +01:00
|
|
|
score += score_missing(e1.mode, e1.path);
|
|
|
|
else if (cmp > 0)
|
2007-02-16 01:32:45 +01:00
|
|
|
/* path2 does not appear in one */
|
2013-03-24 23:46:28 +01:00
|
|
|
score += score_missing(e2.mode, e2.path);
|
2016-04-18 01:10:39 +02:00
|
|
|
else if (oidcmp(e1.oid, e2.oid))
|
2007-02-16 01:32:45 +01:00
|
|
|
/* they are different */
|
2013-03-24 23:46:28 +01:00
|
|
|
score += score_differs(e1.mode, e2.mode, e1.path);
|
2007-02-16 01:32:45 +01:00
|
|
|
else
|
|
|
|
/* same subtree or blob */
|
2013-03-24 23:46:28 +01:00
|
|
|
score += score_matches(e1.mode, e2.mode, e1.path);
|
2007-02-16 01:32:45 +01:00
|
|
|
}
|
|
|
|
free(one_buf);
|
|
|
|
free(two_buf);
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Match one itself and its subtrees with two and pick the best match.
|
|
|
|
*/
|
2016-04-18 01:10:41 +02:00
|
|
|
static void match_trees(const struct object_id *hash1,
|
|
|
|
const struct object_id *hash2,
|
2007-02-16 01:32:45 +01:00
|
|
|
int *best_score,
|
|
|
|
char **best_match,
|
2007-10-21 06:12:12 +02:00
|
|
|
const char *base,
|
2007-02-16 01:32:45 +01:00
|
|
|
int recurse_limit)
|
|
|
|
{
|
|
|
|
struct tree_desc one;
|
2013-06-13 20:19:28 +02:00
|
|
|
void *one_buf = fill_tree_desc_strict(&one, hash1);
|
2007-02-16 01:32:45 +01:00
|
|
|
|
|
|
|
while (one.size) {
|
|
|
|
const char *path;
|
2016-04-18 01:10:40 +02:00
|
|
|
const struct object_id *elem;
|
2007-02-16 01:32:45 +01:00
|
|
|
unsigned mode;
|
|
|
|
int score;
|
|
|
|
|
|
|
|
elem = tree_entry_extract(&one, &path, &mode);
|
|
|
|
if (!S_ISDIR(mode))
|
|
|
|
goto next;
|
2016-04-18 01:10:41 +02:00
|
|
|
score = score_trees(elem, hash2);
|
2007-02-16 01:32:45 +01:00
|
|
|
if (*best_score < score) {
|
|
|
|
free(*best_match);
|
2014-06-19 23:24:33 +02:00
|
|
|
*best_match = xstrfmt("%s%s", base, path);
|
2007-02-16 01:32:45 +01:00
|
|
|
*best_score = score;
|
|
|
|
}
|
|
|
|
if (recurse_limit) {
|
2014-06-19 23:24:33 +02:00
|
|
|
char *newbase = xstrfmt("%s%s/", base, path);
|
2016-04-18 01:10:41 +02:00
|
|
|
match_trees(elem, hash2, best_score, best_match,
|
2007-02-16 01:32:45 +01:00
|
|
|
newbase, recurse_limit - 1);
|
|
|
|
free(newbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
update_tree_entry(&one);
|
|
|
|
}
|
|
|
|
free(one_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-01-28 01:13:15 +01:00
|
|
|
* A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
|
|
|
|
* replacing it with another tree "oid2".
|
2007-02-16 01:32:45 +01:00
|
|
|
*/
|
2018-01-28 01:13:15 +01:00
|
|
|
static int splice_tree(const struct object_id *oid1, const char *prefix,
|
|
|
|
const struct object_id *oid2, struct object_id *result)
|
2007-02-16 01:32:45 +01:00
|
|
|
{
|
|
|
|
char *subpath;
|
|
|
|
int toplen;
|
|
|
|
char *buf;
|
|
|
|
unsigned long sz;
|
|
|
|
struct tree_desc desc;
|
2018-01-28 01:13:15 +01:00
|
|
|
struct object_id *rewrite_here;
|
|
|
|
const struct object_id *rewrite_with;
|
|
|
|
struct object_id subtree;
|
2007-02-16 01:32:45 +01:00
|
|
|
enum object_type type;
|
|
|
|
int status;
|
|
|
|
|
2014-03-08 07:48:31 +01:00
|
|
|
subpath = strchrnul(prefix, '/');
|
|
|
|
toplen = subpath - prefix;
|
|
|
|
if (*subpath)
|
2007-02-16 01:32:45 +01:00
|
|
|
subpath++;
|
|
|
|
|
sha1_file: convert read_sha1_file to struct object_id
Convert read_sha1_file to take a pointer to struct object_id and rename
it read_object_file. Do the same for read_sha1_file_extended.
Convert one use in grep.c to use the new function without any other code
change, since the pointer being passed is a void pointer that is already
initialized with a pointer to struct object_id. Update the declaration
and definitions of the modified functions, and apply the following
semantic patch to convert the remaining callers:
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1.hash, E2, E3)
+ read_object_file(&E1, E2, E3)
@@
expression E1, E2, E3;
@@
- read_sha1_file(E1->hash, E2, E3)
+ read_object_file(E1, E2, E3)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1.hash, E2, E3, E4)
+ read_object_file_extended(&E1, E2, E3, E4)
@@
expression E1, E2, E3, E4;
@@
- read_sha1_file_extended(E1->hash, E2, E3, E4)
+ read_object_file_extended(E1, E2, E3, E4)
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-03-12 03:27:53 +01:00
|
|
|
buf = read_object_file(oid1, &type, &sz);
|
2007-02-16 01:32:45 +01:00
|
|
|
if (!buf)
|
2018-01-28 01:13:15 +01:00
|
|
|
die("cannot read tree %s", oid_to_hex(oid1));
|
2007-02-16 01:32:45 +01:00
|
|
|
init_tree_desc(&desc, buf, sz);
|
|
|
|
|
|
|
|
rewrite_here = NULL;
|
|
|
|
while (desc.size) {
|
|
|
|
const char *name;
|
|
|
|
unsigned mode;
|
2016-04-18 01:10:40 +02:00
|
|
|
const struct object_id *oid;
|
2007-02-16 01:32:45 +01:00
|
|
|
|
2016-04-18 01:10:40 +02:00
|
|
|
oid = tree_entry_extract(&desc, &name, &mode);
|
2007-02-16 01:32:45 +01:00
|
|
|
if (strlen(name) == toplen &&
|
|
|
|
!memcmp(name, prefix, toplen)) {
|
|
|
|
if (!S_ISDIR(mode))
|
2018-01-28 01:13:15 +01:00
|
|
|
die("entry %s in tree %s is not a tree", name,
|
|
|
|
oid_to_hex(oid1));
|
|
|
|
rewrite_here = (struct object_id *)oid;
|
2007-02-16 01:32:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
update_tree_entry(&desc);
|
|
|
|
}
|
|
|
|
if (!rewrite_here)
|
2018-01-28 01:13:15 +01:00
|
|
|
die("entry %.*s not found in tree %s", toplen, prefix,
|
|
|
|
oid_to_hex(oid1));
|
2014-03-08 07:48:31 +01:00
|
|
|
if (*subpath) {
|
2018-01-28 01:13:15 +01:00
|
|
|
status = splice_tree(rewrite_here, subpath, oid2, &subtree);
|
2007-02-16 01:32:45 +01:00
|
|
|
if (status)
|
|
|
|
return status;
|
2018-01-28 01:13:15 +01:00
|
|
|
rewrite_with = &subtree;
|
|
|
|
} else {
|
|
|
|
rewrite_with = oid2;
|
2007-02-16 01:32:45 +01:00
|
|
|
}
|
2018-01-28 01:13:15 +01:00
|
|
|
oidcpy(rewrite_here, rewrite_with);
|
2018-01-28 01:13:19 +01:00
|
|
|
status = write_object_file(buf, sz, tree_type, result);
|
2007-02-16 01:32:45 +01:00
|
|
|
free(buf);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-04-18 01:10:38 +02:00
|
|
|
void shift_tree(const struct object_id *hash1,
|
|
|
|
const struct object_id *hash2,
|
|
|
|
struct object_id *shifted,
|
2007-02-16 01:32:45 +01:00
|
|
|
int depth_limit)
|
|
|
|
{
|
|
|
|
char *add_prefix;
|
|
|
|
char *del_prefix;
|
|
|
|
int add_score, del_score;
|
|
|
|
|
2008-07-01 07:18:57 +02:00
|
|
|
/*
|
|
|
|
* NEEDSWORK: this limits the recursion depth to hardcoded
|
|
|
|
* value '2' to avoid excessive overhead.
|
|
|
|
*/
|
|
|
|
if (!depth_limit)
|
|
|
|
depth_limit = 2;
|
|
|
|
|
2016-04-18 01:10:41 +02:00
|
|
|
add_score = del_score = score_trees(hash1, hash2);
|
2007-02-16 01:32:45 +01:00
|
|
|
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.
|
|
|
|
*/
|
2016-04-18 01:10:41 +02:00
|
|
|
match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
|
2007-02-16 01:32:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See if two's subtree resembles one; if so we need to
|
|
|
|
* pick only subtree of two.
|
|
|
|
*/
|
2016-04-18 01:10:41 +02:00
|
|
|
match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
|
2007-02-16 01:32:45 +01:00
|
|
|
|
|
|
|
/* Assume we do not have to do any shifting */
|
2016-04-18 01:10:38 +02:00
|
|
|
oidcpy(shifted, hash2);
|
2007-02-16 01:32:45 +01:00
|
|
|
|
|
|
|
if (add_score < del_score) {
|
|
|
|
/* We need to pick a subtree of two */
|
|
|
|
unsigned mode;
|
|
|
|
|
|
|
|
if (!*del_prefix)
|
|
|
|
return;
|
|
|
|
|
2018-03-12 03:27:51 +01:00
|
|
|
if (get_tree_entry(hash2, del_prefix, shifted, &mode))
|
2007-02-16 01:32:45 +01:00
|
|
|
die("cannot find path %s in tree %s",
|
2016-04-18 01:10:38 +02:00
|
|
|
del_prefix, oid_to_hex(hash2));
|
2007-02-16 01:32:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*add_prefix)
|
|
|
|
return;
|
|
|
|
|
2018-01-28 01:13:15 +01:00
|
|
|
splice_tree(hash1, add_prefix, hash2, shifted);
|
2007-02-16 01:32:45 +01:00
|
|
|
}
|
2008-07-01 07:18:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-04-18 01:10:38 +02:00
|
|
|
void shift_tree_by(const struct object_id *hash1,
|
|
|
|
const struct object_id *hash2,
|
|
|
|
struct object_id *shifted,
|
2008-07-01 07:18:57 +02:00
|
|
|
const char *shift_prefix)
|
|
|
|
{
|
2016-04-18 01:10:38 +02:00
|
|
|
struct object_id sub1, sub2;
|
2008-07-01 07:18:57 +02:00
|
|
|
unsigned mode1, mode2;
|
|
|
|
unsigned candidate = 0;
|
|
|
|
|
|
|
|
/* Can hash2 be a tree at shift_prefix in tree hash1? */
|
2018-03-12 03:27:51 +01:00
|
|
|
if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
|
2008-07-01 07:18:57 +02:00
|
|
|
S_ISDIR(mode1))
|
|
|
|
candidate |= 1;
|
|
|
|
|
|
|
|
/* Can hash1 be a tree at shift_prefix in tree hash2? */
|
2018-03-12 03:27:51 +01:00
|
|
|
if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
|
2008-07-01 07:18:57 +02:00
|
|
|
S_ISDIR(mode2))
|
|
|
|
candidate |= 2;
|
|
|
|
|
|
|
|
if (candidate == 3) {
|
|
|
|
/* Both are plausible -- we need to evaluate the score */
|
2016-04-18 01:10:41 +02:00
|
|
|
int best_score = score_trees(hash1, hash2);
|
2008-07-01 07:18:57 +02:00
|
|
|
int score;
|
|
|
|
|
|
|
|
candidate = 0;
|
2016-04-18 01:10:41 +02:00
|
|
|
score = score_trees(&sub1, hash2);
|
2008-07-01 07:18:57 +02:00
|
|
|
if (score > best_score) {
|
|
|
|
candidate = 1;
|
|
|
|
best_score = score;
|
|
|
|
}
|
2016-04-18 01:10:41 +02:00
|
|
|
score = score_trees(&sub2, hash1);
|
2008-07-01 07:18:57 +02:00
|
|
|
if (score > best_score)
|
|
|
|
candidate = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!candidate) {
|
|
|
|
/* Neither is plausible -- do not shift */
|
2016-04-18 01:10:38 +02:00
|
|
|
oidcpy(shifted, hash2);
|
2008-07-01 07:18:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (candidate == 1)
|
|
|
|
/*
|
|
|
|
* shift tree2 down by adding shift_prefix above it
|
|
|
|
* to match tree1.
|
|
|
|
*/
|
2018-01-28 01:13:15 +01:00
|
|
|
splice_tree(hash1, shift_prefix, hash2, shifted);
|
2008-07-01 07:18:57 +02:00
|
|
|
else
|
|
|
|
/*
|
|
|
|
* shift tree2 up by removing shift_prefix from it
|
|
|
|
* to match tree1.
|
|
|
|
*/
|
2016-04-18 01:10:38 +02:00
|
|
|
oidcpy(shifted, &sub2);
|
2008-07-01 07:18:57 +02:00
|
|
|
}
|