2019-06-25 15:40:27 +02:00
|
|
|
#include "cache.h"
|
2019-06-25 15:40:28 +02:00
|
|
|
#include "object-store.h"
|
2019-06-25 15:40:27 +02:00
|
|
|
#include "promisor-remote.h"
|
|
|
|
#include "config.h"
|
2019-06-25 15:40:37 +02:00
|
|
|
#include "transport.h"
|
2020-08-18 06:01:36 +02:00
|
|
|
#include "strvec.h"
|
2019-06-25 15:40:37 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
struct promisor_remote_config {
|
|
|
|
struct promisor_remote *promisors;
|
|
|
|
struct promisor_remote **promisors_tail;
|
|
|
|
};
|
2019-06-25 15:40:38 +02:00
|
|
|
|
2021-06-16 00:41:42 +02:00
|
|
|
static int fetch_objects(struct repository *repo,
|
|
|
|
const char *remote_name,
|
2019-06-25 15:40:37 +02:00
|
|
|
const struct object_id *oids,
|
|
|
|
int oid_nr)
|
|
|
|
{
|
2020-08-18 06:01:36 +02:00
|
|
|
struct child_process child = CHILD_PROCESS_INIT;
|
2019-06-25 15:40:37 +02:00
|
|
|
int i;
|
2020-08-18 06:01:36 +02:00
|
|
|
FILE *child_in;
|
|
|
|
|
|
|
|
child.git_cmd = 1;
|
|
|
|
child.in = -1;
|
2021-06-17 19:13:26 +02:00
|
|
|
if (repo != the_repository)
|
|
|
|
prepare_other_repo_env(&child.env_array, repo->gitdir);
|
2020-08-18 06:01:36 +02:00
|
|
|
strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
|
|
|
|
"fetch", remote_name, "--no-tags",
|
|
|
|
"--no-write-fetch-head", "--recurse-submodules=no",
|
|
|
|
"--filter=blob:none", "--stdin", NULL);
|
|
|
|
if (start_command(&child))
|
|
|
|
die(_("promisor-remote: unable to fork off fetch subprocess"));
|
|
|
|
child_in = xfdopen(child.in, "w");
|
2019-06-25 15:40:37 +02:00
|
|
|
|
2021-06-16 00:41:42 +02:00
|
|
|
trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
|
|
|
|
|
2019-06-25 15:40:37 +02:00
|
|
|
for (i = 0; i < oid_nr; i++) {
|
2020-08-18 06:01:36 +02:00
|
|
|
if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
|
|
|
|
die_errno(_("promisor-remote: could not write to fetch subprocess"));
|
|
|
|
if (fputc('\n', child_in) < 0)
|
|
|
|
die_errno(_("promisor-remote: could not write to fetch subprocess"));
|
2019-06-25 15:40:37 +02:00
|
|
|
}
|
2020-08-18 06:01:36 +02:00
|
|
|
|
|
|
|
if (fclose(child_in) < 0)
|
|
|
|
die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
|
|
|
|
return finish_command(&child) ? -1 : 0;
|
2019-06-25 15:40:37 +02:00
|
|
|
}
|
2019-06-25 15:40:27 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
|
|
|
|
const char *remote_name)
|
2019-06-25 15:40:27 +02:00
|
|
|
{
|
|
|
|
struct promisor_remote *r;
|
|
|
|
|
|
|
|
if (*remote_name == '/') {
|
|
|
|
warning(_("promisor remote name cannot begin with '/': %s"),
|
|
|
|
remote_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FLEX_ALLOC_STR(r, name, remote_name);
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
*config->promisors_tail = r;
|
|
|
|
config->promisors_tail = &r->next;
|
2019-06-25 15:40:27 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
|
|
|
|
const char *remote_name,
|
2019-06-25 15:40:27 +02:00
|
|
|
struct promisor_remote **previous)
|
|
|
|
{
|
|
|
|
struct promisor_remote *r, *p;
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
for (p = NULL, r = config->promisors; r; p = r, r = r->next)
|
2019-06-25 15:40:27 +02:00
|
|
|
if (!strcmp(r->name, remote_name)) {
|
|
|
|
if (previous)
|
|
|
|
*previous = p;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
|
|
|
|
struct promisor_remote *r,
|
2019-06-25 15:40:30 +02:00
|
|
|
struct promisor_remote *previous)
|
|
|
|
{
|
2022-05-02 18:50:37 +02:00
|
|
|
if (!r->next)
|
2019-10-01 00:03:55 +02:00
|
|
|
return;
|
|
|
|
|
2019-06-25 15:40:30 +02:00
|
|
|
if (previous)
|
|
|
|
previous->next = r->next;
|
|
|
|
else
|
2021-06-17 19:13:23 +02:00
|
|
|
config->promisors = r->next ? r->next : r;
|
2019-06-25 15:40:30 +02:00
|
|
|
r->next = NULL;
|
2021-06-17 19:13:23 +02:00
|
|
|
*config->promisors_tail = r;
|
|
|
|
config->promisors_tail = &r->next;
|
2019-06-25 15:40:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 15:40:27 +02:00
|
|
|
static int promisor_remote_config(const char *var, const char *value, void *data)
|
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
struct promisor_remote_config *config = data;
|
2019-06-25 15:40:27 +02:00
|
|
|
const char *name;
|
2020-04-10 21:44:28 +02:00
|
|
|
size_t namelen;
|
2019-06-25 15:40:27 +02:00
|
|
|
const char *subkey;
|
|
|
|
|
|
|
|
if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!strcmp(subkey, "promisor")) {
|
|
|
|
char *remote_name;
|
|
|
|
|
|
|
|
if (!git_config_bool(var, value))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
remote_name = xmemdupz(name, namelen);
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
if (!promisor_remote_lookup(config, remote_name, NULL))
|
|
|
|
promisor_remote_new(config, remote_name);
|
2019-06-25 15:40:27 +02:00
|
|
|
|
|
|
|
free(remote_name);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-25 15:40:32 +02:00
|
|
|
if (!strcmp(subkey, "partialclonefilter")) {
|
|
|
|
struct promisor_remote *r;
|
|
|
|
char *remote_name = xmemdupz(name, namelen);
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
r = promisor_remote_lookup(config, remote_name, NULL);
|
2019-06-25 15:40:32 +02:00
|
|
|
if (!r)
|
2021-06-17 19:13:23 +02:00
|
|
|
r = promisor_remote_new(config, remote_name);
|
2019-06-25 15:40:32 +02:00
|
|
|
|
|
|
|
free(remote_name);
|
|
|
|
|
|
|
|
if (!r)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return git_config_string(&r->partial_clone_filter, var, value);
|
|
|
|
}
|
2019-06-25 15:40:27 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
static void promisor_remote_init(struct repository *r)
|
2019-06-25 15:40:27 +02:00
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
struct promisor_remote_config *config;
|
|
|
|
|
|
|
|
if (r->promisor_remote_config)
|
2019-06-25 15:40:27 +02:00
|
|
|
return;
|
2021-06-17 19:13:23 +02:00
|
|
|
config = r->promisor_remote_config =
|
|
|
|
xcalloc(sizeof(*r->promisor_remote_config), 1);
|
|
|
|
config->promisors_tail = &config->promisors;
|
2019-06-25 15:40:27 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
repo_config(r, promisor_remote_config, config);
|
2019-06-25 15:40:30 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
if (r->repository_format_partial_clone) {
|
2019-06-25 15:40:30 +02:00
|
|
|
struct promisor_remote *o, *previous;
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
o = promisor_remote_lookup(config,
|
|
|
|
r->repository_format_partial_clone,
|
2019-06-25 15:40:30 +02:00
|
|
|
&previous);
|
|
|
|
if (o)
|
2021-06-17 19:13:23 +02:00
|
|
|
promisor_remote_move_to_tail(config, o, previous);
|
2019-06-25 15:40:30 +02:00
|
|
|
else
|
2021-06-17 19:13:23 +02:00
|
|
|
promisor_remote_new(config, r->repository_format_partial_clone);
|
2019-06-25 15:40:30 +02:00
|
|
|
}
|
2019-06-25 15:40:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
void promisor_remote_clear(struct promisor_remote_config *config)
|
2019-06-25 15:40:29 +02:00
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
while (config->promisors) {
|
|
|
|
struct promisor_remote *r = config->promisors;
|
|
|
|
config->promisors = config->promisors->next;
|
2019-06-25 15:40:29 +02:00
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
config->promisors_tail = &config->promisors;
|
2019-06-25 15:40:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
void repo_promisor_remote_reinit(struct repository *r)
|
2019-06-25 15:40:29 +02:00
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
promisor_remote_clear(r->promisor_remote_config);
|
|
|
|
FREE_AND_NULL(r->promisor_remote_config);
|
|
|
|
promisor_remote_init(r);
|
2019-06-25 15:40:29 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
struct promisor_remote *repo_promisor_remote_find(struct repository *r,
|
|
|
|
const char *remote_name)
|
2019-06-25 15:40:27 +02:00
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
promisor_remote_init(r);
|
2019-06-25 15:40:27 +02:00
|
|
|
|
|
|
|
if (!remote_name)
|
2021-06-17 19:13:23 +02:00
|
|
|
return r->promisor_remote_config->promisors;
|
2019-06-25 15:40:27 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
|
2019-06-25 15:40:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
int repo_has_promisor_remote(struct repository *r)
|
2019-06-25 15:40:27 +02:00
|
|
|
{
|
2021-06-17 19:13:23 +02:00
|
|
|
return !!repo_promisor_remote_find(r, NULL);
|
2019-06-25 15:40:27 +02:00
|
|
|
}
|
2019-06-25 15:40:28 +02:00
|
|
|
|
|
|
|
static int remove_fetched_oids(struct repository *repo,
|
|
|
|
struct object_id **oids,
|
|
|
|
int oid_nr, int to_free)
|
|
|
|
{
|
|
|
|
int i, remaining_nr = 0;
|
|
|
|
int *remaining = xcalloc(oid_nr, sizeof(*remaining));
|
|
|
|
struct object_id *old_oids = *oids;
|
|
|
|
struct object_id *new_oids;
|
|
|
|
|
|
|
|
for (i = 0; i < oid_nr; i++)
|
|
|
|
if (oid_object_info_extended(repo, &old_oids[i], NULL,
|
|
|
|
OBJECT_INFO_SKIP_FETCH_OBJECT)) {
|
|
|
|
remaining[i] = 1;
|
|
|
|
remaining_nr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (remaining_nr) {
|
|
|
|
int j = 0;
|
2021-03-13 17:17:22 +01:00
|
|
|
CALLOC_ARRAY(new_oids, remaining_nr);
|
2019-06-25 15:40:28 +02:00
|
|
|
for (i = 0; i < oid_nr; i++)
|
|
|
|
if (remaining[i])
|
|
|
|
oidcpy(&new_oids[j++], &old_oids[i]);
|
|
|
|
*oids = new_oids;
|
|
|
|
if (to_free)
|
|
|
|
free(old_oids);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(remaining);
|
|
|
|
|
|
|
|
return remaining_nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int promisor_remote_get_direct(struct repository *repo,
|
|
|
|
const struct object_id *oids,
|
|
|
|
int oid_nr)
|
|
|
|
{
|
|
|
|
struct promisor_remote *r;
|
|
|
|
struct object_id *remaining_oids = (struct object_id *)oids;
|
|
|
|
int remaining_nr = oid_nr;
|
|
|
|
int to_free = 0;
|
|
|
|
int res = -1;
|
|
|
|
|
2020-04-02 21:19:16 +02:00
|
|
|
if (oid_nr == 0)
|
|
|
|
return 0;
|
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
promisor_remote_init(repo);
|
2019-06-25 15:40:28 +02:00
|
|
|
|
2021-06-17 19:13:23 +02:00
|
|
|
for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
|
2021-06-16 00:41:42 +02:00
|
|
|
if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
|
2019-06-25 15:40:28 +02:00
|
|
|
if (remaining_nr == 1)
|
|
|
|
continue;
|
|
|
|
remaining_nr = remove_fetched_oids(repo, &remaining_oids,
|
|
|
|
remaining_nr, to_free);
|
|
|
|
if (remaining_nr) {
|
|
|
|
to_free = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_free)
|
|
|
|
free(remaining_oids);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|