Update draft release notes to 1.7.10.3
[git/gitweb.git] / transport.c
blob2dfac700b630aabb2d1014c51a08889b3a3bae82
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"
11 #include "branch.h"
12 #include "url.h"
13 #include "submodule.h"
15 /* rsync support */
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).
23 * Appears refactoring this from refs.c is too cumbersome.
26 static int str_cmp(const void *a, const void *b)
28 const char *s1 = a;
29 const char *s2 = b;
31 return strcmp(s1, s2);
34 /* path->buf + name_offset is expected to point to "refs/" */
36 static int read_loose_refs(struct strbuf *path, int name_offset,
37 struct ref **tail)
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;
47 if (!dir)
48 return -1;
50 memset (&list, 0, sizeof(list));
52 while ((de = readdir(dir))) {
53 if (is_dot_or_dotdot(de->d_name))
54 continue;
55 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
56 list.entries[list.nr++] = xstrdup(de->d_name);
58 closedir(dir);
60 /* sort the list */
62 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
64 pathlen = path->len;
65 strbuf_addch(path, '/');
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;
74 if (fd < 0)
75 continue;
76 next = alloc_ref(path->buf + name_offset);
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;
83 close(fd);
84 (*tail)->next = next;
85 *tail = next;
88 strbuf_setlen(path, pathlen);
90 for (i = 0; i < list.nr; i++)
91 free(list.entries[i]);
92 free(list.entries);
94 return 0;
97 /* insert the packed refs for which no loose refs were found */
99 static void insert_packed_refs(const char *packed_refs, struct ref **list)
101 FILE *f = fopen(packed_refs, "r");
102 static char buffer[PATH_MAX];
104 if (!f)
105 return;
107 for (;;) {
108 int cmp = cmp, len;
110 if (!fgets(buffer, sizeof(buffer), f)) {
111 fclose(f);
112 return;
115 if (hexval(buffer[0]) > 0xf)
116 continue;
117 len = strlen(buffer);
118 if (len && buffer[len - 1] == '\n')
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) {
127 struct ref *next = alloc_ref(buffer + 41);
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;
134 next->next = (*list)->next;
135 (*list)->next = next;
136 list = &(*list)->next;
141 static void set_upstreams(struct transport *transport, struct ref *refs,
142 int pretend)
144 struct ref *ref;
145 for (ref = refs; ref; ref = ref->next) {
146 const char *localname;
147 const char *tmp;
148 const char *remotename;
149 unsigned char sha[20];
150 int flag = 0;
152 * Check suitability for tracking. Must be successful /
153 * already up-to-date ref create/modify (not delete).
155 if (ref->status != REF_STATUS_OK &&
156 ref->status != REF_STATUS_UPTODATE)
157 continue;
158 if (!ref->peer_ref)
159 continue;
160 if (is_null_sha1(ref->new_sha1))
161 continue;
163 /* Follow symbolic refs (mainly for HEAD). */
164 localname = ref->peer_ref->name;
165 remotename = ref->name;
166 tmp = resolve_ref_unsafe(localname, sha, 1, &flag);
167 if (tmp && flag & REF_ISSYMREF &&
168 !prefixcmp(tmp, "refs/heads/"))
169 localname = tmp;
171 /* Both source and destination must be local branches. */
172 if (!localname || prefixcmp(localname, "refs/heads/"))
173 continue;
174 if (!remotename || prefixcmp(remotename, "refs/heads/"))
175 continue;
177 if (!pretend)
178 install_branch_config(BRANCH_CONFIG_VERBOSE,
179 localname + 11, transport->remote->name,
180 remotename);
181 else
182 printf("Would set upstream of '%s' to '%s' of '%s'\n",
183 localname + 11, remotename + 11,
184 transport->remote->name);
188 static const char *rsync_url(const char *url)
190 return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url;
193 static struct ref *get_refs_via_rsync(struct transport *transport, int for_push)
195 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
196 struct ref dummy = {NULL}, *tail = &dummy;
197 struct child_process rsync;
198 const char *args[5];
199 int temp_dir_len;
201 if (for_push)
202 return NULL;
204 /* copy the refs to the temporary directory */
206 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
207 if (!mkdtemp(temp_dir.buf))
208 die_errno ("Could not make temporary directory");
209 temp_dir_len = temp_dir.len;
211 strbuf_addstr(&buf, rsync_url(transport->url));
212 strbuf_addstr(&buf, "/refs");
214 memset(&rsync, 0, sizeof(rsync));
215 rsync.argv = args;
216 rsync.stdout_to_stderr = 1;
217 args[0] = "rsync";
218 args[1] = (transport->verbose > 1) ? "-rv" : "-r";
219 args[2] = buf.buf;
220 args[3] = temp_dir.buf;
221 args[4] = NULL;
223 if (run_command(&rsync))
224 die ("Could not run rsync to get refs");
226 strbuf_reset(&buf);
227 strbuf_addstr(&buf, rsync_url(transport->url));
228 strbuf_addstr(&buf, "/packed-refs");
230 args[2] = buf.buf;
232 if (run_command(&rsync))
233 die ("Could not run rsync to get refs");
235 /* read the copied refs */
237 strbuf_addstr(&temp_dir, "/refs");
238 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
239 strbuf_setlen(&temp_dir, temp_dir_len);
241 tail = &dummy;
242 strbuf_addstr(&temp_dir, "/packed-refs");
243 insert_packed_refs(temp_dir.buf, &tail);
244 strbuf_setlen(&temp_dir, temp_dir_len);
246 if (remove_dir_recursively(&temp_dir, 0))
247 warning ("Error removing temporary directory %s.",
248 temp_dir.buf);
250 strbuf_release(&buf);
251 strbuf_release(&temp_dir);
253 return dummy.next;
256 static int fetch_objs_via_rsync(struct transport *transport,
257 int nr_objs, struct ref **to_fetch)
259 struct strbuf buf = STRBUF_INIT;
260 struct child_process rsync;
261 const char *args[8];
262 int result;
264 strbuf_addstr(&buf, rsync_url(transport->url));
265 strbuf_addstr(&buf, "/objects/");
267 memset(&rsync, 0, sizeof(rsync));
268 rsync.argv = args;
269 rsync.stdout_to_stderr = 1;
270 args[0] = "rsync";
271 args[1] = (transport->verbose > 1) ? "-rv" : "-r";
272 args[2] = "--ignore-existing";
273 args[3] = "--exclude";
274 args[4] = "info";
275 args[5] = buf.buf;
276 args[6] = get_object_directory();
277 args[7] = NULL;
279 /* NEEDSWORK: handle one level of alternates */
280 result = run_command(&rsync);
282 strbuf_release(&buf);
284 return result;
287 static int write_one_ref(const char *name, const unsigned char *sha1,
288 int flags, void *data)
290 struct strbuf *buf = data;
291 int len = buf->len;
292 FILE *f;
294 /* when called via for_each_ref(), flags is non-zero */
295 if (flags && prefixcmp(name, "refs/heads/") &&
296 prefixcmp(name, "refs/tags/"))
297 return 0;
299 strbuf_addstr(buf, name);
300 if (safe_create_leading_directories(buf->buf) ||
301 !(f = fopen(buf->buf, "w")) ||
302 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
303 fclose(f))
304 return error("problems writing temporary file %s", buf->buf);
305 strbuf_setlen(buf, len);
306 return 0;
309 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
310 int refspec_nr, const char **refspec)
312 int i;
314 for (i = 0; i < refspec_nr; i++) {
315 unsigned char sha1[20];
316 char *ref;
318 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
319 return error("Could not get ref %s", refspec[i]);
321 if (write_one_ref(ref, sha1, 0, temp_dir)) {
322 free(ref);
323 return -1;
325 free(ref);
327 return 0;
330 static int rsync_transport_push(struct transport *transport,
331 int refspec_nr, const char **refspec, int flags)
333 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
334 int result = 0, i;
335 struct child_process rsync;
336 const char *args[10];
338 if (flags & TRANSPORT_PUSH_MIRROR)
339 return error("rsync transport does not support mirror mode");
341 /* first push the objects */
343 strbuf_addstr(&buf, rsync_url(transport->url));
344 strbuf_addch(&buf, '/');
346 memset(&rsync, 0, sizeof(rsync));
347 rsync.argv = args;
348 rsync.stdout_to_stderr = 1;
349 i = 0;
350 args[i++] = "rsync";
351 args[i++] = "-a";
352 if (flags & TRANSPORT_PUSH_DRY_RUN)
353 args[i++] = "--dry-run";
354 if (transport->verbose > 1)
355 args[i++] = "-v";
356 args[i++] = "--ignore-existing";
357 args[i++] = "--exclude";
358 args[i++] = "info";
359 args[i++] = get_object_directory();
360 args[i++] = buf.buf;
361 args[i++] = NULL;
363 if (run_command(&rsync))
364 return error("Could not push objects to %s",
365 rsync_url(transport->url));
367 /* copy the refs to the temporary directory; they could be packed. */
369 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
370 if (!mkdtemp(temp_dir.buf))
371 die_errno ("Could not make temporary directory");
372 strbuf_addch(&temp_dir, '/');
374 if (flags & TRANSPORT_PUSH_ALL) {
375 if (for_each_ref(write_one_ref, &temp_dir))
376 return -1;
377 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
378 return -1;
380 i = 2;
381 if (flags & TRANSPORT_PUSH_DRY_RUN)
382 args[i++] = "--dry-run";
383 if (!(flags & TRANSPORT_PUSH_FORCE))
384 args[i++] = "--ignore-existing";
385 args[i++] = temp_dir.buf;
386 args[i++] = rsync_url(transport->url);
387 args[i++] = NULL;
388 if (run_command(&rsync))
389 result = error("Could not push to %s",
390 rsync_url(transport->url));
392 if (remove_dir_recursively(&temp_dir, 0))
393 warning ("Could not remove temporary directory %s.",
394 temp_dir.buf);
396 strbuf_release(&buf);
397 strbuf_release(&temp_dir);
399 return result;
402 struct bundle_transport_data {
403 int fd;
404 struct bundle_header header;
407 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
409 struct bundle_transport_data *data = transport->data;
410 struct ref *result = NULL;
411 int i;
413 if (for_push)
414 return NULL;
416 if (data->fd > 0)
417 close(data->fd);
418 data->fd = read_bundle_header(transport->url, &data->header);
419 if (data->fd < 0)
420 die ("Could not read bundle '%s'.", transport->url);
421 for (i = 0; i < data->header.references.nr; i++) {
422 struct ref_list_entry *e = data->header.references.list + i;
423 struct ref *ref = alloc_ref(e->name);
424 hashcpy(ref->old_sha1, e->sha1);
425 ref->next = result;
426 result = ref;
428 return result;
431 static int fetch_refs_from_bundle(struct transport *transport,
432 int nr_heads, struct ref **to_fetch)
434 struct bundle_transport_data *data = transport->data;
435 return unbundle(&data->header, data->fd,
436 transport->progress ? BUNDLE_VERBOSE : 0);
439 static int close_bundle(struct transport *transport)
441 struct bundle_transport_data *data = transport->data;
442 if (data->fd > 0)
443 close(data->fd);
444 free(data);
445 return 0;
448 struct git_transport_data {
449 struct git_transport_options options;
450 struct child_process *conn;
451 int fd[2];
452 unsigned got_remote_heads : 1;
453 struct extra_have_objects extra_have;
456 static int set_git_option(struct git_transport_options *opts,
457 const char *name, const char *value)
459 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
460 opts->uploadpack = value;
461 return 0;
462 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
463 opts->receivepack = value;
464 return 0;
465 } else if (!strcmp(name, TRANS_OPT_THIN)) {
466 opts->thin = !!value;
467 return 0;
468 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
469 opts->followtags = !!value;
470 return 0;
471 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
472 opts->keep = !!value;
473 return 0;
474 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
475 if (!value)
476 opts->depth = 0;
477 else {
478 char *end;
479 opts->depth = strtol(value, &end, 0);
480 if (*end)
481 die("transport: invalid depth option '%s'", value);
483 return 0;
485 return 1;
488 static int connect_setup(struct transport *transport, int for_push, int verbose)
490 struct git_transport_data *data = transport->data;
492 if (data->conn)
493 return 0;
495 data->conn = git_connect(data->fd, transport->url,
496 for_push ? data->options.receivepack :
497 data->options.uploadpack,
498 verbose ? CONNECT_VERBOSE : 0);
500 return 0;
503 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
505 struct git_transport_data *data = transport->data;
506 struct ref *refs;
508 connect_setup(transport, for_push, 0);
509 get_remote_heads(data->fd[0], &refs,
510 for_push ? REF_NORMAL : 0, &data->extra_have);
511 data->got_remote_heads = 1;
513 return refs;
516 static int fetch_refs_via_pack(struct transport *transport,
517 int nr_heads, struct ref **to_fetch)
519 struct git_transport_data *data = transport->data;
520 char **heads = xmalloc(nr_heads * sizeof(*heads));
521 char **origh = xmalloc(nr_heads * sizeof(*origh));
522 const struct ref *refs;
523 char *dest = xstrdup(transport->url);
524 struct fetch_pack_args args;
525 int i;
526 struct ref *refs_tmp = NULL;
528 memset(&args, 0, sizeof(args));
529 args.uploadpack = data->options.uploadpack;
530 args.keep_pack = data->options.keep;
531 args.lock_pack = 1;
532 args.use_thin_pack = data->options.thin;
533 args.include_tag = data->options.followtags;
534 args.verbose = (transport->verbose > 1);
535 args.quiet = (transport->verbose < 0);
536 args.no_progress = !transport->progress;
537 args.depth = data->options.depth;
539 for (i = 0; i < nr_heads; i++)
540 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
542 if (!data->got_remote_heads) {
543 connect_setup(transport, 0, 0);
544 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL);
545 data->got_remote_heads = 1;
548 refs = fetch_pack(&args, data->fd, data->conn,
549 refs_tmp ? refs_tmp : transport->remote_refs,
550 dest, nr_heads, heads, &transport->pack_lockfile);
551 close(data->fd[0]);
552 close(data->fd[1]);
553 if (finish_connect(data->conn))
554 refs = NULL;
555 data->conn = NULL;
556 data->got_remote_heads = 0;
558 free_refs(refs_tmp);
560 for (i = 0; i < nr_heads; i++)
561 free(origh[i]);
562 free(origh);
563 free(heads);
564 free(dest);
565 return (refs ? 0 : -1);
568 static int push_had_errors(struct ref *ref)
570 for (; ref; ref = ref->next) {
571 switch (ref->status) {
572 case REF_STATUS_NONE:
573 case REF_STATUS_UPTODATE:
574 case REF_STATUS_OK:
575 break;
576 default:
577 return 1;
580 return 0;
583 int transport_refs_pushed(struct ref *ref)
585 for (; ref; ref = ref->next) {
586 switch(ref->status) {
587 case REF_STATUS_NONE:
588 case REF_STATUS_UPTODATE:
589 break;
590 default:
591 return 1;
594 return 0;
597 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
599 struct refspec rs;
601 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
602 return;
604 rs.src = ref->name;
605 rs.dst = NULL;
607 if (!remote_find_tracking(remote, &rs)) {
608 if (verbose)
609 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
610 if (ref->deletion) {
611 delete_ref(rs.dst, NULL, 0);
612 } else
613 update_ref("update by push", rs.dst,
614 ref->new_sha1, NULL, 0, 0);
615 free(rs.dst);
619 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
621 if (porcelain) {
622 if (from)
623 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
624 else
625 fprintf(stdout, "%c\t:%s\t", flag, to->name);
626 if (msg)
627 fprintf(stdout, "%s (%s)\n", summary, msg);
628 else
629 fprintf(stdout, "%s\n", summary);
630 } else {
631 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
632 if (from)
633 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
634 else
635 fputs(prettify_refname(to->name), stderr);
636 if (msg) {
637 fputs(" (", stderr);
638 fputs(msg, stderr);
639 fputc(')', stderr);
641 fputc('\n', stderr);
645 static const char *status_abbrev(unsigned char sha1[20])
647 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
650 static void print_ok_ref_status(struct ref *ref, int porcelain)
652 if (ref->deletion)
653 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
654 else if (is_null_sha1(ref->old_sha1))
655 print_ref_status('*',
656 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
657 "[new branch]"),
658 ref, ref->peer_ref, NULL, porcelain);
659 else {
660 char quickref[84];
661 char type;
662 const char *msg;
664 strcpy(quickref, status_abbrev(ref->old_sha1));
665 if (ref->nonfastforward) {
666 strcat(quickref, "...");
667 type = '+';
668 msg = "forced update";
669 } else {
670 strcat(quickref, "..");
671 type = ' ';
672 msg = NULL;
674 strcat(quickref, status_abbrev(ref->new_sha1));
676 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
680 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
682 if (!count)
683 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
685 switch(ref->status) {
686 case REF_STATUS_NONE:
687 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
688 break;
689 case REF_STATUS_REJECT_NODELETE:
690 print_ref_status('!', "[rejected]", ref, NULL,
691 "remote does not support deleting refs", porcelain);
692 break;
693 case REF_STATUS_UPTODATE:
694 print_ref_status('=', "[up to date]", ref,
695 ref->peer_ref, NULL, porcelain);
696 break;
697 case REF_STATUS_REJECT_NONFASTFORWARD:
698 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
699 "non-fast-forward", porcelain);
700 break;
701 case REF_STATUS_REMOTE_REJECT:
702 print_ref_status('!', "[remote rejected]", ref,
703 ref->deletion ? NULL : ref->peer_ref,
704 ref->remote_status, porcelain);
705 break;
706 case REF_STATUS_EXPECTING_REPORT:
707 print_ref_status('!', "[remote failure]", ref,
708 ref->deletion ? NULL : ref->peer_ref,
709 "remote failed to report status", porcelain);
710 break;
711 case REF_STATUS_OK:
712 print_ok_ref_status(ref, porcelain);
713 break;
716 return 1;
719 void transport_print_push_status(const char *dest, struct ref *refs,
720 int verbose, int porcelain, int *nonfastforward)
722 struct ref *ref;
723 int n = 0;
724 unsigned char head_sha1[20];
725 char *head;
727 head = resolve_refdup("HEAD", head_sha1, 1, NULL);
729 if (verbose) {
730 for (ref = refs; ref; ref = ref->next)
731 if (ref->status == REF_STATUS_UPTODATE)
732 n += print_one_push_status(ref, dest, n, porcelain);
735 for (ref = refs; ref; ref = ref->next)
736 if (ref->status == REF_STATUS_OK)
737 n += print_one_push_status(ref, dest, n, porcelain);
739 *nonfastforward = 0;
740 for (ref = refs; ref; ref = ref->next) {
741 if (ref->status != REF_STATUS_NONE &&
742 ref->status != REF_STATUS_UPTODATE &&
743 ref->status != REF_STATUS_OK)
744 n += print_one_push_status(ref, dest, n, porcelain);
745 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD &&
746 *nonfastforward != NON_FF_HEAD) {
747 if (!strcmp(head, ref->name))
748 *nonfastforward = NON_FF_HEAD;
749 else
750 *nonfastforward = NON_FF_OTHER;
755 void transport_verify_remote_names(int nr_heads, const char **heads)
757 int i;
759 for (i = 0; i < nr_heads; i++) {
760 const char *local = heads[i];
761 const char *remote = strrchr(heads[i], ':');
763 if (*local == '+')
764 local++;
766 /* A matching refspec is okay. */
767 if (remote == local && remote[1] == '\0')
768 continue;
770 remote = remote ? (remote + 1) : local;
771 if (check_refname_format(remote,
772 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
773 die("remote part of refspec is not a valid name in %s",
774 heads[i]);
778 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
780 struct git_transport_data *data = transport->data;
781 struct send_pack_args args;
782 int ret;
784 if (!data->got_remote_heads) {
785 struct ref *tmp_refs;
786 connect_setup(transport, 1, 0);
788 get_remote_heads(data->fd[0], &tmp_refs, REF_NORMAL, NULL);
789 data->got_remote_heads = 1;
792 memset(&args, 0, sizeof(args));
793 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
794 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
795 args.use_thin_pack = data->options.thin;
796 args.verbose = (transport->verbose > 0);
797 args.quiet = (transport->verbose < 0);
798 args.progress = transport->progress;
799 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
800 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
802 ret = send_pack(&args, data->fd, data->conn, remote_refs,
803 &data->extra_have);
805 close(data->fd[1]);
806 close(data->fd[0]);
807 ret |= finish_connect(data->conn);
808 data->conn = NULL;
809 data->got_remote_heads = 0;
811 return ret;
814 static int connect_git(struct transport *transport, const char *name,
815 const char *executable, int fd[2])
817 struct git_transport_data *data = transport->data;
818 data->conn = git_connect(data->fd, transport->url,
819 executable, 0);
820 fd[0] = data->fd[0];
821 fd[1] = data->fd[1];
822 return 0;
825 static int disconnect_git(struct transport *transport)
827 struct git_transport_data *data = transport->data;
828 if (data->conn) {
829 if (data->got_remote_heads)
830 packet_flush(data->fd[1]);
831 close(data->fd[0]);
832 close(data->fd[1]);
833 finish_connect(data->conn);
836 free(data);
837 return 0;
840 void transport_take_over(struct transport *transport,
841 struct child_process *child)
843 struct git_transport_data *data;
845 if (!transport->smart_options)
846 die("Bug detected: Taking over transport requires non-NULL "
847 "smart_options field.");
849 data = xcalloc(1, sizeof(*data));
850 data->options = *transport->smart_options;
851 data->conn = child;
852 data->fd[0] = data->conn->out;
853 data->fd[1] = data->conn->in;
854 data->got_remote_heads = 0;
855 transport->data = data;
857 transport->set_option = NULL;
858 transport->get_refs_list = get_refs_via_connect;
859 transport->fetch = fetch_refs_via_pack;
860 transport->push = NULL;
861 transport->push_refs = git_transport_push;
862 transport->disconnect = disconnect_git;
863 transport->smart_options = &(data->options);
866 static int is_local(const char *url)
868 const char *colon = strchr(url, ':');
869 const char *slash = strchr(url, '/');
870 return !colon || (slash && slash < colon) ||
871 has_dos_drive_prefix(url);
874 static int is_file(const char *url)
876 struct stat buf;
877 if (stat(url, &buf))
878 return 0;
879 return S_ISREG(buf.st_mode);
882 static int external_specification_len(const char *url)
884 return strchr(url, ':') - url;
887 struct transport *transport_get(struct remote *remote, const char *url)
889 const char *helper;
890 struct transport *ret = xcalloc(1, sizeof(*ret));
892 ret->progress = isatty(2);
894 if (!remote)
895 die("No remote provided to transport_get()");
897 ret->got_remote_refs = 0;
898 ret->remote = remote;
899 helper = remote->foreign_vcs;
901 if (!url && remote->url)
902 url = remote->url[0];
903 ret->url = url;
905 /* maybe it is a foreign URL? */
906 if (url) {
907 const char *p = url;
909 while (is_urlschemechar(p == url, *p))
910 p++;
911 if (!prefixcmp(p, "::"))
912 helper = xstrndup(url, p - url);
915 if (helper) {
916 transport_helper_init(ret, helper);
917 } else if (!prefixcmp(url, "rsync:")) {
918 ret->get_refs_list = get_refs_via_rsync;
919 ret->fetch = fetch_objs_via_rsync;
920 ret->push = rsync_transport_push;
921 ret->smart_options = NULL;
922 } else if (is_local(url) && is_file(url) && is_bundle(url, 1)) {
923 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
924 ret->data = data;
925 ret->get_refs_list = get_refs_from_bundle;
926 ret->fetch = fetch_refs_from_bundle;
927 ret->disconnect = close_bundle;
928 ret->smart_options = NULL;
929 } else if (!is_url(url)
930 || !prefixcmp(url, "file://")
931 || !prefixcmp(url, "git://")
932 || !prefixcmp(url, "ssh://")
933 || !prefixcmp(url, "git+ssh://")
934 || !prefixcmp(url, "ssh+git://")) {
935 /* These are builtin smart transports. */
936 struct git_transport_data *data = xcalloc(1, sizeof(*data));
937 ret->data = data;
938 ret->set_option = NULL;
939 ret->get_refs_list = get_refs_via_connect;
940 ret->fetch = fetch_refs_via_pack;
941 ret->push_refs = git_transport_push;
942 ret->connect = connect_git;
943 ret->disconnect = disconnect_git;
944 ret->smart_options = &(data->options);
946 data->conn = NULL;
947 data->got_remote_heads = 0;
948 } else {
949 /* Unknown protocol in URL. Pass to external handler. */
950 int len = external_specification_len(url);
951 char *handler = xmalloc(len + 1);
952 handler[len] = 0;
953 strncpy(handler, url, len);
954 transport_helper_init(ret, handler);
957 if (ret->smart_options) {
958 ret->smart_options->thin = 1;
959 ret->smart_options->uploadpack = "git-upload-pack";
960 if (remote->uploadpack)
961 ret->smart_options->uploadpack = remote->uploadpack;
962 ret->smart_options->receivepack = "git-receive-pack";
963 if (remote->receivepack)
964 ret->smart_options->receivepack = remote->receivepack;
967 return ret;
970 int transport_set_option(struct transport *transport,
971 const char *name, const char *value)
973 int git_reports = 1, protocol_reports = 1;
975 if (transport->smart_options)
976 git_reports = set_git_option(transport->smart_options,
977 name, value);
979 if (transport->set_option)
980 protocol_reports = transport->set_option(transport, name,
981 value);
983 /* If either report is 0, report 0 (success). */
984 if (!git_reports || !protocol_reports)
985 return 0;
986 /* If either reports -1 (invalid value), report -1. */
987 if ((git_reports == -1) || (protocol_reports == -1))
988 return -1;
989 /* Otherwise if both report unknown, report unknown. */
990 return 1;
993 void transport_set_verbosity(struct transport *transport, int verbosity,
994 int force_progress)
996 if (verbosity >= 1)
997 transport->verbose = verbosity <= 3 ? verbosity : 3;
998 if (verbosity < 0)
999 transport->verbose = -1;
1002 * Rules used to determine whether to report progress (processing aborts
1003 * when a rule is satisfied):
1005 * . Report progress, if force_progress is 1 (ie. --progress).
1006 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1007 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1008 * . Report progress if isatty(2) is 1.
1010 if (force_progress >= 0)
1011 transport->progress = !!force_progress;
1012 else
1013 transport->progress = verbosity >= 0 && isatty(2);
1016 int transport_push(struct transport *transport,
1017 int refspec_nr, const char **refspec, int flags,
1018 int *nonfastforward)
1020 *nonfastforward = 0;
1021 transport_verify_remote_names(refspec_nr, refspec);
1023 if (transport->push) {
1024 /* Maybe FIXME. But no important transport uses this case. */
1025 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1026 die("This transport does not support using --set-upstream");
1028 return transport->push(transport, refspec_nr, refspec, flags);
1029 } else if (transport->push_refs) {
1030 struct ref *remote_refs =
1031 transport->get_refs_list(transport, 1);
1032 struct ref *local_refs = get_local_heads();
1033 int match_flags = MATCH_REFS_NONE;
1034 int verbose = (transport->verbose > 0);
1035 int quiet = (transport->verbose < 0);
1036 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1037 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1038 int push_ret, ret, err;
1040 if (flags & TRANSPORT_PUSH_ALL)
1041 match_flags |= MATCH_REFS_ALL;
1042 if (flags & TRANSPORT_PUSH_MIRROR)
1043 match_flags |= MATCH_REFS_MIRROR;
1044 if (flags & TRANSPORT_PUSH_PRUNE)
1045 match_flags |= MATCH_REFS_PRUNE;
1047 if (match_push_refs(local_refs, &remote_refs,
1048 refspec_nr, refspec, match_flags)) {
1049 return -1;
1052 set_ref_status_for_push(remote_refs,
1053 flags & TRANSPORT_PUSH_MIRROR,
1054 flags & TRANSPORT_PUSH_FORCE);
1056 if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1057 struct ref *ref = remote_refs;
1058 for (; ref; ref = ref->next)
1059 if (!is_null_sha1(ref->new_sha1) &&
1060 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1061 die("There are unpushed submodules, aborting.");
1064 push_ret = transport->push_refs(transport, remote_refs, flags);
1065 err = push_had_errors(remote_refs);
1066 ret = push_ret | err;
1068 if (!quiet || err)
1069 transport_print_push_status(transport->url, remote_refs,
1070 verbose | porcelain, porcelain,
1071 nonfastforward);
1073 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1074 set_upstreams(transport, remote_refs, pretend);
1076 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1077 struct ref *ref;
1078 for (ref = remote_refs; ref; ref = ref->next)
1079 transport_update_tracking_ref(transport->remote, ref, verbose);
1082 if (porcelain && !push_ret)
1083 puts("Done");
1084 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1085 fprintf(stderr, "Everything up-to-date\n");
1087 return ret;
1089 return 1;
1092 const struct ref *transport_get_remote_refs(struct transport *transport)
1094 if (!transport->got_remote_refs) {
1095 transport->remote_refs = transport->get_refs_list(transport, 0);
1096 transport->got_remote_refs = 1;
1099 return transport->remote_refs;
1102 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1104 int rc;
1105 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1106 struct ref **heads = NULL;
1107 struct ref *rm;
1109 for (rm = refs; rm; rm = rm->next) {
1110 nr_refs++;
1111 if (rm->peer_ref &&
1112 !is_null_sha1(rm->old_sha1) &&
1113 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1114 continue;
1115 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1116 heads[nr_heads++] = rm;
1119 if (!nr_heads) {
1121 * When deepening of a shallow repository is requested,
1122 * then local and remote refs are likely to still be equal.
1123 * Just feed them all to the fetch method in that case.
1124 * This condition shouldn't be met in a non-deepening fetch
1125 * (see builtin-fetch.c:quickfetch()).
1127 heads = xmalloc(nr_refs * sizeof(*heads));
1128 for (rm = refs; rm; rm = rm->next)
1129 heads[nr_heads++] = rm;
1132 rc = transport->fetch(transport, nr_heads, heads);
1134 free(heads);
1135 return rc;
1138 void transport_unlock_pack(struct transport *transport)
1140 if (transport->pack_lockfile) {
1141 unlink_or_warn(transport->pack_lockfile);
1142 free(transport->pack_lockfile);
1143 transport->pack_lockfile = NULL;
1147 int transport_connect(struct transport *transport, const char *name,
1148 const char *exec, int fd[2])
1150 if (transport->connect)
1151 return transport->connect(transport, name, exec, fd);
1152 else
1153 die("Operation not supported by protocol");
1156 int transport_disconnect(struct transport *transport)
1158 int ret = 0;
1159 if (transport->disconnect)
1160 ret = transport->disconnect(transport);
1161 free(transport);
1162 return ret;
1166 * Strip username (and password) from a URL and return
1167 * it in a newly allocated string.
1169 char *transport_anonymize_url(const char *url)
1171 char *anon_url, *scheme_prefix, *anon_part;
1172 size_t anon_len, prefix_len = 0;
1174 anon_part = strchr(url, '@');
1175 if (is_local(url) || !anon_part)
1176 goto literal_copy;
1178 anon_len = strlen(++anon_part);
1179 scheme_prefix = strstr(url, "://");
1180 if (!scheme_prefix) {
1181 if (!strchr(anon_part, ':'))
1182 /* cannot be "me@there:/path/name" */
1183 goto literal_copy;
1184 } else {
1185 const char *cp;
1186 /* make sure scheme is reasonable */
1187 for (cp = url; cp < scheme_prefix; cp++) {
1188 switch (*cp) {
1189 /* RFC 1738 2.1 */
1190 case '+': case '.': case '-':
1191 break; /* ok */
1192 default:
1193 if (isalnum(*cp))
1194 break;
1195 /* it isn't */
1196 goto literal_copy;
1199 /* @ past the first slash does not count */
1200 cp = strchr(scheme_prefix + 3, '/');
1201 if (cp && cp < anon_part)
1202 goto literal_copy;
1203 prefix_len = scheme_prefix - url + 3;
1205 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1206 memcpy(anon_url, url, prefix_len);
1207 memcpy(anon_url + prefix_len, anon_part, anon_len);
1208 return anon_url;
1209 literal_copy:
1210 return xstrdup(url);
1213 struct alternate_refs_data {
1214 alternate_ref_fn *fn;
1215 void *data;
1218 static int refs_from_alternate_cb(struct alternate_object_database *e,
1219 void *data)
1221 char *other;
1222 size_t len;
1223 struct remote *remote;
1224 struct transport *transport;
1225 const struct ref *extra;
1226 struct alternate_refs_data *cb = data;
1228 e->name[-1] = '\0';
1229 other = xstrdup(real_path(e->base));
1230 e->name[-1] = '/';
1231 len = strlen(other);
1233 while (other[len-1] == '/')
1234 other[--len] = '\0';
1235 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1236 return 0;
1237 /* Is this a git repository with refs? */
1238 memcpy(other + len - 8, "/refs", 6);
1239 if (!is_directory(other))
1240 return 0;
1241 other[len - 8] = '\0';
1242 remote = remote_get(other);
1243 transport = transport_get(remote, other);
1244 for (extra = transport_get_remote_refs(transport);
1245 extra;
1246 extra = extra->next)
1247 cb->fn(extra, cb->data);
1248 transport_disconnect(transport);
1249 free(other);
1250 return 0;
1253 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1255 struct alternate_refs_data cb;
1256 cb.fn = fn;
1257 cb.data = data;
1258 foreach_alt_odb(refs_from_alternate_cb, &cb);