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