Merge branch 'ab/progress-cleanup' into seen
[git] / promisor-remote.c
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"
7
8 struct promisor_remote_config {
9         struct promisor_remote *promisors;
10         struct promisor_remote **promisors_tail;
11 };
12
13 static int fetch_objects(struct repository *repo,
14                          const char *remote_name,
15                          const struct object_id *oids,
16                          int oid_nr)
17 {
18         struct child_process child = CHILD_PROCESS_INIT;
19         int i;
20         FILE *child_in;
21
22         child.git_cmd = 1;
23         child.in = -1;
24         if (repo != the_repository)
25                 prepare_other_repo_env(&child.env_array, repo->gitdir);
26         strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
27                      "fetch", remote_name, "--no-tags",
28                      "--no-write-fetch-head", "--recurse-submodules=no",
29                      "--filter=blob:none", "--stdin", NULL);
30         if (start_command(&child))
31                 die(_("promisor-remote: unable to fork off fetch subprocess"));
32         child_in = xfdopen(child.in, "w");
33
34         for (i = 0; i < oid_nr; i++) {
35                 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
36                         die_errno(_("promisor-remote: could not write to fetch subprocess"));
37                 if (fputc('\n', child_in) < 0)
38                         die_errno(_("promisor-remote: could not write to fetch subprocess"));
39         }
40
41         if (fclose(child_in) < 0)
42                 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
43         return finish_command(&child) ? -1 : 0;
44 }
45
46 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
47                                                    const char *remote_name)
48 {
49         struct promisor_remote *r;
50
51         if (*remote_name == '/') {
52                 warning(_("promisor remote name cannot begin with '/': %s"),
53                         remote_name);
54                 return NULL;
55         }
56
57         FLEX_ALLOC_STR(r, name, remote_name);
58
59         *config->promisors_tail = r;
60         config->promisors_tail = &r->next;
61
62         return r;
63 }
64
65 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
66                                                       const char *remote_name,
67                                                       struct promisor_remote **previous)
68 {
69         struct promisor_remote *r, *p;
70
71         for (p = NULL, r = config->promisors; r; p = r, r = r->next)
72                 if (!strcmp(r->name, remote_name)) {
73                         if (previous)
74                                 *previous = p;
75                         return r;
76                 }
77
78         return NULL;
79 }
80
81 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
82                                          struct promisor_remote *r,
83                                          struct promisor_remote *previous)
84 {
85         if (r->next == NULL)
86                 return;
87
88         if (previous)
89                 previous->next = r->next;
90         else
91                 config->promisors = r->next ? r->next : r;
92         r->next = NULL;
93         *config->promisors_tail = r;
94         config->promisors_tail = &r->next;
95 }
96
97 static int promisor_remote_config(const char *var, const char *value, void *data)
98 {
99         struct promisor_remote_config *config = data;
100         const char *name;
101         size_t namelen;
102         const char *subkey;
103
104         if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
105                 return 0;
106
107         if (!strcmp(subkey, "promisor")) {
108                 char *remote_name;
109
110                 if (!git_config_bool(var, value))
111                         return 0;
112
113                 remote_name = xmemdupz(name, namelen);
114
115                 if (!promisor_remote_lookup(config, remote_name, NULL))
116                         promisor_remote_new(config, remote_name);
117
118                 free(remote_name);
119                 return 0;
120         }
121         if (!strcmp(subkey, "partialclonefilter")) {
122                 struct promisor_remote *r;
123                 char *remote_name = xmemdupz(name, namelen);
124
125                 r = promisor_remote_lookup(config, remote_name, NULL);
126                 if (!r)
127                         r = promisor_remote_new(config, remote_name);
128
129                 free(remote_name);
130
131                 if (!r)
132                         return 0;
133
134                 return git_config_string(&r->partial_clone_filter, var, value);
135         }
136
137         return 0;
138 }
139
140 static void promisor_remote_init(struct repository *r)
141 {
142         struct promisor_remote_config *config;
143
144         if (r->promisor_remote_config)
145                 return;
146         config = r->promisor_remote_config =
147                 xcalloc(sizeof(*r->promisor_remote_config), 1);
148         config->promisors_tail = &config->promisors;
149
150         repo_config(r, promisor_remote_config, config);
151
152         if (r->repository_format_partial_clone) {
153                 struct promisor_remote *o, *previous;
154
155                 o = promisor_remote_lookup(config,
156                                            r->repository_format_partial_clone,
157                                            &previous);
158                 if (o)
159                         promisor_remote_move_to_tail(config, o, previous);
160                 else
161                         promisor_remote_new(config, r->repository_format_partial_clone);
162         }
163 }
164
165 void promisor_remote_clear(struct promisor_remote_config *config)
166 {
167         while (config->promisors) {
168                 struct promisor_remote *r = config->promisors;
169                 config->promisors = config->promisors->next;
170                 free(r);
171         }
172
173         config->promisors_tail = &config->promisors;
174 }
175
176 void repo_promisor_remote_reinit(struct repository *r)
177 {
178         promisor_remote_clear(r->promisor_remote_config);
179         FREE_AND_NULL(r->promisor_remote_config);
180         promisor_remote_init(r);
181 }
182
183 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
184                                                   const char *remote_name)
185 {
186         promisor_remote_init(r);
187
188         if (!remote_name)
189                 return r->promisor_remote_config->promisors;
190
191         return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
192 }
193
194 int repo_has_promisor_remote(struct repository *r)
195 {
196         return !!repo_promisor_remote_find(r, NULL);
197 }
198
199 static int remove_fetched_oids(struct repository *repo,
200                                struct object_id **oids,
201                                int oid_nr, int to_free)
202 {
203         int i, remaining_nr = 0;
204         int *remaining = xcalloc(oid_nr, sizeof(*remaining));
205         struct object_id *old_oids = *oids;
206         struct object_id *new_oids;
207
208         for (i = 0; i < oid_nr; i++)
209                 if (oid_object_info_extended(repo, &old_oids[i], NULL,
210                                              OBJECT_INFO_SKIP_FETCH_OBJECT)) {
211                         remaining[i] = 1;
212                         remaining_nr++;
213                 }
214
215         if (remaining_nr) {
216                 int j = 0;
217                 CALLOC_ARRAY(new_oids, remaining_nr);
218                 for (i = 0; i < oid_nr; i++)
219                         if (remaining[i])
220                                 oidcpy(&new_oids[j++], &old_oids[i]);
221                 *oids = new_oids;
222                 if (to_free)
223                         free(old_oids);
224         }
225
226         free(remaining);
227
228         return remaining_nr;
229 }
230
231 int promisor_remote_get_direct(struct repository *repo,
232                                const struct object_id *oids,
233                                int oid_nr)
234 {
235         struct promisor_remote *r;
236         struct object_id *remaining_oids = (struct object_id *)oids;
237         int remaining_nr = oid_nr;
238         int to_free = 0;
239         int res = -1;
240
241         if (oid_nr == 0)
242                 return 0;
243
244         promisor_remote_init(repo);
245
246         for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
247                 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
248                         if (remaining_nr == 1)
249                                 continue;
250                         remaining_nr = remove_fetched_oids(repo, &remaining_oids,
251                                                          remaining_nr, to_free);
252                         if (remaining_nr) {
253                                 to_free = 1;
254                                 continue;
255                         }
256                 }
257                 res = 0;
258                 break;
259         }
260
261         if (to_free)
262                 free(remaining_oids);
263
264         return res;
265 }