3 #include "run-command.h"
5 #include "fetch-pack.h"
13 #include "submodule.h"
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)
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,
39 DIR *dir = opendir(path->buf);
50 memset (&list, 0, sizeof(list));
52 while ((de = readdir(dir))) {
53 if (is_dot_or_dotdot(de->d_name))
55 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
56 list.entries[list.nr++] = xstrdup(de->d_name);
62 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
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);
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)) {
88 strbuf_setlen(path, pathlen);
90 for (i = 0; i < list.nr; i++)
91 free(list.entries[i]);
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];
110 if (!fgets(buffer, sizeof(buffer), f)) {
115 if (hexval(buffer[0]) > 0xf)
117 len = strlen(buffer);
118 if (len && buffer[len - 1] == '\n')
119 buffer[--len] = '\0';
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);
129 if (get_sha1_hex(buffer, next->old_sha1)) {
130 warning ("invalid SHA-1: %s", buffer);
134 next->next = (*list)->next;
135 (*list)->next = next;
136 list = &(*list)->next;
141 static void set_upstreams(struct transport *transport, struct ref *refs,
145 for (ref = refs; ref; ref = ref->next) {
146 const char *localname;
148 const char *remotename;
149 unsigned char sha[20];
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)
160 if (is_null_sha1(ref->new_sha1))
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/"))
171 /* Both source and destination must be local branches. */
172 if (!localname || prefixcmp(localname, "refs/heads/"))
174 if (!remotename || prefixcmp(remotename, "refs/heads/"))
178 install_branch_config(BRANCH_CONFIG_VERBOSE,
179 localname + 11, transport->remote->name,
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;
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));
216 rsync.stdout_to_stderr = 1;
218 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
220 args[3] = temp_dir.buf;
223 if (run_command(&rsync))
224 die ("Could not run rsync to get refs");
227 strbuf_addstr(&buf, rsync_url(transport->url));
228 strbuf_addstr(&buf, "/packed-refs");
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);
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.",
250 strbuf_release(&buf);
251 strbuf_release(&temp_dir);
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;
264 strbuf_addstr(&buf, rsync_url(transport->url));
265 strbuf_addstr(&buf, "/objects/");
267 memset(&rsync, 0, sizeof(rsync));
269 rsync.stdout_to_stderr = 1;
271 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
272 args[2] = "--ignore-existing";
273 args[3] = "--exclude";
276 args[6] = get_object_directory();
279 /* NEEDSWORK: handle one level of alternates */
280 result = run_command(&rsync);
282 strbuf_release(&buf);
287 static int write_one_ref(const char *name, const unsigned char *sha1,
288 int flags, void *data)
290 struct strbuf *buf = data;
294 /* when called via for_each_ref(), flags is non-zero */
295 if (flags && prefixcmp(name, "refs/heads/") &&
296 prefixcmp(name, "refs/tags/"))
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 ||
304 return error("problems writing temporary file %s", buf->buf);
305 strbuf_setlen(buf, len);
309 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
310 int refspec_nr, const char **refspec)
314 for (i = 0; i < refspec_nr; i++) {
315 unsigned char sha1[20];
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)) {
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;
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));
348 rsync.stdout_to_stderr = 1;
352 if (flags & TRANSPORT_PUSH_DRY_RUN)
353 args[i++] = "--dry-run";
354 if (transport->verbose > 0)
356 args[i++] = "--ignore-existing";
357 args[i++] = "--exclude";
359 args[i++] = get_object_directory();
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))
377 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
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);
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.",
396 strbuf_release(&buf);
397 strbuf_release(&temp_dir);
402 struct bundle_transport_data {
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;
418 data->fd = read_bundle_header(transport->url, &data->header);
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);
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);
438 static int close_bundle(struct transport *transport)
440 struct bundle_transport_data *data = transport->data;
447 struct git_transport_data {
448 struct git_transport_options options;
449 struct child_process *conn;
451 unsigned got_remote_heads : 1;
452 struct extra_have_objects extra_have;
455 static int set_git_option(struct git_transport_options *opts,
456 const char *name, const char *value)
458 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
459 opts->uploadpack = value;
461 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
462 opts->receivepack = value;
464 } else if (!strcmp(name, TRANS_OPT_THIN)) {
465 opts->thin = !!value;
467 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
468 opts->followtags = !!value;
470 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
471 opts->keep = !!value;
473 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
477 opts->depth = atoi(value);
483 static int connect_setup(struct transport *transport, int for_push, int verbose)
485 struct git_transport_data *data = transport->data;
486 struct strbuf sb = STRBUF_INIT;
491 strbuf_addstr(&sb, for_push ? data->options.receivepack :
492 data->options.uploadpack);
493 if (for_push && transport->verbose < 0)
494 strbuf_addstr(&sb, " --quiet");
495 data->conn = git_connect(data->fd, transport->url, sb.buf,
496 verbose ? CONNECT_VERBOSE : 0);
502 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
504 struct git_transport_data *data = transport->data;
507 connect_setup(transport, for_push, 0);
508 get_remote_heads(data->fd[0], &refs, 0, NULL,
509 for_push ? REF_NORMAL : 0, &data->extra_have);
510 data->got_remote_heads = 1;
515 static int fetch_refs_via_pack(struct transport *transport,
516 int nr_heads, struct ref **to_fetch)
518 struct git_transport_data *data = transport->data;
519 char **heads = xmalloc(nr_heads * sizeof(*heads));
520 char **origh = xmalloc(nr_heads * sizeof(*origh));
521 const struct ref *refs;
522 char *dest = xstrdup(transport->url);
523 struct fetch_pack_args args;
525 struct ref *refs_tmp = NULL;
527 memset(&args, 0, sizeof(args));
528 args.uploadpack = data->options.uploadpack;
529 args.keep_pack = data->options.keep;
531 args.use_thin_pack = data->options.thin;
532 args.include_tag = data->options.followtags;
533 args.verbose = (transport->verbose > 0);
534 args.quiet = (transport->verbose < 0);
535 args.no_progress = !transport->progress;
536 args.depth = data->options.depth;
538 for (i = 0; i < nr_heads; i++)
539 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
541 if (!data->got_remote_heads) {
542 connect_setup(transport, 0, 0);
543 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
544 data->got_remote_heads = 1;
547 refs = fetch_pack(&args, data->fd, data->conn,
548 refs_tmp ? refs_tmp : transport->remote_refs,
549 dest, nr_heads, heads, &transport->pack_lockfile);
552 if (finish_connect(data->conn))
555 data->got_remote_heads = 0;
559 for (i = 0; i < nr_heads; i++)
564 return (refs ? 0 : -1);
567 static int push_had_errors(struct ref *ref)
569 for (; ref; ref = ref->next) {
570 switch (ref->status) {
571 case REF_STATUS_NONE:
572 case REF_STATUS_UPTODATE:
582 int transport_refs_pushed(struct ref *ref)
584 for (; ref; ref = ref->next) {
585 switch(ref->status) {
586 case REF_STATUS_NONE:
587 case REF_STATUS_UPTODATE:
596 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
600 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
606 if (!remote_find_tracking(remote, &rs)) {
608 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
610 delete_ref(rs.dst, NULL, 0);
612 update_ref("update by push", rs.dst,
613 ref->new_sha1, NULL, 0, 0);
618 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
622 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
624 fprintf(stdout, "%c\t:%s\t", flag, to->name);
626 fprintf(stdout, "%s (%s)\n", summary, msg);
628 fprintf(stdout, "%s\n", summary);
630 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
632 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
634 fputs(prettify_refname(to->name), stderr);
644 static const char *status_abbrev(unsigned char sha1[20])
646 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
649 static void print_ok_ref_status(struct ref *ref, int porcelain)
652 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
653 else if (is_null_sha1(ref->old_sha1))
654 print_ref_status('*',
655 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
657 ref, ref->peer_ref, NULL, porcelain);
663 strcpy(quickref, status_abbrev(ref->old_sha1));
664 if (ref->nonfastforward) {
665 strcat(quickref, "...");
667 msg = "forced update";
669 strcat(quickref, "..");
673 strcat(quickref, status_abbrev(ref->new_sha1));
675 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
679 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
682 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
684 switch(ref->status) {
685 case REF_STATUS_NONE:
686 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
688 case REF_STATUS_REJECT_NODELETE:
689 print_ref_status('!', "[rejected]", ref, NULL,
690 "remote does not support deleting refs", porcelain);
692 case REF_STATUS_UPTODATE:
693 print_ref_status('=', "[up to date]", ref,
694 ref->peer_ref, NULL, porcelain);
696 case REF_STATUS_REJECT_NONFASTFORWARD:
697 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
698 "non-fast-forward", porcelain);
700 case REF_STATUS_REMOTE_REJECT:
701 print_ref_status('!', "[remote rejected]", ref,
702 ref->deletion ? NULL : ref->peer_ref,
703 ref->remote_status, porcelain);
705 case REF_STATUS_EXPECTING_REPORT:
706 print_ref_status('!', "[remote failure]", ref,
707 ref->deletion ? NULL : ref->peer_ref,
708 "remote failed to report status", porcelain);
711 print_ok_ref_status(ref, porcelain);
718 void transport_print_push_status(const char *dest, struct ref *refs,
719 int verbose, int porcelain, int *nonfastforward)
725 for (ref = refs; ref; ref = ref->next)
726 if (ref->status == REF_STATUS_UPTODATE)
727 n += print_one_push_status(ref, dest, n, porcelain);
730 for (ref = refs; ref; ref = ref->next)
731 if (ref->status == REF_STATUS_OK)
732 n += print_one_push_status(ref, dest, n, porcelain);
735 for (ref = refs; ref; ref = ref->next) {
736 if (ref->status != REF_STATUS_NONE &&
737 ref->status != REF_STATUS_UPTODATE &&
738 ref->status != REF_STATUS_OK)
739 n += print_one_push_status(ref, dest, n, porcelain);
740 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
745 void transport_verify_remote_names(int nr_heads, const char **heads)
749 for (i = 0; i < nr_heads; i++) {
750 const char *local = heads[i];
751 const char *remote = strrchr(heads[i], ':');
756 /* A matching refspec is okay. */
757 if (remote == local && remote[1] == '\0')
760 remote = remote ? (remote + 1) : local;
761 switch (check_ref_format(remote)) {
763 case CHECK_REF_FORMAT_ONELEVEL:
764 /* ok but a single level -- that is fine for
767 case CHECK_REF_FORMAT_WILDCARD:
768 /* ok but ends with a pattern-match character */
771 die("remote part of refspec is not a valid name in %s",
776 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
778 struct git_transport_data *data = transport->data;
779 struct send_pack_args args;
782 if (!data->got_remote_heads) {
783 struct ref *tmp_refs;
784 connect_setup(transport, 1, 0);
786 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
788 data->got_remote_heads = 1;
791 memset(&args, 0, sizeof(args));
792 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
793 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
794 args.use_thin_pack = data->options.thin;
795 args.verbose = (transport->verbose > 0);
796 args.quiet = (transport->verbose < 0);
797 args.progress = transport->progress;
798 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
799 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
801 ret = send_pack(&args, data->fd, data->conn, remote_refs,
806 ret |= finish_connect(data->conn);
808 data->got_remote_heads = 0;
813 static int connect_git(struct transport *transport, const char *name,
814 const char *executable, int fd[2])
816 struct git_transport_data *data = transport->data;
817 data->conn = git_connect(data->fd, transport->url,
824 static int disconnect_git(struct transport *transport)
826 struct git_transport_data *data = transport->data;
828 if (data->got_remote_heads)
829 packet_flush(data->fd[1]);
832 finish_connect(data->conn);
839 void transport_take_over(struct transport *transport,
840 struct child_process *child)
842 struct git_transport_data *data;
844 if (!transport->smart_options)
845 die("Bug detected: Taking over transport requires non-NULL "
846 "smart_options field.");
848 data = xcalloc(1, sizeof(*data));
849 data->options = *transport->smart_options;
851 data->fd[0] = data->conn->out;
852 data->fd[1] = data->conn->in;
853 data->got_remote_heads = 0;
854 transport->data = data;
856 transport->set_option = NULL;
857 transport->get_refs_list = get_refs_via_connect;
858 transport->fetch = fetch_refs_via_pack;
859 transport->push = NULL;
860 transport->push_refs = git_transport_push;
861 transport->disconnect = disconnect_git;
862 transport->smart_options = &(data->options);
865 static int is_local(const char *url)
867 const char *colon = strchr(url, ':');
868 const char *slash = strchr(url, '/');
869 return !colon || (slash && slash < colon) ||
870 has_dos_drive_prefix(url);
873 static int is_file(const char *url)
878 return S_ISREG(buf.st_mode);
881 static int external_specification_len(const char *url)
883 return strchr(url, ':') - url;
886 struct transport *transport_get(struct remote *remote, const char *url)
889 struct transport *ret = xcalloc(1, sizeof(*ret));
891 ret->progress = isatty(2);
894 die("No remote provided to transport_get()");
896 ret->got_remote_refs = 0;
897 ret->remote = remote;
898 helper = remote->foreign_vcs;
900 if (!url && remote->url)
901 url = remote->url[0];
904 /* maybe it is a foreign URL? */
908 while (is_urlschemechar(p == url, *p))
910 if (!prefixcmp(p, "::"))
911 helper = xstrndup(url, p - url);
915 transport_helper_init(ret, helper);
916 } else if (!prefixcmp(url, "rsync:")) {
917 ret->get_refs_list = get_refs_via_rsync;
918 ret->fetch = fetch_objs_via_rsync;
919 ret->push = rsync_transport_push;
920 ret->smart_options = NULL;
921 } else if (is_local(url) && is_file(url)) {
922 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
924 ret->get_refs_list = get_refs_from_bundle;
925 ret->fetch = fetch_refs_from_bundle;
926 ret->disconnect = close_bundle;
927 ret->smart_options = NULL;
928 } else if (!is_url(url)
929 || !prefixcmp(url, "file://")
930 || !prefixcmp(url, "git://")
931 || !prefixcmp(url, "ssh://")
932 || !prefixcmp(url, "git+ssh://")
933 || !prefixcmp(url, "ssh+git://")) {
934 /* These are builtin smart transports. */
935 struct git_transport_data *data = xcalloc(1, sizeof(*data));
937 ret->set_option = NULL;
938 ret->get_refs_list = get_refs_via_connect;
939 ret->fetch = fetch_refs_via_pack;
940 ret->push_refs = git_transport_push;
941 ret->connect = connect_git;
942 ret->disconnect = disconnect_git;
943 ret->smart_options = &(data->options);
946 data->got_remote_heads = 0;
948 /* Unknown protocol in URL. Pass to external handler. */
949 int len = external_specification_len(url);
950 char *handler = xmalloc(len + 1);
952 strncpy(handler, url, len);
953 transport_helper_init(ret, handler);
956 if (ret->smart_options) {
957 ret->smart_options->thin = 1;
958 ret->smart_options->uploadpack = "git-upload-pack";
959 if (remote->uploadpack)
960 ret->smart_options->uploadpack = remote->uploadpack;
961 ret->smart_options->receivepack = "git-receive-pack";
962 if (remote->receivepack)
963 ret->smart_options->receivepack = remote->receivepack;
969 int transport_set_option(struct transport *transport,
970 const char *name, const char *value)
972 int git_reports = 1, protocol_reports = 1;
974 if (transport->smart_options)
975 git_reports = set_git_option(transport->smart_options,
978 if (transport->set_option)
979 protocol_reports = transport->set_option(transport, name,
982 /* If either report is 0, report 0 (success). */
983 if (!git_reports || !protocol_reports)
985 /* If either reports -1 (invalid value), report -1. */
986 if ((git_reports == -1) || (protocol_reports == -1))
988 /* Otherwise if both report unknown, report unknown. */
992 void transport_set_verbosity(struct transport *transport, int verbosity,
996 transport->verbose = verbosity <= 3 ? verbosity : 3;
998 transport->verbose = -1;
1001 * Rules used to determine whether to report progress (processing aborts
1002 * when a rule is satisfied):
1004 * 1. Report progress, if force_progress is 1 (ie. --progress).
1005 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1006 * 3. Report progress if isatty(2) is 1.
1008 transport->progress = force_progress || (verbosity >= 0 && isatty(2));
1011 int transport_push(struct transport *transport,
1012 int refspec_nr, const char **refspec, int flags,
1013 int *nonfastforward)
1015 *nonfastforward = 0;
1016 transport_verify_remote_names(refspec_nr, refspec);
1018 if (transport->push) {
1019 /* Maybe FIXME. But no important transport uses this case. */
1020 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1021 die("This transport does not support using --set-upstream");
1023 return transport->push(transport, refspec_nr, refspec, flags);
1024 } else if (transport->push_refs) {
1025 struct ref *remote_refs =
1026 transport->get_refs_list(transport, 1);
1027 struct ref *local_refs = get_local_heads();
1028 int match_flags = MATCH_REFS_NONE;
1029 int verbose = (transport->verbose > 0);
1030 int quiet = (transport->verbose < 0);
1031 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1032 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1033 int push_ret, ret, err;
1035 if (flags & TRANSPORT_PUSH_ALL)
1036 match_flags |= MATCH_REFS_ALL;
1037 if (flags & TRANSPORT_PUSH_MIRROR)
1038 match_flags |= MATCH_REFS_MIRROR;
1040 if (match_refs(local_refs, &remote_refs,
1041 refspec_nr, refspec, match_flags)) {
1045 set_ref_status_for_push(remote_refs,
1046 flags & TRANSPORT_PUSH_MIRROR,
1047 flags & TRANSPORT_PUSH_FORCE);
1049 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
1050 struct ref *ref = remote_refs;
1051 for (; ref; ref = ref->next)
1052 if (!is_null_sha1(ref->new_sha1))
1053 push_unpushed_submodules(ref->new_sha1,transport->remote->name);
1056 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1057 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
1058 struct ref *ref = remote_refs;
1059 for (; ref; ref = ref->next)
1060 if (!is_null_sha1(ref->new_sha1) &&
1061 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1062 die("There are unpushed submodules, aborting.");
1065 push_ret = transport->push_refs(transport, remote_refs, flags);
1066 err = push_had_errors(remote_refs);
1067 ret = push_ret | err;
1070 transport_print_push_status(transport->url, remote_refs,
1071 verbose | porcelain, porcelain,
1074 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1075 set_upstreams(transport, remote_refs, pretend);
1077 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1079 for (ref = remote_refs; ref; ref = ref->next)
1080 transport_update_tracking_ref(transport->remote, ref, verbose);
1083 if (porcelain && !push_ret)
1085 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1086 fprintf(stderr, "Everything up-to-date\n");
1093 const struct ref *transport_get_remote_refs(struct transport *transport)
1095 if (!transport->got_remote_refs) {
1096 transport->remote_refs = transport->get_refs_list(transport, 0);
1097 transport->got_remote_refs = 1;
1100 return transport->remote_refs;
1103 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1106 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1107 struct ref **heads = NULL;
1110 for (rm = refs; rm; rm = rm->next) {
1113 !is_null_sha1(rm->old_sha1) &&
1114 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1116 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1117 heads[nr_heads++] = rm;
1122 * When deepening of a shallow repository is requested,
1123 * then local and remote refs are likely to still be equal.
1124 * Just feed them all to the fetch method in that case.
1125 * This condition shouldn't be met in a non-deepening fetch
1126 * (see builtin-fetch.c:quickfetch()).
1128 heads = xmalloc(nr_refs * sizeof(*heads));
1129 for (rm = refs; rm; rm = rm->next)
1130 heads[nr_heads++] = rm;
1133 rc = transport->fetch(transport, nr_heads, heads);
1139 void transport_unlock_pack(struct transport *transport)
1141 if (transport->pack_lockfile) {
1142 unlink_or_warn(transport->pack_lockfile);
1143 free(transport->pack_lockfile);
1144 transport->pack_lockfile = NULL;
1148 int transport_connect(struct transport *transport, const char *name,
1149 const char *exec, int fd[2])
1151 if (transport->connect)
1152 return transport->connect(transport, name, exec, fd);
1154 die("Operation not supported by protocol");
1157 int transport_disconnect(struct transport *transport)
1160 if (transport->disconnect)
1161 ret = transport->disconnect(transport);
1167 * Strip username (and password) from an url and return
1168 * it in a newly allocated string.
1170 char *transport_anonymize_url(const char *url)
1172 char *anon_url, *scheme_prefix, *anon_part;
1173 size_t anon_len, prefix_len = 0;
1175 anon_part = strchr(url, '@');
1176 if (is_local(url) || !anon_part)
1179 anon_len = strlen(++anon_part);
1180 scheme_prefix = strstr(url, "://");
1181 if (!scheme_prefix) {
1182 if (!strchr(anon_part, ':'))
1183 /* cannot be "me@there:/path/name" */
1187 /* make sure scheme is reasonable */
1188 for (cp = url; cp < scheme_prefix; cp++) {
1191 case '+': case '.': case '-':
1200 /* @ past the first slash does not count */
1201 cp = strchr(scheme_prefix + 3, '/');
1202 if (cp && cp < anon_part)
1204 prefix_len = scheme_prefix - url + 3;
1206 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1207 memcpy(anon_url, url, prefix_len);
1208 memcpy(anon_url + prefix_len, anon_part, anon_len);
1211 return xstrdup(url);
1214 struct alternate_refs_data {
1215 alternate_ref_fn *fn;
1219 static int refs_from_alternate_cb(struct alternate_object_database *e,
1224 struct remote *remote;
1225 struct transport *transport;
1226 const struct ref *extra;
1227 struct alternate_refs_data *cb = data;
1230 other = xstrdup(real_path(e->base));
1232 len = strlen(other);
1234 while (other[len-1] == '/')
1235 other[--len] = '\0';
1236 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1238 /* Is this a git repository with refs? */
1239 memcpy(other + len - 8, "/refs", 6);
1240 if (!is_directory(other))
1242 other[len - 8] = '\0';
1243 remote = remote_get(other);
1244 transport = transport_get(remote, other);
1245 for (extra = transport_get_remote_refs(transport);
1247 extra = extra->next)
1248 cb->fn(extra, cb->data);
1249 transport_disconnect(transport);
1254 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1256 struct alternate_refs_data cb;
1259 foreach_alt_odb(refs_from_alternate_cb, &cb);