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-03-21 14:25:56 +08:00
|
|
|
#include "git-compat-util.h"
|
2018-07-17 02:44:01 +08:00
|
|
|
#include "skipping.h"
|
|
|
|
#include "../commit.h"
|
|
|
|
#include "../fetch-negotiator.h"
|
2023-02-24 08:09:27 +08:00
|
|
|
#include "../hex.h"
|
2018-07-17 02:44:01 +08:00
|
|
|
#include "../prio-queue.h"
|
|
|
|
#include "../refs.h"
|
2023-04-23 04:17:20 +08:00
|
|
|
#include "../repository.h"
|
2018-07-17 02:44:01 +08:00
|
|
|
#include "../tag.h"
|
|
|
|
|
|
|
|
/* Remember to update object flag allocation in object.h */
|
|
|
|
/*
|
|
|
|
* Both us and the server know that both parties have this object.
|
|
|
|
*/
|
|
|
|
#define COMMON (1U << 2)
|
|
|
|
/*
|
|
|
|
* The server has told us that it has this object. We still need to tell the
|
|
|
|
* server that we have this object (or one of its descendants), but since we are
|
|
|
|
* going to do that, we do not need to tell the server about its ancestors.
|
|
|
|
*/
|
|
|
|
#define ADVERTISED (1U << 3)
|
|
|
|
/*
|
|
|
|
* This commit has entered the priority queue.
|
|
|
|
*/
|
|
|
|
#define SEEN (1U << 4)
|
|
|
|
/*
|
|
|
|
* This commit has left the priority queue.
|
|
|
|
*/
|
|
|
|
#define POPPED (1U << 5)
|
|
|
|
|
|
|
|
static int marked;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An entry in the priority queue.
|
|
|
|
*/
|
|
|
|
struct entry {
|
|
|
|
struct commit *commit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used only if commit is not COMMON.
|
|
|
|
*/
|
|
|
|
uint16_t original_ttl;
|
|
|
|
uint16_t ttl;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct data {
|
|
|
|
struct prio_queue rev_list;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The number of non-COMMON commits in rev_list.
|
|
|
|
*/
|
|
|
|
int non_common_revs;
|
|
|
|
};
|
|
|
|
|
2023-02-24 14:39:27 +08:00
|
|
|
static int compare(const void *a_, const void *b_, void *data UNUSED)
|
2018-07-17 02:44:01 +08:00
|
|
|
{
|
|
|
|
const struct entry *a = a_;
|
|
|
|
const struct entry *b = b_;
|
|
|
|
return compare_commits_by_commit_date(a->commit, b->commit, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct entry *rev_list_push(struct data *data, struct commit *commit, int mark)
|
|
|
|
{
|
|
|
|
struct entry *entry;
|
|
|
|
commit->object.flags |= mark | SEEN;
|
|
|
|
|
2021-03-14 00:17:22 +08:00
|
|
|
CALLOC_ARRAY(entry, 1);
|
2018-07-17 02:44:01 +08:00
|
|
|
entry->commit = commit;
|
|
|
|
prio_queue_put(&data->rev_list, entry);
|
|
|
|
|
|
|
|
if (!(mark & COMMON))
|
|
|
|
data->non_common_revs++;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clear_marks(const char *refname, const struct object_id *oid,
|
2022-08-26 01:09:48 +08:00
|
|
|
int flag UNUSED,
|
|
|
|
void *cb_data UNUSED)
|
2018-07-17 02:44:01 +08:00
|
|
|
{
|
2018-08-03 06:30:46 +08:00
|
|
|
struct object *o = deref_tag(the_repository, parse_object(the_repository, oid), refname, 0);
|
2018-07-17 02:44:01 +08:00
|
|
|
|
|
|
|
if (o && o->type == OBJ_COMMIT)
|
|
|
|
clear_commit_marks((struct commit *)o,
|
|
|
|
COMMON | ADVERTISED | SEEN | POPPED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-04-26 21:15:04 +08:00
|
|
|
* Mark this SEEN commit and all its parsed SEEN ancestors as COMMON.
|
2018-07-17 02:44:01 +08:00
|
|
|
*/
|
2022-10-26 07:29:34 +08:00
|
|
|
static void mark_common(struct data *data, struct commit *seen_commit)
|
2018-07-17 02:44:01 +08:00
|
|
|
{
|
2022-10-26 07:29:34 +08:00
|
|
|
struct prio_queue queue = { NULL };
|
|
|
|
struct commit *c;
|
|
|
|
|
2023-04-26 21:15:04 +08:00
|
|
|
if (seen_commit->object.flags & COMMON)
|
|
|
|
return;
|
|
|
|
|
2022-10-26 07:29:34 +08:00
|
|
|
prio_queue_put(&queue, seen_commit);
|
2023-04-26 21:15:04 +08:00
|
|
|
seen_commit->object.flags |= COMMON;
|
2022-10-26 07:29:34 +08:00
|
|
|
while ((c = prio_queue_get(&queue))) {
|
|
|
|
struct commit_list *p;
|
2023-04-26 21:15:04 +08:00
|
|
|
|
2022-10-26 07:29:34 +08:00
|
|
|
if (!(c->object.flags & POPPED))
|
|
|
|
data->non_common_revs--;
|
|
|
|
|
|
|
|
if (!c->object.parsed)
|
2023-04-26 21:15:04 +08:00
|
|
|
continue;
|
2022-10-26 07:29:34 +08:00
|
|
|
for (p = c->parents; p; p = p->next) {
|
2023-04-26 21:15:04 +08:00
|
|
|
if (!(p->item->object.flags & SEEN) ||
|
|
|
|
(p->item->object.flags & COMMON))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p->item->object.flags |= COMMON;
|
|
|
|
prio_queue_put(&queue, p->item);
|
2022-10-26 07:29:34 +08:00
|
|
|
}
|
2018-07-17 02:44:01 +08:00
|
|
|
}
|
2023-04-26 21:15:04 +08:00
|
|
|
|
|
|
|
clear_prio_queue(&queue);
|
2018-07-17 02:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that the priority queue has an entry for to_push, and ensure that the
|
|
|
|
* entry has the correct flags and ttl.
|
|
|
|
*
|
|
|
|
* This function returns 1 if an entry was found or created, and 0 otherwise
|
|
|
|
* (because the entry for this commit had already been popped).
|
|
|
|
*/
|
|
|
|
static int push_parent(struct data *data, struct entry *entry,
|
|
|
|
struct commit *to_push)
|
|
|
|
{
|
|
|
|
struct entry *parent_entry;
|
|
|
|
|
|
|
|
if (to_push->object.flags & SEEN) {
|
|
|
|
int i;
|
|
|
|
if (to_push->object.flags & POPPED)
|
|
|
|
/*
|
|
|
|
* The entry for this commit has already been popped,
|
|
|
|
* due to clock skew. Pretend that this parent does not
|
|
|
|
* exist.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Find the existing entry and use it.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < data->rev_list.nr; i++) {
|
|
|
|
parent_entry = data->rev_list.array[i].data;
|
|
|
|
if (parent_entry->commit == to_push)
|
|
|
|
goto parent_found;
|
|
|
|
}
|
|
|
|
BUG("missing parent in priority queue");
|
|
|
|
parent_found:
|
|
|
|
;
|
|
|
|
} else {
|
|
|
|
parent_entry = rev_list_push(data, to_push, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->commit->object.flags & (COMMON | ADVERTISED)) {
|
|
|
|
mark_common(data, to_push);
|
|
|
|
} else {
|
|
|
|
uint16_t new_original_ttl = entry->ttl
|
|
|
|
? entry->original_ttl : entry->original_ttl * 3 / 2 + 1;
|
|
|
|
uint16_t new_ttl = entry->ttl
|
|
|
|
? entry->ttl - 1 : new_original_ttl;
|
|
|
|
if (parent_entry->original_ttl < new_original_ttl) {
|
|
|
|
parent_entry->original_ttl = new_original_ttl;
|
|
|
|
parent_entry->ttl = new_ttl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct object_id *get_rev(struct data *data)
|
|
|
|
{
|
|
|
|
struct commit *to_send = NULL;
|
|
|
|
|
|
|
|
while (to_send == NULL) {
|
|
|
|
struct entry *entry;
|
|
|
|
struct commit *commit;
|
|
|
|
struct commit_list *p;
|
|
|
|
int parent_pushed = 0;
|
|
|
|
|
|
|
|
if (data->rev_list.nr == 0 || data->non_common_revs == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
entry = prio_queue_get(&data->rev_list);
|
|
|
|
commit = entry->commit;
|
|
|
|
commit->object.flags |= POPPED;
|
|
|
|
if (!(commit->object.flags & COMMON))
|
|
|
|
data->non_common_revs--;
|
|
|
|
|
|
|
|
if (!(commit->object.flags & COMMON) && !entry->ttl)
|
|
|
|
to_send = commit;
|
|
|
|
|
2023-03-28 21:58:48 +08:00
|
|
|
repo_parse_commit(the_repository, commit);
|
2018-07-17 02:44:01 +08:00
|
|
|
for (p = commit->parents; p; p = p->next)
|
|
|
|
parent_pushed |= push_parent(data, entry, p->item);
|
|
|
|
|
|
|
|
if (!(commit->object.flags & COMMON) && !parent_pushed)
|
|
|
|
/*
|
|
|
|
* This commit has no parents, or all of its parents
|
|
|
|
* have already been popped (due to clock skew), so send
|
|
|
|
* it anyway.
|
|
|
|
*/
|
|
|
|
to_send = commit;
|
|
|
|
|
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &to_send->object.oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void known_common(struct fetch_negotiator *n, struct commit *c)
|
|
|
|
{
|
|
|
|
if (c->object.flags & SEEN)
|
|
|
|
return;
|
|
|
|
rev_list_push(n->data, c, ADVERTISED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_tip(struct fetch_negotiator *n, struct commit *c)
|
|
|
|
{
|
|
|
|
n->known_common = NULL;
|
|
|
|
if (c->object.flags & SEEN)
|
|
|
|
return;
|
|
|
|
rev_list_push(n->data, c, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct object_id *next(struct fetch_negotiator *n)
|
|
|
|
{
|
|
|
|
n->known_common = NULL;
|
|
|
|
n->add_tip = NULL;
|
|
|
|
return get_rev(n->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ack(struct fetch_negotiator *n, struct commit *c)
|
|
|
|
{
|
|
|
|
int known_to_be_common = !!(c->object.flags & COMMON);
|
|
|
|
if (!(c->object.flags & SEEN))
|
|
|
|
die("received ack for commit %s not sent as 'have'\n",
|
|
|
|
oid_to_hex(&c->object.oid));
|
|
|
|
mark_common(n->data, c);
|
|
|
|
return known_to_be_common;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release(struct fetch_negotiator *n)
|
|
|
|
{
|
|
|
|
clear_prio_queue(&((struct data *)n->data)->rev_list);
|
|
|
|
FREE_AND_NULL(n->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void skipping_negotiator_init(struct fetch_negotiator *negotiator)
|
|
|
|
{
|
|
|
|
struct data *data;
|
|
|
|
negotiator->known_common = known_common;
|
|
|
|
negotiator->add_tip = add_tip;
|
|
|
|
negotiator->next = next;
|
|
|
|
negotiator->ack = ack;
|
|
|
|
negotiator->release = release;
|
2021-03-14 00:17:22 +08:00
|
|
|
negotiator->data = CALLOC_ARRAY(data, 1);
|
2018-07-17 02:44:01 +08:00
|
|
|
data->rev_list.compare = compare;
|
|
|
|
|
|
|
|
if (marked)
|
2024-05-07 15:11:53 +08:00
|
|
|
refs_for_each_ref(get_main_ref_store(the_repository),
|
|
|
|
clear_marks, NULL);
|
2018-07-17 02:44:01 +08:00
|
|
|
marked = 1;
|
|
|
|
}
|