Ensure builtin-fetch honors {fetch,transfer}.unpackLimit
[git/git.git] / transport.c
CommitLineData
9b288516
DB
1#include "cache.h"
2#include "transport.h"
3#include "run-command.h"
c29727d5
DB
4#include "http.h"
5#include "pkt-line.h"
6#include "fetch-pack.h"
7#include "walker.h"
c7a8a162 8#include "bundle.h"
c29727d5
DB
9
10/* Generic functions for using commit walkers */
11
1788c39c 12static int fetch_objs_via_walker(struct transport *transport,
425b1393 13 int nr_objs, struct ref **to_fetch)
c29727d5
DB
14{
15 char *dest = xstrdup(transport->url);
16 struct walker *walker = transport->data;
425b1393
SP
17 char **objs = xmalloc(nr_objs * sizeof(*objs));
18 int i;
c29727d5
DB
19
20 walker->get_all = 1;
21 walker->get_tree = 1;
22 walker->get_history = 1;
23 walker->get_verbosely = transport->verbose;
24 walker->get_recover = 0;
25
425b1393
SP
26 for (i = 0; i < nr_objs; i++)
27 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
28
b6abb48a 29 if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
c29727d5
DB
30 die("Fetch failed.");
31
425b1393
SP
32 for (i = 0; i < nr_objs; i++)
33 free(objs[i]);
34 free(objs);
c29727d5
DB
35 free(dest);
36 return 0;
37}
38
39static int disconnect_walker(struct transport *transport)
40{
41 struct walker *walker = transport->data;
42 if (walker)
43 walker_free(walker);
44 return 0;
45}
9b288516 46
9b288516
DB
47static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
48 const char **argv;
49 int argc;
50 int err;
51
52 argv = xmalloc((refspec_nr + 11) * sizeof(char *));
53 argv[0] = "http-push";
54 argc = 1;
55 if (flags & TRANSPORT_PUSH_ALL)
56 argv[argc++] = "--all";
57 if (flags & TRANSPORT_PUSH_FORCE)
58 argv[argc++] = "--force";
59 argv[argc++] = transport->url;
60 while (refspec_nr--)
61 argv[argc++] = *refspec++;
62 argv[argc] = NULL;
63 err = run_command_v_opt(argv, RUN_GIT_CMD);
64 switch (err) {
65 case -ERR_RUN_COMMAND_FORK:
66 error("unable to fork for %s", argv[0]);
67 case -ERR_RUN_COMMAND_EXEC:
68 error("unable to exec %s", argv[0]);
69 break;
70 case -ERR_RUN_COMMAND_WAITPID:
71 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
72 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
73 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
74 error("%s died with strange error", argv[0]);
75 }
76 return !!err;
77}
78
c29727d5
DB
79#ifndef NO_CURL
80static int missing__target(int code, int result)
81{
82 return /* file:// URL -- do we ever use one??? */
83 (result == CURLE_FILE_COULDNT_READ_FILE) ||
84 /* http:// and https:// URL */
85 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
86 /* ftp:// URL */
87 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
88 ;
89}
90
91#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
92
93static struct ref *get_refs_via_curl(const struct transport *transport)
94{
95 struct buffer buffer;
96 char *data, *start, *mid;
97 char *ref_name;
98 char *refs_url;
99 int i = 0;
100
101 struct active_request_slot *slot;
102 struct slot_results results;
103
104 struct ref *refs = NULL;
105 struct ref *ref = NULL;
106 struct ref *last_ref = NULL;
107
108 data = xmalloc(4096);
109 buffer.size = 4096;
110 buffer.posn = 0;
111 buffer.buffer = data;
112
113 refs_url = xmalloc(strlen(transport->url) + 11);
114 sprintf(refs_url, "%s/info/refs", transport->url);
115
116 http_init();
117
118 slot = get_active_slot();
119 slot->results = &results;
120 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
121 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
122 curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
123 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
124 if (start_active_slot(slot)) {
125 run_active_slot(slot);
126 if (results.curl_result != CURLE_OK) {
127 if (missing_target(&results)) {
128 free(buffer.buffer);
129 return NULL;
130 } else {
131 free(buffer.buffer);
132 error("%s", curl_errorstr);
133 return NULL;
134 }
135 }
136 } else {
137 free(buffer.buffer);
138 error("Unable to start request");
139 return NULL;
140 }
141
142 http_cleanup();
143
144 data = buffer.buffer;
145 start = NULL;
146 mid = data;
147 while (i < buffer.posn) {
148 if (!start)
149 start = &data[i];
150 if (data[i] == '\t')
151 mid = &data[i];
152 if (data[i] == '\n') {
153 data[i] = 0;
154 ref_name = mid + 1;
155 ref = xmalloc(sizeof(struct ref) +
156 strlen(ref_name) + 1);
157 memset(ref, 0, sizeof(struct ref));
158 strcpy(ref->name, ref_name);
159 get_sha1_hex(start, ref->old_sha1);
160 if (!refs)
161 refs = ref;
162 if (last_ref)
163 last_ref->next = ref;
164 last_ref = ref;
165 start = NULL;
166 }
167 i++;
168 }
169
170 free(buffer.buffer);
171
172 return refs;
173}
174
e5f4e214
SP
175static int fetch_objs_via_curl(struct transport *transport,
176 int nr_objs, struct ref **to_fetch)
177{
178 if (!transport->data)
179 transport->data = get_http_walker(transport->url);
180 return fetch_objs_via_walker(transport, nr_objs, to_fetch);
181}
182
c29727d5
DB
183#else
184
185static struct ref *get_refs_via_curl(const struct transport *transport)
186{
187 die("Cannot fetch from '%s' without curl ...", transport->url);
188 return NULL;
189}
190
e5f4e214
SP
191static int fetch_objs_via_curl(struct transport *transport,
192 int nr_objs, struct ref **to_fetch)
193{
194 die("Cannot fetch from '%s' without curl ...", transport->url);
195 return -1;
196}
197
c29727d5
DB
198#endif
199
c7a8a162
JS
200struct bundle_transport_data {
201 int fd;
202 struct bundle_header header;
203};
204
205static struct ref *get_refs_from_bundle(const struct transport *transport)
206{
207 struct bundle_transport_data *data = transport->data;
208 struct ref *result = NULL;
209 int i;
210
211 if (data->fd > 0)
212 close(data->fd);
213 data->fd = read_bundle_header(transport->url, &data->header);
214 if (data->fd < 0)
215 die ("Could not read bundle '%s'.", transport->url);
216 for (i = 0; i < data->header.references.nr; i++) {
217 struct ref_list_entry *e = data->header.references.list + i;
218 struct ref *ref = alloc_ref(strlen(e->name));
219 hashcpy(ref->old_sha1, e->sha1);
220 strcpy(ref->name, e->name);
221 ref->next = result;
222 result = ref;
223 }
224 return result;
225}
226
1788c39c 227static int fetch_refs_from_bundle(struct transport *transport,
425b1393 228 int nr_heads, struct ref **to_fetch)
c7a8a162
JS
229{
230 struct bundle_transport_data *data = transport->data;
231 return unbundle(&data->header, data->fd);
232}
233
234static int close_bundle(struct transport *transport)
235{
236 struct bundle_transport_data *data = transport->data;
237 if (data->fd > 0)
238 close(data->fd);
239 return 0;
240}
241
9b288516
DB
242struct git_transport_data {
243 unsigned thin : 1;
c29727d5 244 unsigned keep : 1;
c29727d5 245 int depth;
c29727d5 246 const char *uploadpack;
9b288516
DB
247 const char *receivepack;
248};
249
250static int set_git_option(struct transport *connection,
251 const char *name, const char *value)
252{
253 struct git_transport_data *data = connection->data;
c29727d5
DB
254 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
255 data->uploadpack = value;
256 return 0;
257 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
9b288516
DB
258 data->receivepack = value;
259 return 0;
260 } else if (!strcmp(name, TRANS_OPT_THIN)) {
261 data->thin = !!value;
262 return 0;
c29727d5
DB
263 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
264 data->keep = !!value;
265 return 0;
c29727d5
DB
266 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
267 if (!value)
268 data->depth = 0;
269 else
270 data->depth = atoi(value);
271 return 0;
9b288516
DB
272 }
273 return 1;
274}
275
c29727d5
DB
276static struct ref *get_refs_via_connect(const struct transport *transport)
277{
278 struct git_transport_data *data = transport->data;
279 struct ref *refs;
280 int fd[2];
281 pid_t pid;
282 char *dest = xstrdup(transport->url);
283
284 pid = git_connect(fd, dest, data->uploadpack, 0);
285
286 if (pid < 0)
287 die("Failed to connect to \"%s\"", transport->url);
288
289 get_remote_heads(fd[0], &refs, 0, NULL, 0);
290 packet_flush(fd[1]);
291
292 finish_connect(pid);
293
294 free(dest);
295
296 return refs;
297}
298
1788c39c 299static int fetch_refs_via_pack(struct transport *transport,
425b1393 300 int nr_heads, struct ref **to_fetch)
c29727d5
DB
301{
302 struct git_transport_data *data = transport->data;
425b1393 303 char **heads = xmalloc(nr_heads * sizeof(*heads));
e4cd6c7a 304 char **origh = xmalloc(nr_heads * sizeof(*origh));
c29727d5
DB
305 struct ref *refs;
306 char *dest = xstrdup(transport->url);
307 struct fetch_pack_args args;
425b1393 308 int i;
c29727d5 309
fa740529 310 memset(&args, 0, sizeof(args));
c29727d5 311 args.uploadpack = data->uploadpack;
c29727d5 312 args.keep_pack = data->keep;
fa740529 313 args.lock_pack = 1;
c29727d5 314 args.use_thin_pack = data->thin;
c29727d5
DB
315 args.verbose = transport->verbose;
316 args.depth = data->depth;
c29727d5 317
425b1393 318 for (i = 0; i < nr_heads; i++)
e4cd6c7a 319 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
fa740529 320 refs = fetch_pack(&args, dest, nr_heads, heads, &transport->pack_lockfile);
c29727d5 321
425b1393 322 for (i = 0; i < nr_heads; i++)
e4cd6c7a
SP
323 free(origh[i]);
324 free(origh);
325 free(heads);
c29727d5 326 free_refs(refs);
c29727d5
DB
327 free(dest);
328 return 0;
329}
330
9b288516
DB
331static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
332 struct git_transport_data *data = transport->data;
333 const char **argv;
334 char *rem;
335 int argc;
336 int err;
337
338 argv = xmalloc((refspec_nr + 11) * sizeof(char *));
339 argv[0] = "send-pack";
340 argc = 1;
341 if (flags & TRANSPORT_PUSH_ALL)
342 argv[argc++] = "--all";
343 if (flags & TRANSPORT_PUSH_FORCE)
344 argv[argc++] = "--force";
345 if (data->receivepack) {
346 char *rp = xmalloc(strlen(data->receivepack) + 16);
347 sprintf(rp, "--receive-pack=%s", data->receivepack);
348 argv[argc++] = rp;
349 }
350 if (data->thin)
351 argv[argc++] = "--thin";
352 rem = xmalloc(strlen(transport->remote->name) + 10);
353 sprintf(rem, "--remote=%s", transport->remote->name);
354 argv[argc++] = rem;
355 argv[argc++] = transport->url;
356 while (refspec_nr--)
357 argv[argc++] = *refspec++;
358 argv[argc] = NULL;
359 err = run_command_v_opt(argv, RUN_GIT_CMD);
360 switch (err) {
361 case -ERR_RUN_COMMAND_FORK:
362 error("unable to fork for %s", argv[0]);
363 case -ERR_RUN_COMMAND_EXEC:
364 error("unable to exec %s", argv[0]);
365 break;
366 case -ERR_RUN_COMMAND_WAITPID:
367 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
368 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
369 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
370 error("%s died with strange error", argv[0]);
371 }
372 return !!err;
373}
374
9b288516
DB
375static int is_local(const char *url)
376{
377 const char *colon = strchr(url, ':');
378 const char *slash = strchr(url, '/');
379 return !colon || (slash && slash < colon);
380}
381
382static int is_file(const char *url)
383{
384 struct stat buf;
385 if (stat(url, &buf))
386 return 0;
387 return S_ISREG(buf.st_mode);
388}
389
e5f4e214 390struct transport *transport_get(struct remote *remote, const char *url)
9b288516 391{
8eb554ae
SP
392 struct transport *ret = xcalloc(1, sizeof(*ret));
393
394 ret->remote = remote;
395 ret->url = url;
8eb554ae 396
9b288516 397 if (!prefixcmp(url, "rsync://")) {
824d5776
SP
398 /* not supported; don't populate any ops */
399
8eb554ae
SP
400 } else if (!prefixcmp(url, "http://")
401 || !prefixcmp(url, "https://")
402 || !prefixcmp(url, "ftp://")) {
824d5776
SP
403 ret->get_refs_list = get_refs_via_curl;
404 ret->fetch = fetch_objs_via_curl;
405 ret->push = curl_transport_push;
406 ret->disconnect = disconnect_walker;
407
9b288516 408 } else if (is_local(url) && is_file(url)) {
c7a8a162 409 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
c7a8a162 410 ret->data = data;
824d5776
SP
411 ret->get_refs_list = get_refs_from_bundle;
412 ret->fetch = fetch_refs_from_bundle;
413 ret->disconnect = close_bundle;
414
9b288516
DB
415 } else {
416 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 417 ret->data = data;
824d5776
SP
418 ret->set_option = set_git_option;
419 ret->get_refs_list = get_refs_via_connect;
420 ret->fetch = fetch_refs_via_pack;
421 ret->push = git_transport_push;
422
9b288516 423 data->thin = 1;
c29727d5
DB
424 data->uploadpack = "git-upload-pack";
425 if (remote && remote->uploadpack)
426 data->uploadpack = remote->uploadpack;
9b288516
DB
427 data->receivepack = "git-receive-pack";
428 if (remote && remote->receivepack)
429 data->receivepack = remote->receivepack;
9b288516 430 }
8eb554ae 431
9b288516
DB
432 return ret;
433}
434
435int transport_set_option(struct transport *transport,
436 const char *name, const char *value)
437{
824d5776
SP
438 if (transport->set_option)
439 return transport->set_option(transport, name, value);
ab865e6e 440 return 1;
9b288516
DB
441}
442
443int transport_push(struct transport *transport,
444 int refspec_nr, const char **refspec, int flags)
445{
824d5776 446 if (!transport->push)
9b288516 447 return 1;
824d5776 448 return transport->push(transport, refspec_nr, refspec, flags);
9b288516
DB
449}
450
c29727d5
DB
451struct ref *transport_get_remote_refs(struct transport *transport)
452{
453 if (!transport->remote_refs)
824d5776 454 transport->remote_refs = transport->get_refs_list(transport);
c29727d5
DB
455 return transport->remote_refs;
456}
457
c29727d5
DB
458int transport_fetch_refs(struct transport *transport, struct ref *refs)
459{
425b1393 460 int rc;
7a2bff45 461 int nr_heads = 0, nr_alloc = 0;
425b1393 462 struct ref **heads = NULL;
c29727d5 463 struct ref *rm;
c29727d5
DB
464
465 for (rm = refs; rm; rm = rm->next) {
466 if (rm->peer_ref &&
467 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
468 continue;
7a2bff45 469 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 470 heads[nr_heads++] = rm;
c29727d5
DB
471 }
472
824d5776 473 rc = transport->fetch(transport, nr_heads, heads);
c29727d5 474 free(heads);
425b1393 475 return rc;
c29727d5
DB
476}
477
1788c39c
SP
478void transport_unlock_pack(struct transport *transport)
479{
480 if (transport->pack_lockfile) {
481 unlink(transport->pack_lockfile);
482 free(transport->pack_lockfile);
483 transport->pack_lockfile = NULL;
484 }
485}
486
9b288516
DB
487int transport_disconnect(struct transport *transport)
488{
489 int ret = 0;
824d5776
SP
490 if (transport->disconnect)
491 ret = transport->disconnect(transport);
9b288516
DB
492 free(transport);
493 return ret;
494}