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;
490 data->conn = git_connect(data->fd, transport->url,
491 for_push ? data->options.receivepack :
492 data->options.uploadpack,
493 verbose ? CONNECT_VERBOSE : 0);
498 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
500 struct git_transport_data *data = transport->data;
503 connect_setup(transport, for_push, 0);
504 get_remote_heads(data->fd[0], &refs, 0, NULL,
505 for_push ? REF_NORMAL : 0, &data->extra_have);
506 data->got_remote_heads = 1;
511 static int fetch_refs_via_pack(struct transport *transport,
512 int nr_heads, struct ref **to_fetch)
514 struct git_transport_data *data = transport->data;
515 char **heads = xmalloc(nr_heads * sizeof(*heads));
516 char **origh = xmalloc(nr_heads * sizeof(*origh));
517 const struct ref *refs;
518 char *dest = xstrdup(transport->url);
519 struct fetch_pack_args args;
521 struct ref *refs_tmp = NULL;
523 memset(&args, 0, sizeof(args));
524 args.uploadpack = data->options.uploadpack;
525 args.keep_pack = data->options.keep;
527 args.use_thin_pack = data->options.thin;
528 args.include_tag = data->options.followtags;
529 args.verbose = (transport->verbose > 0);
530 args.quiet = (transport->verbose < 0);
531 args.no_progress = !transport->progress;
532 args.depth = data->options.depth;
534 for (i = 0; i < nr_heads; i++)
535 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
537 if (!data->got_remote_heads) {
538 connect_setup(transport, 0, 0);
539 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL);
540 data->got_remote_heads = 1;
543 refs = fetch_pack(&args, data->fd, data->conn,
544 refs_tmp ? refs_tmp : transport->remote_refs,
545 dest, nr_heads, heads, &transport->pack_lockfile);
548 if (finish_connect(data->conn))
551 data->got_remote_heads = 0;
555 for (i = 0; i < nr_heads; i++)
560 return (refs ? 0 : -1);
563 static int push_had_errors(struct ref *ref)
565 for (; ref; ref = ref->next) {
566 switch (ref->status) {
567 case REF_STATUS_NONE:
568 case REF_STATUS_UPTODATE:
578 int transport_refs_pushed(struct ref *ref)
580 for (; ref; ref = ref->next) {
581 switch(ref->status) {
582 case REF_STATUS_NONE:
583 case REF_STATUS_UPTODATE:
592 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
596 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
602 if (!remote_find_tracking(remote, &rs)) {
604 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
606 delete_ref(rs.dst, NULL, 0);
608 update_ref("update by push", rs.dst,
609 ref->new_sha1, NULL, 0, 0);
614 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
618 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
620 fprintf(stdout, "%c\t:%s\t", flag, to->name);
622 fprintf(stdout, "%s (%s)\n", summary, msg);
624 fprintf(stdout, "%s\n", summary);
626 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
628 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
630 fputs(prettify_refname(to->name), stderr);
640 static const char *status_abbrev(unsigned char sha1[20])
642 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
645 static void print_ok_ref_status(struct ref *ref, int porcelain)
648 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
649 else if (is_null_sha1(ref->old_sha1))
650 print_ref_status('*',
651 (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" :
653 ref, ref->peer_ref, NULL, porcelain);
659 strcpy(quickref, status_abbrev(ref->old_sha1));
660 if (ref->nonfastforward) {
661 strcat(quickref, "...");
663 msg = "forced update";
665 strcat(quickref, "..");
669 strcat(quickref, status_abbrev(ref->new_sha1));
671 print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain);
675 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
678 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
680 switch(ref->status) {
681 case REF_STATUS_NONE:
682 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
684 case REF_STATUS_REJECT_NODELETE:
685 print_ref_status('!', "[rejected]", ref, NULL,
686 "remote does not support deleting refs", porcelain);
688 case REF_STATUS_UPTODATE:
689 print_ref_status('=', "[up to date]", ref,
690 ref->peer_ref, NULL, porcelain);
692 case REF_STATUS_REJECT_NONFASTFORWARD:
693 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
694 "non-fast-forward", porcelain);
696 case REF_STATUS_REMOTE_REJECT:
697 print_ref_status('!', "[remote rejected]", ref,
698 ref->deletion ? NULL : ref->peer_ref,
699 ref->remote_status, porcelain);
701 case REF_STATUS_EXPECTING_REPORT:
702 print_ref_status('!', "[remote failure]", ref,
703 ref->deletion ? NULL : ref->peer_ref,
704 "remote failed to report status", porcelain);
707 print_ok_ref_status(ref, porcelain);
714 void transport_print_push_status(const char *dest, struct ref *refs,
715 int verbose, int porcelain, int *nonfastforward)
721 for (ref = refs; ref; ref = ref->next)
722 if (ref->status == REF_STATUS_UPTODATE)
723 n += print_one_push_status(ref, dest, n, porcelain);
726 for (ref = refs; ref; ref = ref->next)
727 if (ref->status == REF_STATUS_OK)
728 n += print_one_push_status(ref, dest, n, porcelain);
731 for (ref = refs; ref; ref = ref->next) {
732 if (ref->status != REF_STATUS_NONE &&
733 ref->status != REF_STATUS_UPTODATE &&
734 ref->status != REF_STATUS_OK)
735 n += print_one_push_status(ref, dest, n, porcelain);
736 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD)
741 void transport_verify_remote_names(int nr_heads, const char **heads)
745 for (i = 0; i < nr_heads; i++) {
746 const char *local = heads[i];
747 const char *remote = strrchr(heads[i], ':');
752 /* A matching refspec is okay. */
753 if (remote == local && remote[1] == '\0')
756 remote = remote ? (remote + 1) : local;
757 switch (check_ref_format(remote)) {
759 case CHECK_REF_FORMAT_ONELEVEL:
760 /* ok but a single level -- that is fine for
763 case CHECK_REF_FORMAT_WILDCARD:
764 /* ok but ends with a pattern-match character */
767 die("remote part of refspec is not a valid name in %s",
772 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
774 struct git_transport_data *data = transport->data;
775 struct send_pack_args args;
778 if (!data->got_remote_heads) {
779 struct ref *tmp_refs;
780 connect_setup(transport, 1, 0);
782 get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL,
784 data->got_remote_heads = 1;
787 memset(&args, 0, sizeof(args));
788 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
789 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
790 args.use_thin_pack = data->options.thin;
791 args.verbose = (transport->verbose > 0);
792 args.quiet = (transport->verbose < 0);
793 args.progress = transport->progress;
794 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
795 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
797 ret = send_pack(&args, data->fd, data->conn, remote_refs,
802 ret |= finish_connect(data->conn);
804 data->got_remote_heads = 0;
809 static int connect_git(struct transport *transport, const char *name,
810 const char *executable, int fd[2])
812 struct git_transport_data *data = transport->data;
813 data->conn = git_connect(data->fd, transport->url,
820 static int disconnect_git(struct transport *transport)
822 struct git_transport_data *data = transport->data;
824 if (data->got_remote_heads)
825 packet_flush(data->fd[1]);
828 finish_connect(data->conn);
835 void transport_take_over(struct transport *transport,
836 struct child_process *child)
838 struct git_transport_data *data;
840 if (!transport->smart_options)
841 die("Bug detected: Taking over transport requires non-NULL "
842 "smart_options field.");
844 data = xcalloc(1, sizeof(*data));
845 data->options = *transport->smart_options;
847 data->fd[0] = data->conn->out;
848 data->fd[1] = data->conn->in;
849 data->got_remote_heads = 0;
850 transport->data = data;
852 transport->set_option = NULL;
853 transport->get_refs_list = get_refs_via_connect;
854 transport->fetch = fetch_refs_via_pack;
855 transport->push = NULL;
856 transport->push_refs = git_transport_push;
857 transport->disconnect = disconnect_git;
858 transport->smart_options = &(data->options);
861 static int is_local(const char *url)
863 const char *colon = strchr(url, ':');
864 const char *slash = strchr(url, '/');
865 return !colon || (slash && slash < colon) ||
866 has_dos_drive_prefix(url);
869 static int is_file(const char *url)
874 return S_ISREG(buf.st_mode);
877 static int external_specification_len(const char *url)
879 return strchr(url, ':') - url;
882 struct transport *transport_get(struct remote *remote, const char *url)
885 struct transport *ret = xcalloc(1, sizeof(*ret));
887 ret->progress = isatty(2);
890 die("No remote provided to transport_get()");
892 ret->got_remote_refs = 0;
893 ret->remote = remote;
894 helper = remote->foreign_vcs;
896 if (!url && remote->url)
897 url = remote->url[0];
900 /* maybe it is a foreign URL? */
904 while (is_urlschemechar(p == url, *p))
906 if (!prefixcmp(p, "::"))
907 helper = xstrndup(url, p - url);
911 transport_helper_init(ret, helper);
912 } else if (!prefixcmp(url, "rsync:")) {
913 ret->get_refs_list = get_refs_via_rsync;
914 ret->fetch = fetch_objs_via_rsync;
915 ret->push = rsync_transport_push;
916 ret->smart_options = NULL;
917 } else if (is_local(url) && is_file(url)) {
918 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
920 ret->get_refs_list = get_refs_from_bundle;
921 ret->fetch = fetch_refs_from_bundle;
922 ret->disconnect = close_bundle;
923 ret->smart_options = NULL;
924 } else if (!is_url(url)
925 || !prefixcmp(url, "file://")
926 || !prefixcmp(url, "git://")
927 || !prefixcmp(url, "ssh://")
928 || !prefixcmp(url, "git+ssh://")
929 || !prefixcmp(url, "ssh+git://")) {
930 /* These are builtin smart transports. */
931 struct git_transport_data *data = xcalloc(1, sizeof(*data));
933 ret->set_option = NULL;
934 ret->get_refs_list = get_refs_via_connect;
935 ret->fetch = fetch_refs_via_pack;
936 ret->push_refs = git_transport_push;
937 ret->connect = connect_git;
938 ret->disconnect = disconnect_git;
939 ret->smart_options = &(data->options);
942 data->got_remote_heads = 0;
944 /* Unknown protocol in URL. Pass to external handler. */
945 int len = external_specification_len(url);
946 char *handler = xmalloc(len + 1);
948 strncpy(handler, url, len);
949 transport_helper_init(ret, handler);
952 if (ret->smart_options) {
953 ret->smart_options->thin = 1;
954 ret->smart_options->uploadpack = "git-upload-pack";
955 if (remote->uploadpack)
956 ret->smart_options->uploadpack = remote->uploadpack;
957 ret->smart_options->receivepack = "git-receive-pack";
958 if (remote->receivepack)
959 ret->smart_options->receivepack = remote->receivepack;
965 int transport_set_option(struct transport *transport,
966 const char *name, const char *value)
968 int git_reports = 1, protocol_reports = 1;
970 if (transport->smart_options)
971 git_reports = set_git_option(transport->smart_options,
974 if (transport->set_option)
975 protocol_reports = transport->set_option(transport, name,
978 /* If either report is 0, report 0 (success). */
979 if (!git_reports || !protocol_reports)
981 /* If either reports -1 (invalid value), report -1. */
982 if ((git_reports == -1) || (protocol_reports == -1))
984 /* Otherwise if both report unknown, report unknown. */
988 void transport_set_verbosity(struct transport *transport, int verbosity,
992 transport->verbose = verbosity <= 3 ? verbosity : 3;
994 transport->verbose = -1;
997 * Rules used to determine whether to report progress (processing aborts
998 * when a rule is satisfied):
1000 * 1. Report progress, if force_progress is 1 (ie. --progress).
1001 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1002 * 3. Report progress if isatty(2) is 1.
1004 transport->progress = force_progress || (verbosity >= 0 && isatty(2));
1007 int transport_push(struct transport *transport,
1008 int refspec_nr, const char **refspec, int flags,
1009 int *nonfastforward)
1011 *nonfastforward = 0;
1012 transport_verify_remote_names(refspec_nr, refspec);
1014 if (transport->push) {
1015 /* Maybe FIXME. But no important transport uses this case. */
1016 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1017 die("This transport does not support using --set-upstream");
1019 return transport->push(transport, refspec_nr, refspec, flags);
1020 } else if (transport->push_refs) {
1021 struct ref *remote_refs =
1022 transport->get_refs_list(transport, 1);
1023 struct ref *local_refs = get_local_heads();
1024 int match_flags = MATCH_REFS_NONE;
1025 int verbose = (transport->verbose > 0);
1026 int quiet = (transport->verbose < 0);
1027 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1028 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1029 int push_ret, ret, err;
1031 if (flags & TRANSPORT_PUSH_ALL)
1032 match_flags |= MATCH_REFS_ALL;
1033 if (flags & TRANSPORT_PUSH_MIRROR)
1034 match_flags |= MATCH_REFS_MIRROR;
1036 if (match_refs(local_refs, &remote_refs,
1037 refspec_nr, refspec, match_flags)) {
1041 set_ref_status_for_push(remote_refs,
1042 flags & TRANSPORT_PUSH_MIRROR,
1043 flags & TRANSPORT_PUSH_FORCE);
1045 if ((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) && !is_bare_repository()) {
1046 struct ref *ref = remote_refs;
1047 for (; ref; ref = ref->next)
1048 if (!is_null_sha1(ref->new_sha1) &&
1049 check_submodule_needs_pushing(ref->new_sha1,transport->remote->name))
1050 die("There are unpushed submodules, aborting.");
1053 push_ret = transport->push_refs(transport, remote_refs, flags);
1054 err = push_had_errors(remote_refs);
1055 ret = push_ret | err;
1058 transport_print_push_status(transport->url, remote_refs,
1059 verbose | porcelain, porcelain,
1062 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1063 set_upstreams(transport, remote_refs, pretend);
1065 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1067 for (ref = remote_refs; ref; ref = ref->next)
1068 transport_update_tracking_ref(transport->remote, ref, verbose);
1071 if (porcelain && !push_ret)
1073 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1074 fprintf(stderr, "Everything up-to-date\n");
1081 const struct ref *transport_get_remote_refs(struct transport *transport)
1083 if (!transport->got_remote_refs) {
1084 transport->remote_refs = transport->get_refs_list(transport, 0);
1085 transport->got_remote_refs = 1;
1088 return transport->remote_refs;
1091 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1094 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1095 struct ref **heads = NULL;
1098 for (rm = refs; rm; rm = rm->next) {
1101 !is_null_sha1(rm->old_sha1) &&
1102 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
1104 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1105 heads[nr_heads++] = rm;
1110 * When deepening of a shallow repository is requested,
1111 * then local and remote refs are likely to still be equal.
1112 * Just feed them all to the fetch method in that case.
1113 * This condition shouldn't be met in a non-deepening fetch
1114 * (see builtin-fetch.c:quickfetch()).
1116 heads = xmalloc(nr_refs * sizeof(*heads));
1117 for (rm = refs; rm; rm = rm->next)
1118 heads[nr_heads++] = rm;
1121 rc = transport->fetch(transport, nr_heads, heads);
1127 void transport_unlock_pack(struct transport *transport)
1129 if (transport->pack_lockfile) {
1130 unlink_or_warn(transport->pack_lockfile);
1131 free(transport->pack_lockfile);
1132 transport->pack_lockfile = NULL;
1136 int transport_connect(struct transport *transport, const char *name,
1137 const char *exec, int fd[2])
1139 if (transport->connect)
1140 return transport->connect(transport, name, exec, fd);
1142 die("Operation not supported by protocol");
1145 int transport_disconnect(struct transport *transport)
1148 if (transport->disconnect)
1149 ret = transport->disconnect(transport);
1155 * Strip username (and password) from an url and return
1156 * it in a newly allocated string.
1158 char *transport_anonymize_url(const char *url)
1160 char *anon_url, *scheme_prefix, *anon_part;
1161 size_t anon_len, prefix_len = 0;
1163 anon_part = strchr(url, '@');
1164 if (is_local(url) || !anon_part)
1167 anon_len = strlen(++anon_part);
1168 scheme_prefix = strstr(url, "://");
1169 if (!scheme_prefix) {
1170 if (!strchr(anon_part, ':'))
1171 /* cannot be "me@there:/path/name" */
1175 /* make sure scheme is reasonable */
1176 for (cp = url; cp < scheme_prefix; cp++) {
1179 case '+': case '.': case '-':
1188 /* @ past the first slash does not count */
1189 cp = strchr(scheme_prefix + 3, '/');
1190 if (cp && cp < anon_part)
1192 prefix_len = scheme_prefix - url + 3;
1194 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1195 memcpy(anon_url, url, prefix_len);
1196 memcpy(anon_url + prefix_len, anon_part, anon_len);
1199 return xstrdup(url);
1202 struct alternate_refs_data {
1203 alternate_ref_fn *fn;
1207 static int refs_from_alternate_cb(struct alternate_object_database *e,
1212 struct remote *remote;
1213 struct transport *transport;
1214 const struct ref *extra;
1215 struct alternate_refs_data *cb = data;
1218 other = xstrdup(real_path(e->base));
1220 len = strlen(other);
1222 while (other[len-1] == '/')
1223 other[--len] = '\0';
1224 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1226 /* Is this a git repository with refs? */
1227 memcpy(other + len - 8, "/refs", 6);
1228 if (!is_directory(other))
1230 other[len - 8] = '\0';
1231 remote = remote_get(other);
1232 transport = transport_get(remote, other);
1233 for (extra = transport_get_remote_refs(transport);
1235 extra = extra->next)
1236 cb->fn(extra, cb->data);
1237 transport_disconnect(transport);
1242 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1244 struct alternate_refs_data cb;
1247 foreach_alt_odb(refs_from_alternate_cb, &cb);