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}
78if (!strcmp(subkey, "partialclonefilter")) {
79struct promisor_remote *r;
80char *remote_name = xmemdupz(name, namelen);
8182
r = promisor_remote_lookup(remote_name, NULL);
83if (!r)
84r = promisor_remote_new(remote_name);
8586
free(remote_name);
8788
if (!r)
89return 0;
9091
return git_config_string(&r->partial_clone_filter, var, value);
92}
9394
return 0;
95}
9697
static int initialized;
9899
static void promisor_remote_init(void)
100{
101if (initialized)
102return;
103initialized = 1;
104105
git_config(promisor_remote_config, NULL);
106107
if (repository_format_partial_clone) {
108struct promisor_remote *o, *previous;
109110
o = promisor_remote_lookup(repository_format_partial_clone,
111&previous);
112if (o)
113promisor_remote_move_to_tail(o, previous);
114else
115promisor_remote_new(repository_format_partial_clone);
116}
117}
118119
static void promisor_remote_clear(void)
120{
121while (promisors) {
122struct promisor_remote *r = promisors;
123promisors = promisors->next;
124free(r);
125}
126127
promisors_tail = &promisors;
128}
129130
void promisor_remote_reinit(void)
131{
132initialized = 0;
133promisor_remote_clear();
134promisor_remote_init();
135}
136137
struct promisor_remote *promisor_remote_find(const char *remote_name)
138{
139promisor_remote_init();
140141
if (!remote_name)
142return promisors;
143144
return promisor_remote_lookup(remote_name, NULL);
145}
146147
int has_promisor_remote(void)
148{
149return !!promisor_remote_find(NULL);
150}
151152
static int remove_fetched_oids(struct repository *repo,
153struct object_id **oids,
154int oid_nr, int to_free)
155{
156int i, remaining_nr = 0;
157int *remaining = xcalloc(oid_nr, sizeof(*remaining));
158struct object_id *old_oids = *oids;
159struct object_id *new_oids;
160161
for (i = 0; i < oid_nr; i++)
162if (oid_object_info_extended(repo, &old_oids[i], NULL,
163OBJECT_INFO_SKIP_FETCH_OBJECT)) {
164remaining[i] = 1;
165remaining_nr++;
166}
167168
if (remaining_nr) {
169int j = 0;
170new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
171for (i = 0; i < oid_nr; i++)
172if (remaining[i])
173oidcpy(&new_oids[j++], &old_oids[i]);
174*oids = new_oids;
175if (to_free)
176free(old_oids);
177}
178179
free(remaining);
180181
return remaining_nr;
182}
183184
int promisor_remote_get_direct(struct repository *repo,
185const struct object_id *oids,
186int oid_nr)
187{
188struct promisor_remote *r;
189struct object_id *remaining_oids = (struct object_id *)oids;
190int remaining_nr = oid_nr;
191int to_free = 0;
192int res = -1;
193194
promisor_remote_init();
195196
for (r = promisors; r; r = r->next) {
197if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
198if (remaining_nr == 1)
199continue;
200remaining_nr = remove_fetched_oids(repo, &remaining_oids,
201remaining_nr, to_free);
202if (remaining_nr) {
203to_free = 1;
204continue;
205}
206}
207res = 0;
208break;
209}
210211
if (to_free)
212free(remaining_oids);
213214
return res;
215}