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