Windows: A rudimentary poll() emulation.
[git/git.git] / transport.c
CommitLineData
9b288516
DB
1#include "cache.h"
2#include "transport.h"
3#include "run-command.h"
ccfc02a3 4#ifndef NO_CURL
c29727d5 5#include "http.h"
ccfc02a3 6#endif
c29727d5
DB
7#include "pkt-line.h"
8#include "fetch-pack.h"
40cb4fab 9#include "send-pack.h"
c29727d5 10#include "walker.h"
c7a8a162 11#include "bundle.h"
cd547b48
JS
12#include "dir.h"
13#include "refs.h"
14
15/* rsync support */
16
17/*
18 * We copy packed-refs and refs/ into a temporary file, then read the
19 * loose refs recursively (sorting whenever possible), and then inserting
20 * those packed refs that are not yet in the list (not validating, but
21 * assuming that the file is sorted).
22 *
23 * Appears refactoring this from refs.c is too cumbersome.
24 */
25
26static int str_cmp(const void *a, const void *b)
27{
28 const char *s1 = a;
29 const char *s2 = b;
30
31 return strcmp(s1, s2);
32}
33
34/* path->buf + name_offset is expected to point to "refs/" */
35
36static int read_loose_refs(struct strbuf *path, int name_offset,
37 struct ref **tail)
38{
39 DIR *dir = opendir(path->buf);
40 struct dirent *de;
41 struct {
42 char **entries;
43 int nr, alloc;
44 } list;
45 int i, pathlen;
46
47 if (!dir)
48 return -1;
49
50 memset (&list, 0, sizeof(list));
51
52 while ((de = readdir(dir))) {
53 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
54 (de->d_name[1] == '.' &&
55 de->d_name[2] == '\0')))
56 continue;
57 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
58 list.entries[list.nr++] = xstrdup(de->d_name);
59 }
60 closedir(dir);
61
62 /* sort the list */
63
64 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
65
66 pathlen = path->len;
67 strbuf_addch(path, '/');
68
69 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
70 strbuf_addstr(path, list.entries[i]);
71 if (read_loose_refs(path, name_offset, tail)) {
72 int fd = open(path->buf, O_RDONLY);
73 char buffer[40];
74 struct ref *next;
75
76 if (fd < 0)
77 continue;
78 next = alloc_ref(path->len - name_offset + 1);
79 if (read_in_full(fd, buffer, 40) != 40 ||
80 get_sha1_hex(buffer, next->old_sha1)) {
81 close(fd);
82 free(next);
83 continue;
84 }
85 close(fd);
86 strcpy(next->name, path->buf + name_offset);
87 (*tail)->next = next;
88 *tail = next;
89 }
90 }
91 strbuf_setlen(path, pathlen);
92
93 for (i = 0; i < list.nr; i++)
94 free(list.entries[i]);
95 free(list.entries);
96
97 return 0;
98}
99
100/* insert the packed refs for which no loose refs were found */
101
102static void insert_packed_refs(const char *packed_refs, struct ref **list)
103{
104 FILE *f = fopen(packed_refs, "r");
105 static char buffer[PATH_MAX];
106
107 if (!f)
108 return;
109
110 for (;;) {
47ec7943 111 int cmp = cmp, len;
cd547b48
JS
112
113 if (!fgets(buffer, sizeof(buffer), f)) {
114 fclose(f);
115 return;
116 }
117
118 if (hexval(buffer[0]) > 0xf)
119 continue;
120 len = strlen(buffer);
872c930d 121 if (len && buffer[len - 1] == '\n')
cd547b48
JS
122 buffer[--len] = '\0';
123 if (len < 41)
124 continue;
125 while ((*list)->next &&
126 (cmp = strcmp(buffer + 41,
127 (*list)->next->name)) > 0)
128 list = &(*list)->next;
129 if (!(*list)->next || cmp < 0) {
130 struct ref *next = alloc_ref(len - 40);
131 buffer[40] = '\0';
132 if (get_sha1_hex(buffer, next->old_sha1)) {
133 warning ("invalid SHA-1: %s", buffer);
134 free(next);
135 continue;
136 }
137 strcpy(next->name, buffer + 41);
138 next->next = (*list)->next;
139 (*list)->next = next;
140 list = &(*list)->next;
141 }
142 }
143}
144
4577370e 145static struct ref *get_refs_via_rsync(struct transport *transport)
cd547b48
JS
146{
147 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
148 struct ref dummy, *tail = &dummy;
149 struct child_process rsync;
150 const char *args[5];
151 int temp_dir_len;
152
153 /* copy the refs to the temporary directory */
154
155 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
156 if (!mkdtemp(temp_dir.buf))
157 die ("Could not make temporary directory");
158 temp_dir_len = temp_dir.len;
159
160 strbuf_addstr(&buf, transport->url);
161 strbuf_addstr(&buf, "/refs");
162
163 memset(&rsync, 0, sizeof(rsync));
164 rsync.argv = args;
165 rsync.stdout_to_stderr = 1;
166 args[0] = "rsync";
2b5a06ed 167 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
168 args[2] = buf.buf;
169 args[3] = temp_dir.buf;
170 args[4] = NULL;
171
172 if (run_command(&rsync))
173 die ("Could not run rsync to get refs");
174
175 strbuf_reset(&buf);
176 strbuf_addstr(&buf, transport->url);
177 strbuf_addstr(&buf, "/packed-refs");
178
179 args[2] = buf.buf;
180
181 if (run_command(&rsync))
182 die ("Could not run rsync to get refs");
183
184 /* read the copied refs */
185
186 strbuf_addstr(&temp_dir, "/refs");
187 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
188 strbuf_setlen(&temp_dir, temp_dir_len);
189
190 tail = &dummy;
191 strbuf_addstr(&temp_dir, "/packed-refs");
192 insert_packed_refs(temp_dir.buf, &tail);
193 strbuf_setlen(&temp_dir, temp_dir_len);
194
195 if (remove_dir_recursively(&temp_dir, 0))
196 warning ("Error removing temporary directory %s.",
197 temp_dir.buf);
198
199 strbuf_release(&buf);
200 strbuf_release(&temp_dir);
201
202 return dummy.next;
203}
204
205static int fetch_objs_via_rsync(struct transport *transport,
2d5c298f 206 int nr_objs, const struct ref **to_fetch)
cd547b48
JS
207{
208 struct strbuf buf = STRBUF_INIT;
209 struct child_process rsync;
210 const char *args[8];
211 int result;
212
213 strbuf_addstr(&buf, transport->url);
214 strbuf_addstr(&buf, "/objects/");
215
216 memset(&rsync, 0, sizeof(rsync));
217 rsync.argv = args;
218 rsync.stdout_to_stderr = 1;
219 args[0] = "rsync";
2b5a06ed 220 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
221 args[2] = "--ignore-existing";
222 args[3] = "--exclude";
223 args[4] = "info";
224 args[5] = buf.buf;
225 args[6] = get_object_directory();
226 args[7] = NULL;
227
228 /* NEEDSWORK: handle one level of alternates */
229 result = run_command(&rsync);
230
231 strbuf_release(&buf);
232
233 return result;
234}
235
236static int write_one_ref(const char *name, const unsigned char *sha1,
237 int flags, void *data)
238{
239 struct strbuf *buf = data;
240 int len = buf->len;
241 FILE *f;
242
243 /* when called via for_each_ref(), flags is non-zero */
244 if (flags && prefixcmp(name, "refs/heads/") &&
245 prefixcmp(name, "refs/tags/"))
246 return 0;
247
248 strbuf_addstr(buf, name);
249 if (safe_create_leading_directories(buf->buf) ||
250 !(f = fopen(buf->buf, "w")) ||
251 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
252 fclose(f))
253 return error("problems writing temporary file %s", buf->buf);
254 strbuf_setlen(buf, len);
255 return 0;
256}
257
258static int write_refs_to_temp_dir(struct strbuf *temp_dir,
259 int refspec_nr, const char **refspec)
260{
261 int i;
262
263 for (i = 0; i < refspec_nr; i++) {
264 unsigned char sha1[20];
265 char *ref;
266
267 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
268 return error("Could not get ref %s", refspec[i]);
269
270 if (write_one_ref(ref, sha1, 0, temp_dir)) {
271 free(ref);
272 return -1;
273 }
274 free(ref);
275 }
276 return 0;
277}
278
279static int rsync_transport_push(struct transport *transport,
280 int refspec_nr, const char **refspec, int flags)
281{
282 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
283 int result = 0, i;
284 struct child_process rsync;
ee020f35 285 const char *args[10];
cd547b48 286
94c89ba6
AW
287 if (flags & TRANSPORT_PUSH_MIRROR)
288 return error("rsync transport does not support mirror mode");
289
cd547b48
JS
290 /* first push the objects */
291
292 strbuf_addstr(&buf, transport->url);
293 strbuf_addch(&buf, '/');
294
295 memset(&rsync, 0, sizeof(rsync));
296 rsync.argv = args;
297 rsync.stdout_to_stderr = 1;
ee020f35
SP
298 i = 0;
299 args[i++] = "rsync";
300 args[i++] = "-a";
301 if (flags & TRANSPORT_PUSH_DRY_RUN)
302 args[i++] = "--dry-run";
303 if (transport->verbose > 0)
304 args[i++] = "-v";
305 args[i++] = "--ignore-existing";
306 args[i++] = "--exclude";
307 args[i++] = "info";
308 args[i++] = get_object_directory();
309 args[i++] = buf.buf;
310 args[i++] = NULL;
cd547b48
JS
311
312 if (run_command(&rsync))
313 return error("Could not push objects to %s", transport->url);
314
315 /* copy the refs to the temporary directory; they could be packed. */
316
317 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
318 if (!mkdtemp(temp_dir.buf))
319 die ("Could not make temporary directory");
320 strbuf_addch(&temp_dir, '/');
321
322 if (flags & TRANSPORT_PUSH_ALL) {
323 if (for_each_ref(write_one_ref, &temp_dir))
324 return -1;
325 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
326 return -1;
327
ee020f35
SP
328 i = 2;
329 if (flags & TRANSPORT_PUSH_DRY_RUN)
330 args[i++] = "--dry-run";
331 if (!(flags & TRANSPORT_PUSH_FORCE))
332 args[i++] = "--ignore-existing";
cd547b48
JS
333 args[i++] = temp_dir.buf;
334 args[i++] = transport->url;
335 args[i++] = NULL;
336 if (run_command(&rsync))
337 result = error("Could not push to %s", transport->url);
338
339 if (remove_dir_recursively(&temp_dir, 0))
340 warning ("Could not remove temporary directory %s.",
341 temp_dir.buf);
342
343 strbuf_release(&buf);
344 strbuf_release(&temp_dir);
345
346 return result;
347}
c29727d5
DB
348
349/* Generic functions for using commit walkers */
350
b9c506f7 351#ifndef NO_CURL /* http fetch is the only user */
1788c39c 352static int fetch_objs_via_walker(struct transport *transport,
2d5c298f 353 int nr_objs, const struct ref **to_fetch)
c29727d5
DB
354{
355 char *dest = xstrdup(transport->url);
356 struct walker *walker = transport->data;
425b1393
SP
357 char **objs = xmalloc(nr_objs * sizeof(*objs));
358 int i;
c29727d5
DB
359
360 walker->get_all = 1;
361 walker->get_tree = 1;
362 walker->get_history = 1;
2b5a06ed 363 walker->get_verbosely = transport->verbose >= 0;
c29727d5
DB
364 walker->get_recover = 0;
365
425b1393
SP
366 for (i = 0; i < nr_objs; i++)
367 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
368
b6abb48a 369 if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
c29727d5
DB
370 die("Fetch failed.");
371
425b1393
SP
372 for (i = 0; i < nr_objs; i++)
373 free(objs[i]);
374 free(objs);
c29727d5
DB
375 free(dest);
376 return 0;
377}
b9c506f7 378#endif /* NO_CURL */
c29727d5
DB
379
380static int disconnect_walker(struct transport *transport)
381{
382 struct walker *walker = transport->data;
383 if (walker)
384 walker_free(walker);
385 return 0;
386}
9b288516 387
ccfc02a3 388#ifndef NO_CURL
f3fa1838
JH
389static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
390{
9b288516
DB
391 const char **argv;
392 int argc;
393 int err;
394
94c89ba6
AW
395 if (flags & TRANSPORT_PUSH_MIRROR)
396 return error("http transport does not support mirror mode");
397
1b2486d7 398 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
9b288516
DB
399 argv[0] = "http-push";
400 argc = 1;
401 if (flags & TRANSPORT_PUSH_ALL)
402 argv[argc++] = "--all";
403 if (flags & TRANSPORT_PUSH_FORCE)
404 argv[argc++] = "--force";
fe5d1d3e
SP
405 if (flags & TRANSPORT_PUSH_DRY_RUN)
406 argv[argc++] = "--dry-run";
1b2486d7
SP
407 if (flags & TRANSPORT_PUSH_VERBOSE)
408 argv[argc++] = "--verbose";
9b288516
DB
409 argv[argc++] = transport->url;
410 while (refspec_nr--)
411 argv[argc++] = *refspec++;
412 argv[argc] = NULL;
413 err = run_command_v_opt(argv, RUN_GIT_CMD);
414 switch (err) {
415 case -ERR_RUN_COMMAND_FORK:
416 error("unable to fork for %s", argv[0]);
417 case -ERR_RUN_COMMAND_EXEC:
418 error("unable to exec %s", argv[0]);
419 break;
420 case -ERR_RUN_COMMAND_WAITPID:
421 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
422 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
423 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
424 error("%s died with strange error", argv[0]);
425 }
426 return !!err;
427}
428
4577370e 429static struct ref *get_refs_via_curl(struct transport *transport)
c29727d5 430{
028c2976 431 struct strbuf buffer = STRBUF_INIT;
c29727d5
DB
432 char *data, *start, *mid;
433 char *ref_name;
434 char *refs_url;
435 int i = 0;
436
437 struct active_request_slot *slot;
438 struct slot_results results;
439
440 struct ref *refs = NULL;
441 struct ref *ref = NULL;
442 struct ref *last_ref = NULL;
443
be885d96
DB
444 struct walker *walker;
445
9fd9279b 446 if (!transport->data)
9fc6440d
MH
447 transport->data = get_http_walker(transport->url,
448 transport->remote);
9fd9279b 449
be885d96
DB
450 walker = transport->data;
451
c29727d5
DB
452 refs_url = xmalloc(strlen(transport->url) + 11);
453 sprintf(refs_url, "%s/info/refs", transport->url);
454
c29727d5
DB
455 slot = get_active_slot();
456 slot->results = &results;
457 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
458 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
459 curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
460 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
14c98218 461
c29727d5
DB
462 if (start_active_slot(slot)) {
463 run_active_slot(slot);
464 if (results.curl_result != CURLE_OK) {
028c2976 465 strbuf_release(&buffer);
c29727d5 466 if (missing_target(&results)) {
c29727d5
DB
467 return NULL;
468 } else {
c29727d5
DB
469 error("%s", curl_errorstr);
470 return NULL;
471 }
472 }
473 } else {
028c2976 474 strbuf_release(&buffer);
c29727d5
DB
475 error("Unable to start request");
476 return NULL;
477 }
478
028c2976 479 data = buffer.buf;
c29727d5
DB
480 start = NULL;
481 mid = data;
028c2976 482 while (i < buffer.len) {
c29727d5
DB
483 if (!start)
484 start = &data[i];
485 if (data[i] == '\t')
486 mid = &data[i];
487 if (data[i] == '\n') {
488 data[i] = 0;
489 ref_name = mid + 1;
490 ref = xmalloc(sizeof(struct ref) +
491 strlen(ref_name) + 1);
492 memset(ref, 0, sizeof(struct ref));
493 strcpy(ref->name, ref_name);
494 get_sha1_hex(start, ref->old_sha1);
495 if (!refs)
496 refs = ref;
497 if (last_ref)
498 last_ref->next = ref;
499 last_ref = ref;
500 start = NULL;
501 }
502 i++;
503 }
504
028c2976 505 strbuf_release(&buffer);
c29727d5 506
737922aa 507 ref = alloc_ref_from_str("HEAD");
be885d96
DB
508 if (!walker->fetch_ref(walker, ref) &&
509 !resolve_remote_symref(ref, refs)) {
510 ref->next = refs;
511 refs = ref;
512 } else {
513 free(ref);
514 }
515
c29727d5
DB
516 return refs;
517}
518
e5f4e214 519static int fetch_objs_via_curl(struct transport *transport,
2d5c298f 520 int nr_objs, const struct ref **to_fetch)
e5f4e214
SP
521{
522 if (!transport->data)
9fc6440d
MH
523 transport->data = get_http_walker(transport->url,
524 transport->remote);
e5f4e214
SP
525 return fetch_objs_via_walker(transport, nr_objs, to_fetch);
526}
527
c29727d5
DB
528#endif
529
c7a8a162
JS
530struct bundle_transport_data {
531 int fd;
532 struct bundle_header header;
533};
534
4577370e 535static struct ref *get_refs_from_bundle(struct transport *transport)
c7a8a162
JS
536{
537 struct bundle_transport_data *data = transport->data;
538 struct ref *result = NULL;
539 int i;
540
541 if (data->fd > 0)
542 close(data->fd);
543 data->fd = read_bundle_header(transport->url, &data->header);
544 if (data->fd < 0)
545 die ("Could not read bundle '%s'.", transport->url);
546 for (i = 0; i < data->header.references.nr; i++) {
547 struct ref_list_entry *e = data->header.references.list + i;
737922aa 548 struct ref *ref = alloc_ref_from_str(e->name);
c7a8a162 549 hashcpy(ref->old_sha1, e->sha1);
c7a8a162
JS
550 ref->next = result;
551 result = ref;
552 }
553 return result;
554}
555
1788c39c 556static int fetch_refs_from_bundle(struct transport *transport,
2d5c298f 557 int nr_heads, const struct ref **to_fetch)
c7a8a162
JS
558{
559 struct bundle_transport_data *data = transport->data;
560 return unbundle(&data->header, data->fd);
561}
562
563static int close_bundle(struct transport *transport)
564{
565 struct bundle_transport_data *data = transport->data;
566 if (data->fd > 0)
567 close(data->fd);
f4e95765 568 free(data);
c7a8a162
JS
569 return 0;
570}
571
9b288516
DB
572struct git_transport_data {
573 unsigned thin : 1;
c29727d5 574 unsigned keep : 1;
41fa7d2e 575 unsigned followtags : 1;
c29727d5 576 int depth;
ba227857
DB
577 struct child_process *conn;
578 int fd[2];
c29727d5 579 const char *uploadpack;
9b288516
DB
580 const char *receivepack;
581};
582
583static int set_git_option(struct transport *connection,
584 const char *name, const char *value)
585{
586 struct git_transport_data *data = connection->data;
c29727d5
DB
587 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
588 data->uploadpack = value;
589 return 0;
590 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
9b288516
DB
591 data->receivepack = value;
592 return 0;
593 } else if (!strcmp(name, TRANS_OPT_THIN)) {
594 data->thin = !!value;
595 return 0;
41fa7d2e
SP
596 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
597 data->followtags = !!value;
598 return 0;
c29727d5
DB
599 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
600 data->keep = !!value;
601 return 0;
c29727d5
DB
602 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
603 if (!value)
604 data->depth = 0;
605 else
606 data->depth = atoi(value);
607 return 0;
9b288516
DB
608 }
609 return 1;
610}
611
ba227857
DB
612static int connect_setup(struct transport *transport)
613{
614 struct git_transport_data *data = transport->data;
615 data->conn = git_connect(data->fd, transport->url, data->uploadpack, 0);
616 return 0;
617}
618
4577370e 619static struct ref *get_refs_via_connect(struct transport *transport)
c29727d5
DB
620{
621 struct git_transport_data *data = transport->data;
622 struct ref *refs;
c29727d5 623
ba227857
DB
624 connect_setup(transport);
625 get_remote_heads(data->fd[0], &refs, 0, NULL, 0);
c29727d5
DB
626
627 return refs;
628}
629
1788c39c 630static int fetch_refs_via_pack(struct transport *transport,
2d5c298f 631 int nr_heads, const struct ref **to_fetch)
c29727d5
DB
632{
633 struct git_transport_data *data = transport->data;
425b1393 634 char **heads = xmalloc(nr_heads * sizeof(*heads));
e4cd6c7a 635 char **origh = xmalloc(nr_heads * sizeof(*origh));
ba227857 636 const struct ref *refs;
c29727d5
DB
637 char *dest = xstrdup(transport->url);
638 struct fetch_pack_args args;
425b1393 639 int i;
00183cbb 640 struct ref *refs_tmp = NULL;
c29727d5 641
fa740529 642 memset(&args, 0, sizeof(args));
c29727d5 643 args.uploadpack = data->uploadpack;
c29727d5 644 args.keep_pack = data->keep;
fa740529 645 args.lock_pack = 1;
c29727d5 646 args.use_thin_pack = data->thin;
41fa7d2e 647 args.include_tag = data->followtags;
2b5a06ed 648 args.verbose = transport->verbose > 0;
c29727d5 649 args.depth = data->depth;
c29727d5 650
425b1393 651 for (i = 0; i < nr_heads; i++)
e4cd6c7a 652 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
ba227857 653
ba227857 654 if (!data->conn) {
ba227857
DB
655 connect_setup(transport);
656 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0);
ba227857
DB
657 }
658
00183cbb
DB
659 refs = fetch_pack(&args, data->fd, data->conn,
660 refs_tmp ? refs_tmp : transport->remote_refs,
ba227857
DB
661 dest, nr_heads, heads, &transport->pack_lockfile);
662 close(data->fd[0]);
663 close(data->fd[1]);
664 if (finish_connect(data->conn))
665 refs = NULL;
666 data->conn = NULL;
c29727d5 667
00183cbb
DB
668 free_refs(refs_tmp);
669
425b1393 670 for (i = 0; i < nr_heads; i++)
e4cd6c7a
SP
671 free(origh[i]);
672 free(origh);
673 free(heads);
c29727d5 674 free(dest);
c6bc4005 675 return (refs ? 0 : -1);
c29727d5
DB
676}
677
f3fa1838
JH
678static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
679{
9b288516 680 struct git_transport_data *data = transport->data;
40cb4fab 681 struct send_pack_args args;
9b288516 682
40cb4fab
DB
683 args.receivepack = data->receivepack;
684 args.send_all = !!(flags & TRANSPORT_PUSH_ALL);
94c89ba6 685 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab
DB
686 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
687 args.use_thin_pack = data->thin;
a108e538 688 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
40cb4fab
DB
689 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
690
691 return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec);
9b288516
DB
692}
693
f4e95765
SP
694static int disconnect_git(struct transport *transport)
695{
ba227857
DB
696 struct git_transport_data *data = transport->data;
697 if (data->conn) {
698 packet_flush(data->fd[1]);
699 close(data->fd[0]);
700 close(data->fd[1]);
701 finish_connect(data->conn);
702 }
703
704 free(data);
f4e95765
SP
705 return 0;
706}
707
9b288516
DB
708static int is_local(const char *url)
709{
710 const char *colon = strchr(url, ':');
711 const char *slash = strchr(url, '/');
712 return !colon || (slash && slash < colon);
713}
714
715static int is_file(const char *url)
716{
717 struct stat buf;
718 if (stat(url, &buf))
719 return 0;
720 return S_ISREG(buf.st_mode);
721}
722
e5f4e214 723struct transport *transport_get(struct remote *remote, const char *url)
9b288516 724{
8eb554ae
SP
725 struct transport *ret = xcalloc(1, sizeof(*ret));
726
727 ret->remote = remote;
728 ret->url = url;
8eb554ae 729
9b288516 730 if (!prefixcmp(url, "rsync://")) {
cd547b48
JS
731 ret->get_refs_list = get_refs_via_rsync;
732 ret->fetch = fetch_objs_via_rsync;
733 ret->push = rsync_transport_push;
824d5776 734
8eb554ae
SP
735 } else if (!prefixcmp(url, "http://")
736 || !prefixcmp(url, "https://")
737 || !prefixcmp(url, "ftp://")) {
ccfc02a3
JS
738#ifdef NO_CURL
739 error("git was compiled without libcurl support.");
740#else
824d5776
SP
741 ret->get_refs_list = get_refs_via_curl;
742 ret->fetch = fetch_objs_via_curl;
743 ret->push = curl_transport_push;
ccfc02a3 744#endif
824d5776
SP
745 ret->disconnect = disconnect_walker;
746
9b288516 747 } else if (is_local(url) && is_file(url)) {
c7a8a162 748 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
c7a8a162 749 ret->data = data;
824d5776
SP
750 ret->get_refs_list = get_refs_from_bundle;
751 ret->fetch = fetch_refs_from_bundle;
752 ret->disconnect = close_bundle;
753
9b288516
DB
754 } else {
755 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 756 ret->data = data;
824d5776
SP
757 ret->set_option = set_git_option;
758 ret->get_refs_list = get_refs_via_connect;
759 ret->fetch = fetch_refs_via_pack;
760 ret->push = git_transport_push;
f4e95765 761 ret->disconnect = disconnect_git;
824d5776 762
9b288516 763 data->thin = 1;
ba227857 764 data->conn = NULL;
c29727d5
DB
765 data->uploadpack = "git-upload-pack";
766 if (remote && remote->uploadpack)
767 data->uploadpack = remote->uploadpack;
9b288516
DB
768 data->receivepack = "git-receive-pack";
769 if (remote && remote->receivepack)
770 data->receivepack = remote->receivepack;
9b288516 771 }
8eb554ae 772
9b288516
DB
773 return ret;
774}
775
776int transport_set_option(struct transport *transport,
777 const char *name, const char *value)
778{
824d5776
SP
779 if (transport->set_option)
780 return transport->set_option(transport, name, value);
ab865e6e 781 return 1;
9b288516
DB
782}
783
784int transport_push(struct transport *transport,
785 int refspec_nr, const char **refspec, int flags)
786{
824d5776 787 if (!transport->push)
9b288516 788 return 1;
824d5776 789 return transport->push(transport, refspec_nr, refspec, flags);
9b288516
DB
790}
791
4577370e 792const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5
DB
793{
794 if (!transport->remote_refs)
824d5776 795 transport->remote_refs = transport->get_refs_list(transport);
c29727d5
DB
796 return transport->remote_refs;
797}
798
2d5c298f 799int transport_fetch_refs(struct transport *transport, const struct ref *refs)
c29727d5 800{
425b1393 801 int rc;
7a2bff45 802 int nr_heads = 0, nr_alloc = 0;
2d5c298f
DB
803 const struct ref **heads = NULL;
804 const struct ref *rm;
c29727d5
DB
805
806 for (rm = refs; rm; rm = rm->next) {
807 if (rm->peer_ref &&
808 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
809 continue;
7a2bff45 810 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 811 heads[nr_heads++] = rm;
c29727d5
DB
812 }
813
824d5776 814 rc = transport->fetch(transport, nr_heads, heads);
c29727d5 815 free(heads);
425b1393 816 return rc;
c29727d5
DB
817}
818
1788c39c
SP
819void transport_unlock_pack(struct transport *transport)
820{
821 if (transport->pack_lockfile) {
822 unlink(transport->pack_lockfile);
823 free(transport->pack_lockfile);
824 transport->pack_lockfile = NULL;
825 }
826}
827
9b288516
DB
828int transport_disconnect(struct transport *transport)
829{
830 int ret = 0;
824d5776
SP
831 if (transport->disconnect)
832 ret = transport->disconnect(transport);
9b288516
DB
833 free(transport);
834 return ret;
835}