3 #include "run-command.h"
5 #include "fetch-pack.h"
15 #include "submodule.h"
16 #include "string-list.h"
17 #include "sha1-array.h"
20 static void set_tracking(struct transport *transport, struct ref *refs,
21 int pretend, int publish)
24 for (ref = refs; ref; ref = ref->next) {
25 const char *localname;
27 const char *remotename;
28 unsigned char sha[20];
31 * Check suitability for tracking. Must be successful /
32 * already up-to-date ref create/modify (not delete).
34 if (ref->status != REF_STATUS_OK &&
35 ref->status != REF_STATUS_UPTODATE)
39 if (is_null_oid(&ref->new_oid))
42 /* Follow symbolic refs (mainly for HEAD). */
43 localname = ref->peer_ref->name;
44 remotename = ref->name;
45 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
47 if (tmp && flag & REF_ISSYMREF &&
48 starts_with(tmp, "refs/heads/"))
51 /* Both source and destination must be local branches. */
52 if (!localname || !starts_with(localname, "refs/heads/"))
54 if (!remotename || !starts_with(remotename, "refs/heads/"))
59 install_branch_publish(localname + 11,
60 transport->remote->name,
63 install_branch_config(BRANCH_CONFIG_VERBOSE,
64 localname + 11, transport->remote->name,
67 printf("Would set %s of '%s' to '%s' of '%s'\n",
68 publish ? "publish" : "upstream",
69 localname + 11, remotename + 11,
70 transport->remote->name);
74 struct bundle_transport_data {
76 struct bundle_header header;
79 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
81 struct bundle_transport_data *data = transport->data;
82 struct ref *result = NULL;
90 data->fd = read_bundle_header(transport->url, &data->header);
92 die ("Could not read bundle '%s'.", transport->url);
93 for (i = 0; i < data->header.references.nr; i++) {
94 struct ref_list_entry *e = data->header.references.list + i;
95 struct ref *ref = alloc_ref(e->name);
96 hashcpy(ref->old_oid.hash, e->sha1);
103 static int fetch_refs_from_bundle(struct transport *transport,
104 int nr_heads, struct ref **to_fetch)
106 struct bundle_transport_data *data = transport->data;
107 return unbundle(&data->header, data->fd,
108 transport->progress ? BUNDLE_VERBOSE : 0);
111 static int close_bundle(struct transport *transport)
113 struct bundle_transport_data *data = transport->data;
120 struct git_transport_data {
121 struct git_transport_options options;
122 struct child_process *conn;
124 unsigned got_remote_heads : 1;
125 struct sha1_array extra_have;
126 struct sha1_array shallow;
129 static int set_git_option(struct git_transport_options *opts,
130 const char *name, const char *value)
132 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
133 opts->uploadpack = value;
135 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
136 opts->receivepack = value;
138 } else if (!strcmp(name, TRANS_OPT_THIN)) {
139 opts->thin = !!value;
141 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
142 opts->followtags = !!value;
144 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
145 opts->keep = !!value;
147 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
148 opts->update_shallow = !!value;
150 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
155 opts->depth = strtol(value, &end, 0);
157 die("transport: invalid depth option '%s'", value);
164 static int connect_setup(struct transport *transport, int for_push)
166 struct git_transport_data *data = transport->data;
167 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
172 switch (transport->family) {
173 case TRANSPORT_FAMILY_ALL: break;
174 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
175 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
178 data->conn = git_connect(data->fd, transport->url,
179 for_push ? data->options.receivepack :
180 data->options.uploadpack,
186 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
188 struct git_transport_data *data = transport->data;
191 connect_setup(transport, for_push);
192 get_remote_heads(data->fd[0], NULL, 0, &refs,
193 for_push ? REF_NORMAL : 0,
196 data->got_remote_heads = 1;
201 static int fetch_refs_via_pack(struct transport *transport,
202 int nr_heads, struct ref **to_fetch)
204 struct git_transport_data *data = transport->data;
206 char *dest = xstrdup(transport->url);
207 struct fetch_pack_args args;
208 struct ref *refs_tmp = NULL;
210 memset(&args, 0, sizeof(args));
211 args.uploadpack = data->options.uploadpack;
212 args.keep_pack = data->options.keep;
214 args.use_thin_pack = data->options.thin;
215 args.include_tag = data->options.followtags;
216 args.verbose = (transport->verbose > 1);
217 args.quiet = (transport->verbose < 0);
218 args.no_progress = !transport->progress;
219 args.depth = data->options.depth;
220 args.check_self_contained_and_connected =
221 data->options.check_self_contained_and_connected;
222 args.cloning = transport->cloning;
223 args.update_shallow = data->options.update_shallow;
225 if (!data->got_remote_heads) {
226 connect_setup(transport, 0);
227 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
228 NULL, &data->shallow);
229 data->got_remote_heads = 1;
232 refs = fetch_pack(&args, data->fd, data->conn,
233 refs_tmp ? refs_tmp : transport->remote_refs,
234 dest, to_fetch, nr_heads, &data->shallow,
235 &transport->pack_lockfile);
238 if (finish_connect(data->conn)) {
243 data->got_remote_heads = 0;
244 data->options.self_contained_and_connected =
245 args.self_contained_and_connected;
250 return (refs ? 0 : -1);
253 static int push_had_errors(struct ref *ref)
255 for (; ref; ref = ref->next) {
256 switch (ref->status) {
257 case REF_STATUS_NONE:
258 case REF_STATUS_UPTODATE:
268 int transport_refs_pushed(struct ref *ref)
270 for (; ref; ref = ref->next) {
271 switch(ref->status) {
272 case REF_STATUS_NONE:
273 case REF_STATUS_UPTODATE:
282 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
286 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
292 if (!remote_find_tracking(remote, &rs)) {
294 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
296 delete_ref(rs.dst, NULL, 0);
298 update_ref("update by push", rs.dst,
299 ref->new_oid.hash, NULL, 0, 0);
304 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
308 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
310 fprintf(stdout, "%c\t:%s\t", flag, to->name);
312 fprintf(stdout, "%s (%s)\n", summary, msg);
314 fprintf(stdout, "%s\n", summary);
316 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
318 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
320 fputs(prettify_refname(to->name), stderr);
330 static const char *status_abbrev(unsigned char sha1[20])
332 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
335 static void print_ok_ref_status(struct ref *ref, int porcelain)
338 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
339 else if (is_null_oid(&ref->old_oid))
340 print_ref_status('*',
341 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
343 ref, ref->peer_ref, NULL, porcelain);
345 struct strbuf quickref = STRBUF_INIT;
349 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
350 if (ref->forced_update) {
351 strbuf_addstr(&quickref, "...");
353 msg = "forced update";
355 strbuf_addstr(&quickref, "..");
359 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
361 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
362 strbuf_release(&quickref);
366 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
369 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
371 switch(ref->status) {
372 case REF_STATUS_NONE:
373 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
375 case REF_STATUS_REJECT_NODELETE:
376 print_ref_status('!', "[rejected]", ref, NULL,
377 "remote does not support deleting refs", porcelain);
379 case REF_STATUS_UPTODATE:
380 print_ref_status('=', "[up to date]", ref,
381 ref->peer_ref, NULL, porcelain);
383 case REF_STATUS_REJECT_NONFASTFORWARD:
384 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
385 "non-fast-forward", porcelain);
387 case REF_STATUS_REJECT_ALREADY_EXISTS:
388 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
389 "already exists", porcelain);
391 case REF_STATUS_REJECT_FETCH_FIRST:
392 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
393 "fetch first", porcelain);
395 case REF_STATUS_REJECT_NEEDS_FORCE:
396 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
397 "needs force", porcelain);
399 case REF_STATUS_REJECT_STALE:
400 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
401 "stale info", porcelain);
403 case REF_STATUS_REJECT_SHALLOW:
404 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
405 "new shallow roots not allowed", porcelain);
407 case REF_STATUS_REMOTE_REJECT:
408 print_ref_status('!', "[remote rejected]", ref,
409 ref->deletion ? NULL : ref->peer_ref,
410 ref->remote_status, porcelain);
412 case REF_STATUS_EXPECTING_REPORT:
413 print_ref_status('!', "[remote failure]", ref,
414 ref->deletion ? NULL : ref->peer_ref,
415 "remote failed to report status", porcelain);
417 case REF_STATUS_ATOMIC_PUSH_FAILED:
418 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
419 "atomic push failed", porcelain);
422 print_ok_ref_status(ref, porcelain);
429 void transport_print_push_status(const char *dest, struct ref *refs,
430 int verbose, int porcelain, unsigned int *reject_reasons)
434 unsigned char head_sha1[20];
437 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
440 for (ref = refs; ref; ref = ref->next)
441 if (ref->status == REF_STATUS_UPTODATE)
442 n += print_one_push_status(ref, dest, n, porcelain);
445 for (ref = refs; ref; ref = ref->next)
446 if (ref->status == REF_STATUS_OK)
447 n += print_one_push_status(ref, dest, n, porcelain);
450 for (ref = refs; ref; ref = ref->next) {
451 if (ref->status != REF_STATUS_NONE &&
452 ref->status != REF_STATUS_UPTODATE &&
453 ref->status != REF_STATUS_OK)
454 n += print_one_push_status(ref, dest, n, porcelain);
455 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
456 if (head != NULL && !strcmp(head, ref->name))
457 *reject_reasons |= REJECT_NON_FF_HEAD;
459 *reject_reasons |= REJECT_NON_FF_OTHER;
460 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
461 *reject_reasons |= REJECT_ALREADY_EXISTS;
462 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
463 *reject_reasons |= REJECT_FETCH_FIRST;
464 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
465 *reject_reasons |= REJECT_NEEDS_FORCE;
471 void transport_verify_remote_names(int nr_heads, const char **heads)
475 for (i = 0; i < nr_heads; i++) {
476 const char *local = heads[i];
477 const char *remote = strrchr(heads[i], ':');
482 /* A matching refspec is okay. */
483 if (remote == local && remote[1] == '\0')
486 remote = remote ? (remote + 1) : local;
487 if (check_refname_format(remote,
488 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
489 die("remote part of refspec is not a valid name in %s",
494 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
496 struct git_transport_data *data = transport->data;
497 struct send_pack_args args;
500 if (!data->got_remote_heads) {
501 struct ref *tmp_refs;
502 connect_setup(transport, 1);
504 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
505 NULL, &data->shallow);
506 data->got_remote_heads = 1;
509 memset(&args, 0, sizeof(args));
510 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
511 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
512 args.use_thin_pack = data->options.thin;
513 args.verbose = (transport->verbose > 0);
514 args.quiet = (transport->verbose < 0);
515 args.progress = transport->progress;
516 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
517 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
518 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
519 args.url = transport->url;
521 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
522 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
523 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
524 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
526 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
528 ret = send_pack(&args, data->fd, data->conn, remote_refs,
533 ret |= finish_connect(data->conn);
535 data->got_remote_heads = 0;
540 static int connect_git(struct transport *transport, const char *name,
541 const char *executable, int fd[2])
543 struct git_transport_data *data = transport->data;
544 data->conn = git_connect(data->fd, transport->url,
551 static int disconnect_git(struct transport *transport)
553 struct git_transport_data *data = transport->data;
555 if (data->got_remote_heads)
556 packet_flush(data->fd[1]);
559 finish_connect(data->conn);
566 void transport_take_over(struct transport *transport,
567 struct child_process *child)
569 struct git_transport_data *data;
571 if (!transport->smart_options)
572 die("Bug detected: Taking over transport requires non-NULL "
573 "smart_options field.");
575 data = xcalloc(1, sizeof(*data));
576 data->options = *transport->smart_options;
578 data->fd[0] = data->conn->out;
579 data->fd[1] = data->conn->in;
580 data->got_remote_heads = 0;
581 transport->data = data;
583 transport->set_option = NULL;
584 transport->get_refs_list = get_refs_via_connect;
585 transport->fetch = fetch_refs_via_pack;
586 transport->push = NULL;
587 transport->push_refs = git_transport_push;
588 transport->disconnect = disconnect_git;
589 transport->smart_options = &(data->options);
591 transport->cannot_reuse = 1;
594 static int is_file(const char *url)
599 return S_ISREG(buf.st_mode);
602 static int external_specification_len(const char *url)
604 return strchr(url, ':') - url;
607 static const struct string_list *protocol_whitelist(void)
609 static int enabled = -1;
610 static struct string_list allowed = STRING_LIST_INIT_DUP;
613 const char *v = getenv("GIT_ALLOW_PROTOCOL");
615 string_list_split(&allowed, v, ':', -1);
616 string_list_sort(&allowed);
623 return enabled ? &allowed : NULL;
626 int is_transport_allowed(const char *type)
628 const struct string_list *allowed = protocol_whitelist();
629 return !allowed || string_list_has_string(allowed, type);
632 void transport_check_allowed(const char *type)
634 if (!is_transport_allowed(type))
635 die("transport '%s' not allowed", type);
638 int transport_restrict_protocols(void)
640 return !!protocol_whitelist();
643 struct transport *transport_get(struct remote *remote, const char *url)
646 struct transport *ret = xcalloc(1, sizeof(*ret));
648 ret->progress = isatty(2);
651 die("No remote provided to transport_get()");
653 ret->got_remote_refs = 0;
654 ret->remote = remote;
655 helper = remote->foreign_vcs;
657 if (!url && remote->url)
658 url = remote->url[0];
661 /* maybe it is a foreign URL? */
665 while (is_urlschemechar(p == url, *p))
667 if (starts_with(p, "::"))
668 helper = xstrndup(url, p - url);
672 transport_helper_init(ret, helper);
673 } else if (starts_with(url, "rsync:")) {
674 die("git-over-rsync is no longer supported");
675 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
676 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
677 transport_check_allowed("file");
679 ret->get_refs_list = get_refs_from_bundle;
680 ret->fetch = fetch_refs_from_bundle;
681 ret->disconnect = close_bundle;
682 ret->smart_options = NULL;
683 } else if (!is_url(url)
684 || starts_with(url, "file://")
685 || starts_with(url, "git://")
686 || starts_with(url, "ssh://")
687 || starts_with(url, "git+ssh://") /* deprecated - do not use */
688 || starts_with(url, "ssh+git://") /* deprecated - do not use */
691 * These are builtin smart transports; "allowed" transports
692 * will be checked individually in git_connect.
694 struct git_transport_data *data = xcalloc(1, sizeof(*data));
696 ret->set_option = NULL;
697 ret->get_refs_list = get_refs_via_connect;
698 ret->fetch = fetch_refs_via_pack;
699 ret->push_refs = git_transport_push;
700 ret->connect = connect_git;
701 ret->disconnect = disconnect_git;
702 ret->smart_options = &(data->options);
705 data->got_remote_heads = 0;
707 /* Unknown protocol in URL. Pass to external handler. */
708 int len = external_specification_len(url);
709 char *handler = xmemdupz(url, len);
710 transport_helper_init(ret, handler);
713 if (ret->smart_options) {
714 ret->smart_options->thin = 1;
715 ret->smart_options->uploadpack = "git-upload-pack";
716 if (remote->uploadpack)
717 ret->smart_options->uploadpack = remote->uploadpack;
718 ret->smart_options->receivepack = "git-receive-pack";
719 if (remote->receivepack)
720 ret->smart_options->receivepack = remote->receivepack;
726 int transport_set_option(struct transport *transport,
727 const char *name, const char *value)
729 int git_reports = 1, protocol_reports = 1;
731 if (transport->smart_options)
732 git_reports = set_git_option(transport->smart_options,
735 if (transport->set_option)
736 protocol_reports = transport->set_option(transport, name,
739 /* If either report is 0, report 0 (success). */
740 if (!git_reports || !protocol_reports)
742 /* If either reports -1 (invalid value), report -1. */
743 if ((git_reports == -1) || (protocol_reports == -1))
745 /* Otherwise if both report unknown, report unknown. */
749 void transport_set_verbosity(struct transport *transport, int verbosity,
753 transport->verbose = verbosity <= 3 ? verbosity : 3;
755 transport->verbose = -1;
758 * Rules used to determine whether to report progress (processing aborts
759 * when a rule is satisfied):
761 * . Report progress, if force_progress is 1 (ie. --progress).
762 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
763 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
764 * . Report progress if isatty(2) is 1.
766 if (force_progress >= 0)
767 transport->progress = !!force_progress;
769 transport->progress = verbosity >= 0 && isatty(2);
772 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
776 fprintf(stderr, "The following submodule paths contain changes that can\n"
777 "not be found on any remote:\n");
778 for (i = 0; i < needs_pushing->nr; i++)
779 printf(" %s\n", needs_pushing->items[i].string);
780 fprintf(stderr, "\nPlease try\n\n"
781 " git push --recurse-submodules=on-demand\n\n"
782 "or cd to the path and use\n\n"
784 "to push them to a remote.\n\n");
786 string_list_clear(needs_pushing, 0);
791 static int run_pre_push_hook(struct transport *transport,
792 struct ref *remote_refs)
796 struct child_process proc = CHILD_PROCESS_INIT;
800 if (!(argv[0] = find_hook("pre-push")))
803 argv[1] = transport->remote->name;
804 argv[2] = transport->url;
810 if (start_command(&proc)) {
811 finish_command(&proc);
815 sigchain_push(SIGPIPE, SIG_IGN);
817 strbuf_init(&buf, 256);
819 for (r = remote_refs; r; r = r->next) {
820 if (!r->peer_ref) continue;
821 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
822 if (r->status == REF_STATUS_REJECT_STALE) continue;
823 if (r->status == REF_STATUS_UPTODATE) continue;
826 strbuf_addf( &buf, "%s %s %s %s\n",
827 r->peer_ref->name, oid_to_hex(&r->new_oid),
828 r->name, oid_to_hex(&r->old_oid));
830 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
831 /* We do not mind if a hook does not read all refs. */
838 strbuf_release(&buf);
844 sigchain_pop(SIGPIPE);
846 x = finish_command(&proc);
853 int transport_push(struct transport *transport,
854 int refspec_nr, const char **refspec, int flags,
855 unsigned int *reject_reasons)
858 transport_verify_remote_names(refspec_nr, refspec);
860 if (transport->push) {
861 /* Maybe FIXME. But no important transport uses this case. */
862 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
863 die("This transport does not support using --set-upstream");
864 if (flags & TRANSPORT_PUSH_SET_PUBLISH)
865 die("This transport does not support using --set-publish");
867 return transport->push(transport, refspec_nr, refspec, flags);
868 } else if (transport->push_refs) {
869 struct ref *remote_refs;
870 struct ref *local_refs = get_local_heads();
871 int match_flags = MATCH_REFS_NONE;
872 int verbose = (transport->verbose > 0);
873 int quiet = (transport->verbose < 0);
874 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
875 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
876 int push_ret, ret, err;
878 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
881 remote_refs = transport->get_refs_list(transport, 1);
883 if (flags & TRANSPORT_PUSH_ALL)
884 match_flags |= MATCH_REFS_ALL;
885 if (flags & TRANSPORT_PUSH_MIRROR)
886 match_flags |= MATCH_REFS_MIRROR;
887 if (flags & TRANSPORT_PUSH_PRUNE)
888 match_flags |= MATCH_REFS_PRUNE;
889 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
890 match_flags |= MATCH_REFS_FOLLOW_TAGS;
892 if (match_push_refs(local_refs, &remote_refs,
893 refspec_nr, refspec, match_flags)) {
897 if (transport->smart_options &&
898 transport->smart_options->cas &&
899 !is_empty_cas(transport->smart_options->cas))
900 apply_push_cas(transport->smart_options->cas,
901 transport->remote, remote_refs);
903 set_ref_status_for_push(remote_refs,
904 flags & TRANSPORT_PUSH_MIRROR,
905 flags & TRANSPORT_PUSH_FORCE);
907 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
908 if (run_pre_push_hook(transport, remote_refs))
911 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
912 struct ref *ref = remote_refs;
913 for (; ref; ref = ref->next)
914 if (!is_null_oid(&ref->new_oid) &&
915 !push_unpushed_submodules(ref->new_oid.hash,
916 transport->remote->name))
917 die ("Failed to push all needed submodules!");
920 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
921 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
922 struct ref *ref = remote_refs;
923 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
925 for (; ref; ref = ref->next)
926 if (!is_null_oid(&ref->new_oid) &&
927 find_unpushed_submodules(ref->new_oid.hash,
928 transport->remote->name, &needs_pushing))
929 die_with_unpushed_submodules(&needs_pushing);
932 push_ret = transport->push_refs(transport, remote_refs, flags);
933 err = push_had_errors(remote_refs);
934 ret = push_ret | err;
937 transport_print_push_status(transport->url, remote_refs,
938 verbose | porcelain, porcelain,
941 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
942 set_tracking(transport, remote_refs, pretend, 0);
943 if (flags & TRANSPORT_PUSH_SET_PUBLISH)
944 set_tracking(transport, remote_refs, pretend, 1);
946 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
948 for (ref = remote_refs; ref; ref = ref->next)
949 transport_update_tracking_ref(transport->remote, ref, verbose);
952 if (porcelain && !push_ret)
954 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
955 fprintf(stderr, "Everything up-to-date\n");
962 const struct ref *transport_get_remote_refs(struct transport *transport)
964 if (!transport->got_remote_refs) {
965 transport->remote_refs = transport->get_refs_list(transport, 0);
966 transport->got_remote_refs = 1;
969 return transport->remote_refs;
972 int transport_fetch_refs(struct transport *transport, struct ref *refs)
975 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
976 struct ref **heads = NULL;
979 for (rm = refs; rm; rm = rm->next) {
982 !is_null_oid(&rm->old_oid) &&
983 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
985 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
986 heads[nr_heads++] = rm;
991 * When deepening of a shallow repository is requested,
992 * then local and remote refs are likely to still be equal.
993 * Just feed them all to the fetch method in that case.
994 * This condition shouldn't be met in a non-deepening fetch
995 * (see builtin/fetch.c:quickfetch()).
997 ALLOC_ARRAY(heads, nr_refs);
998 for (rm = refs; rm; rm = rm->next)
999 heads[nr_heads++] = rm;
1002 rc = transport->fetch(transport, nr_heads, heads);
1008 void transport_unlock_pack(struct transport *transport)
1010 if (transport->pack_lockfile) {
1011 unlink_or_warn(transport->pack_lockfile);
1012 free(transport->pack_lockfile);
1013 transport->pack_lockfile = NULL;
1017 int transport_connect(struct transport *transport, const char *name,
1018 const char *exec, int fd[2])
1020 if (transport->connect)
1021 return transport->connect(transport, name, exec, fd);
1023 die("Operation not supported by protocol");
1026 int transport_disconnect(struct transport *transport)
1029 if (transport->disconnect)
1030 ret = transport->disconnect(transport);
1036 * Strip username (and password) from a URL and return
1037 * it in a newly allocated string.
1039 char *transport_anonymize_url(const char *url)
1041 char *scheme_prefix, *anon_part;
1042 size_t anon_len, prefix_len = 0;
1044 anon_part = strchr(url, '@');
1045 if (url_is_local_not_ssh(url) || !anon_part)
1048 anon_len = strlen(++anon_part);
1049 scheme_prefix = strstr(url, "://");
1050 if (!scheme_prefix) {
1051 if (!strchr(anon_part, ':'))
1052 /* cannot be "me@there:/path/name" */
1056 /* make sure scheme is reasonable */
1057 for (cp = url; cp < scheme_prefix; cp++) {
1060 case '+': case '.': case '-':
1069 /* @ past the first slash does not count */
1070 cp = strchr(scheme_prefix + 3, '/');
1071 if (cp && cp < anon_part)
1073 prefix_len = scheme_prefix - url + 3;
1075 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1076 (int)anon_len, anon_part);
1078 return xstrdup(url);
1081 struct alternate_refs_data {
1082 alternate_ref_fn *fn;
1086 static int refs_from_alternate_cb(struct alternate_object_database *e,
1091 struct remote *remote;
1092 struct transport *transport;
1093 const struct ref *extra;
1094 struct alternate_refs_data *cb = data;
1097 other = xstrdup(real_path(e->base));
1099 len = strlen(other);
1101 while (other[len-1] == '/')
1102 other[--len] = '\0';
1103 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1105 /* Is this a git repository with refs? */
1106 memcpy(other + len - 8, "/refs", 6);
1107 if (!is_directory(other))
1109 other[len - 8] = '\0';
1110 remote = remote_get(other);
1111 transport = transport_get(remote, other);
1112 for (extra = transport_get_remote_refs(transport);
1114 extra = extra->next)
1115 cb->fn(extra, cb->data);
1116 transport_disconnect(transport);
1122 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1124 struct alternate_refs_data cb;
1127 foreach_alt_odb(refs_from_alternate_cb, &cb);