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