rsync transport: allow local paths, and fix tests
[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
7efaeba2
JS
141static const char *rsync_url(const char *url)
142{
143 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
144}
145
4577370e 146static struct ref *get_refs_via_rsync(struct transport *transport)
cd547b48
JS
147{
148 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
149 struct ref dummy, *tail = &dummy;
150 struct child_process rsync;
151 const char *args[5];
152 int temp_dir_len;
153
154 /* copy the refs to the temporary directory */
155
156 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
157 if (!mkdtemp(temp_dir.buf))
158 die ("Could not make temporary directory");
159 temp_dir_len = temp_dir.len;
160
7efaeba2 161 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
162 strbuf_addstr(&buf, "/refs");
163
164 memset(&rsync, 0, sizeof(rsync));
165 rsync.argv = args;
166 rsync.stdout_to_stderr = 1;
167 args[0] = "rsync";
2b5a06ed 168 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
169 args[2] = buf.buf;
170 args[3] = temp_dir.buf;
171 args[4] = NULL;
172
173 if (run_command(&rsync))
174 die ("Could not run rsync to get refs");
175
176 strbuf_reset(&buf);
7efaeba2 177 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
178 strbuf_addstr(&buf, "/packed-refs");
179
180 args[2] = buf.buf;
181
182 if (run_command(&rsync))
183 die ("Could not run rsync to get refs");
184
185 /* read the copied refs */
186
187 strbuf_addstr(&temp_dir, "/refs");
188 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
189 strbuf_setlen(&temp_dir, temp_dir_len);
190
191 tail = &dummy;
192 strbuf_addstr(&temp_dir, "/packed-refs");
193 insert_packed_refs(temp_dir.buf, &tail);
194 strbuf_setlen(&temp_dir, temp_dir_len);
195
196 if (remove_dir_recursively(&temp_dir, 0))
197 warning ("Error removing temporary directory %s.",
198 temp_dir.buf);
199
200 strbuf_release(&buf);
201 strbuf_release(&temp_dir);
202
203 return dummy.next;
204}
205
206static int fetch_objs_via_rsync(struct transport *transport,
2d5c298f 207 int nr_objs, const struct ref **to_fetch)
cd547b48
JS
208{
209 struct strbuf buf = STRBUF_INIT;
210 struct child_process rsync;
211 const char *args[8];
212 int result;
213
7efaeba2 214 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
215 strbuf_addstr(&buf, "/objects/");
216
217 memset(&rsync, 0, sizeof(rsync));
218 rsync.argv = args;
219 rsync.stdout_to_stderr = 1;
220 args[0] = "rsync";
2b5a06ed 221 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
cd547b48
JS
222 args[2] = "--ignore-existing";
223 args[3] = "--exclude";
224 args[4] = "info";
225 args[5] = buf.buf;
226 args[6] = get_object_directory();
227 args[7] = NULL;
228
229 /* NEEDSWORK: handle one level of alternates */
230 result = run_command(&rsync);
231
232 strbuf_release(&buf);
233
234 return result;
235}
236
237static int write_one_ref(const char *name, const unsigned char *sha1,
238 int flags, void *data)
239{
240 struct strbuf *buf = data;
241 int len = buf->len;
242 FILE *f;
243
244 /* when called via for_each_ref(), flags is non-zero */
245 if (flags && prefixcmp(name, "refs/heads/") &&
246 prefixcmp(name, "refs/tags/"))
247 return 0;
248
249 strbuf_addstr(buf, name);
250 if (safe_create_leading_directories(buf->buf) ||
251 !(f = fopen(buf->buf, "w")) ||
252 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
253 fclose(f))
254 return error("problems writing temporary file %s", buf->buf);
255 strbuf_setlen(buf, len);
256 return 0;
257}
258
259static int write_refs_to_temp_dir(struct strbuf *temp_dir,
260 int refspec_nr, const char **refspec)
261{
262 int i;
263
264 for (i = 0; i < refspec_nr; i++) {
265 unsigned char sha1[20];
266 char *ref;
267
268 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
269 return error("Could not get ref %s", refspec[i]);
270
271 if (write_one_ref(ref, sha1, 0, temp_dir)) {
272 free(ref);
273 return -1;
274 }
275 free(ref);
276 }
277 return 0;
278}
279
280static int rsync_transport_push(struct transport *transport,
281 int refspec_nr, const char **refspec, int flags)
282{
283 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
284 int result = 0, i;
285 struct child_process rsync;
ee020f35 286 const char *args[10];
cd547b48 287
94c89ba6
AW
288 if (flags & TRANSPORT_PUSH_MIRROR)
289 return error("rsync transport does not support mirror mode");
290
cd547b48
JS
291 /* first push the objects */
292
7efaeba2 293 strbuf_addstr(&buf, rsync_url(transport->url));
cd547b48
JS
294 strbuf_addch(&buf, '/');
295
296 memset(&rsync, 0, sizeof(rsync));
297 rsync.argv = args;
298 rsync.stdout_to_stderr = 1;
ee020f35
SP
299 i = 0;
300 args[i++] = "rsync";
301 args[i++] = "-a";
302 if (flags & TRANSPORT_PUSH_DRY_RUN)
303 args[i++] = "--dry-run";
304 if (transport->verbose > 0)
305 args[i++] = "-v";
306 args[i++] = "--ignore-existing";
307 args[i++] = "--exclude";
308 args[i++] = "info";
309 args[i++] = get_object_directory();
310 args[i++] = buf.buf;
311 args[i++] = NULL;
cd547b48
JS
312
313 if (run_command(&rsync))
7efaeba2
JS
314 return error("Could not push objects to %s",
315 rsync_url(transport->url));
cd547b48
JS
316
317 /* copy the refs to the temporary directory; they could be packed. */
318
319 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
320 if (!mkdtemp(temp_dir.buf))
321 die ("Could not make temporary directory");
322 strbuf_addch(&temp_dir, '/');
323
324 if (flags & TRANSPORT_PUSH_ALL) {
325 if (for_each_ref(write_one_ref, &temp_dir))
326 return -1;
327 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
328 return -1;
329
ee020f35
SP
330 i = 2;
331 if (flags & TRANSPORT_PUSH_DRY_RUN)
332 args[i++] = "--dry-run";
333 if (!(flags & TRANSPORT_PUSH_FORCE))
334 args[i++] = "--ignore-existing";
cd547b48 335 args[i++] = temp_dir.buf;
7efaeba2 336 args[i++] = rsync_url(transport->url);
cd547b48
JS
337 args[i++] = NULL;
338 if (run_command(&rsync))
7efaeba2
JS
339 result = error("Could not push to %s",
340 rsync_url(transport->url));
cd547b48
JS
341
342 if (remove_dir_recursively(&temp_dir, 0))
343 warning ("Could not remove temporary directory %s.",
344 temp_dir.buf);
345
346 strbuf_release(&buf);
347 strbuf_release(&temp_dir);
348
349 return result;
350}
c29727d5
DB
351
352/* Generic functions for using commit walkers */
353
b9c506f7 354#ifndef NO_CURL /* http fetch is the only user */
1788c39c 355static int fetch_objs_via_walker(struct transport *transport,
2d5c298f 356 int nr_objs, const struct ref **to_fetch)
c29727d5
DB
357{
358 char *dest = xstrdup(transport->url);
359 struct walker *walker = transport->data;
425b1393
SP
360 char **objs = xmalloc(nr_objs * sizeof(*objs));
361 int i;
c29727d5
DB
362
363 walker->get_all = 1;
364 walker->get_tree = 1;
365 walker->get_history = 1;
2b5a06ed 366 walker->get_verbosely = transport->verbose >= 0;
c29727d5
DB
367 walker->get_recover = 0;
368
425b1393
SP
369 for (i = 0; i < nr_objs; i++)
370 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
371
b6abb48a 372 if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
c29727d5
DB
373 die("Fetch failed.");
374
425b1393
SP
375 for (i = 0; i < nr_objs; i++)
376 free(objs[i]);
377 free(objs);
c29727d5
DB
378 free(dest);
379 return 0;
380}
b9c506f7 381#endif /* NO_CURL */
c29727d5
DB
382
383static int disconnect_walker(struct transport *transport)
384{
385 struct walker *walker = transport->data;
386 if (walker)
387 walker_free(walker);
388 return 0;
389}
9b288516 390
ccfc02a3 391#ifndef NO_CURL
f3fa1838
JH
392static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
393{
9b288516
DB
394 const char **argv;
395 int argc;
396 int err;
397
94c89ba6
AW
398 if (flags & TRANSPORT_PUSH_MIRROR)
399 return error("http transport does not support mirror mode");
400
1b2486d7 401 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
9b288516
DB
402 argv[0] = "http-push";
403 argc = 1;
404 if (flags & TRANSPORT_PUSH_ALL)
405 argv[argc++] = "--all";
406 if (flags & TRANSPORT_PUSH_FORCE)
407 argv[argc++] = "--force";
fe5d1d3e
SP
408 if (flags & TRANSPORT_PUSH_DRY_RUN)
409 argv[argc++] = "--dry-run";
1b2486d7
SP
410 if (flags & TRANSPORT_PUSH_VERBOSE)
411 argv[argc++] = "--verbose";
9b288516
DB
412 argv[argc++] = transport->url;
413 while (refspec_nr--)
414 argv[argc++] = *refspec++;
415 argv[argc] = NULL;
416 err = run_command_v_opt(argv, RUN_GIT_CMD);
417 switch (err) {
418 case -ERR_RUN_COMMAND_FORK:
419 error("unable to fork for %s", argv[0]);
420 case -ERR_RUN_COMMAND_EXEC:
421 error("unable to exec %s", argv[0]);
422 break;
423 case -ERR_RUN_COMMAND_WAITPID:
424 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
425 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
426 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
427 error("%s died with strange error", argv[0]);
428 }
429 return !!err;
430}
431
4577370e 432static struct ref *get_refs_via_curl(struct transport *transport)
c29727d5 433{
028c2976 434 struct strbuf buffer = STRBUF_INIT;
c29727d5
DB
435 char *data, *start, *mid;
436 char *ref_name;
437 char *refs_url;
438 int i = 0;
439
440 struct active_request_slot *slot;
441 struct slot_results results;
442
443 struct ref *refs = NULL;
444 struct ref *ref = NULL;
445 struct ref *last_ref = NULL;
446
be885d96
DB
447 struct walker *walker;
448
9fd9279b 449 if (!transport->data)
9fc6440d
MH
450 transport->data = get_http_walker(transport->url,
451 transport->remote);
9fd9279b 452
be885d96
DB
453 walker = transport->data;
454
c29727d5
DB
455 refs_url = xmalloc(strlen(transport->url) + 11);
456 sprintf(refs_url, "%s/info/refs", transport->url);
457
c29727d5
DB
458 slot = get_active_slot();
459 slot->results = &results;
460 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
461 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
462 curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
463 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
14c98218 464
c29727d5
DB
465 if (start_active_slot(slot)) {
466 run_active_slot(slot);
467 if (results.curl_result != CURLE_OK) {
028c2976 468 strbuf_release(&buffer);
fd35e426
PB
469 if (missing_target(&results))
470 die("%s not found: did you run git update-server-info on the server?", refs_url);
471 else
472 die("%s download error - %s", refs_url, curl_errorstr);
c29727d5
DB
473 }
474 } else {
028c2976 475 strbuf_release(&buffer);
fd35e426 476 die("Unable to start HTTP request");
c29727d5
DB
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
59c69c0c 507 ref = alloc_ref("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;
59c69c0c 548 struct ref *ref = alloc_ref(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 624 connect_setup(transport);
40c155ff 625 get_remote_heads(data->fd[0], &refs, 0, NULL, 0, NULL);
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;
9b373229 648 args.verbose = (transport->verbose > 0);
fe8aa148 649 args.quiet = (transport->verbose < 0);
21188b1e 650 args.no_progress = args.quiet || (!transport->progress && !isatty(1));
c29727d5 651 args.depth = data->depth;
c29727d5 652
425b1393 653 for (i = 0; i < nr_heads; i++)
e4cd6c7a 654 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
ba227857 655
ba227857 656 if (!data->conn) {
ba227857 657 connect_setup(transport);
40c155ff 658 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
ba227857
DB
659 }
660
00183cbb
DB
661 refs = fetch_pack(&args, data->fd, data->conn,
662 refs_tmp ? refs_tmp : transport->remote_refs,
ba227857
DB
663 dest, nr_heads, heads, &transport->pack_lockfile);
664 close(data->fd[0]);
665 close(data->fd[1]);
666 if (finish_connect(data->conn))
667 refs = NULL;
668 data->conn = NULL;
c29727d5 669
00183cbb
DB
670 free_refs(refs_tmp);
671
425b1393 672 for (i = 0; i < nr_heads; i++)
e4cd6c7a
SP
673 free(origh[i]);
674 free(origh);
675 free(heads);
c29727d5 676 free(dest);
c6bc4005 677 return (refs ? 0 : -1);
c29727d5
DB
678}
679
f3fa1838
JH
680static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
681{
9b288516 682 struct git_transport_data *data = transport->data;
40cb4fab 683 struct send_pack_args args;
9b288516 684
40cb4fab
DB
685 args.receivepack = data->receivepack;
686 args.send_all = !!(flags & TRANSPORT_PUSH_ALL);
94c89ba6 687 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
40cb4fab
DB
688 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
689 args.use_thin_pack = data->thin;
a108e538 690 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
40cb4fab
DB
691 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
692
693 return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec);
9b288516
DB
694}
695
f4e95765
SP
696static int disconnect_git(struct transport *transport)
697{
ba227857
DB
698 struct git_transport_data *data = transport->data;
699 if (data->conn) {
700 packet_flush(data->fd[1]);
701 close(data->fd[0]);
702 close(data->fd[1]);
703 finish_connect(data->conn);
704 }
705
706 free(data);
f4e95765
SP
707 return 0;
708}
709
9b288516
DB
710static int is_local(const char *url)
711{
712 const char *colon = strchr(url, ':');
713 const char *slash = strchr(url, '/');
be501813
JS
714 return !colon || (slash && slash < colon) ||
715 has_dos_drive_prefix(url);
9b288516
DB
716}
717
718static int is_file(const char *url)
719{
720 struct stat buf;
721 if (stat(url, &buf))
722 return 0;
723 return S_ISREG(buf.st_mode);
724}
725
e5f4e214 726struct transport *transport_get(struct remote *remote, const char *url)
9b288516 727{
8eb554ae
SP
728 struct transport *ret = xcalloc(1, sizeof(*ret));
729
730 ret->remote = remote;
731 ret->url = url;
8eb554ae 732
7efaeba2 733 if (!prefixcmp(url, "rsync:")) {
cd547b48
JS
734 ret->get_refs_list = get_refs_via_rsync;
735 ret->fetch = fetch_objs_via_rsync;
736 ret->push = rsync_transport_push;
824d5776 737
8eb554ae
SP
738 } else if (!prefixcmp(url, "http://")
739 || !prefixcmp(url, "https://")
740 || !prefixcmp(url, "ftp://")) {
ccfc02a3
JS
741#ifdef NO_CURL
742 error("git was compiled without libcurl support.");
743#else
824d5776
SP
744 ret->get_refs_list = get_refs_via_curl;
745 ret->fetch = fetch_objs_via_curl;
746 ret->push = curl_transport_push;
ccfc02a3 747#endif
824d5776
SP
748 ret->disconnect = disconnect_walker;
749
9b288516 750 } else if (is_local(url) && is_file(url)) {
c7a8a162 751 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
c7a8a162 752 ret->data = data;
824d5776
SP
753 ret->get_refs_list = get_refs_from_bundle;
754 ret->fetch = fetch_refs_from_bundle;
755 ret->disconnect = close_bundle;
756
9b288516
DB
757 } else {
758 struct git_transport_data *data = xcalloc(1, sizeof(*data));
9b288516 759 ret->data = data;
824d5776
SP
760 ret->set_option = set_git_option;
761 ret->get_refs_list = get_refs_via_connect;
762 ret->fetch = fetch_refs_via_pack;
763 ret->push = git_transport_push;
f4e95765 764 ret->disconnect = disconnect_git;
824d5776 765
9b288516 766 data->thin = 1;
ba227857 767 data->conn = NULL;
c29727d5
DB
768 data->uploadpack = "git-upload-pack";
769 if (remote && remote->uploadpack)
770 data->uploadpack = remote->uploadpack;
9b288516
DB
771 data->receivepack = "git-receive-pack";
772 if (remote && remote->receivepack)
773 data->receivepack = remote->receivepack;
9b288516 774 }
8eb554ae 775
9b288516
DB
776 return ret;
777}
778
779int transport_set_option(struct transport *transport,
780 const char *name, const char *value)
781{
824d5776
SP
782 if (transport->set_option)
783 return transport->set_option(transport, name, value);
ab865e6e 784 return 1;
9b288516
DB
785}
786
787int transport_push(struct transport *transport,
788 int refspec_nr, const char **refspec, int flags)
789{
824d5776 790 if (!transport->push)
9b288516 791 return 1;
824d5776 792 return transport->push(transport, refspec_nr, refspec, flags);
9b288516
DB
793}
794
4577370e 795const struct ref *transport_get_remote_refs(struct transport *transport)
c29727d5
DB
796{
797 if (!transport->remote_refs)
824d5776 798 transport->remote_refs = transport->get_refs_list(transport);
c29727d5
DB
799 return transport->remote_refs;
800}
801
2d5c298f 802int transport_fetch_refs(struct transport *transport, const struct ref *refs)
c29727d5 803{
425b1393 804 int rc;
7a2bff45 805 int nr_heads = 0, nr_alloc = 0;
2d5c298f
DB
806 const struct ref **heads = NULL;
807 const struct ref *rm;
c29727d5
DB
808
809 for (rm = refs; rm; rm = rm->next) {
810 if (rm->peer_ref &&
811 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
812 continue;
7a2bff45 813 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
425b1393 814 heads[nr_heads++] = rm;
c29727d5
DB
815 }
816
824d5776 817 rc = transport->fetch(transport, nr_heads, heads);
c29727d5 818 free(heads);
425b1393 819 return rc;
c29727d5
DB
820}
821
1788c39c
SP
822void transport_unlock_pack(struct transport *transport)
823{
824 if (transport->pack_lockfile) {
825 unlink(transport->pack_lockfile);
826 free(transport->pack_lockfile);
827 transport->pack_lockfile = NULL;
828 }
829}
830
9b288516
DB
831int transport_disconnect(struct transport *transport)
832{
833 int ret = 0;
824d5776
SP
834 if (transport->disconnect)
835 ret = transport->disconnect(transport);
9b288516
DB
836 free(transport);
837 return ret;
838}