git-http-fetch: not a builtin
[git/dscho.git] / transport.c
blob9935c85cf337ef7e99bee38c8e49684df8dd6b3a
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
5 #include "fetch-pack.h"
6 #include "send-pack.h"
7 #include "walker.h"
8 #include "bundle.h"
9 #include "dir.h"
10 #include "refs.h"
12 /* rsync support */
15 * We copy packed-refs and refs/ into a temporary file, then read the
16 * loose refs recursively (sorting whenever possible), and then inserting
17 * those packed refs that are not yet in the list (not validating, but
18 * assuming that the file is sorted).
20 * Appears refactoring this from refs.c is too cumbersome.
23 static int str_cmp(const void *a, const void *b)
25 const char *s1 = a;
26 const char *s2 = b;
28 return strcmp(s1, s2);
31 /* path->buf + name_offset is expected to point to "refs/" */
33 static int read_loose_refs(struct strbuf *path, int name_offset,
34 struct ref **tail)
36 DIR *dir = opendir(path->buf);
37 struct dirent *de;
38 struct {
39 char **entries;
40 int nr, alloc;
41 } list;
42 int i, pathlen;
44 if (!dir)
45 return -1;
47 memset (&list, 0, sizeof(list));
49 while ((de = readdir(dir))) {
50 if (is_dot_or_dotdot(de->d_name))
51 continue;
52 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
53 list.entries[list.nr++] = xstrdup(de->d_name);
55 closedir(dir);
57 /* sort the list */
59 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
61 pathlen = path->len;
62 strbuf_addch(path, '/');
64 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
65 strbuf_addstr(path, list.entries[i]);
66 if (read_loose_refs(path, name_offset, tail)) {
67 int fd = open(path->buf, O_RDONLY);
68 char buffer[40];
69 struct ref *next;
71 if (fd < 0)
72 continue;
73 next = alloc_ref(path->buf + name_offset);
74 if (read_in_full(fd, buffer, 40) != 40 ||
75 get_sha1_hex(buffer, next->old_sha1)) {
76 close(fd);
77 free(next);
78 continue;
80 close(fd);
81 (*tail)->next = next;
82 *tail = next;
85 strbuf_setlen(path, pathlen);
87 for (i = 0; i < list.nr; i++)
88 free(list.entries[i]);
89 free(list.entries);
91 return 0;
94 /* insert the packed refs for which no loose refs were found */
96 static void insert_packed_refs(const char *packed_refs, struct ref **list)
98 FILE *f = fopen(packed_refs, "r");
99 static char buffer[PATH_MAX];
101 if (!f)
102 return;
104 for (;;) {
105 int cmp = cmp, len;
107 if (!fgets(buffer, sizeof(buffer), f)) {
108 fclose(f);
109 return;
112 if (hexval(buffer[0]) > 0xf)
113 continue;
114 len = strlen(buffer);
115 if (len && buffer[len - 1] == '\n')
116 buffer[--len] = '\0';
117 if (len < 41)
118 continue;
119 while ((*list)->next &&
120 (cmp = strcmp(buffer + 41,
121 (*list)->next->name)) > 0)
122 list = &(*list)->next;
123 if (!(*list)->next || cmp < 0) {
124 struct ref *next = alloc_ref(buffer + 41);
125 buffer[40] = '\0';
126 if (get_sha1_hex(buffer, next->old_sha1)) {
127 warning ("invalid SHA-1: %s", buffer);
128 free(next);
129 continue;
131 next->next = (*list)->next;
132 (*list)->next = next;
133 list = &(*list)->next;
138 static const char *rsync_url(const char *url)
140 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
143 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
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;
151 if (for_push)
152 return NULL;
154 /* copy the refs to the temporary directory */
156 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
157 if (!mkdtemp(temp_dir.buf))
158 die_errno ("Could not make temporary directory");
159 temp_dir_len = temp_dir.len;
161 strbuf_addstr(&buf, rsync_url(transport->url));
162 strbuf_addstr(&buf, "/refs");
164 memset(&rsync, 0, sizeof(rsync));
165 rsync.argv = args;
166 rsync.stdout_to_stderr = 1;
167 args[0] = "rsync";
168 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
169 args[2] = buf.buf;
170 args[3] = temp_dir.buf;
171 args[4] = NULL;
173 if (run_command(&rsync))
174 die ("Could not run rsync to get refs");
176 strbuf_reset(&buf);
177 strbuf_addstr(&buf, rsync_url(transport->url));
178 strbuf_addstr(&buf, "/packed-refs");
180 args[2] = buf.buf;
182 if (run_command(&rsync))
183 die ("Could not run rsync to get refs");
185 /* read the copied refs */
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);
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);
196 if (remove_dir_recursively(&temp_dir, 0))
197 warning ("Error removing temporary directory %s.",
198 temp_dir.buf);
200 strbuf_release(&buf);
201 strbuf_release(&temp_dir);
203 return dummy.next;
206 static int fetch_objs_via_rsync(struct transport *transport,
207 int nr_objs, const struct ref **to_fetch)
209 struct strbuf buf = STRBUF_INIT;
210 struct child_process rsync;
211 const char *args[8];
212 int result;
214 strbuf_addstr(&buf, rsync_url(transport->url));
215 strbuf_addstr(&buf, "/objects/");
217 memset(&rsync, 0, sizeof(rsync));
218 rsync.argv = args;
219 rsync.stdout_to_stderr = 1;
220 args[0] = "rsync";
221 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
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;
229 /* NEEDSWORK: handle one level of alternates */
230 result = run_command(&rsync);
232 strbuf_release(&buf);
234 return result;
237 static int write_one_ref(const char *name, const unsigned char *sha1,
238 int flags, void *data)
240 struct strbuf *buf = data;
241 int len = buf->len;
242 FILE *f;
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;
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;
259 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
260 int refspec_nr, const char **refspec)
262 int i;
264 for (i = 0; i < refspec_nr; i++) {
265 unsigned char sha1[20];
266 char *ref;
268 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
269 return error("Could not get ref %s", refspec[i]);
271 if (write_one_ref(ref, sha1, 0, temp_dir)) {
272 free(ref);
273 return -1;
275 free(ref);
277 return 0;
280 static int rsync_transport_push(struct transport *transport,
281 int refspec_nr, const char **refspec, int flags)
283 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
284 int result = 0, i;
285 struct child_process rsync;
286 const char *args[10];
288 if (flags & TRANSPORT_PUSH_MIRROR)
289 return error("rsync transport does not support mirror mode");
291 /* first push the objects */
293 strbuf_addstr(&buf, rsync_url(transport->url));
294 strbuf_addch(&buf, '/');
296 memset(&rsync, 0, sizeof(rsync));
297 rsync.argv = args;
298 rsync.stdout_to_stderr = 1;
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;
313 if (run_command(&rsync))
314 return error("Could not push objects to %s",
315 rsync_url(transport->url));
317 /* copy the refs to the temporary directory; they could be packed. */
319 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
320 if (!mkdtemp(temp_dir.buf))
321 die_errno ("Could not make temporary directory");
322 strbuf_addch(&temp_dir, '/');
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;
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";
335 args[i++] = temp_dir.buf;
336 args[i++] = rsync_url(transport->url);
337 args[i++] = NULL;
338 if (run_command(&rsync))
339 result = error("Could not push to %s",
340 rsync_url(transport->url));
342 if (remove_dir_recursively(&temp_dir, 0))
343 warning ("Could not remove temporary directory %s.",
344 temp_dir.buf);
346 strbuf_release(&buf);
347 strbuf_release(&temp_dir);
349 return result;
352 #ifndef NO_CURL
353 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
355 const char **argv;
356 int argc;
357 int err;
359 if (flags & TRANSPORT_PUSH_MIRROR)
360 return error("http transport does not support mirror mode");
362 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
363 argv[0] = "http-push";
364 argc = 1;
365 if (flags & TRANSPORT_PUSH_ALL)
366 argv[argc++] = "--all";
367 if (flags & TRANSPORT_PUSH_FORCE)
368 argv[argc++] = "--force";
369 if (flags & TRANSPORT_PUSH_DRY_RUN)
370 argv[argc++] = "--dry-run";
371 if (flags & TRANSPORT_PUSH_VERBOSE)
372 argv[argc++] = "--verbose";
373 argv[argc++] = transport->url;
374 while (refspec_nr--)
375 argv[argc++] = *refspec++;
376 argv[argc] = NULL;
377 err = run_command_v_opt(argv, RUN_GIT_CMD);
378 switch (err) {
379 case -ERR_RUN_COMMAND_FORK:
380 error("unable to fork for %s", argv[0]);
381 case -ERR_RUN_COMMAND_EXEC:
382 error("unable to exec %s", argv[0]);
383 break;
384 case -ERR_RUN_COMMAND_WAITPID:
385 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
386 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
387 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
388 error("%s died with strange error", argv[0]);
390 return !!err;
393 #endif
395 struct bundle_transport_data {
396 int fd;
397 struct bundle_header header;
400 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
402 struct bundle_transport_data *data = transport->data;
403 struct ref *result = NULL;
404 int i;
406 if (for_push)
407 return NULL;
409 if (data->fd > 0)
410 close(data->fd);
411 data->fd = read_bundle_header(transport->url, &data->header);
412 if (data->fd < 0)
413 die ("Could not read bundle '%s'.", transport->url);
414 for (i = 0; i < data->header.references.nr; i++) {
415 struct ref_list_entry *e = data->header.references.list + i;
416 struct ref *ref = alloc_ref(e->name);
417 hashcpy(ref->old_sha1, e->sha1);
418 ref->next = result;
419 result = ref;
421 return result;
424 static int fetch_refs_from_bundle(struct transport *transport,
425 int nr_heads, const struct ref **to_fetch)
427 struct bundle_transport_data *data = transport->data;
428 return unbundle(&data->header, data->fd);
431 static int close_bundle(struct transport *transport)
433 struct bundle_transport_data *data = transport->data;
434 if (data->fd > 0)
435 close(data->fd);
436 free(data);
437 return 0;
440 struct git_transport_data {
441 unsigned thin : 1;
442 unsigned keep : 1;
443 unsigned followtags : 1;
444 int depth;
445 struct child_process *conn;
446 int fd[2];
447 const char *uploadpack;
448 const char *receivepack;
449 struct extra_have_objects extra_have;
452 static int set_git_option(struct transport *connection,
453 const char *name, const char *value)
455 struct git_transport_data *data = connection->data;
456 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
457 data->uploadpack = value;
458 return 0;
459 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
460 data->receivepack = value;
461 return 0;
462 } else if (!strcmp(name, TRANS_OPT_THIN)) {
463 data->thin = !!value;
464 return 0;
465 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
466 data->followtags = !!value;
467 return 0;
468 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
469 data->keep = !!value;
470 return 0;
471 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
472 if (!value)
473 data->depth = 0;
474 else
475 data->depth = atoi(value);
476 return 0;
478 return 1;
481 static int connect_setup(struct transport *transport, int for_push, int verbose)
483 struct git_transport_data *data = transport->data;
484 data->conn = git_connect(data->fd, transport->url,
485 for_push ? data->receivepack : data->uploadpack,
486 verbose ? CONNECT_VERBOSE : 0);
487 return 0;
490 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
492 struct git_transport_data *data = transport->data;
493 struct ref *refs;
495 connect_setup(transport, for_push, 0);
496 get_remote_heads(data->fd[0], &refs, 0, NULL,
497 for_push ? REF_NORMAL : 0, &data->extra_have);
499 return refs;
502 static int fetch_refs_via_pack(struct transport *transport,
503 int nr_heads, const struct ref **to_fetch)
505 struct git_transport_data *data = transport->data;
506 char **heads = xmalloc(nr_heads * sizeof(*heads));
507 char **origh = xmalloc(nr_heads * sizeof(*origh));
508 const struct ref *refs;
509 char *dest = xstrdup(transport->url);
510 struct fetch_pack_args args;
511 int i;
512 struct ref *refs_tmp = NULL;
514 memset(&args, 0, sizeof(args));
515 args.uploadpack = data->uploadpack;
516 args.keep_pack = data->keep;
517 args.lock_pack = 1;
518 args.use_thin_pack = data->thin;
519 args.include_tag = data->followtags;
520 args.verbose = (transport->verbose > 0);
521 args.quiet = (transport->verbose < 0);
522 args.no_progress = args.quiet || (!transport->progress && !isatty(1));
523 args.depth = data->depth;
525 for (i = 0; i < nr_heads; i++)
526 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
528 if (!data->conn) {
529 connect_setup(transport, 0, 0);
530 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
533 refs = fetch_pack(&args, data->fd, data->conn,
534 refs_tmp ? refs_tmp : transport->remote_refs,
535 dest, nr_heads, heads, &transport->pack_lockfile);
536 close(data->fd[0]);
537 close(data->fd[1]);
538 if (finish_connect(data->conn))
539 refs = NULL;
540 data->conn = NULL;
542 free_refs(refs_tmp);
544 for (i = 0; i < nr_heads; i++)
545 free(origh[i]);
546 free(origh);
547 free(heads);
548 free(dest);
549 return (refs ? 0 : -1);
552 static int refs_pushed(struct ref *ref)
554 for (; ref; ref = ref->next) {
555 switch(ref->status) {
556 case REF_STATUS_NONE:
557 case REF_STATUS_UPTODATE:
558 break;
559 default:
560 return 1;
563 return 0;
566 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
568 struct refspec rs;
570 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
571 return;
573 rs.src = ref->name;
574 rs.dst = NULL;
576 if (!remote_find_tracking(remote, &rs)) {
577 if (verbose)
578 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
579 if (ref->deletion) {
580 delete_ref(rs.dst, NULL, 0);
581 } else
582 update_ref("update by push", rs.dst,
583 ref->new_sha1, NULL, 0, 0);
584 free(rs.dst);
588 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
590 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
592 if (porcelain) {
593 if (from)
594 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
595 else
596 fprintf(stdout, "%c\t:%s\t", flag, to->name);
597 if (msg)
598 fprintf(stdout, "%s (%s)\n", summary, msg);
599 else
600 fprintf(stdout, "%s\n", summary);
601 } else {
602 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
603 if (from)
604 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
605 else
606 fputs(prettify_refname(to->name), stderr);
607 if (msg) {
608 fputs(" (", stderr);
609 fputs(msg, stderr);
610 fputc(')', stderr);
612 fputc('\n', stderr);
616 static const char *status_abbrev(unsigned char sha1[20])
618 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
621 static void print_ok_ref_status(struct ref *ref, int porcelain)
623 if (ref->deletion)
624 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
625 else if (is_null_sha1(ref->old_sha1))
626 print_ref_status('*',
627 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
628 "[new branch]"),
629 ref, ref->peer_ref, NULL, porcelain);
630 else {
631 char quickref[84];
632 char type;
633 const char *msg;
635 strcpy(quickref, status_abbrev(ref->old_sha1));
636 if (ref->nonfastforward) {
637 strcat(quickref, "...");
638 type = '+';
639 msg = "forced update";
640 } else {
641 strcat(quickref, "..");
642 type = ' ';
643 msg = NULL;
645 strcat(quickref, status_abbrev(ref->new_sha1));
647 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
651 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
653 if (!count)
654 fprintf(stderr, "To %s\n", dest);
656 switch(ref->status) {
657 case REF_STATUS_NONE:
658 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
659 break;
660 case REF_STATUS_REJECT_NODELETE:
661 print_ref_status('!', "[rejected]", ref, NULL,
662 "remote does not support deleting refs", porcelain);
663 break;
664 case REF_STATUS_UPTODATE:
665 print_ref_status('=', "[up to date]", ref,
666 ref->peer_ref, NULL, porcelain);
667 break;
668 case REF_STATUS_REJECT_NONFASTFORWARD:
669 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
670 "non-fast forward", porcelain);
671 break;
672 case REF_STATUS_REMOTE_REJECT:
673 print_ref_status('!', "[remote rejected]", ref,
674 ref->deletion ? NULL : ref->peer_ref,
675 ref->remote_status, porcelain);
676 break;
677 case REF_STATUS_EXPECTING_REPORT:
678 print_ref_status('!', "[remote failure]", ref,
679 ref->deletion ? NULL : ref->peer_ref,
680 "remote failed to report status", porcelain);
681 break;
682 case REF_STATUS_OK:
683 print_ok_ref_status(ref, porcelain);
684 break;
687 return 1;
690 static void print_push_status(const char *dest, struct ref *refs,
691 int verbose, int porcelain)
693 struct ref *ref;
694 int n = 0;
696 if (verbose) {
697 for (ref = refs; ref; ref = ref->next)
698 if (ref->status == REF_STATUS_UPTODATE)
699 n += print_one_push_status(ref, dest, n, porcelain);
702 for (ref = refs; ref; ref = ref->next)
703 if (ref->status == REF_STATUS_OK)
704 n += print_one_push_status(ref, dest, n, porcelain);
706 for (ref = refs; ref; ref = ref->next) {
707 if (ref->status != REF_STATUS_NONE &&
708 ref->status != REF_STATUS_UPTODATE &&
709 ref->status != REF_STATUS_OK)
710 n += print_one_push_status(ref, dest, n, porcelain);
714 static void verify_remote_names(int nr_heads, const char **heads)
716 int i;
718 for (i = 0; i < nr_heads; i++) {
719 const char *local = heads[i];
720 const char *remote = strrchr(heads[i], ':');
722 if (*local == '+')
723 local++;
725 /* A matching refspec is okay. */
726 if (remote == local && remote[1] == '\0')
727 continue;
729 remote = remote ? (remote + 1) : local;
730 switch (check_ref_format(remote)) {
731 case 0: /* ok */
732 case CHECK_REF_FORMAT_ONELEVEL:
733 /* ok but a single level -- that is fine for
734 * a match pattern.
736 case CHECK_REF_FORMAT_WILDCARD:
737 /* ok but ends with a pattern-match character */
738 continue;
740 die("remote part of refspec is not a valid name in %s",
741 heads[i]);
745 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
747 struct git_transport_data *data = transport->data;
748 struct send_pack_args args;
749 int ret;
751 if (!data->conn) {
752 struct ref *tmp_refs;
753 connect_setup(transport, 1, 0);
755 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
756 NULL);
759 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
760 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
761 args.use_thin_pack = data->thin;
762 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
763 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
765 ret = send_pack(&args, data->fd, data->conn, remote_refs,
766 &data->extra_have);
768 close(data->fd[1]);
769 close(data->fd[0]);
770 ret |= finish_connect(data->conn);
771 data->conn = NULL;
773 return ret;
776 static int disconnect_git(struct transport *transport)
778 struct git_transport_data *data = transport->data;
779 if (data->conn) {
780 packet_flush(data->fd[1]);
781 close(data->fd[0]);
782 close(data->fd[1]);
783 finish_connect(data->conn);
786 free(data);
787 return 0;
790 static int is_local(const char *url)
792 const char *colon = strchr(url, ':');
793 const char *slash = strchr(url, '/');
794 return !colon || (slash && slash < colon) ||
795 has_dos_drive_prefix(url);
798 static int is_file(const char *url)
800 struct stat buf;
801 if (stat(url, &buf))
802 return 0;
803 return S_ISREG(buf.st_mode);
806 struct transport *transport_get(struct remote *remote, const char *url)
808 struct transport *ret = xcalloc(1, sizeof(*ret));
810 ret->remote = remote;
811 ret->url = url;
813 if (!prefixcmp(url, "rsync:")) {
814 ret->get_refs_list = get_refs_via_rsync;
815 ret->fetch = fetch_objs_via_rsync;
816 ret->push = rsync_transport_push;
818 } else if (!prefixcmp(url, "http://")
819 || !prefixcmp(url, "https://")
820 || !prefixcmp(url, "ftp://")) {
821 transport_helper_init(ret);
822 #ifdef NO_CURL
823 error("git was compiled without libcurl support.");
824 #else
825 ret->push = curl_transport_push;
826 #endif
828 } else if (is_local(url) && is_file(url)) {
829 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
830 ret->data = data;
831 ret->get_refs_list = get_refs_from_bundle;
832 ret->fetch = fetch_refs_from_bundle;
833 ret->disconnect = close_bundle;
835 } else {
836 struct git_transport_data *data = xcalloc(1, sizeof(*data));
837 ret->data = data;
838 ret->set_option = set_git_option;
839 ret->get_refs_list = get_refs_via_connect;
840 ret->fetch = fetch_refs_via_pack;
841 ret->push_refs = git_transport_push;
842 ret->disconnect = disconnect_git;
844 data->thin = 1;
845 data->conn = NULL;
846 data->uploadpack = "git-upload-pack";
847 if (remote && remote->uploadpack)
848 data->uploadpack = remote->uploadpack;
849 data->receivepack = "git-receive-pack";
850 if (remote && remote->receivepack)
851 data->receivepack = remote->receivepack;
854 return ret;
857 int transport_set_option(struct transport *transport,
858 const char *name, const char *value)
860 if (transport->set_option)
861 return transport->set_option(transport, name, value);
862 return 1;
865 int transport_push(struct transport *transport,
866 int refspec_nr, const char **refspec, int flags)
868 verify_remote_names(refspec_nr, refspec);
870 if (transport->push)
871 return transport->push(transport, refspec_nr, refspec, flags);
872 if (transport->push_refs) {
873 struct ref *remote_refs =
874 transport->get_refs_list(transport, 1);
875 struct ref *local_refs = get_local_heads();
876 int match_flags = MATCH_REFS_NONE;
877 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
878 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
879 int ret;
881 if (flags & TRANSPORT_PUSH_ALL)
882 match_flags |= MATCH_REFS_ALL;
883 if (flags & TRANSPORT_PUSH_MIRROR)
884 match_flags |= MATCH_REFS_MIRROR;
886 if (match_refs(local_refs, &remote_refs,
887 refspec_nr, refspec, match_flags)) {
888 return -1;
891 ret = transport->push_refs(transport, remote_refs, flags);
893 print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain);
895 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
896 struct ref *ref;
897 for (ref = remote_refs; ref; ref = ref->next)
898 update_tracking_ref(transport->remote, ref, verbose);
901 if (!ret && !refs_pushed(remote_refs))
902 fprintf(stderr, "Everything up-to-date\n");
903 return ret;
905 return 1;
908 const struct ref *transport_get_remote_refs(struct transport *transport)
910 if (!transport->remote_refs)
911 transport->remote_refs = transport->get_refs_list(transport, 0);
912 return transport->remote_refs;
915 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
917 int rc;
918 int nr_heads = 0, nr_alloc = 0;
919 const struct ref **heads = NULL;
920 const struct ref *rm;
922 for (rm = refs; rm; rm = rm->next) {
923 if (rm->peer_ref &&
924 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
925 continue;
926 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
927 heads[nr_heads++] = rm;
930 rc = transport->fetch(transport, nr_heads, heads);
931 free(heads);
932 return rc;
935 void transport_unlock_pack(struct transport *transport)
937 if (transport->pack_lockfile) {
938 unlink_or_warn(transport->pack_lockfile);
939 free(transport->pack_lockfile);
940 transport->pack_lockfile = NULL;
944 int transport_disconnect(struct transport *transport)
946 int ret = 0;
947 if (transport->disconnect)
948 ret = transport->disconnect(transport);
949 free(transport);
950 return ret;
954 * Strip username (and password) from an url and return
955 * it in a newly allocated string.
957 char *transport_anonymize_url(const char *url)
959 char *anon_url, *scheme_prefix, *anon_part;
960 size_t anon_len, prefix_len = 0;
962 anon_part = strchr(url, '@');
963 if (is_local(url) || !anon_part)
964 goto literal_copy;
966 anon_len = strlen(++anon_part);
967 scheme_prefix = strstr(url, "://");
968 if (!scheme_prefix) {
969 if (!strchr(anon_part, ':'))
970 /* cannot be "me@there:/path/name" */
971 goto literal_copy;
972 } else {
973 const char *cp;
974 /* make sure scheme is reasonable */
975 for (cp = url; cp < scheme_prefix; cp++) {
976 switch (*cp) {
977 /* RFC 1738 2.1 */
978 case '+': case '.': case '-':
979 break; /* ok */
980 default:
981 if (isalnum(*cp))
982 break;
983 /* it isn't */
984 goto literal_copy;
987 /* @ past the first slash does not count */
988 cp = strchr(scheme_prefix + 3, '/');
989 if (cp && cp < anon_part)
990 goto literal_copy;
991 prefix_len = scheme_prefix - url + 3;
993 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
994 memcpy(anon_url, url, prefix_len);
995 memcpy(anon_url + prefix_len, anon_part, anon_len);
996 return anon_url;
997 literal_copy:
998 return xstrdup(url);