Merge branch 'master' of github.com:Softcatala/git-po
[alt-git.git] / promisor-remote.c
blob3c572b1c811e0b8912a11e3f193eb79f131ee763
1 #include "cache.h"
2 #include "object-store.h"
3 #include "promisor-remote.h"
4 #include "config.h"
5 #include "transport.h"
6 #include "strvec.h"
8 static char *repository_format_partial_clone;
10 void set_repository_format_partial_clone(char *partial_clone)
12 repository_format_partial_clone = xstrdup_or_null(partial_clone);
15 static int fetch_objects(const char *remote_name,
16 const struct object_id *oids,
17 int oid_nr)
19 struct child_process child = CHILD_PROCESS_INIT;
20 int i;
21 FILE *child_in;
23 child.git_cmd = 1;
24 child.in = -1;
25 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
26 "fetch", remote_name, "--no-tags",
27 "--no-write-fetch-head", "--recurse-submodules=no",
28 "--filter=blob:none", "--stdin", NULL);
29 if (start_command(&child))
30 die(_("promisor-remote: unable to fork off fetch subprocess"));
31 child_in = xfdopen(child.in, "w");
33 for (i = 0; i < oid_nr; i++) {
34 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
35 die_errno(_("promisor-remote: could not write to fetch subprocess"));
36 if (fputc('\n', child_in) < 0)
37 die_errno(_("promisor-remote: could not write to fetch subprocess"));
40 if (fclose(child_in) < 0)
41 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
42 return finish_command(&child) ? -1 : 0;
45 static struct promisor_remote *promisors;
46 static struct promisor_remote **promisors_tail = &promisors;
48 static struct promisor_remote *promisor_remote_new(const char *remote_name)
50 struct promisor_remote *r;
52 if (*remote_name == '/') {
53 warning(_("promisor remote name cannot begin with '/': %s"),
54 remote_name);
55 return NULL;
58 FLEX_ALLOC_STR(r, name, remote_name);
60 *promisors_tail = r;
61 promisors_tail = &r->next;
63 return r;
66 static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
67 struct promisor_remote **previous)
69 struct promisor_remote *r, *p;
71 for (p = NULL, r = promisors; r; p = r, r = r->next)
72 if (!strcmp(r->name, remote_name)) {
73 if (previous)
74 *previous = p;
75 return r;
78 return NULL;
81 static void promisor_remote_move_to_tail(struct promisor_remote *r,
82 struct promisor_remote *previous)
84 if (r->next == NULL)
85 return;
87 if (previous)
88 previous->next = r->next;
89 else
90 promisors = r->next ? r->next : r;
91 r->next = NULL;
92 *promisors_tail = r;
93 promisors_tail = &r->next;
96 static int promisor_remote_config(const char *var, const char *value, void *data)
98 const char *name;
99 size_t namelen;
100 const char *subkey;
102 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
103 return 0;
105 if (!strcmp(subkey, "promisor")) {
106 char *remote_name;
108 if (!git_config_bool(var, value))
109 return 0;
111 remote_name = xmemdupz(name, namelen);
113 if (!promisor_remote_lookup(remote_name, NULL))
114 promisor_remote_new(remote_name);
116 free(remote_name);
117 return 0;
119 if (!strcmp(subkey, "partialclonefilter")) {
120 struct promisor_remote *r;
121 char *remote_name = xmemdupz(name, namelen);
123 r = promisor_remote_lookup(remote_name, NULL);
124 if (!r)
125 r = promisor_remote_new(remote_name);
127 free(remote_name);
129 if (!r)
130 return 0;
132 return git_config_string(&r->partial_clone_filter, var, value);
135 return 0;
138 static int initialized;
140 static void promisor_remote_init(void)
142 if (initialized)
143 return;
144 initialized = 1;
146 git_config(promisor_remote_config, NULL);
148 if (repository_format_partial_clone) {
149 struct promisor_remote *o, *previous;
151 o = promisor_remote_lookup(repository_format_partial_clone,
152 &previous);
153 if (o)
154 promisor_remote_move_to_tail(o, previous);
155 else
156 promisor_remote_new(repository_format_partial_clone);
160 static void promisor_remote_clear(void)
162 while (promisors) {
163 struct promisor_remote *r = promisors;
164 promisors = promisors->next;
165 free(r);
168 promisors_tail = &promisors;
171 void promisor_remote_reinit(void)
173 initialized = 0;
174 promisor_remote_clear();
175 promisor_remote_init();
178 struct promisor_remote *promisor_remote_find(const char *remote_name)
180 promisor_remote_init();
182 if (!remote_name)
183 return promisors;
185 return promisor_remote_lookup(remote_name, NULL);
188 int has_promisor_remote(void)
190 return !!promisor_remote_find(NULL);
193 static int remove_fetched_oids(struct repository *repo,
194 struct object_id **oids,
195 int oid_nr, int to_free)
197 int i, remaining_nr = 0;
198 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
199 struct object_id *old_oids = *oids;
200 struct object_id *new_oids;
202 for (i = 0; i < oid_nr; i++)
203 if (oid_object_info_extended(repo, &old_oids[i], NULL,
204 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
205 remaining[i] = 1;
206 remaining_nr++;
209 if (remaining_nr) {
210 int j = 0;
211 new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
212 for (i = 0; i < oid_nr; i++)
213 if (remaining[i])
214 oidcpy(&new_oids[j++], &old_oids[i]);
215 *oids = new_oids;
216 if (to_free)
217 free(old_oids);
220 free(remaining);
222 return remaining_nr;
225 int promisor_remote_get_direct(struct repository *repo,
226 const struct object_id *oids,
227 int oid_nr)
229 struct promisor_remote *r;
230 struct object_id *remaining_oids = (struct object_id *)oids;
231 int remaining_nr = oid_nr;
232 int to_free = 0;
233 int res = -1;
235 if (oid_nr == 0)
236 return 0;
238 promisor_remote_init();
240 for (r = promisors; r; r = r->next) {
241 if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
242 if (remaining_nr == 1)
243 continue;
244 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
245 remaining_nr, to_free);
246 if (remaining_nr) {
247 to_free = 1;
248 continue;
251 res = 0;
252 break;
255 if (to_free)
256 free(remaining_oids);
258 return res;