Merge branch 'jc/check-ref-format-fixup' into next
[git/dscho.git] / transport.c
blob57138d908a5749d378af441e8d3cfe8150b27271
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(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 > 0) ? "-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 > 0) ? "-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 > 0)
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 opts->depth = atoi(value);
479 return 0;
481 return 1;
484 static int connect_setup(struct transport *transport, int for_push, int verbose)
486 struct git_transport_data *data = transport->data;
488 if (data->conn)
489 return 0;
491 data->conn = git_connect(data->fd, transport->url,
492 for_push ? data->options.receivepack :
493 data->options.uploadpack,
494 verbose ? CONNECT_VERBOSE : 0);
496 return 0;
499 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
501 struct git_transport_data *data = transport->data;
502 struct ref *refs;
504 connect_setup(transport, for_push, 0);
505 get_remote_heads(data->fd[0], &refs, 0, NULL,
506 for_push ? REF_NORMAL : 0, &data->extra_have);
507 data->got_remote_heads = 1;
509 return refs;
512 static int fetch_refs_via_pack(struct transport *transport,
513 int nr_heads, struct ref **to_fetch)
515 struct git_transport_data *data = transport->data;
516 char **heads = xmalloc(nr_heads * sizeof(*heads));
517 char **origh = xmalloc(nr_heads * sizeof(*origh));
518 const struct ref *refs;
519 char *dest = xstrdup(transport->url);
520 struct fetch_pack_args args;
521 int i;
522 struct ref *refs_tmp = NULL;
524 memset(&args, 0, sizeof(args));
525 args.uploadpack = data->options.uploadpack;
526 args.keep_pack = data->options.keep;
527 args.lock_pack = 1;
528 args.use_thin_pack = data->options.thin;
529 args.include_tag = data->options.followtags;
530 args.verbose = (transport->verbose > 0);
531 args.quiet = (transport->verbose < 0);
532 args.no_progress = !transport->progress;
533 args.depth = data->options.depth;
535 for (i = 0; i < nr_heads; i++)
536 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
538 if (!data->got_remote_heads) {
539 connect_setup(transport, 0, 0);
540 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
541 data->got_remote_heads = 1;
544 refs = fetch_pack(&args, data->fd, data->conn,
545 refs_tmp ? refs_tmp : transport->remote_refs,
546 dest, nr_heads, heads, &transport->pack_lockfile);
547 close(data->fd[0]);
548 close(data->fd[1]);
549 if (finish_connect(data->conn))
550 refs = NULL;
551 data->conn = NULL;
552 data->got_remote_heads = 0;
554 free_refs(refs_tmp);
556 for (i = 0; i < nr_heads; i++)
557 free(origh[i]);
558 free(origh);
559 free(heads);
560 free(dest);
561 return (refs ? 0 : -1);
564 static int push_had_errors(struct ref *ref)
566 for (; ref; ref = ref->next) {
567 switch (ref->status) {
568 case REF_STATUS_NONE:
569 case REF_STATUS_UPTODATE:
570 case REF_STATUS_OK:
571 break;
572 default:
573 return 1;
576 return 0;
579 int transport_refs_pushed(struct ref *ref)
581 for (; ref; ref = ref->next) {
582 switch(ref->status) {
583 case REF_STATUS_NONE:
584 case REF_STATUS_UPTODATE:
585 break;
586 default:
587 return 1;
590 return 0;
593 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
595 struct refspec rs;
597 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
598 return;
600 rs.src = ref->name;
601 rs.dst = NULL;
603 if (!remote_find_tracking(remote, &rs)) {
604 if (verbose)
605 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
606 if (ref->deletion) {
607 delete_ref(rs.dst, NULL, 0);
608 } else
609 update_ref("update by push", rs.dst,
610 ref->new_sha1, NULL, 0, 0);
611 free(rs.dst);
615 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
617 if (porcelain) {
618 if (from)
619 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
620 else
621 fprintf(stdout, "%c\t:%s\t", flag, to->name);
622 if (msg)
623 fprintf(stdout, "%s (%s)\n", summary, msg);
624 else
625 fprintf(stdout, "%s\n", summary);
626 } else {
627 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
628 if (from)
629 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
630 else
631 fputs(prettify_refname(to->name), stderr);
632 if (msg) {
633 fputs(" (", stderr);
634 fputs(msg, stderr);
635 fputc(')', stderr);
637 fputc('\n', stderr);
641 static const char *status_abbrev(unsigned char sha1[20])
643 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
646 static void print_ok_ref_status(struct ref *ref, int porcelain)
648 if (ref->deletion)
649 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
650 else if (is_null_sha1(ref->old_sha1))
651 print_ref_status('*',
652 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
653 "[new branch]"),
654 ref, ref->peer_ref, NULL, porcelain);
655 else {
656 char quickref[84];
657 char type;
658 const char *msg;
660 strcpy(quickref, status_abbrev(ref->old_sha1));
661 if (ref->nonfastforward) {
662 strcat(quickref, "...");
663 type = '+';
664 msg = "forced update";
665 } else {
666 strcat(quickref, "..");
667 type = ' ';
668 msg = NULL;
670 strcat(quickref, status_abbrev(ref->new_sha1));
672 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
676 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
678 if (!count)
679 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
681 switch(ref->status) {
682 case REF_STATUS_NONE:
683 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
684 break;
685 case REF_STATUS_REJECT_NODELETE:
686 print_ref_status('!', "[rejected]", ref, NULL,
687 "remote does not support deleting refs", porcelain);
688 break;
689 case REF_STATUS_UPTODATE:
690 print_ref_status('=', "[up to date]", ref,
691 ref->peer_ref, NULL, porcelain);
692 break;
693 case REF_STATUS_REJECT_NONFASTFORWARD:
694 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
695 "non-fast-forward", porcelain);
696 break;
697 case REF_STATUS_REMOTE_REJECT:
698 print_ref_status('!', "[remote rejected]", ref,
699 ref->deletion ? NULL : ref->peer_ref,
700 ref->remote_status, porcelain);
701 break;
702 case REF_STATUS_EXPECTING_REPORT:
703 print_ref_status('!', "[remote failure]", ref,
704 ref->deletion ? NULL : ref->peer_ref,
705 "remote failed to report status", porcelain);
706 break;
707 case REF_STATUS_OK:
708 print_ok_ref_status(ref, porcelain);
709 break;
712 return 1;
715 void transport_print_push_status(const char *dest, struct ref *refs,
716 int verbose, int porcelain, int *nonfastforward)
718 struct ref *ref;
719 int n = 0;
721 if (verbose) {
722 for (ref = refs; ref; ref = ref->next)
723 if (ref->status == REF_STATUS_UPTODATE)
724 n += print_one_push_status(ref, dest, n, porcelain);
727 for (ref = refs; ref; ref = ref->next)
728 if (ref->status == REF_STATUS_OK)
729 n += print_one_push_status(ref, dest, n, porcelain);
731 *nonfastforward = 0;
732 for (ref = refs; ref; ref = ref->next) {
733 if (ref->status != REF_STATUS_NONE &&
734 ref->status != REF_STATUS_UPTODATE &&
735 ref->status != REF_STATUS_OK)
736 n += print_one_push_status(ref, dest, n, porcelain);
737 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
738 *nonfastforward = 1;
742 void transport_verify_remote_names(int nr_heads, const char **heads)
744 int i;
746 for (i = 0; i < nr_heads; i++) {
747 const char *local = heads[i];
748 const char *remote = strrchr(heads[i], ':');
750 if (*local == '+')
751 local++;
753 /* A matching refspec is okay. */
754 if (remote == local && remote[1] == '\0')
755 continue;
757 remote = remote ? (remote + 1) : local;
758 if (check_refname_format(remote,
759 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
760 die("remote part of refspec is not a valid name in %s",
761 heads[i]);
765 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
767 struct git_transport_data *data = transport->data;
768 struct send_pack_args args;
769 int ret;
771 if (!data->got_remote_heads) {
772 struct ref *tmp_refs;
773 connect_setup(transport, 1, 0);
775 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
776 NULL);
777 data->got_remote_heads = 1;
780 memset(&args, 0, sizeof(args));
781 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
782 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
783 args.use_thin_pack = data->options.thin;
784 args.verbose = (transport->verbose > 0);
785 args.quiet = (transport->verbose < 0);
786 args.progress = transport->progress;
787 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
788 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
790 ret = send_pack(&args, data->fd, data->conn, remote_refs,
791 &data->extra_have);
793 close(data->fd[1]);
794 close(data->fd[0]);
795 ret |= finish_connect(data->conn);
796 data->conn = NULL;
797 data->got_remote_heads = 0;
799 return ret;
802 static int connect_git(struct transport *transport, const char *name,
803 const char *executable, int fd[2])
805 struct git_transport_data *data = transport->data;
806 data->conn = git_connect(data->fd, transport->url,
807 executable, 0);
808 fd[0] = data->fd[0];
809 fd[1] = data->fd[1];
810 return 0;
813 static int disconnect_git(struct transport *transport)
815 struct git_transport_data *data = transport->data;
816 if (data->conn) {
817 if (data->got_remote_heads)
818 packet_flush(data->fd[1]);
819 close(data->fd[0]);
820 close(data->fd[1]);
821 finish_connect(data->conn);
824 free(data);
825 return 0;
828 void transport_take_over(struct transport *transport,
829 struct child_process *child)
831 struct git_transport_data *data;
833 if (!transport->smart_options)
834 die("Bug detected: Taking over transport requires non-NULL "
835 "smart_options field.");
837 data = xcalloc(1, sizeof(*data));
838 data->options = *transport->smart_options;
839 data->conn = child;
840 data->fd[0] = data->conn->out;
841 data->fd[1] = data->conn->in;
842 data->got_remote_heads = 0;
843 transport->data = data;
845 transport->set_option = NULL;
846 transport->get_refs_list = get_refs_via_connect;
847 transport->fetch = fetch_refs_via_pack;
848 transport->push = NULL;
849 transport->push_refs = git_transport_push;
850 transport->disconnect = disconnect_git;
851 transport->smart_options = &(data->options);
854 static int is_local(const char *url)
856 const char *colon = strchr(url, ':');
857 const char *slash = strchr(url, '/');
858 return !colon || (slash && slash < colon) ||
859 has_dos_drive_prefix(url);
862 static int is_gitfile(const char *url)
864 struct stat st;
865 char buf[9];
866 int fd, len;
867 if (stat(url, &st))
868 return 0;
869 if (!S_ISREG(st.st_mode))
870 return 0;
871 if (st.st_size < 10 || st.st_size > 9 + PATH_MAX)
872 return 0;
874 fd = open(url, O_RDONLY);
875 if (fd < 0)
876 die_errno("Error opening '%s'", url);
877 len = read_in_full(fd, buf, sizeof(buf));
878 close(fd);
879 if (len != sizeof(buf))
880 die("Error reading %s", url);
881 return !prefixcmp(buf, "gitdir: ");
884 static int is_file(const char *url)
886 struct stat buf;
887 if (stat(url, &buf))
888 return 0;
889 return S_ISREG(buf.st_mode);
892 static int external_specification_len(const char *url)
894 return strchr(url, ':') - url;
897 struct transport *transport_get(struct remote *remote, const char *url)
899 const char *helper;
900 struct transport *ret = xcalloc(1, sizeof(*ret));
902 ret->progress = isatty(2);
904 if (!remote)
905 die("No remote provided to transport_get()");
907 ret->got_remote_refs = 0;
908 ret->remote = remote;
909 helper = remote->foreign_vcs;
911 if (!url && remote->url)
912 url = remote->url[0];
913 ret->url = url;
915 /* maybe it is a foreign URL? */
916 if (url) {
917 const char *p = url;
919 while (is_urlschemechar(p == url, *p))
920 p++;
921 if (!prefixcmp(p, "::"))
922 helper = xstrndup(url, p - url);
925 if (helper) {
926 transport_helper_init(ret, helper);
927 } else if (!prefixcmp(url, "rsync:")) {
928 ret->get_refs_list = get_refs_via_rsync;
929 ret->fetch = fetch_objs_via_rsync;
930 ret->push = rsync_transport_push;
931 ret->smart_options = NULL;
932 } else if (is_local(url) && is_file(url) && !is_gitfile(url)) {
933 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
934 ret->data = data;
935 ret->get_refs_list = get_refs_from_bundle;
936 ret->fetch = fetch_refs_from_bundle;
937 ret->disconnect = close_bundle;
938 ret->smart_options = NULL;
939 } else if (!is_url(url)
940 || !prefixcmp(url, "file://")
941 || !prefixcmp(url, "git://")
942 || !prefixcmp(url, "ssh://")
943 || !prefixcmp(url, "git+ssh://")
944 || !prefixcmp(url, "ssh+git://")) {
945 /* These are builtin smart transports. */
946 struct git_transport_data *data = xcalloc(1, sizeof(*data));
947 ret->data = data;
948 ret->set_option = NULL;
949 ret->get_refs_list = get_refs_via_connect;
950 ret->fetch = fetch_refs_via_pack;
951 ret->push_refs = git_transport_push;
952 ret->connect = connect_git;
953 ret->disconnect = disconnect_git;
954 ret->smart_options = &(data->options);
956 data->conn = NULL;
957 data->got_remote_heads = 0;
958 } else {
959 /* Unknown protocol in URL. Pass to external handler. */
960 int len = external_specification_len(url);
961 char *handler = xmalloc(len + 1);
962 handler[len] = 0;
963 strncpy(handler, url, len);
964 transport_helper_init(ret, handler);
967 if (ret->smart_options) {
968 ret->smart_options->thin = 1;
969 ret->smart_options->uploadpack = "git-upload-pack";
970 if (remote->uploadpack)
971 ret->smart_options->uploadpack = remote->uploadpack;
972 ret->smart_options->receivepack = "git-receive-pack";
973 if (remote->receivepack)
974 ret->smart_options->receivepack = remote->receivepack;
977 return ret;
980 int transport_set_option(struct transport *transport,
981 const char *name, const char *value)
983 int git_reports = 1, protocol_reports = 1;
985 if (transport->smart_options)
986 git_reports = set_git_option(transport->smart_options,
987 name, value);
989 if (transport->set_option)
990 protocol_reports = transport->set_option(transport, name,
991 value);
993 /* If either report is 0, report 0 (success). */
994 if (!git_reports || !protocol_reports)
995 return 0;
996 /* If either reports -1 (invalid value), report -1. */
997 if ((git_reports == -1) || (protocol_reports == -1))
998 return -1;
999 /* Otherwise if both report unknown, report unknown. */
1000 return 1;
1003 void transport_set_verbosity(struct transport *transport, int verbosity,
1004 int force_progress)
1006 if (verbosity >= 2)
1007 transport->verbose = verbosity <= 3 ? verbosity : 3;
1008 if (verbosity < 0)
1009 transport->verbose = -1;
1012 * Rules used to determine whether to report progress (processing aborts
1013 * when a rule is satisfied):
1015 * 1. Report progress, if force_progress is 1 (ie. --progress).
1016 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1017 * 3. Report progress if isatty(2) is 1.
1019 transport->progress = force_progress || (verbosity >= 0 && isatty(2));
1022 int transport_push(struct transport *transport,
1023 int refspec_nr, const char **refspec, int flags,
1024 int *nonfastforward)
1026 *nonfastforward = 0;
1027 transport_verify_remote_names(refspec_nr, refspec);
1029 if (transport->push) {
1030 /* Maybe FIXME. But no important transport uses this case. */
1031 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1032 die("This transport does not support using --set-upstream");
1034 return transport->push(transport, refspec_nr, refspec, flags);
1035 } else if (transport->push_refs) {
1036 struct ref *remote_refs =
1037 transport->get_refs_list(transport, 1);
1038 struct ref *local_refs = get_local_heads();
1039 int match_flags = MATCH_REFS_NONE;
1040 int verbose = (transport->verbose > 0);
1041 int quiet = (transport->verbose < 0);
1042 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1043 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1044 int push_ret, ret, err;
1046 if (flags & TRANSPORT_PUSH_ALL)
1047 match_flags |= MATCH_REFS_ALL;
1048 if (flags & TRANSPORT_PUSH_MIRROR)
1049 match_flags |= MATCH_REFS_MIRROR;
1051 if (match_refs(local_refs, &remote_refs,
1052 refspec_nr, refspec, match_flags)) {
1053 return -1;
1056 set_ref_status_for_push(remote_refs,
1057 flags & TRANSPORT_PUSH_MIRROR,
1058 flags & TRANSPORT_PUSH_FORCE);
1060 if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1061 struct ref *ref = remote_refs;
1062 for (; ref; ref = ref->next)
1063 if (!is_null_sha1(ref->new_sha1) &&
1064 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1065 die("There are unpushed submodules, aborting.");
1068 push_ret = transport->push_refs(transport, remote_refs, flags);
1069 err = push_had_errors(remote_refs);
1070 ret = push_ret | err;
1072 if (!quiet || err)
1073 transport_print_push_status(transport->url, remote_refs,
1074 verbose | porcelain, porcelain,
1075 nonfastforward);
1077 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1078 set_upstreams(transport, remote_refs, pretend);
1080 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1081 struct ref *ref;
1082 for (ref = remote_refs; ref; ref = ref->next)
1083 transport_update_tracking_ref(transport->remote, ref, verbose);
1086 if (porcelain && !push_ret)
1087 puts("Done");
1088 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1089 fprintf(stderr, "Everything up-to-date\n");
1091 return ret;
1093 return 1;
1096 const struct ref *transport_get_remote_refs(struct transport *transport)
1098 if (!transport->got_remote_refs) {
1099 transport->remote_refs = transport->get_refs_list(transport, 0);
1100 transport->got_remote_refs = 1;
1103 return transport->remote_refs;
1106 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1108 int rc;
1109 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1110 struct ref **heads = NULL;
1111 struct ref *rm;
1113 for (rm = refs; rm; rm = rm->next) {
1114 nr_refs++;
1115 if (rm->peer_ref &&
1116 !is_null_sha1(rm->old_sha1) &&
1117 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1118 continue;
1119 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1120 heads[nr_heads++] = rm;
1123 if (!nr_heads) {
1125 * When deepening of a shallow repository is requested,
1126 * then local and remote refs are likely to still be equal.
1127 * Just feed them all to the fetch method in that case.
1128 * This condition shouldn't be met in a non-deepening fetch
1129 * (see builtin-fetch.c:quickfetch()).
1131 heads = xmalloc(nr_refs * sizeof(*heads));
1132 for (rm = refs; rm; rm = rm->next)
1133 heads[nr_heads++] = rm;
1136 rc = transport->fetch(transport, nr_heads, heads);
1138 free(heads);
1139 return rc;
1142 void transport_unlock_pack(struct transport *transport)
1144 if (transport->pack_lockfile) {
1145 unlink_or_warn(transport->pack_lockfile);
1146 free(transport->pack_lockfile);
1147 transport->pack_lockfile = NULL;
1151 int transport_connect(struct transport *transport, const char *name,
1152 const char *exec, int fd[2])
1154 if (transport->connect)
1155 return transport->connect(transport, name, exec, fd);
1156 else
1157 die("Operation not supported by protocol");
1160 int transport_disconnect(struct transport *transport)
1162 int ret = 0;
1163 if (transport->disconnect)
1164 ret = transport->disconnect(transport);
1165 free(transport);
1166 return ret;
1170 * Strip username (and password) from an url and return
1171 * it in a newly allocated string.
1173 char *transport_anonymize_url(const char *url)
1175 char *anon_url, *scheme_prefix, *anon_part;
1176 size_t anon_len, prefix_len = 0;
1178 anon_part = strchr(url, '@');
1179 if (is_local(url) || !anon_part)
1180 goto literal_copy;
1182 anon_len = strlen(++anon_part);
1183 scheme_prefix = strstr(url, "://");
1184 if (!scheme_prefix) {
1185 if (!strchr(anon_part, ':'))
1186 /* cannot be "me@there:/path/name" */
1187 goto literal_copy;
1188 } else {
1189 const char *cp;
1190 /* make sure scheme is reasonable */
1191 for (cp = url; cp < scheme_prefix; cp++) {
1192 switch (*cp) {
1193 /* RFC 1738 2.1 */
1194 case '+': case '.': case '-':
1195 break; /* ok */
1196 default:
1197 if (isalnum(*cp))
1198 break;
1199 /* it isn't */
1200 goto literal_copy;
1203 /* @ past the first slash does not count */
1204 cp = strchr(scheme_prefix + 3, '/');
1205 if (cp && cp < anon_part)
1206 goto literal_copy;
1207 prefix_len = scheme_prefix - url + 3;
1209 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1210 memcpy(anon_url, url, prefix_len);
1211 memcpy(anon_url + prefix_len, anon_part, anon_len);
1212 return anon_url;
1213 literal_copy:
1214 return xstrdup(url);
1217 struct alternate_refs_data {
1218 alternate_ref_fn *fn;
1219 void *data;
1222 static int refs_from_alternate_cb(struct alternate_object_database *e,
1223 void *data)
1225 char *other;
1226 size_t len;
1227 struct remote *remote;
1228 struct transport *transport;
1229 const struct ref *extra;
1230 struct alternate_refs_data *cb = data;
1232 e->name[-1] = '\0';
1233 other = xstrdup(real_path(e->base));
1234 e->name[-1] = '/';
1235 len = strlen(other);
1237 while (other[len-1] == '/')
1238 other[--len] = '\0';
1239 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1240 return 0;
1241 /* Is this a git repository with refs? */
1242 memcpy(other + len - 8, "/refs", 6);
1243 if (!is_directory(other))
1244 return 0;
1245 other[len - 8] = '\0';
1246 remote = remote_get(other);
1247 transport = transport_get(remote, other);
1248 for (extra = transport_get_remote_refs(transport);
1249 extra;
1250 extra = extra->next)
1251 cb->fn(extra, cb->data);
1252 transport_disconnect(transport);
1253 free(other);
1254 return 0;
1257 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1259 struct alternate_refs_data cb;
1260 cb.fn = fn;
1261 cb.data = data;
1262 foreach_alt_odb(refs_from_alternate_cb, &cb);