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_upstreams(struct transport *transport, struct ref *refs,
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/"))
58 install_branch_config(BRANCH_CONFIG_VERBOSE,
59 localname + 11, transport->remote->name,
62 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
63 localname + 11, remotename + 11,
64 transport->remote->name);
68 struct bundle_transport_data {
70 struct bundle_header header;
73 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
75 struct bundle_transport_data *data = transport->data;
76 struct ref *result = NULL;
84 data->fd = read_bundle_header(transport->url, &data->header);
86 die ("Could not read bundle '%s'.", transport->url);
87 for (i = 0; i < data->header.references.nr; i++) {
88 struct ref_list_entry *e = data->header.references.list + i;
89 struct ref *ref = alloc_ref(e->name);
90 hashcpy(ref->old_oid.hash, e->sha1);
97 static int fetch_refs_from_bundle(struct transport *transport,
98 int nr_heads, struct ref **to_fetch)
100 struct bundle_transport_data *data = transport->data;
101 return unbundle(&data->header, data->fd,
102 transport->progress ? BUNDLE_VERBOSE : 0);
105 static int close_bundle(struct transport *transport)
107 struct bundle_transport_data *data = transport->data;
114 struct git_transport_data {
115 struct git_transport_options options;
116 struct child_process *conn;
118 unsigned got_remote_heads : 1;
119 struct sha1_array extra_have;
120 struct sha1_array shallow;
123 static int set_git_option(struct git_transport_options *opts,
124 const char *name, const char *value)
126 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
127 opts->uploadpack = value;
129 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
130 opts->receivepack = value;
132 } else if (!strcmp(name, TRANS_OPT_THIN)) {
133 opts->thin = !!value;
135 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
136 opts->followtags = !!value;
138 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
139 opts->keep = !!value;
141 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
142 opts->update_shallow = !!value;
144 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
149 opts->depth = strtol(value, &end, 0);
151 die(_("transport: invalid depth option '%s'"), value);
158 static int connect_setup(struct transport *transport, int for_push)
160 struct git_transport_data *data = transport->data;
161 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
166 switch (transport->family) {
167 case TRANSPORT_FAMILY_ALL: break;
168 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
169 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
172 data->conn = git_connect(data->fd, transport->url,
173 for_push ? data->options.receivepack :
174 data->options.uploadpack,
180 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
182 struct git_transport_data *data = transport->data;
185 connect_setup(transport, for_push);
186 get_remote_heads(data->fd[0], NULL, 0, &refs,
187 for_push ? REF_NORMAL : 0,
190 data->got_remote_heads = 1;
195 static int fetch_refs_via_pack(struct transport *transport,
196 int nr_heads, struct ref **to_fetch)
198 struct git_transport_data *data = transport->data;
200 char *dest = xstrdup(transport->url);
201 struct fetch_pack_args args;
202 struct ref *refs_tmp = NULL;
204 memset(&args, 0, sizeof(args));
205 args.uploadpack = data->options.uploadpack;
206 args.keep_pack = data->options.keep;
208 args.use_thin_pack = data->options.thin;
209 args.include_tag = data->options.followtags;
210 args.verbose = (transport->verbose > 1);
211 args.quiet = (transport->verbose < 0);
212 args.no_progress = !transport->progress;
213 args.depth = data->options.depth;
214 args.check_self_contained_and_connected =
215 data->options.check_self_contained_and_connected;
216 args.cloning = transport->cloning;
217 args.update_shallow = data->options.update_shallow;
219 if (!data->got_remote_heads) {
220 connect_setup(transport, 0);
221 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
222 NULL, &data->shallow);
223 data->got_remote_heads = 1;
226 refs = fetch_pack(&args, data->fd, data->conn,
227 refs_tmp ? refs_tmp : transport->remote_refs,
228 dest, to_fetch, nr_heads, &data->shallow,
229 &transport->pack_lockfile);
232 if (finish_connect(data->conn)) {
237 data->got_remote_heads = 0;
238 data->options.self_contained_and_connected =
239 args.self_contained_and_connected;
244 return (refs ? 0 : -1);
247 static int push_had_errors(struct ref *ref)
249 for (; ref; ref = ref->next) {
250 switch (ref->status) {
251 case REF_STATUS_NONE:
252 case REF_STATUS_UPTODATE:
262 int transport_refs_pushed(struct ref *ref)
264 for (; ref; ref = ref->next) {
265 switch(ref->status) {
266 case REF_STATUS_NONE:
267 case REF_STATUS_UPTODATE:
276 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
280 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
286 if (!remote_find_tracking(remote, &rs)) {
288 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
290 delete_ref(rs.dst, NULL, 0);
292 update_ref("update by push", rs.dst,
293 ref->new_oid.hash, NULL, 0, 0);
298 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
302 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
304 fprintf(stdout, "%c\t:%s\t", flag, to->name);
306 fprintf(stdout, "%s (%s)\n", summary, msg);
308 fprintf(stdout, "%s\n", summary);
310 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
312 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
314 fputs(prettify_refname(to->name), stderr);
324 static const char *status_abbrev(unsigned char sha1[20])
326 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
329 static void print_ok_ref_status(struct ref *ref, int porcelain)
332 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
333 else if (is_null_oid(&ref->old_oid))
334 print_ref_status('*',
335 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
337 ref, ref->peer_ref, NULL, porcelain);
339 struct strbuf quickref = STRBUF_INIT;
343 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
344 if (ref->forced_update) {
345 strbuf_addstr(&quickref, "...");
347 msg = "forced update";
349 strbuf_addstr(&quickref, "..");
353 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
355 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
356 strbuf_release(&quickref);
360 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
363 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
365 switch(ref->status) {
366 case REF_STATUS_NONE:
367 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
369 case REF_STATUS_REJECT_NODELETE:
370 print_ref_status('!', "[rejected]", ref, NULL,
371 "remote does not support deleting refs", porcelain);
373 case REF_STATUS_UPTODATE:
374 print_ref_status('=', "[up to date]", ref,
375 ref->peer_ref, NULL, porcelain);
377 case REF_STATUS_REJECT_NONFASTFORWARD:
378 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
379 "non-fast-forward", porcelain);
381 case REF_STATUS_REJECT_ALREADY_EXISTS:
382 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
383 "already exists", porcelain);
385 case REF_STATUS_REJECT_FETCH_FIRST:
386 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
387 "fetch first", porcelain);
389 case REF_STATUS_REJECT_NEEDS_FORCE:
390 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
391 "needs force", porcelain);
393 case REF_STATUS_REJECT_STALE:
394 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
395 "stale info", porcelain);
397 case REF_STATUS_REJECT_SHALLOW:
398 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
399 "new shallow roots not allowed", porcelain);
401 case REF_STATUS_REMOTE_REJECT:
402 print_ref_status('!', "[remote rejected]", ref,
403 ref->deletion ? NULL : ref->peer_ref,
404 ref->remote_status, porcelain);
406 case REF_STATUS_EXPECTING_REPORT:
407 print_ref_status('!', "[remote failure]", ref,
408 ref->deletion ? NULL : ref->peer_ref,
409 "remote failed to report status", porcelain);
411 case REF_STATUS_ATOMIC_PUSH_FAILED:
412 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
413 "atomic push failed", porcelain);
416 print_ok_ref_status(ref, porcelain);
423 void transport_print_push_status(const char *dest, struct ref *refs,
424 int verbose, int porcelain, unsigned int *reject_reasons)
428 unsigned char head_sha1[20];
431 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
434 for (ref = refs; ref; ref = ref->next)
435 if (ref->status == REF_STATUS_UPTODATE)
436 n += print_one_push_status(ref, dest, n, porcelain);
439 for (ref = refs; ref; ref = ref->next)
440 if (ref->status == REF_STATUS_OK)
441 n += print_one_push_status(ref, dest, n, porcelain);
444 for (ref = refs; ref; ref = ref->next) {
445 if (ref->status != REF_STATUS_NONE &&
446 ref->status != REF_STATUS_UPTODATE &&
447 ref->status != REF_STATUS_OK)
448 n += print_one_push_status(ref, dest, n, porcelain);
449 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
450 if (head != NULL && !strcmp(head, ref->name))
451 *reject_reasons |= REJECT_NON_FF_HEAD;
453 *reject_reasons |= REJECT_NON_FF_OTHER;
454 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
455 *reject_reasons |= REJECT_ALREADY_EXISTS;
456 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
457 *reject_reasons |= REJECT_FETCH_FIRST;
458 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
459 *reject_reasons |= REJECT_NEEDS_FORCE;
465 void transport_verify_remote_names(int nr_heads, const char **heads)
469 for (i = 0; i < nr_heads; i++) {
470 const char *local = heads[i];
471 const char *remote = strrchr(heads[i], ':');
476 /* A matching refspec is okay. */
477 if (remote == local && remote[1] == '\0')
480 remote = remote ? (remote + 1) : local;
481 if (check_refname_format(remote,
482 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
483 die("remote part of refspec is not a valid name in %s",
488 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
490 struct git_transport_data *data = transport->data;
491 struct send_pack_args args;
494 if (!data->got_remote_heads) {
495 struct ref *tmp_refs;
496 connect_setup(transport, 1);
498 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
499 NULL, &data->shallow);
500 data->got_remote_heads = 1;
503 memset(&args, 0, sizeof(args));
504 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
505 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
506 args.use_thin_pack = data->options.thin;
507 args.verbose = (transport->verbose > 0);
508 args.quiet = (transport->verbose < 0);
509 args.progress = transport->progress;
510 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
511 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
512 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
513 args.url = transport->url;
515 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
516 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
517 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
518 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
520 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
522 ret = send_pack(&args, data->fd, data->conn, remote_refs,
527 ret |= finish_connect(data->conn);
529 data->got_remote_heads = 0;
534 static int connect_git(struct transport *transport, const char *name,
535 const char *executable, int fd[2])
537 struct git_transport_data *data = transport->data;
538 data->conn = git_connect(data->fd, transport->url,
545 static int disconnect_git(struct transport *transport)
547 struct git_transport_data *data = transport->data;
549 if (data->got_remote_heads)
550 packet_flush(data->fd[1]);
553 finish_connect(data->conn);
560 void transport_take_over(struct transport *transport,
561 struct child_process *child)
563 struct git_transport_data *data;
565 if (!transport->smart_options)
566 die("Bug detected: Taking over transport requires non-NULL "
567 "smart_options field.");
569 data = xcalloc(1, sizeof(*data));
570 data->options = *transport->smart_options;
572 data->fd[0] = data->conn->out;
573 data->fd[1] = data->conn->in;
574 data->got_remote_heads = 0;
575 transport->data = data;
577 transport->set_option = NULL;
578 transport->get_refs_list = get_refs_via_connect;
579 transport->fetch = fetch_refs_via_pack;
580 transport->push = NULL;
581 transport->push_refs = git_transport_push;
582 transport->disconnect = disconnect_git;
583 transport->smart_options = &(data->options);
585 transport->cannot_reuse = 1;
588 static int is_file(const char *url)
593 return S_ISREG(buf.st_mode);
596 static int external_specification_len(const char *url)
598 return strchr(url, ':') - url;
601 static const struct string_list *protocol_whitelist(void)
603 static int enabled = -1;
604 static struct string_list allowed = STRING_LIST_INIT_DUP;
607 const char *v = getenv("GIT_ALLOW_PROTOCOL");
609 string_list_split(&allowed, v, ':', -1);
610 string_list_sort(&allowed);
617 return enabled ? &allowed : NULL;
620 int is_transport_allowed(const char *type)
622 const struct string_list *allowed = protocol_whitelist();
623 return !allowed || string_list_has_string(allowed, type);
626 void transport_check_allowed(const char *type)
628 if (!is_transport_allowed(type))
629 die("transport '%s' not allowed", type);
632 int transport_restrict_protocols(void)
634 return !!protocol_whitelist();
637 struct transport *transport_get(struct remote *remote, const char *url)
640 struct transport *ret = xcalloc(1, sizeof(*ret));
642 ret->progress = isatty(2);
645 die("No remote provided to transport_get()");
647 ret->got_remote_refs = 0;
648 ret->remote = remote;
649 helper = remote->foreign_vcs;
651 if (!url && remote->url)
652 url = remote->url[0];
655 /* maybe it is a foreign URL? */
659 while (is_urlschemechar(p == url, *p))
661 if (starts_with(p, "::"))
662 helper = xstrndup(url, p - url);
666 transport_helper_init(ret, helper);
667 } else if (starts_with(url, "rsync:")) {
668 die("git-over-rsync is no longer supported");
669 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
670 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
671 transport_check_allowed("file");
673 ret->get_refs_list = get_refs_from_bundle;
674 ret->fetch = fetch_refs_from_bundle;
675 ret->disconnect = close_bundle;
676 ret->smart_options = NULL;
677 } else if (!is_url(url)
678 || starts_with(url, "file://")
679 || starts_with(url, "git://")
680 || starts_with(url, "ssh://")
681 || starts_with(url, "git+ssh://") /* deprecated - do not use */
682 || starts_with(url, "ssh+git://") /* deprecated - do not use */
685 * These are builtin smart transports; "allowed" transports
686 * will be checked individually in git_connect.
688 struct git_transport_data *data = xcalloc(1, sizeof(*data));
690 ret->set_option = NULL;
691 ret->get_refs_list = get_refs_via_connect;
692 ret->fetch = fetch_refs_via_pack;
693 ret->push_refs = git_transport_push;
694 ret->connect = connect_git;
695 ret->disconnect = disconnect_git;
696 ret->smart_options = &(data->options);
699 data->got_remote_heads = 0;
701 /* Unknown protocol in URL. Pass to external handler. */
702 int len = external_specification_len(url);
703 char *handler = xmemdupz(url, len);
704 transport_helper_init(ret, handler);
707 if (ret->smart_options) {
708 ret->smart_options->thin = 1;
709 ret->smart_options->uploadpack = "git-upload-pack";
710 if (remote->uploadpack)
711 ret->smart_options->uploadpack = remote->uploadpack;
712 ret->smart_options->receivepack = "git-receive-pack";
713 if (remote->receivepack)
714 ret->smart_options->receivepack = remote->receivepack;
720 int transport_set_option(struct transport *transport,
721 const char *name, const char *value)
723 int git_reports = 1, protocol_reports = 1;
725 if (transport->smart_options)
726 git_reports = set_git_option(transport->smart_options,
729 if (transport->set_option)
730 protocol_reports = transport->set_option(transport, name,
733 /* If either report is 0, report 0 (success). */
734 if (!git_reports || !protocol_reports)
736 /* If either reports -1 (invalid value), report -1. */
737 if ((git_reports == -1) || (protocol_reports == -1))
739 /* Otherwise if both report unknown, report unknown. */
743 void transport_set_verbosity(struct transport *transport, int verbosity,
747 transport->verbose = verbosity <= 3 ? verbosity : 3;
749 transport->verbose = -1;
752 * Rules used to determine whether to report progress (processing aborts
753 * when a rule is satisfied):
755 * . Report progress, if force_progress is 1 (ie. --progress).
756 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
757 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
758 * . Report progress if isatty(2) is 1.
760 if (force_progress >= 0)
761 transport->progress = !!force_progress;
763 transport->progress = verbosity >= 0 && isatty(2);
766 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
770 fprintf(stderr, _("The following submodule paths contain changes that can\n"
771 "not be found on any remote:\n"));
772 for (i = 0; i < needs_pushing->nr; i++)
773 printf(" %s\n", needs_pushing->items[i].string);
774 fprintf(stderr, _("\nPlease try\n\n"
775 " git push --recurse-submodules=on-demand\n\n"
776 "or cd to the path and use\n\n"
778 "to push them to a remote.\n\n"));
780 string_list_clear(needs_pushing, 0);
785 static int run_pre_push_hook(struct transport *transport,
786 struct ref *remote_refs)
790 struct child_process proc = CHILD_PROCESS_INIT;
794 if (!(argv[0] = find_hook("pre-push")))
797 argv[1] = transport->remote->name;
798 argv[2] = transport->url;
804 if (start_command(&proc)) {
805 finish_command(&proc);
809 sigchain_push(SIGPIPE, SIG_IGN);
811 strbuf_init(&buf, 256);
813 for (r = remote_refs; r; r = r->next) {
814 if (!r->peer_ref) continue;
815 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
816 if (r->status == REF_STATUS_REJECT_STALE) continue;
817 if (r->status == REF_STATUS_UPTODATE) continue;
820 strbuf_addf( &buf, "%s %s %s %s\n",
821 r->peer_ref->name, oid_to_hex(&r->new_oid),
822 r->name, oid_to_hex(&r->old_oid));
824 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
825 /* We do not mind if a hook does not read all refs. */
832 strbuf_release(&buf);
838 sigchain_pop(SIGPIPE);
840 x = finish_command(&proc);
847 int transport_push(struct transport *transport,
848 int refspec_nr, const char **refspec, int flags,
849 unsigned int *reject_reasons)
852 transport_verify_remote_names(refspec_nr, refspec);
854 if (transport->push) {
855 /* Maybe FIXME. But no important transport uses this case. */
856 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
857 die("This transport does not support using --set-upstream");
859 return transport->push(transport, refspec_nr, refspec, flags);
860 } else if (transport->push_refs) {
861 struct ref *remote_refs;
862 struct ref *local_refs = get_local_heads();
863 int match_flags = MATCH_REFS_NONE;
864 int verbose = (transport->verbose > 0);
865 int quiet = (transport->verbose < 0);
866 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
867 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
868 int push_ret, ret, err;
870 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
873 remote_refs = transport->get_refs_list(transport, 1);
875 if (flags & TRANSPORT_PUSH_ALL)
876 match_flags |= MATCH_REFS_ALL;
877 if (flags & TRANSPORT_PUSH_MIRROR)
878 match_flags |= MATCH_REFS_MIRROR;
879 if (flags & TRANSPORT_PUSH_PRUNE)
880 match_flags |= MATCH_REFS_PRUNE;
881 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
882 match_flags |= MATCH_REFS_FOLLOW_TAGS;
884 if (match_push_refs(local_refs, &remote_refs,
885 refspec_nr, refspec, match_flags)) {
889 if (transport->smart_options &&
890 transport->smart_options->cas &&
891 !is_empty_cas(transport->smart_options->cas))
892 apply_push_cas(transport->smart_options->cas,
893 transport->remote, remote_refs);
895 set_ref_status_for_push(remote_refs,
896 flags & TRANSPORT_PUSH_MIRROR,
897 flags & TRANSPORT_PUSH_FORCE);
899 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
900 if (run_pre_push_hook(transport, remote_refs))
903 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
904 struct ref *ref = remote_refs;
905 for (; ref; ref = ref->next)
906 if (!is_null_oid(&ref->new_oid) &&
907 !push_unpushed_submodules(ref->new_oid.hash,
908 transport->remote->name))
909 die ("Failed to push all needed submodules!");
912 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
913 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
914 struct ref *ref = remote_refs;
915 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
917 for (; ref; ref = ref->next)
918 if (!is_null_oid(&ref->new_oid) &&
919 find_unpushed_submodules(ref->new_oid.hash,
920 transport->remote->name, &needs_pushing))
921 die_with_unpushed_submodules(&needs_pushing);
924 push_ret = transport->push_refs(transport, remote_refs, flags);
925 err = push_had_errors(remote_refs);
926 ret = push_ret | err;
929 transport_print_push_status(transport->url, remote_refs,
930 verbose | porcelain, porcelain,
933 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
934 set_upstreams(transport, remote_refs, pretend);
936 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
938 for (ref = remote_refs; ref; ref = ref->next)
939 transport_update_tracking_ref(transport->remote, ref, verbose);
942 if (porcelain && !push_ret)
944 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
945 fprintf(stderr, "Everything up-to-date\n");
952 const struct ref *transport_get_remote_refs(struct transport *transport)
954 if (!transport->got_remote_refs) {
955 transport->remote_refs = transport->get_refs_list(transport, 0);
956 transport->got_remote_refs = 1;
959 return transport->remote_refs;
962 int transport_fetch_refs(struct transport *transport, struct ref *refs)
965 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
966 struct ref **heads = NULL;
969 for (rm = refs; rm; rm = rm->next) {
972 !is_null_oid(&rm->old_oid) &&
973 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
975 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
976 heads[nr_heads++] = rm;
981 * When deepening of a shallow repository is requested,
982 * then local and remote refs are likely to still be equal.
983 * Just feed them all to the fetch method in that case.
984 * This condition shouldn't be met in a non-deepening fetch
985 * (see builtin/fetch.c:quickfetch()).
987 ALLOC_ARRAY(heads, nr_refs);
988 for (rm = refs; rm; rm = rm->next)
989 heads[nr_heads++] = rm;
992 rc = transport->fetch(transport, nr_heads, heads);
998 void transport_unlock_pack(struct transport *transport)
1000 if (transport->pack_lockfile) {
1001 unlink_or_warn(transport->pack_lockfile);
1002 free(transport->pack_lockfile);
1003 transport->pack_lockfile = NULL;
1007 int transport_connect(struct transport *transport, const char *name,
1008 const char *exec, int fd[2])
1010 if (transport->connect)
1011 return transport->connect(transport, name, exec, fd);
1013 die("Operation not supported by protocol");
1016 int transport_disconnect(struct transport *transport)
1019 if (transport->disconnect)
1020 ret = transport->disconnect(transport);
1026 * Strip username (and password) from a URL and return
1027 * it in a newly allocated string.
1029 char *transport_anonymize_url(const char *url)
1031 char *scheme_prefix, *anon_part;
1032 size_t anon_len, prefix_len = 0;
1034 anon_part = strchr(url, '@');
1035 if (url_is_local_not_ssh(url) || !anon_part)
1038 anon_len = strlen(++anon_part);
1039 scheme_prefix = strstr(url, "://");
1040 if (!scheme_prefix) {
1041 if (!strchr(anon_part, ':'))
1042 /* cannot be "me@there:/path/name" */
1046 /* make sure scheme is reasonable */
1047 for (cp = url; cp < scheme_prefix; cp++) {
1050 case '+': case '.': case '-':
1059 /* @ past the first slash does not count */
1060 cp = strchr(scheme_prefix + 3, '/');
1061 if (cp && cp < anon_part)
1063 prefix_len = scheme_prefix - url + 3;
1065 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1066 (int)anon_len, anon_part);
1068 return xstrdup(url);
1071 struct alternate_refs_data {
1072 alternate_ref_fn *fn;
1076 static int refs_from_alternate_cb(struct alternate_object_database *e,
1081 struct remote *remote;
1082 struct transport *transport;
1083 const struct ref *extra;
1084 struct alternate_refs_data *cb = data;
1087 other = xstrdup(real_path(e->base));
1089 len = strlen(other);
1091 while (other[len-1] == '/')
1092 other[--len] = '\0';
1093 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1095 /* Is this a git repository with refs? */
1096 memcpy(other + len - 8, "/refs", 6);
1097 if (!is_directory(other))
1099 other[len - 8] = '\0';
1100 remote = remote_get(other);
1101 transport = transport_get(remote, other);
1102 for (extra = transport_get_remote_refs(transport);
1104 extra = extra->next)
1105 cb->fn(extra, cb->data);
1106 transport_disconnect(transport);
1112 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1114 struct alternate_refs_data cb;
1117 foreach_alt_odb(refs_from_alternate_cb, &cb);