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