Merge branch 'np/maint-limit-delta-cache' into next
[git/dscho.git] / transport.c
blobe62f7e9561ce84d2504796d46e65ecc9589d47ab
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;
358 if (flags & TRANSPORT_PUSH_MIRROR)
359 return error("http transport does not support mirror mode");
361 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
362 argv[0] = "http-push";
363 argc = 1;
364 if (flags & TRANSPORT_PUSH_ALL)
365 argv[argc++] = "--all";
366 if (flags & TRANSPORT_PUSH_FORCE)
367 argv[argc++] = "--force";
368 if (flags & TRANSPORT_PUSH_DRY_RUN)
369 argv[argc++] = "--dry-run";
370 if (flags & TRANSPORT_PUSH_VERBOSE)
371 argv[argc++] = "--verbose";
372 argv[argc++] = transport->url;
373 while (refspec_nr--)
374 argv[argc++] = *refspec++;
375 argv[argc] = NULL;
376 return !!run_command_v_opt(argv, RUN_GIT_CMD);
379 #endif
381 struct bundle_transport_data {
382 int fd;
383 struct bundle_header header;
386 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
388 struct bundle_transport_data *data = transport->data;
389 struct ref *result = NULL;
390 int i;
392 if (for_push)
393 return NULL;
395 if (data->fd > 0)
396 close(data->fd);
397 data->fd = read_bundle_header(transport->url, &data->header);
398 if (data->fd < 0)
399 die ("Could not read bundle '%s'.", transport->url);
400 for (i = 0; i < data->header.references.nr; i++) {
401 struct ref_list_entry *e = data->header.references.list + i;
402 struct ref *ref = alloc_ref(e->name);
403 hashcpy(ref->old_sha1, e->sha1);
404 ref->next = result;
405 result = ref;
407 return result;
410 static int fetch_refs_from_bundle(struct transport *transport,
411 int nr_heads, const struct ref **to_fetch)
413 struct bundle_transport_data *data = transport->data;
414 return unbundle(&data->header, data->fd);
417 static int close_bundle(struct transport *transport)
419 struct bundle_transport_data *data = transport->data;
420 if (data->fd > 0)
421 close(data->fd);
422 free(data);
423 return 0;
426 struct git_transport_data {
427 unsigned thin : 1;
428 unsigned keep : 1;
429 unsigned followtags : 1;
430 int depth;
431 struct child_process *conn;
432 int fd[2];
433 const char *uploadpack;
434 const char *receivepack;
435 struct extra_have_objects extra_have;
438 static int set_git_option(struct transport *connection,
439 const char *name, const char *value)
441 struct git_transport_data *data = connection->data;
442 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
443 data->uploadpack = value;
444 return 0;
445 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
446 data->receivepack = value;
447 return 0;
448 } else if (!strcmp(name, TRANS_OPT_THIN)) {
449 data->thin = !!value;
450 return 0;
451 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
452 data->followtags = !!value;
453 return 0;
454 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
455 data->keep = !!value;
456 return 0;
457 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
458 if (!value)
459 data->depth = 0;
460 else
461 data->depth = atoi(value);
462 return 0;
464 return 1;
467 static int connect_setup(struct transport *transport, int for_push, int verbose)
469 struct git_transport_data *data = transport->data;
470 data->conn = git_connect(data->fd, transport->url,
471 for_push ? data->receivepack : data->uploadpack,
472 verbose ? CONNECT_VERBOSE : 0);
473 return 0;
476 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
478 struct git_transport_data *data = transport->data;
479 struct ref *refs;
481 connect_setup(transport, for_push, 0);
482 get_remote_heads(data->fd[0], &refs, 0, NULL,
483 for_push ? REF_NORMAL : 0, &data->extra_have);
485 return refs;
488 static int fetch_refs_via_pack(struct transport *transport,
489 int nr_heads, const struct ref **to_fetch)
491 struct git_transport_data *data = transport->data;
492 char **heads = xmalloc(nr_heads * sizeof(*heads));
493 char **origh = xmalloc(nr_heads * sizeof(*origh));
494 const struct ref *refs;
495 char *dest = xstrdup(transport->url);
496 struct fetch_pack_args args;
497 int i;
498 struct ref *refs_tmp = NULL;
500 memset(&args, 0, sizeof(args));
501 args.uploadpack = data->uploadpack;
502 args.keep_pack = data->keep;
503 args.lock_pack = 1;
504 args.use_thin_pack = data->thin;
505 args.include_tag = data->followtags;
506 args.verbose = (transport->verbose > 0);
507 args.quiet = (transport->verbose < 0);
508 args.no_progress = args.quiet || (!transport->progress && !isatty(1));
509 args.depth = data->depth;
511 for (i = 0; i < nr_heads; i++)
512 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
514 if (!data->conn) {
515 connect_setup(transport, 0, 0);
516 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
519 refs = fetch_pack(&args, data->fd, data->conn,
520 refs_tmp ? refs_tmp : transport->remote_refs,
521 dest, nr_heads, heads, &transport->pack_lockfile);
522 close(data->fd[0]);
523 close(data->fd[1]);
524 if (finish_connect(data->conn))
525 refs = NULL;
526 data->conn = NULL;
528 free_refs(refs_tmp);
530 for (i = 0; i < nr_heads; i++)
531 free(origh[i]);
532 free(origh);
533 free(heads);
534 free(dest);
535 return (refs ? 0 : -1);
538 static int refs_pushed(struct ref *ref)
540 for (; ref; ref = ref->next) {
541 switch(ref->status) {
542 case REF_STATUS_NONE:
543 case REF_STATUS_UPTODATE:
544 break;
545 default:
546 return 1;
549 return 0;
552 static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
554 struct refspec rs;
556 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
557 return;
559 rs.src = ref->name;
560 rs.dst = NULL;
562 if (!remote_find_tracking(remote, &rs)) {
563 if (verbose)
564 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
565 if (ref->deletion) {
566 delete_ref(rs.dst, NULL, 0);
567 } else
568 update_ref("update by push", rs.dst,
569 ref->new_sha1, NULL, 0, 0);
570 free(rs.dst);
574 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
576 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
578 if (porcelain) {
579 if (from)
580 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
581 else
582 fprintf(stdout, "%c\t:%s\t", flag, to->name);
583 if (msg)
584 fprintf(stdout, "%s (%s)\n", summary, msg);
585 else
586 fprintf(stdout, "%s\n", summary);
587 } else {
588 fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary);
589 if (from)
590 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
591 else
592 fputs(prettify_refname(to->name), stderr);
593 if (msg) {
594 fputs(" (", stderr);
595 fputs(msg, stderr);
596 fputc(')', stderr);
598 fputc('\n', stderr);
602 static const char *status_abbrev(unsigned char sha1[20])
604 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
607 static void print_ok_ref_status(struct ref *ref, int porcelain)
609 if (ref->deletion)
610 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
611 else if (is_null_sha1(ref->old_sha1))
612 print_ref_status('*',
613 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
614 "[new branch]"),
615 ref, ref->peer_ref, NULL, porcelain);
616 else {
617 char quickref[84];
618 char type;
619 const char *msg;
621 strcpy(quickref, status_abbrev(ref->old_sha1));
622 if (ref->nonfastforward) {
623 strcat(quickref, "...");
624 type = '+';
625 msg = "forced update";
626 } else {
627 strcat(quickref, "..");
628 type = ' ';
629 msg = NULL;
631 strcat(quickref, status_abbrev(ref->new_sha1));
633 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
637 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
639 if (!count)
640 fprintf(stderr, "To %s\n", dest);
642 switch(ref->status) {
643 case REF_STATUS_NONE:
644 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
645 break;
646 case REF_STATUS_REJECT_NODELETE:
647 print_ref_status('!', "[rejected]", ref, NULL,
648 "remote does not support deleting refs", porcelain);
649 break;
650 case REF_STATUS_UPTODATE:
651 print_ref_status('=', "[up to date]", ref,
652 ref->peer_ref, NULL, porcelain);
653 break;
654 case REF_STATUS_REJECT_NONFASTFORWARD:
655 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
656 "non-fast forward", porcelain);
657 break;
658 case REF_STATUS_REMOTE_REJECT:
659 print_ref_status('!', "[remote rejected]", ref,
660 ref->deletion ? NULL : ref->peer_ref,
661 ref->remote_status, porcelain);
662 break;
663 case REF_STATUS_EXPECTING_REPORT:
664 print_ref_status('!', "[remote failure]", ref,
665 ref->deletion ? NULL : ref->peer_ref,
666 "remote failed to report status", porcelain);
667 break;
668 case REF_STATUS_OK:
669 print_ok_ref_status(ref, porcelain);
670 break;
673 return 1;
676 static void print_push_status(const char *dest, struct ref *refs,
677 int verbose, int porcelain)
679 struct ref *ref;
680 int n = 0;
682 if (verbose) {
683 for (ref = refs; ref; ref = ref->next)
684 if (ref->status == REF_STATUS_UPTODATE)
685 n += print_one_push_status(ref, dest, n, porcelain);
688 for (ref = refs; ref; ref = ref->next)
689 if (ref->status == REF_STATUS_OK)
690 n += print_one_push_status(ref, dest, n, porcelain);
692 for (ref = refs; ref; ref = ref->next) {
693 if (ref->status != REF_STATUS_NONE &&
694 ref->status != REF_STATUS_UPTODATE &&
695 ref->status != REF_STATUS_OK)
696 n += print_one_push_status(ref, dest, n, porcelain);
700 static void verify_remote_names(int nr_heads, const char **heads)
702 int i;
704 for (i = 0; i < nr_heads; i++) {
705 const char *local = heads[i];
706 const char *remote = strrchr(heads[i], ':');
708 if (*local == '+')
709 local++;
711 /* A matching refspec is okay. */
712 if (remote == local && remote[1] == '\0')
713 continue;
715 remote = remote ? (remote + 1) : local;
716 switch (check_ref_format(remote)) {
717 case 0: /* ok */
718 case CHECK_REF_FORMAT_ONELEVEL:
719 /* ok but a single level -- that is fine for
720 * a match pattern.
722 case CHECK_REF_FORMAT_WILDCARD:
723 /* ok but ends with a pattern-match character */
724 continue;
726 die("remote part of refspec is not a valid name in %s",
727 heads[i]);
731 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
733 struct git_transport_data *data = transport->data;
734 struct send_pack_args args;
735 int ret;
737 if (!data->conn) {
738 struct ref *tmp_refs;
739 connect_setup(transport, 1, 0);
741 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
742 NULL);
745 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
746 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
747 args.use_thin_pack = data->thin;
748 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
749 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
751 ret = send_pack(&args, data->fd, data->conn, remote_refs,
752 &data->extra_have);
754 close(data->fd[1]);
755 close(data->fd[0]);
756 ret |= finish_connect(data->conn);
757 data->conn = NULL;
759 return ret;
762 static int disconnect_git(struct transport *transport)
764 struct git_transport_data *data = transport->data;
765 if (data->conn) {
766 packet_flush(data->fd[1]);
767 close(data->fd[0]);
768 close(data->fd[1]);
769 finish_connect(data->conn);
772 free(data);
773 return 0;
776 static int is_local(const char *url)
778 const char *colon = strchr(url, ':');
779 const char *slash = strchr(url, '/');
780 return !colon || (slash && slash < colon) ||
781 has_dos_drive_prefix(url);
784 static int is_file(const char *url)
786 struct stat buf;
787 if (stat(url, &buf))
788 return 0;
789 return S_ISREG(buf.st_mode);
792 struct transport *transport_get(struct remote *remote, const char *url)
794 struct transport *ret = xcalloc(1, sizeof(*ret));
796 ret->remote = remote;
797 ret->url = url;
799 if (!prefixcmp(url, "rsync:")) {
800 ret->get_refs_list = get_refs_via_rsync;
801 ret->fetch = fetch_objs_via_rsync;
802 ret->push = rsync_transport_push;
804 } else if (!prefixcmp(url, "http://")
805 || !prefixcmp(url, "https://")
806 || !prefixcmp(url, "ftp://")) {
807 transport_helper_init(ret);
808 #ifdef NO_CURL
809 error("git was compiled without libcurl support.");
810 #else
811 ret->push = curl_transport_push;
812 #endif
814 } else if (is_local(url) && is_file(url)) {
815 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
816 ret->data = data;
817 ret->get_refs_list = get_refs_from_bundle;
818 ret->fetch = fetch_refs_from_bundle;
819 ret->disconnect = close_bundle;
821 } else {
822 struct git_transport_data *data = xcalloc(1, sizeof(*data));
823 ret->data = data;
824 ret->set_option = set_git_option;
825 ret->get_refs_list = get_refs_via_connect;
826 ret->fetch = fetch_refs_via_pack;
827 ret->push_refs = git_transport_push;
828 ret->disconnect = disconnect_git;
830 data->thin = 1;
831 data->conn = NULL;
832 data->uploadpack = "git-upload-pack";
833 if (remote && remote->uploadpack)
834 data->uploadpack = remote->uploadpack;
835 data->receivepack = "git-receive-pack";
836 if (remote && remote->receivepack)
837 data->receivepack = remote->receivepack;
840 return ret;
843 int transport_set_option(struct transport *transport,
844 const char *name, const char *value)
846 if (transport->set_option)
847 return transport->set_option(transport, name, value);
848 return 1;
851 int transport_push(struct transport *transport,
852 int refspec_nr, const char **refspec, int flags)
854 verify_remote_names(refspec_nr, refspec);
856 if (transport->push)
857 return transport->push(transport, refspec_nr, refspec, flags);
858 if (transport->push_refs) {
859 struct ref *remote_refs =
860 transport->get_refs_list(transport, 1);
861 struct ref *local_refs = get_local_heads();
862 int match_flags = MATCH_REFS_NONE;
863 int verbose = flags & TRANSPORT_PUSH_VERBOSE;
864 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
865 int ret;
867 if (flags & TRANSPORT_PUSH_ALL)
868 match_flags |= MATCH_REFS_ALL;
869 if (flags & TRANSPORT_PUSH_MIRROR)
870 match_flags |= MATCH_REFS_MIRROR;
872 if (match_refs(local_refs, &remote_refs,
873 refspec_nr, refspec, match_flags)) {
874 return -1;
877 ret = transport->push_refs(transport, remote_refs, flags);
879 print_push_status(transport->url, remote_refs, verbose | porcelain, porcelain);
881 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
882 struct ref *ref;
883 for (ref = remote_refs; ref; ref = ref->next)
884 update_tracking_ref(transport->remote, ref, verbose);
887 if (!ret && !refs_pushed(remote_refs))
888 fprintf(stderr, "Everything up-to-date\n");
889 return ret;
891 return 1;
894 const struct ref *transport_get_remote_refs(struct transport *transport)
896 if (!transport->remote_refs)
897 transport->remote_refs = transport->get_refs_list(transport, 0);
898 return transport->remote_refs;
901 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
903 int rc;
904 int nr_heads = 0, nr_alloc = 0;
905 const struct ref **heads = NULL;
906 const struct ref *rm;
908 for (rm = refs; rm; rm = rm->next) {
909 if (rm->peer_ref &&
910 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
911 continue;
912 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
913 heads[nr_heads++] = rm;
916 rc = transport->fetch(transport, nr_heads, heads);
917 free(heads);
918 return rc;
921 void transport_unlock_pack(struct transport *transport)
923 if (transport->pack_lockfile) {
924 unlink_or_warn(transport->pack_lockfile);
925 free(transport->pack_lockfile);
926 transport->pack_lockfile = NULL;
930 int transport_disconnect(struct transport *transport)
932 int ret = 0;
933 if (transport->disconnect)
934 ret = transport->disconnect(transport);
935 free(transport);
936 return ret;
940 * Strip username (and password) from an url and return
941 * it in a newly allocated string.
943 char *transport_anonymize_url(const char *url)
945 char *anon_url, *scheme_prefix, *anon_part;
946 size_t anon_len, prefix_len = 0;
948 anon_part = strchr(url, '@');
949 if (is_local(url) || !anon_part)
950 goto literal_copy;
952 anon_len = strlen(++anon_part);
953 scheme_prefix = strstr(url, "://");
954 if (!scheme_prefix) {
955 if (!strchr(anon_part, ':'))
956 /* cannot be "me@there:/path/name" */
957 goto literal_copy;
958 } else {
959 const char *cp;
960 /* make sure scheme is reasonable */
961 for (cp = url; cp < scheme_prefix; cp++) {
962 switch (*cp) {
963 /* RFC 1738 2.1 */
964 case '+': case '.': case '-':
965 break; /* ok */
966 default:
967 if (isalnum(*cp))
968 break;
969 /* it isn't */
970 goto literal_copy;
973 /* @ past the first slash does not count */
974 cp = strchr(scheme_prefix + 3, '/');
975 if (cp && cp < anon_part)
976 goto literal_copy;
977 prefix_len = scheme_prefix - url + 3;
979 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
980 memcpy(anon_url, url, prefix_len);
981 memcpy(anon_url + prefix_len, anon_part, anon_len);
982 return anon_url;
983 literal_copy:
984 return xstrdup(url);