global: introduce `USE_THE_REPOSITORY_VARIABLE` macro
Use of the `the_repository` variable is deprecated nowadays, and we
slowly but steadily convert the codebase to not use it anymore. Instead,
callers should be passing down the repository to work on via parameters.
It is hard though to prove that a given code unit does not use this
variable anymore. The most trivial case, merely demonstrating that there
is no direct use of `the_repository`, is already a bit of a pain during
code reviews as the reviewer needs to manually verify claims made by the
patch author. The bigger problem though is that we have many interfaces
that implicitly rely on `the_repository`.
Introduce a new `USE_THE_REPOSITORY_VARIABLE` macro that allows code
units to opt into usage of `the_repository`. The intent of this macro is
to demonstrate that a certain code unit does not use this variable
anymore, and to keep it from new dependencies on it in future changes,
be it explicit or implicit
For now, the macro only guards `the_repository` itself as well as
`the_hash_algo`. There are many more known interfaces where we have an
implicit dependency on `the_repository`, but those are not guarded at
the current point in time. Over time though, we should start to add
guards as required (or even better, just remove them).
Define the macro as required in our code units. As expected, most of our
code still relies on the global variable. Nearly all of our builtins
rely on the variable as there is no way yet to pass `the_repository` to
their entry point. For now, declare the macro in "biultin.h" to keep the
required changes at least a little bit more contained.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-06-14 14:50:23 +08:00
|
|
|
#define USE_THE_REPOSITORY_VARIABLE
|
|
|
|
|
2023-04-23 04:17:23 +08:00
|
|
|
#include "git-compat-util.h"
|
2023-02-24 08:09:27 +08:00
|
|
|
#include "hex.h"
|
2023-04-23 04:17:15 +08:00
|
|
|
#include "match-trees.h"
|
2023-05-16 14:34:06 +08:00
|
|
|
#include "strbuf.h"
|
2007-02-16 08:32:45 +08:00
|
|
|
#include "tree.h"
|
|
|
|
#include "tree-walk.h"
|
2023-05-16 14:34:06 +08:00
|
|
|
#include "object-store-ll.h"
|
2007-02-16 08:32:45 +08:00
|
|
|
|
2019-01-24 21:11:34 +08:00
|
|
|
static int score_missing(unsigned mode)
|
2007-02-16 08:32:45 +08:00
|
|
|
{
|
|
|
|
int score;
|
|
|
|
|
|
|
|
if (S_ISDIR(mode))
|
|
|
|
score = -1000;
|
|
|
|
else if (S_ISLNK(mode))
|
|
|
|
score = -500;
|
|
|
|
else
|
|
|
|
score = -50;
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:11:34 +08:00
|
|
|
static int score_differs(unsigned mode1, unsigned mode2)
|
2007-02-16 08:32:45 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:11:34 +08:00
|
|
|
static int score_matches(unsigned mode1, unsigned mode2)
|
2007-02-16 08:32:45 +08:00
|
|
|
{
|
|
|
|
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-14 02:19:28 +08:00
|
|
|
static void *fill_tree_desc_strict(struct tree_desc *desc,
|
2016-04-18 07:10:41 +08:00
|
|
|
const struct object_id *hash)
|
2013-06-14 02:19:28 +08:00
|
|
|
{
|
|
|
|
void *buffer;
|
|
|
|
enum object_type type;
|
|
|
|
unsigned long size;
|
|
|
|
|
2023-03-28 21:58:50 +08:00
|
|
|
buffer = repo_read_object_file(the_repository, hash, &type, &size);
|
2013-06-14 02:19:28 +08:00
|
|
|
if (!buffer)
|
2016-04-18 07:10:41 +08:00
|
|
|
die("unable to read tree (%s)", oid_to_hex(hash));
|
2013-06-14 02:19:28 +08:00
|
|
|
if (type != OBJ_TREE)
|
2016-04-18 07:10:41 +08:00
|
|
|
die("%s is not a tree", oid_to_hex(hash));
|
2023-10-02 10:40:28 +08:00
|
|
|
init_tree_desc(desc, hash, buffer, size);
|
2013-06-14 02:19:28 +08:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2013-03-25 06:46:28 +08: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 08:32:45 +08:00
|
|
|
/*
|
|
|
|
* Inspect two trees, and give a score that tells how similar they are.
|
|
|
|
*/
|
2016-04-18 07:10:41 +08:00
|
|
|
static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
|
2007-02-16 08:32:45 +08:00
|
|
|
{
|
|
|
|
struct tree_desc one;
|
|
|
|
struct tree_desc two;
|
2013-06-14 02:19:28 +08:00
|
|
|
void *one_buf = fill_tree_desc_strict(&one, hash1);
|
|
|
|
void *two_buf = fill_tree_desc_strict(&two, hash2);
|
2007-02-16 08:32:45 +08:00
|
|
|
int score = 0;
|
|
|
|
|
2013-03-25 06:46:28 +08:00
|
|
|
for (;;) {
|
2007-02-16 08:32:45 +08:00
|
|
|
int cmp;
|
|
|
|
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
if (one.size && two.size)
|
|
|
|
cmp = base_name_entries_compare(&one.entry, &two.entry);
|
|
|
|
else if (one.size)
|
2007-02-16 08:32:45 +08:00
|
|
|
/* two lacks this entry */
|
2013-03-25 06:46:28 +08:00
|
|
|
cmp = -1;
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
else if (two.size)
|
2013-03-25 06:46:28 +08:00
|
|
|
/* two has more entries */
|
|
|
|
cmp = 1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
if (cmp < 0) {
|
2007-02-16 08:32:45 +08:00
|
|
|
/* path1 does not appear in two */
|
2019-01-24 21:11:34 +08:00
|
|
|
score += score_missing(one.entry.mode);
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
update_tree_entry(&one);
|
|
|
|
} else if (cmp > 0) {
|
2007-02-16 08:32:45 +08:00
|
|
|
/* path2 does not appear in one */
|
2019-01-24 21:11:34 +08:00
|
|
|
score += score_missing(two.entry.mode);
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
update_tree_entry(&two);
|
|
|
|
} else {
|
|
|
|
/* path appears in both */
|
2019-01-15 08:39:44 +08:00
|
|
|
if (!oideq(&one.entry.oid, &two.entry.oid)) {
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
/* they are different */
|
|
|
|
score += score_differs(one.entry.mode,
|
2019-01-24 21:11:34 +08:00
|
|
|
two.entry.mode);
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
} else {
|
|
|
|
/* same subtree or blob */
|
|
|
|
score += score_matches(one.entry.mode,
|
2019-01-24 21:11:34 +08:00
|
|
|
two.entry.mode);
|
score_trees(): fix iteration over trees with missing entries
In score_trees(), we walk over two sorted trees to find
which entries are missing or have different content between
the two. So if we have two trees with these entries:
one two
--- ---
a a
b c
c d
we'd expect the loop to:
- compare "a" to "a"
- compare "b" to "c"; because these are sorted lists, we
know that the second tree does not have "b"
- compare "c" to "c"
- compare "d" to end-of-list; we know that the first tree
does not have "d"
And prior to d8febde370 (match-trees: simplify score_trees()
using tree_entry(), 2013-03-24) that worked. But after that
commit, we mistakenly increment the tree pointers for every
loop iteration, even when we've processed the entry for only
one side. As a result, we end up doing this:
- compare "a" to "a"
- compare "b" to "c"; we know that we do not have "b", but
we still increment both tree pointers; at this point
we're out of sync and all further comparisons are wrong
- compare "c" to "d" and mistakenly claim that the second
tree does not have "c"
- exit the loop, mistakenly not realizing that the first
tree does not have "d"
So contrary to the claim in d8febde370, we really do need to
manually use update_tree_entry(), because advancing the tree
pointer depends on the entry comparison.
That means we must stop using tree_entry() to access each
entry, since it auto-advances the pointer. Instead:
- we'll use tree_desc.size directly to know if there's
anything left to look at (which is what tree_entry() was
doing under the hood)
- rather than do an extra struct assignment to "e1" and
"e2", we can just access the "entry" field of tree_desc
directly
That makes us a little more intimate with the tree_desc
code, but that's not uncommon for its callers.
The included test shows off the bug by adding a new entry
"bar.t", which sorts early in the tree and de-syncs the
comparison for "foo.t", which comes after.
Reported-by: George Shammas <georgyo@gmail.com>
Helped-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-08-03 02:58:21 +08:00
|
|
|
}
|
|
|
|
update_tree_entry(&one);
|
|
|
|
update_tree_entry(&two);
|
|
|
|
}
|
2007-02-16 08:32:45 +08: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 07:10:41 +08:00
|
|
|
static void match_trees(const struct object_id *hash1,
|
|
|
|
const struct object_id *hash2,
|
2007-02-16 08:32:45 +08:00
|
|
|
int *best_score,
|
|
|
|
char **best_match,
|
2007-10-21 12:12:12 +08:00
|
|
|
const char *base,
|
2007-02-16 08:32:45 +08:00
|
|
|
int recurse_limit)
|
|
|
|
{
|
|
|
|
struct tree_desc one;
|
2013-06-14 02:19:28 +08:00
|
|
|
void *one_buf = fill_tree_desc_strict(&one, hash1);
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
while (one.size) {
|
|
|
|
const char *path;
|
2016-04-18 07:10:40 +08:00
|
|
|
const struct object_id *elem;
|
2019-04-05 23:00:12 +08:00
|
|
|
unsigned short mode;
|
2007-02-16 08:32:45 +08:00
|
|
|
int score;
|
|
|
|
|
|
|
|
elem = tree_entry_extract(&one, &path, &mode);
|
|
|
|
if (!S_ISDIR(mode))
|
|
|
|
goto next;
|
2016-04-18 07:10:41 +08:00
|
|
|
score = score_trees(elem, hash2);
|
2007-02-16 08:32:45 +08:00
|
|
|
if (*best_score < score) {
|
|
|
|
free(*best_match);
|
2014-06-20 05:24:33 +08:00
|
|
|
*best_match = xstrfmt("%s%s", base, path);
|
2007-02-16 08:32:45 +08:00
|
|
|
*best_score = score;
|
|
|
|
}
|
|
|
|
if (recurse_limit) {
|
2014-06-20 05:24:33 +08:00
|
|
|
char *newbase = xstrfmt("%s%s/", base, path);
|
2016-04-18 07:10:41 +08:00
|
|
|
match_trees(elem, hash2, best_score, best_match,
|
2007-02-16 08:32:45 +08:00
|
|
|
newbase, recurse_limit - 1);
|
|
|
|
free(newbase);
|
|
|
|
}
|
|
|
|
|
|
|
|
next:
|
|
|
|
update_tree_entry(&one);
|
|
|
|
}
|
|
|
|
free(one_buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-01-28 08:13:15 +08: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 08:32:45 +08:00
|
|
|
*/
|
2018-01-28 08:13:15 +08: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 08:32:45 +08:00
|
|
|
{
|
|
|
|
char *subpath;
|
|
|
|
int toplen;
|
|
|
|
char *buf;
|
|
|
|
unsigned long sz;
|
|
|
|
struct tree_desc desc;
|
2019-01-15 08:39:43 +08:00
|
|
|
unsigned char *rewrite_here;
|
2018-01-28 08:13:15 +08:00
|
|
|
const struct object_id *rewrite_with;
|
|
|
|
struct object_id subtree;
|
2007-02-16 08:32:45 +08:00
|
|
|
enum object_type type;
|
|
|
|
int status;
|
|
|
|
|
2014-03-08 14:48:31 +08:00
|
|
|
subpath = strchrnul(prefix, '/');
|
|
|
|
toplen = subpath - prefix;
|
|
|
|
if (*subpath)
|
2007-02-16 08:32:45 +08:00
|
|
|
subpath++;
|
|
|
|
|
2023-03-28 21:58:50 +08:00
|
|
|
buf = repo_read_object_file(the_repository, oid1, &type, &sz);
|
2007-02-16 08:32:45 +08:00
|
|
|
if (!buf)
|
2018-01-28 08:13:15 +08:00
|
|
|
die("cannot read tree %s", oid_to_hex(oid1));
|
2023-10-02 10:40:28 +08:00
|
|
|
init_tree_desc(&desc, oid1, buf, sz);
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
rewrite_here = NULL;
|
|
|
|
while (desc.size) {
|
|
|
|
const char *name;
|
2019-04-05 23:00:12 +08:00
|
|
|
unsigned short mode;
|
2007-02-16 08:32:45 +08:00
|
|
|
|
2019-01-15 08:39:42 +08:00
|
|
|
tree_entry_extract(&desc, &name, &mode);
|
2007-02-16 08:32:45 +08:00
|
|
|
if (strlen(name) == toplen &&
|
|
|
|
!memcmp(name, prefix, toplen)) {
|
|
|
|
if (!S_ISDIR(mode))
|
2018-01-28 08:13:15 +08:00
|
|
|
die("entry %s in tree %s is not a tree", name,
|
|
|
|
oid_to_hex(oid1));
|
2019-01-15 08:39:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We cast here for two reasons:
|
|
|
|
*
|
|
|
|
* - to flip the "char *" (for the path) to "unsigned
|
|
|
|
* char *" (for the hash stored after it)
|
|
|
|
*
|
|
|
|
* - to discard the "const"; this is OK because we
|
|
|
|
* know it points into our non-const "buf"
|
|
|
|
*/
|
|
|
|
rewrite_here = (unsigned char *)(desc.entry.path +
|
|
|
|
strlen(desc.entry.path) +
|
|
|
|
1);
|
2007-02-16 08:32:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
update_tree_entry(&desc);
|
|
|
|
}
|
|
|
|
if (!rewrite_here)
|
2018-01-28 08:13:15 +08:00
|
|
|
die("entry %.*s not found in tree %s", toplen, prefix,
|
|
|
|
oid_to_hex(oid1));
|
2014-03-08 14:48:31 +08:00
|
|
|
if (*subpath) {
|
2019-01-15 08:39:43 +08:00
|
|
|
struct object_id tree_oid;
|
2024-06-14 14:49:54 +08:00
|
|
|
oidread(&tree_oid, rewrite_here, the_repository->hash_algo);
|
2019-01-15 08:39:43 +08:00
|
|
|
status = splice_tree(&tree_oid, subpath, oid2, &subtree);
|
2007-02-16 08:32:45 +08:00
|
|
|
if (status)
|
|
|
|
return status;
|
2018-01-28 08:13:15 +08:00
|
|
|
rewrite_with = &subtree;
|
|
|
|
} else {
|
|
|
|
rewrite_with = oid2;
|
2007-02-16 08:32:45 +08:00
|
|
|
}
|
2024-06-14 14:49:50 +08:00
|
|
|
hashcpy(rewrite_here, rewrite_with->hash, the_repository->hash_algo);
|
2022-02-05 07:48:26 +08:00
|
|
|
status = write_object_file(buf, sz, OBJ_TREE, result);
|
2007-02-16 08:32:45 +08: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.
|
|
|
|
*/
|
2019-06-27 17:28:51 +08:00
|
|
|
void shift_tree(struct repository *r,
|
|
|
|
const struct object_id *hash1,
|
2016-04-18 07:10:38 +08:00
|
|
|
const struct object_id *hash2,
|
|
|
|
struct object_id *shifted,
|
2007-02-16 08:32:45 +08:00
|
|
|
int depth_limit)
|
|
|
|
{
|
|
|
|
char *add_prefix;
|
|
|
|
char *del_prefix;
|
|
|
|
int add_score, del_score;
|
|
|
|
|
2008-07-01 13:18:57 +08:00
|
|
|
/*
|
|
|
|
* NEEDSWORK: this limits the recursion depth to hardcoded
|
|
|
|
* value '2' to avoid excessive overhead.
|
|
|
|
*/
|
|
|
|
if (!depth_limit)
|
|
|
|
depth_limit = 2;
|
|
|
|
|
2016-04-18 07:10:41 +08:00
|
|
|
add_score = del_score = score_trees(hash1, hash2);
|
2007-02-16 08:32:45 +08: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 07:10:41 +08:00
|
|
|
match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* See if two's subtree resembles one; if so we need to
|
|
|
|
* pick only subtree of two.
|
|
|
|
*/
|
2016-04-18 07:10:41 +08:00
|
|
|
match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
/* Assume we do not have to do any shifting */
|
2016-04-18 07:10:38 +08:00
|
|
|
oidcpy(shifted, hash2);
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
if (add_score < del_score) {
|
|
|
|
/* We need to pick a subtree of two */
|
2019-04-05 23:00:12 +08:00
|
|
|
unsigned short mode;
|
2007-02-16 08:32:45 +08:00
|
|
|
|
|
|
|
if (!*del_prefix)
|
2024-09-05 18:09:36 +08:00
|
|
|
goto out;
|
2007-02-16 08:32:45 +08:00
|
|
|
|
2019-06-27 17:28:51 +08:00
|
|
|
if (get_tree_entry(r, hash2, del_prefix, shifted, &mode))
|
2007-02-16 08:32:45 +08:00
|
|
|
die("cannot find path %s in tree %s",
|
2016-04-18 07:10:38 +08:00
|
|
|
del_prefix, oid_to_hex(hash2));
|
2024-09-05 18:09:36 +08:00
|
|
|
goto out;
|
2007-02-16 08:32:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*add_prefix)
|
2024-09-05 18:09:36 +08:00
|
|
|
goto out;
|
2007-02-16 08:32:45 +08:00
|
|
|
|
2018-01-28 08:13:15 +08:00
|
|
|
splice_tree(hash1, add_prefix, hash2, shifted);
|
2024-09-05 18:09:36 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
free(add_prefix);
|
|
|
|
free(del_prefix);
|
2007-02-16 08:32:45 +08:00
|
|
|
}
|
2008-07-01 13:18:57 +08: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.
|
|
|
|
*/
|
2019-06-27 17:28:51 +08:00
|
|
|
void shift_tree_by(struct repository *r,
|
|
|
|
const struct object_id *hash1,
|
2016-04-18 07:10:38 +08:00
|
|
|
const struct object_id *hash2,
|
|
|
|
struct object_id *shifted,
|
2008-07-01 13:18:57 +08:00
|
|
|
const char *shift_prefix)
|
|
|
|
{
|
2016-04-18 07:10:38 +08:00
|
|
|
struct object_id sub1, sub2;
|
2019-04-05 23:00:12 +08:00
|
|
|
unsigned short mode1, mode2;
|
2008-07-01 13:18:57 +08:00
|
|
|
unsigned candidate = 0;
|
|
|
|
|
|
|
|
/* Can hash2 be a tree at shift_prefix in tree hash1? */
|
2019-06-27 17:28:51 +08:00
|
|
|
if (!get_tree_entry(r, hash1, shift_prefix, &sub1, &mode1) &&
|
2008-07-01 13:18:57 +08:00
|
|
|
S_ISDIR(mode1))
|
|
|
|
candidate |= 1;
|
|
|
|
|
|
|
|
/* Can hash1 be a tree at shift_prefix in tree hash2? */
|
2019-06-27 17:28:51 +08:00
|
|
|
if (!get_tree_entry(r, hash2, shift_prefix, &sub2, &mode2) &&
|
2008-07-01 13:18:57 +08:00
|
|
|
S_ISDIR(mode2))
|
|
|
|
candidate |= 2;
|
|
|
|
|
|
|
|
if (candidate == 3) {
|
|
|
|
/* Both are plausible -- we need to evaluate the score */
|
2016-04-18 07:10:41 +08:00
|
|
|
int best_score = score_trees(hash1, hash2);
|
2008-07-01 13:18:57 +08:00
|
|
|
int score;
|
|
|
|
|
|
|
|
candidate = 0;
|
2016-04-18 07:10:41 +08:00
|
|
|
score = score_trees(&sub1, hash2);
|
2008-07-01 13:18:57 +08:00
|
|
|
if (score > best_score) {
|
|
|
|
candidate = 1;
|
|
|
|
best_score = score;
|
|
|
|
}
|
2016-04-18 07:10:41 +08:00
|
|
|
score = score_trees(&sub2, hash1);
|
2008-07-01 13:18:57 +08:00
|
|
|
if (score > best_score)
|
|
|
|
candidate = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!candidate) {
|
|
|
|
/* Neither is plausible -- do not shift */
|
2016-04-18 07:10:38 +08:00
|
|
|
oidcpy(shifted, hash2);
|
2008-07-01 13:18:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (candidate == 1)
|
|
|
|
/*
|
|
|
|
* shift tree2 down by adding shift_prefix above it
|
|
|
|
* to match tree1.
|
|
|
|
*/
|
2018-01-28 08:13:15 +08:00
|
|
|
splice_tree(hash1, shift_prefix, hash2, shifted);
|
2008-07-01 13:18:57 +08:00
|
|
|
else
|
|
|
|
/*
|
|
|
|
* shift tree2 up by removing shift_prefix from it
|
|
|
|
* to match tree1.
|
|
|
|
*/
|
2016-04-18 07:10:38 +08:00
|
|
|
oidcpy(shifted, &sub2);
|
2008-07-01 13:18:57 +08:00
|
|
|
}
|