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