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 void print_ok_ref_status(struct ref *ref, int porcelain)
327 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
328 else if (is_null_oid(&ref->old_oid))
329 print_ref_status('*',
330 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
332 ref, ref->peer_ref, NULL, porcelain);
334 struct strbuf quickref = STRBUF_INIT;
338 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
340 if (ref->forced_update) {
341 strbuf_addstr(&quickref, "...");
343 msg = "forced update";
345 strbuf_addstr(&quickref, "..");
349 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
352 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
353 strbuf_release(&quickref);
357 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
360 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
362 switch(ref->status) {
363 case REF_STATUS_NONE:
364 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
366 case REF_STATUS_REJECT_NODELETE:
367 print_ref_status('!', "[rejected]", ref, NULL,
368 "remote does not support deleting refs", porcelain);
370 case REF_STATUS_UPTODATE:
371 print_ref_status('=', "[up to date]", ref,
372 ref->peer_ref, NULL, porcelain);
374 case REF_STATUS_REJECT_NONFASTFORWARD:
375 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
376 "non-fast-forward", porcelain);
378 case REF_STATUS_REJECT_ALREADY_EXISTS:
379 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
380 "already exists", porcelain);
382 case REF_STATUS_REJECT_FETCH_FIRST:
383 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
384 "fetch first", porcelain);
386 case REF_STATUS_REJECT_NEEDS_FORCE:
387 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
388 "needs force", porcelain);
390 case REF_STATUS_REJECT_STALE:
391 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
392 "stale info", porcelain);
394 case REF_STATUS_REJECT_SHALLOW:
395 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
396 "new shallow roots not allowed", porcelain);
398 case REF_STATUS_REMOTE_REJECT:
399 print_ref_status('!', "[remote rejected]", ref,
400 ref->deletion ? NULL : ref->peer_ref,
401 ref->remote_status, porcelain);
403 case REF_STATUS_EXPECTING_REPORT:
404 print_ref_status('!', "[remote failure]", ref,
405 ref->deletion ? NULL : ref->peer_ref,
406 "remote failed to report status", porcelain);
408 case REF_STATUS_ATOMIC_PUSH_FAILED:
409 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
410 "atomic push failed", porcelain);
413 print_ok_ref_status(ref, porcelain);
420 void transport_print_push_status(const char *dest, struct ref *refs,
421 int verbose, int porcelain, unsigned int *reject_reasons)
425 unsigned char head_sha1[20];
428 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
431 for (ref = refs; ref; ref = ref->next)
432 if (ref->status == REF_STATUS_UPTODATE)
433 n += print_one_push_status(ref, dest, n, porcelain);
436 for (ref = refs; ref; ref = ref->next)
437 if (ref->status == REF_STATUS_OK)
438 n += print_one_push_status(ref, dest, n, porcelain);
441 for (ref = refs; ref; ref = ref->next) {
442 if (ref->status != REF_STATUS_NONE &&
443 ref->status != REF_STATUS_UPTODATE &&
444 ref->status != REF_STATUS_OK)
445 n += print_one_push_status(ref, dest, n, porcelain);
446 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
447 if (head != NULL && !strcmp(head, ref->name))
448 *reject_reasons |= REJECT_NON_FF_HEAD;
450 *reject_reasons |= REJECT_NON_FF_OTHER;
451 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
452 *reject_reasons |= REJECT_ALREADY_EXISTS;
453 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
454 *reject_reasons |= REJECT_FETCH_FIRST;
455 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
456 *reject_reasons |= REJECT_NEEDS_FORCE;
462 void transport_verify_remote_names(int nr_heads, const char **heads)
466 for (i = 0; i < nr_heads; i++) {
467 const char *local = heads[i];
468 const char *remote = strrchr(heads[i], ':');
473 /* A matching refspec is okay. */
474 if (remote == local && remote[1] == '\0')
477 remote = remote ? (remote + 1) : local;
478 if (check_refname_format(remote,
479 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
480 die("remote part of refspec is not a valid name in %s",
485 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
487 struct git_transport_data *data = transport->data;
488 struct send_pack_args args;
491 if (!data->got_remote_heads) {
492 struct ref *tmp_refs;
493 connect_setup(transport, 1);
495 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
496 NULL, &data->shallow);
497 data->got_remote_heads = 1;
500 memset(&args, 0, sizeof(args));
501 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
502 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
503 args.use_thin_pack = data->options.thin;
504 args.verbose = (transport->verbose > 0);
505 args.quiet = (transport->verbose < 0);
506 args.progress = transport->progress;
507 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
508 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
509 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
510 args.url = transport->url;
512 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
513 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
514 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
515 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
517 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
519 ret = send_pack(&args, data->fd, data->conn, remote_refs,
524 ret |= finish_connect(data->conn);
526 data->got_remote_heads = 0;
531 static int connect_git(struct transport *transport, const char *name,
532 const char *executable, int fd[2])
534 struct git_transport_data *data = transport->data;
535 data->conn = git_connect(data->fd, transport->url,
542 static int disconnect_git(struct transport *transport)
544 struct git_transport_data *data = transport->data;
546 if (data->got_remote_heads)
547 packet_flush(data->fd[1]);
550 finish_connect(data->conn);
557 void transport_take_over(struct transport *transport,
558 struct child_process *child)
560 struct git_transport_data *data;
562 if (!transport->smart_options)
563 die("Bug detected: Taking over transport requires non-NULL "
564 "smart_options field.");
566 data = xcalloc(1, sizeof(*data));
567 data->options = *transport->smart_options;
569 data->fd[0] = data->conn->out;
570 data->fd[1] = data->conn->in;
571 data->got_remote_heads = 0;
572 transport->data = data;
574 transport->set_option = NULL;
575 transport->get_refs_list = get_refs_via_connect;
576 transport->fetch = fetch_refs_via_pack;
577 transport->push = NULL;
578 transport->push_refs = git_transport_push;
579 transport->disconnect = disconnect_git;
580 transport->smart_options = &(data->options);
582 transport->cannot_reuse = 1;
585 static int is_file(const char *url)
590 return S_ISREG(buf.st_mode);
593 static int external_specification_len(const char *url)
595 return strchr(url, ':') - url;
598 static const struct string_list *protocol_whitelist(void)
600 static int enabled = -1;
601 static struct string_list allowed = STRING_LIST_INIT_DUP;
604 const char *v = getenv("GIT_ALLOW_PROTOCOL");
606 string_list_split(&allowed, v, ':', -1);
607 string_list_sort(&allowed);
614 return enabled ? &allowed : NULL;
617 int is_transport_allowed(const char *type)
619 const struct string_list *allowed = protocol_whitelist();
620 return !allowed || string_list_has_string(allowed, type);
623 void transport_check_allowed(const char *type)
625 if (!is_transport_allowed(type))
626 die("transport '%s' not allowed", type);
629 int transport_restrict_protocols(void)
631 return !!protocol_whitelist();
634 struct transport *transport_get(struct remote *remote, const char *url)
637 struct transport *ret = xcalloc(1, sizeof(*ret));
639 ret->progress = isatty(2);
642 die("No remote provided to transport_get()");
644 ret->got_remote_refs = 0;
645 ret->remote = remote;
646 helper = remote->foreign_vcs;
648 if (!url && remote->url)
649 url = remote->url[0];
652 /* maybe it is a foreign URL? */
656 while (is_urlschemechar(p == url, *p))
658 if (starts_with(p, "::"))
659 helper = xstrndup(url, p - url);
663 transport_helper_init(ret, helper);
664 } else if (starts_with(url, "rsync:")) {
665 die("git-over-rsync is no longer supported");
666 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
667 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
668 transport_check_allowed("file");
670 ret->get_refs_list = get_refs_from_bundle;
671 ret->fetch = fetch_refs_from_bundle;
672 ret->disconnect = close_bundle;
673 ret->smart_options = NULL;
674 } else if (!is_url(url)
675 || starts_with(url, "file://")
676 || starts_with(url, "git://")
677 || starts_with(url, "ssh://")
678 || starts_with(url, "git+ssh://") /* deprecated - do not use */
679 || starts_with(url, "ssh+git://") /* deprecated - do not use */
682 * These are builtin smart transports; "allowed" transports
683 * will be checked individually in git_connect.
685 struct git_transport_data *data = xcalloc(1, sizeof(*data));
687 ret->set_option = NULL;
688 ret->get_refs_list = get_refs_via_connect;
689 ret->fetch = fetch_refs_via_pack;
690 ret->push_refs = git_transport_push;
691 ret->connect = connect_git;
692 ret->disconnect = disconnect_git;
693 ret->smart_options = &(data->options);
696 data->got_remote_heads = 0;
698 /* Unknown protocol in URL. Pass to external handler. */
699 int len = external_specification_len(url);
700 char *handler = xmemdupz(url, len);
701 transport_helper_init(ret, handler);
704 if (ret->smart_options) {
705 ret->smart_options->thin = 1;
706 ret->smart_options->uploadpack = "git-upload-pack";
707 if (remote->uploadpack)
708 ret->smart_options->uploadpack = remote->uploadpack;
709 ret->smart_options->receivepack = "git-receive-pack";
710 if (remote->receivepack)
711 ret->smart_options->receivepack = remote->receivepack;
717 int transport_set_option(struct transport *transport,
718 const char *name, const char *value)
720 int git_reports = 1, protocol_reports = 1;
722 if (transport->smart_options)
723 git_reports = set_git_option(transport->smart_options,
726 if (transport->set_option)
727 protocol_reports = transport->set_option(transport, name,
730 /* If either report is 0, report 0 (success). */
731 if (!git_reports || !protocol_reports)
733 /* If either reports -1 (invalid value), report -1. */
734 if ((git_reports == -1) || (protocol_reports == -1))
736 /* Otherwise if both report unknown, report unknown. */
740 void transport_set_verbosity(struct transport *transport, int verbosity,
744 transport->verbose = verbosity <= 3 ? verbosity : 3;
746 transport->verbose = -1;
749 * Rules used to determine whether to report progress (processing aborts
750 * when a rule is satisfied):
752 * . Report progress, if force_progress is 1 (ie. --progress).
753 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
754 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
755 * . Report progress if isatty(2) is 1.
757 if (force_progress >= 0)
758 transport->progress = !!force_progress;
760 transport->progress = verbosity >= 0 && isatty(2);
763 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
767 fprintf(stderr, "The following submodule paths contain changes that can\n"
768 "not be found on any remote:\n");
769 for (i = 0; i < needs_pushing->nr; i++)
770 printf(" %s\n", needs_pushing->items[i].string);
771 fprintf(stderr, "\nPlease try\n\n"
772 " git push --recurse-submodules=on-demand\n\n"
773 "or cd to the path and use\n\n"
775 "to push them to a remote.\n\n");
777 string_list_clear(needs_pushing, 0);
782 static int run_pre_push_hook(struct transport *transport,
783 struct ref *remote_refs)
787 struct child_process proc = CHILD_PROCESS_INIT;
791 if (!(argv[0] = find_hook("pre-push")))
794 argv[1] = transport->remote->name;
795 argv[2] = transport->url;
801 if (start_command(&proc)) {
802 finish_command(&proc);
806 sigchain_push(SIGPIPE, SIG_IGN);
808 strbuf_init(&buf, 256);
810 for (r = remote_refs; r; r = r->next) {
811 if (!r->peer_ref) continue;
812 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
813 if (r->status == REF_STATUS_REJECT_STALE) continue;
814 if (r->status == REF_STATUS_UPTODATE) continue;
817 strbuf_addf( &buf, "%s %s %s %s\n",
818 r->peer_ref->name, oid_to_hex(&r->new_oid),
819 r->name, oid_to_hex(&r->old_oid));
821 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
822 /* We do not mind if a hook does not read all refs. */
829 strbuf_release(&buf);
835 sigchain_pop(SIGPIPE);
837 x = finish_command(&proc);
844 int transport_push(struct transport *transport,
845 int refspec_nr, const char **refspec, int flags,
846 unsigned int *reject_reasons)
849 transport_verify_remote_names(refspec_nr, refspec);
851 if (transport->push) {
852 /* Maybe FIXME. But no important transport uses this case. */
853 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
854 die("This transport does not support using --set-upstream");
856 return transport->push(transport, refspec_nr, refspec, flags);
857 } else if (transport->push_refs) {
858 struct ref *remote_refs;
859 struct ref *local_refs = get_local_heads();
860 int match_flags = MATCH_REFS_NONE;
861 int verbose = (transport->verbose > 0);
862 int quiet = (transport->verbose < 0);
863 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
864 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
865 int push_ret, ret, err;
867 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
870 remote_refs = transport->get_refs_list(transport, 1);
872 if (flags & TRANSPORT_PUSH_ALL)
873 match_flags |= MATCH_REFS_ALL;
874 if (flags & TRANSPORT_PUSH_MIRROR)
875 match_flags |= MATCH_REFS_MIRROR;
876 if (flags & TRANSPORT_PUSH_PRUNE)
877 match_flags |= MATCH_REFS_PRUNE;
878 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
879 match_flags |= MATCH_REFS_FOLLOW_TAGS;
881 if (match_push_refs(local_refs, &remote_refs,
882 refspec_nr, refspec, match_flags)) {
886 if (transport->smart_options &&
887 transport->smart_options->cas &&
888 !is_empty_cas(transport->smart_options->cas))
889 apply_push_cas(transport->smart_options->cas,
890 transport->remote, remote_refs);
892 set_ref_status_for_push(remote_refs,
893 flags & TRANSPORT_PUSH_MIRROR,
894 flags & TRANSPORT_PUSH_FORCE);
896 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
897 if (run_pre_push_hook(transport, remote_refs))
900 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
901 struct ref *ref = remote_refs;
902 for (; ref; ref = ref->next)
903 if (!is_null_oid(&ref->new_oid) &&
904 !push_unpushed_submodules(ref->new_oid.hash,
905 transport->remote->name))
906 die ("Failed to push all needed submodules!");
909 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
910 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
911 struct ref *ref = remote_refs;
912 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
914 for (; ref; ref = ref->next)
915 if (!is_null_oid(&ref->new_oid) &&
916 find_unpushed_submodules(ref->new_oid.hash,
917 transport->remote->name, &needs_pushing))
918 die_with_unpushed_submodules(&needs_pushing);
921 push_ret = transport->push_refs(transport, remote_refs, flags);
922 err = push_had_errors(remote_refs);
923 ret = push_ret | err;
926 transport_print_push_status(transport->url, remote_refs,
927 verbose | porcelain, porcelain,
930 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
931 set_upstreams(transport, remote_refs, pretend);
933 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
935 for (ref = remote_refs; ref; ref = ref->next)
936 transport_update_tracking_ref(transport->remote, ref, verbose);
939 if (porcelain && !push_ret)
941 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
942 fprintf(stderr, "Everything up-to-date\n");
949 const struct ref *transport_get_remote_refs(struct transport *transport)
951 if (!transport->got_remote_refs) {
952 transport->remote_refs = transport->get_refs_list(transport, 0);
953 transport->got_remote_refs = 1;
956 return transport->remote_refs;
959 int transport_fetch_refs(struct transport *transport, struct ref *refs)
962 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
963 struct ref **heads = NULL;
966 for (rm = refs; rm; rm = rm->next) {
969 !is_null_oid(&rm->old_oid) &&
970 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
972 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
973 heads[nr_heads++] = rm;
978 * When deepening of a shallow repository is requested,
979 * then local and remote refs are likely to still be equal.
980 * Just feed them all to the fetch method in that case.
981 * This condition shouldn't be met in a non-deepening fetch
982 * (see builtin/fetch.c:quickfetch()).
984 ALLOC_ARRAY(heads, nr_refs);
985 for (rm = refs; rm; rm = rm->next)
986 heads[nr_heads++] = rm;
989 rc = transport->fetch(transport, nr_heads, heads);
995 void transport_unlock_pack(struct transport *transport)
997 if (transport->pack_lockfile) {
998 unlink_or_warn(transport->pack_lockfile);
999 free(transport->pack_lockfile);
1000 transport->pack_lockfile = NULL;
1004 int transport_connect(struct transport *transport, const char *name,
1005 const char *exec, int fd[2])
1007 if (transport->connect)
1008 return transport->connect(transport, name, exec, fd);
1010 die("Operation not supported by protocol");
1013 int transport_disconnect(struct transport *transport)
1016 if (transport->disconnect)
1017 ret = transport->disconnect(transport);
1023 * Strip username (and password) from a URL and return
1024 * it in a newly allocated string.
1026 char *transport_anonymize_url(const char *url)
1028 char *scheme_prefix, *anon_part;
1029 size_t anon_len, prefix_len = 0;
1031 anon_part = strchr(url, '@');
1032 if (url_is_local_not_ssh(url) || !anon_part)
1035 anon_len = strlen(++anon_part);
1036 scheme_prefix = strstr(url, "://");
1037 if (!scheme_prefix) {
1038 if (!strchr(anon_part, ':'))
1039 /* cannot be "me@there:/path/name" */
1043 /* make sure scheme is reasonable */
1044 for (cp = url; cp < scheme_prefix; cp++) {
1047 case '+': case '.': case '-':
1056 /* @ past the first slash does not count */
1057 cp = strchr(scheme_prefix + 3, '/');
1058 if (cp && cp < anon_part)
1060 prefix_len = scheme_prefix - url + 3;
1062 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1063 (int)anon_len, anon_part);
1065 return xstrdup(url);
1068 struct alternate_refs_data {
1069 alternate_ref_fn *fn;
1073 static int refs_from_alternate_cb(struct alternate_object_database *e,
1078 struct remote *remote;
1079 struct transport *transport;
1080 const struct ref *extra;
1081 struct alternate_refs_data *cb = data;
1084 other = xstrdup(real_path(e->base));
1086 len = strlen(other);
1088 while (other[len-1] == '/')
1089 other[--len] = '\0';
1090 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1092 /* Is this a git repository with refs? */
1093 memcpy(other + len - 8, "/refs", 6);
1094 if (!is_directory(other))
1096 other[len - 8] = '\0';
1097 remote = remote_get(other);
1098 transport = transport_get(remote, other);
1099 for (extra = transport_get_remote_refs(transport);
1101 extra = extra->next)
1102 cb->fn(extra, cb->data);
1103 transport_disconnect(transport);
1109 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1111 struct alternate_refs_data cb;
1114 foreach_alt_odb(refs_from_alternate_cb, &cb);