Merge branch 'js/update-index-ignore-removal-for-skip-worktree'
[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
7 static char *repository_format_partial_clone;
8 static const char *core_partial_clone_filter_default;
9
10 void set_repository_format_partial_clone(char *partial_clone)
11 {
12         repository_format_partial_clone = xstrdup_or_null(partial_clone);
13 }
14
15 static int fetch_refs(const char *remote_name, struct ref *ref)
16 {
17         struct remote *remote;
18         struct transport *transport;
19         int original_fetch_if_missing = fetch_if_missing;
20         int res;
21
22         fetch_if_missing = 0;
23         remote = remote_get(remote_name);
24         if (!remote->url[0])
25                 die(_("Remote with no URL"));
26         transport = transport_get(remote, remote->url[0]);
27
28         transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
29         transport_set_option(transport, TRANS_OPT_NO_DEPENDENTS, "1");
30         res = transport_fetch_refs(transport, ref);
31         fetch_if_missing = original_fetch_if_missing;
32
33         return res;
34 }
35
36 static int fetch_objects(const char *remote_name,
37                          const struct object_id *oids,
38                          int oid_nr)
39 {
40         struct ref *ref = NULL;
41         int i;
42
43         for (i = 0; i < oid_nr; i++) {
44                 struct ref *new_ref = alloc_ref(oid_to_hex(&oids[i]));
45                 oidcpy(&new_ref->old_oid, &oids[i]);
46                 new_ref->exact_oid = 1;
47                 new_ref->next = ref;
48                 ref = new_ref;
49         }
50         return fetch_refs(remote_name, ref);
51 }
52
53 static struct promisor_remote *promisors;
54 static struct promisor_remote **promisors_tail = &promisors;
55
56 static struct promisor_remote *promisor_remote_new(const char *remote_name)
57 {
58         struct promisor_remote *r;
59
60         if (*remote_name == '/') {
61                 warning(_("promisor remote name cannot begin with '/': %s"),
62                         remote_name);
63                 return NULL;
64         }
65
66         FLEX_ALLOC_STR(r, name, remote_name);
67
68         *promisors_tail = r;
69         promisors_tail = &r->next;
70
71         return r;
72 }
73
74 static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
75                                                       struct promisor_remote **previous)
76 {
77         struct promisor_remote *r, *p;
78
79         for (p = NULL, r = promisors; r; p = r, r = r->next)
80                 if (!strcmp(r->name, remote_name)) {
81                         if (previous)
82                                 *previous = p;
83                         return r;
84                 }
85
86         return NULL;
87 }
88
89 static void promisor_remote_move_to_tail(struct promisor_remote *r,
90                                          struct promisor_remote *previous)
91 {
92         if (r->next == NULL)
93                 return;
94
95         if (previous)
96                 previous->next = r->next;
97         else
98                 promisors = r->next ? r->next : r;
99         r->next = NULL;
100         *promisors_tail = r;
101         promisors_tail = &r->next;
102 }
103
104 static int promisor_remote_config(const char *var, const char *value, void *data)
105 {
106         const char *name;
107         int namelen;
108         const char *subkey;
109
110         if (!strcmp(var, "core.partialclonefilter"))
111                 return git_config_string(&core_partial_clone_filter_default,
112                                          var, value);
113
114         if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
115                 return 0;
116
117         if (!strcmp(subkey, "promisor")) {
118                 char *remote_name;
119
120                 if (!git_config_bool(var, value))
121                         return 0;
122
123                 remote_name = xmemdupz(name, namelen);
124
125                 if (!promisor_remote_lookup(remote_name, NULL))
126                         promisor_remote_new(remote_name);
127
128                 free(remote_name);
129                 return 0;
130         }
131         if (!strcmp(subkey, "partialclonefilter")) {
132                 struct promisor_remote *r;
133                 char *remote_name = xmemdupz(name, namelen);
134
135                 r = promisor_remote_lookup(remote_name, NULL);
136                 if (!r)
137                         r = promisor_remote_new(remote_name);
138
139                 free(remote_name);
140
141                 if (!r)
142                         return 0;
143
144                 return git_config_string(&r->partial_clone_filter, var, value);
145         }
146
147         return 0;
148 }
149
150 static int initialized;
151
152 static void promisor_remote_init(void)
153 {
154         if (initialized)
155                 return;
156         initialized = 1;
157
158         git_config(promisor_remote_config, NULL);
159
160         if (repository_format_partial_clone) {
161                 struct promisor_remote *o, *previous;
162
163                 o = promisor_remote_lookup(repository_format_partial_clone,
164                                            &previous);
165                 if (o)
166                         promisor_remote_move_to_tail(o, previous);
167                 else
168                         promisor_remote_new(repository_format_partial_clone);
169         }
170 }
171
172 static void promisor_remote_clear(void)
173 {
174         while (promisors) {
175                 struct promisor_remote *r = promisors;
176                 promisors = promisors->next;
177                 free(r);
178         }
179
180         promisors_tail = &promisors;
181 }
182
183 void promisor_remote_reinit(void)
184 {
185         initialized = 0;
186         promisor_remote_clear();
187         promisor_remote_init();
188 }
189
190 struct promisor_remote *promisor_remote_find(const char *remote_name)
191 {
192         promisor_remote_init();
193
194         if (!remote_name)
195                 return promisors;
196
197         return promisor_remote_lookup(remote_name, NULL);
198 }
199
200 int has_promisor_remote(void)
201 {
202         return !!promisor_remote_find(NULL);
203 }
204
205 static int remove_fetched_oids(struct repository *repo,
206                                struct object_id **oids,
207                                int oid_nr, int to_free)
208 {
209         int i, remaining_nr = 0;
210         int *remaining = xcalloc(oid_nr, sizeof(*remaining));
211         struct object_id *old_oids = *oids;
212         struct object_id *new_oids;
213
214         for (i = 0; i < oid_nr; i++)
215                 if (oid_object_info_extended(repo, &old_oids[i], NULL,
216                                              OBJECT_INFO_SKIP_FETCH_OBJECT)) {
217                         remaining[i] = 1;
218                         remaining_nr++;
219                 }
220
221         if (remaining_nr) {
222                 int j = 0;
223                 new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
224                 for (i = 0; i < oid_nr; i++)
225                         if (remaining[i])
226                                 oidcpy(&new_oids[j++], &old_oids[i]);
227                 *oids = new_oids;
228                 if (to_free)
229                         free(old_oids);
230         }
231
232         free(remaining);
233
234         return remaining_nr;
235 }
236
237 int promisor_remote_get_direct(struct repository *repo,
238                                const struct object_id *oids,
239                                int oid_nr)
240 {
241         struct promisor_remote *r;
242         struct object_id *remaining_oids = (struct object_id *)oids;
243         int remaining_nr = oid_nr;
244         int to_free = 0;
245         int res = -1;
246
247         promisor_remote_init();
248
249         for (r = promisors; r; r = r->next) {
250                 if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
251                         if (remaining_nr == 1)
252                                 continue;
253                         remaining_nr = remove_fetched_oids(repo, &remaining_oids,
254                                                          remaining_nr, to_free);
255                         if (remaining_nr) {
256                                 to_free = 1;
257                                 continue;
258                         }
259                 }
260                 res = 0;
261                 break;
262         }
263
264         if (to_free)
265                 free(remaining_oids);
266
267         return res;
268 }