1#include "cache.h"
2#include "object-store.h"
3#include "promisor-remote.h"
4#include "config.h"
5#include "fetch-object.h"
67
static struct promisor_remote *promisors;
8static struct promisor_remote **promisors_tail = &promisors;
910
static struct promisor_remote *promisor_remote_new(const char *remote_name)
11{
12struct promisor_remote *r;
1314
if (*remote_name == '/') {
15warning(_("promisor remote name cannot begin with '/': %s"),
16remote_name);
17return NULL;
18}
1920
FLEX_ALLOC_STR(r, name, remote_name);
2122
*promisors_tail = r;
23promisors_tail = &r->next;
2425
return r;
26}
2728
static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
29struct promisor_remote **previous)
30{
31struct promisor_remote *r, *p;
3233
for (p = NULL, r = promisors; r; p = r, r = r->next)
34if (!strcmp(r->name, remote_name)) {
35if (previous)
36*previous = p;
37return r;
38}
3940
return NULL;
41}
4243
static void promisor_remote_move_to_tail(struct promisor_remote *r,
44struct promisor_remote *previous)
45{
46if (previous)
47previous->next = r->next;
48else
49promisors = r->next ? r->next : r;
50r->next = NULL;
51*promisors_tail = r;
52promisors_tail = &r->next;
53}
5455
static int promisor_remote_config(const char *var, const char *value, void *data)
56{
57const char *name;
58int namelen;
59const char *subkey;
6061
if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
62return 0;
6364
if (!strcmp(subkey, "promisor")) {
65char *remote_name;
6667
if (!git_config_bool(var, value))
68return 0;
6970
remote_name = xmemdupz(name, namelen);
7172
if (!promisor_remote_lookup(remote_name, NULL))
73promisor_remote_new(remote_name);
7475
free(remote_name);
76return 0;
77}
7879
return 0;
80}
8182
static int initialized;
8384
static void promisor_remote_init(void)
85{
86if (initialized)
87return;
88initialized = 1;
8990
git_config(promisor_remote_config, NULL);
9192
if (repository_format_partial_clone) {
93struct promisor_remote *o, *previous;
9495
o = promisor_remote_lookup(repository_format_partial_clone,
96&previous);
97if (o)
98promisor_remote_move_to_tail(o, previous);
99else
100promisor_remote_new(repository_format_partial_clone);
101}
102}
103104
static void promisor_remote_clear(void)
105{
106while (promisors) {
107struct promisor_remote *r = promisors;
108promisors = promisors->next;
109free(r);
110}
111112
promisors_tail = &promisors;
113}
114115
void promisor_remote_reinit(void)
116{
117initialized = 0;
118promisor_remote_clear();
119promisor_remote_init();
120}
121122
struct promisor_remote *promisor_remote_find(const char *remote_name)
123{
124promisor_remote_init();
125126
if (!remote_name)
127return promisors;
128129
return promisor_remote_lookup(remote_name, NULL);
130}
131132
int has_promisor_remote(void)
133{
134return !!promisor_remote_find(NULL);
135}
136137
static int remove_fetched_oids(struct repository *repo,
138struct object_id **oids,
139int oid_nr, int to_free)
140{
141int i, remaining_nr = 0;
142int *remaining = xcalloc(oid_nr, sizeof(*remaining));
143struct object_id *old_oids = *oids;
144struct object_id *new_oids;
145146
for (i = 0; i < oid_nr; i++)
147if (oid_object_info_extended(repo, &old_oids[i], NULL,
148OBJECT_INFO_SKIP_FETCH_OBJECT)) {
149remaining[i] = 1;
150remaining_nr++;
151}
152153
if (remaining_nr) {
154int j = 0;
155new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
156for (i = 0; i < oid_nr; i++)
157if (remaining[i])
158oidcpy(&new_oids[j++], &old_oids[i]);
159*oids = new_oids;
160if (to_free)
161free(old_oids);
162}
163164
free(remaining);
165166
return remaining_nr;
167}
168169
int promisor_remote_get_direct(struct repository *repo,
170const struct object_id *oids,
171int oid_nr)
172{
173struct promisor_remote *r;
174struct object_id *remaining_oids = (struct object_id *)oids;
175int remaining_nr = oid_nr;
176int to_free = 0;
177int res = -1;
178179
promisor_remote_init();
180181
for (r = promisors; r; r = r->next) {
182if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
183if (remaining_nr == 1)
184continue;
185remaining_nr = remove_fetched_oids(repo, &remaining_oids,
186remaining_nr, to_free);
187if (remaining_nr) {
188to_free = 1;
189continue;
190}
191}
192res = 0;
193break;
194}
195196
if (to_free)
197free(remaining_oids);
198199
return res;
200}