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 data->conn = git_connect(data->fd, transport->url,
167 for_push ? data->options.receivepack :
168 data->options.uploadpack,
174 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
176 struct git_transport_data *data = transport->data;
179 connect_setup(transport, for_push);
180 get_remote_heads(data->fd[0], NULL, 0, &refs,
181 for_push ? REF_NORMAL : 0,
184 data->got_remote_heads = 1;
189 static int fetch_refs_via_pack(struct transport *transport,
190 int nr_heads, struct ref **to_fetch)
192 struct git_transport_data *data = transport->data;
194 char *dest = xstrdup(transport->url);
195 struct fetch_pack_args args;
196 struct ref *refs_tmp = NULL;
198 memset(&args, 0, sizeof(args));
199 args.uploadpack = data->options.uploadpack;
200 args.keep_pack = data->options.keep;
202 args.use_thin_pack = data->options.thin;
203 args.include_tag = data->options.followtags;
204 args.verbose = (transport->verbose > 1);
205 args.quiet = (transport->verbose < 0);
206 args.no_progress = !transport->progress;
207 args.depth = data->options.depth;
208 args.check_self_contained_and_connected =
209 data->options.check_self_contained_and_connected;
210 args.cloning = transport->cloning;
211 args.update_shallow = data->options.update_shallow;
213 if (!data->got_remote_heads) {
214 connect_setup(transport, 0);
215 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
216 NULL, &data->shallow);
217 data->got_remote_heads = 1;
220 refs = fetch_pack(&args, data->fd, data->conn,
221 refs_tmp ? refs_tmp : transport->remote_refs,
222 dest, to_fetch, nr_heads, &data->shallow,
223 &transport->pack_lockfile);
226 if (finish_connect(data->conn)) {
231 data->got_remote_heads = 0;
232 data->options.self_contained_and_connected =
233 args.self_contained_and_connected;
238 return (refs ? 0 : -1);
241 static int push_had_errors(struct ref *ref)
243 for (; ref; ref = ref->next) {
244 switch (ref->status) {
245 case REF_STATUS_NONE:
246 case REF_STATUS_UPTODATE:
256 int transport_refs_pushed(struct ref *ref)
258 for (; ref; ref = ref->next) {
259 switch(ref->status) {
260 case REF_STATUS_NONE:
261 case REF_STATUS_UPTODATE:
270 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
274 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
280 if (!remote_find_tracking(remote, &rs)) {
282 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
284 delete_ref(rs.dst, NULL, 0);
286 update_ref("update by push", rs.dst,
287 ref->new_oid.hash, NULL, 0, 0);
292 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
296 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
298 fprintf(stdout, "%c\t:%s\t", flag, to->name);
300 fprintf(stdout, "%s (%s)\n", summary, msg);
302 fprintf(stdout, "%s\n", summary);
304 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
306 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
308 fputs(prettify_refname(to->name), stderr);
318 static const char *status_abbrev(unsigned char sha1[20])
320 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
323 static void print_ok_ref_status(struct ref *ref, int porcelain)
326 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
327 else if (is_null_oid(&ref->old_oid))
328 print_ref_status('*',
329 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
331 ref, ref->peer_ref, NULL, porcelain);
333 struct strbuf quickref = STRBUF_INIT;
337 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
338 if (ref->forced_update) {
339 strbuf_addstr(&quickref, "...");
341 msg = "forced update";
343 strbuf_addstr(&quickref, "..");
347 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
349 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
350 strbuf_release(&quickref);
354 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
357 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
359 switch(ref->status) {
360 case REF_STATUS_NONE:
361 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
363 case REF_STATUS_REJECT_NODELETE:
364 print_ref_status('!', "[rejected]", ref, NULL,
365 "remote does not support deleting refs", porcelain);
367 case REF_STATUS_UPTODATE:
368 print_ref_status('=', "[up to date]", ref,
369 ref->peer_ref, NULL, porcelain);
371 case REF_STATUS_REJECT_NONFASTFORWARD:
372 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
373 "non-fast-forward", porcelain);
375 case REF_STATUS_REJECT_ALREADY_EXISTS:
376 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
377 "already exists", porcelain);
379 case REF_STATUS_REJECT_FETCH_FIRST:
380 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
381 "fetch first", porcelain);
383 case REF_STATUS_REJECT_NEEDS_FORCE:
384 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
385 "needs force", porcelain);
387 case REF_STATUS_REJECT_STALE:
388 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
389 "stale info", porcelain);
391 case REF_STATUS_REJECT_SHALLOW:
392 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
393 "new shallow roots not allowed", porcelain);
395 case REF_STATUS_REMOTE_REJECT:
396 print_ref_status('!', "[remote rejected]", ref,
397 ref->deletion ? NULL : ref->peer_ref,
398 ref->remote_status, porcelain);
400 case REF_STATUS_EXPECTING_REPORT:
401 print_ref_status('!', "[remote failure]", ref,
402 ref->deletion ? NULL : ref->peer_ref,
403 "remote failed to report status", porcelain);
405 case REF_STATUS_ATOMIC_PUSH_FAILED:
406 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
407 "atomic push failed", porcelain);
410 print_ok_ref_status(ref, porcelain);
417 void transport_print_push_status(const char *dest, struct ref *refs,
418 int verbose, int porcelain, unsigned int *reject_reasons)
422 unsigned char head_sha1[20];
425 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
428 for (ref = refs; ref; ref = ref->next)
429 if (ref->status == REF_STATUS_UPTODATE)
430 n += print_one_push_status(ref, dest, n, porcelain);
433 for (ref = refs; ref; ref = ref->next)
434 if (ref->status == REF_STATUS_OK)
435 n += print_one_push_status(ref, dest, n, porcelain);
438 for (ref = refs; ref; ref = ref->next) {
439 if (ref->status != REF_STATUS_NONE &&
440 ref->status != REF_STATUS_UPTODATE &&
441 ref->status != REF_STATUS_OK)
442 n += print_one_push_status(ref, dest, n, porcelain);
443 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
444 if (head != NULL && !strcmp(head, ref->name))
445 *reject_reasons |= REJECT_NON_FF_HEAD;
447 *reject_reasons |= REJECT_NON_FF_OTHER;
448 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
449 *reject_reasons |= REJECT_ALREADY_EXISTS;
450 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
451 *reject_reasons |= REJECT_FETCH_FIRST;
452 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
453 *reject_reasons |= REJECT_NEEDS_FORCE;
459 void transport_verify_remote_names(int nr_heads, const char **heads)
463 for (i = 0; i < nr_heads; i++) {
464 const char *local = heads[i];
465 const char *remote = strrchr(heads[i], ':');
470 /* A matching refspec is okay. */
471 if (remote == local && remote[1] == '\0')
474 remote = remote ? (remote + 1) : local;
475 if (check_refname_format(remote,
476 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
477 die("remote part of refspec is not a valid name in %s",
482 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
484 struct git_transport_data *data = transport->data;
485 struct send_pack_args args;
488 if (!data->got_remote_heads) {
489 struct ref *tmp_refs;
490 connect_setup(transport, 1);
492 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
493 NULL, &data->shallow);
494 data->got_remote_heads = 1;
497 memset(&args, 0, sizeof(args));
498 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
499 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
500 args.use_thin_pack = data->options.thin;
501 args.verbose = (transport->verbose > 0);
502 args.quiet = (transport->verbose < 0);
503 args.progress = transport->progress;
504 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
505 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
506 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
507 args.url = transport->url;
509 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
510 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
511 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
512 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
514 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
516 ret = send_pack(&args, data->fd, data->conn, remote_refs,
521 ret |= finish_connect(data->conn);
523 data->got_remote_heads = 0;
528 static int connect_git(struct transport *transport, const char *name,
529 const char *executable, int fd[2])
531 struct git_transport_data *data = transport->data;
532 data->conn = git_connect(data->fd, transport->url,
539 static int disconnect_git(struct transport *transport)
541 struct git_transport_data *data = transport->data;
543 if (data->got_remote_heads)
544 packet_flush(data->fd[1]);
547 finish_connect(data->conn);
554 void transport_take_over(struct transport *transport,
555 struct child_process *child)
557 struct git_transport_data *data;
559 if (!transport->smart_options)
560 die("Bug detected: Taking over transport requires non-NULL "
561 "smart_options field.");
563 data = xcalloc(1, sizeof(*data));
564 data->options = *transport->smart_options;
566 data->fd[0] = data->conn->out;
567 data->fd[1] = data->conn->in;
568 data->got_remote_heads = 0;
569 transport->data = data;
571 transport->set_option = NULL;
572 transport->get_refs_list = get_refs_via_connect;
573 transport->fetch = fetch_refs_via_pack;
574 transport->push = NULL;
575 transport->push_refs = git_transport_push;
576 transport->disconnect = disconnect_git;
577 transport->smart_options = &(data->options);
579 transport->cannot_reuse = 1;
582 static int is_file(const char *url)
587 return S_ISREG(buf.st_mode);
590 static int external_specification_len(const char *url)
592 return strchr(url, ':') - url;
595 static const struct string_list *protocol_whitelist(void)
597 static int enabled = -1;
598 static struct string_list allowed = STRING_LIST_INIT_DUP;
601 const char *v = getenv("GIT_ALLOW_PROTOCOL");
603 string_list_split(&allowed, v, ':', -1);
604 string_list_sort(&allowed);
611 return enabled ? &allowed : NULL;
614 int is_transport_allowed(const char *type)
616 const struct string_list *allowed = protocol_whitelist();
617 return !allowed || string_list_has_string(allowed, type);
620 void transport_check_allowed(const char *type)
622 if (!is_transport_allowed(type))
623 die("transport '%s' not allowed", type);
626 int transport_restrict_protocols(void)
628 return !!protocol_whitelist();
631 struct transport *transport_get(struct remote *remote, const char *url)
634 struct transport *ret = xcalloc(1, sizeof(*ret));
636 ret->progress = isatty(2);
639 die("No remote provided to transport_get()");
641 ret->got_remote_refs = 0;
642 ret->remote = remote;
643 helper = remote->foreign_vcs;
645 if (!url && remote->url)
646 url = remote->url[0];
649 /* maybe it is a foreign URL? */
653 while (is_urlschemechar(p == url, *p))
655 if (starts_with(p, "::"))
656 helper = xstrndup(url, p - url);
660 transport_helper_init(ret, helper);
661 } else if (starts_with(url, "rsync:")) {
662 die("git-over-rsync is no longer supported");
663 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
664 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
665 transport_check_allowed("file");
667 ret->get_refs_list = get_refs_from_bundle;
668 ret->fetch = fetch_refs_from_bundle;
669 ret->disconnect = close_bundle;
670 ret->smart_options = NULL;
671 } else if (!is_url(url)
672 || starts_with(url, "file://")
673 || starts_with(url, "git://")
674 || starts_with(url, "ssh://")
675 || starts_with(url, "git+ssh://")
676 || starts_with(url, "ssh+git://")) {
678 * These are builtin smart transports; "allowed" transports
679 * will be checked individually in git_connect.
681 struct git_transport_data *data = xcalloc(1, sizeof(*data));
683 ret->set_option = NULL;
684 ret->get_refs_list = get_refs_via_connect;
685 ret->fetch = fetch_refs_via_pack;
686 ret->push_refs = git_transport_push;
687 ret->connect = connect_git;
688 ret->disconnect = disconnect_git;
689 ret->smart_options = &(data->options);
692 data->got_remote_heads = 0;
694 /* Unknown protocol in URL. Pass to external handler. */
695 int len = external_specification_len(url);
696 char *handler = xmemdupz(url, len);
697 transport_helper_init(ret, handler);
700 if (ret->smart_options) {
701 ret->smart_options->thin = 1;
702 ret->smart_options->uploadpack = "git-upload-pack";
703 if (remote->uploadpack)
704 ret->smart_options->uploadpack = remote->uploadpack;
705 ret->smart_options->receivepack = "git-receive-pack";
706 if (remote->receivepack)
707 ret->smart_options->receivepack = remote->receivepack;
713 int transport_set_option(struct transport *transport,
714 const char *name, const char *value)
716 int git_reports = 1, protocol_reports = 1;
718 if (transport->smart_options)
719 git_reports = set_git_option(transport->smart_options,
722 if (transport->set_option)
723 protocol_reports = transport->set_option(transport, name,
726 /* If either report is 0, report 0 (success). */
727 if (!git_reports || !protocol_reports)
729 /* If either reports -1 (invalid value), report -1. */
730 if ((git_reports == -1) || (protocol_reports == -1))
732 /* Otherwise if both report unknown, report unknown. */
736 void transport_set_verbosity(struct transport *transport, int verbosity,
740 transport->verbose = verbosity <= 3 ? verbosity : 3;
742 transport->verbose = -1;
745 * Rules used to determine whether to report progress (processing aborts
746 * when a rule is satisfied):
748 * . Report progress, if force_progress is 1 (ie. --progress).
749 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
750 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
751 * . Report progress if isatty(2) is 1.
753 if (force_progress >= 0)
754 transport->progress = !!force_progress;
756 transport->progress = verbosity >= 0 && isatty(2);
759 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
763 fprintf(stderr, "The following submodule paths contain changes that can\n"
764 "not be found on any remote:\n");
765 for (i = 0; i < needs_pushing->nr; i++)
766 printf(" %s\n", needs_pushing->items[i].string);
767 fprintf(stderr, "\nPlease try\n\n"
768 " git push --recurse-submodules=on-demand\n\n"
769 "or cd to the path and use\n\n"
771 "to push them to a remote.\n\n");
773 string_list_clear(needs_pushing, 0);
778 static int run_pre_push_hook(struct transport *transport,
779 struct ref *remote_refs)
783 struct child_process proc = CHILD_PROCESS_INIT;
787 if (!(argv[0] = find_hook("pre-push")))
790 argv[1] = transport->remote->name;
791 argv[2] = transport->url;
797 if (start_command(&proc)) {
798 finish_command(&proc);
802 sigchain_push(SIGPIPE, SIG_IGN);
804 strbuf_init(&buf, 256);
806 for (r = remote_refs; r; r = r->next) {
807 if (!r->peer_ref) continue;
808 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
809 if (r->status == REF_STATUS_REJECT_STALE) continue;
810 if (r->status == REF_STATUS_UPTODATE) continue;
813 strbuf_addf( &buf, "%s %s %s %s\n",
814 r->peer_ref->name, oid_to_hex(&r->new_oid),
815 r->name, oid_to_hex(&r->old_oid));
817 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
818 /* We do not mind if a hook does not read all refs. */
825 strbuf_release(&buf);
831 sigchain_pop(SIGPIPE);
833 x = finish_command(&proc);
840 int transport_push(struct transport *transport,
841 int refspec_nr, const char **refspec, int flags,
842 unsigned int *reject_reasons)
845 transport_verify_remote_names(refspec_nr, refspec);
847 if (transport->push) {
848 /* Maybe FIXME. But no important transport uses this case. */
849 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
850 die("This transport does not support using --set-upstream");
852 return transport->push(transport, refspec_nr, refspec, flags);
853 } else if (transport->push_refs) {
854 struct ref *remote_refs;
855 struct ref *local_refs = get_local_heads();
856 int match_flags = MATCH_REFS_NONE;
857 int verbose = (transport->verbose > 0);
858 int quiet = (transport->verbose < 0);
859 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
860 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
861 int push_ret, ret, err;
863 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
866 remote_refs = transport->get_refs_list(transport, 1);
868 if (flags & TRANSPORT_PUSH_ALL)
869 match_flags |= MATCH_REFS_ALL;
870 if (flags & TRANSPORT_PUSH_MIRROR)
871 match_flags |= MATCH_REFS_MIRROR;
872 if (flags & TRANSPORT_PUSH_PRUNE)
873 match_flags |= MATCH_REFS_PRUNE;
874 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
875 match_flags |= MATCH_REFS_FOLLOW_TAGS;
877 if (match_push_refs(local_refs, &remote_refs,
878 refspec_nr, refspec, match_flags)) {
882 if (transport->smart_options &&
883 transport->smart_options->cas &&
884 !is_empty_cas(transport->smart_options->cas))
885 apply_push_cas(transport->smart_options->cas,
886 transport->remote, remote_refs);
888 set_ref_status_for_push(remote_refs,
889 flags & TRANSPORT_PUSH_MIRROR,
890 flags & TRANSPORT_PUSH_FORCE);
892 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
893 if (run_pre_push_hook(transport, remote_refs))
896 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
897 struct ref *ref = remote_refs;
898 for (; ref; ref = ref->next)
899 if (!is_null_oid(&ref->new_oid) &&
900 !push_unpushed_submodules(ref->new_oid.hash,
901 transport->remote->name))
902 die ("Failed to push all needed submodules!");
905 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
906 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
907 struct ref *ref = remote_refs;
908 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
910 for (; ref; ref = ref->next)
911 if (!is_null_oid(&ref->new_oid) &&
912 find_unpushed_submodules(ref->new_oid.hash,
913 transport->remote->name, &needs_pushing))
914 die_with_unpushed_submodules(&needs_pushing);
917 push_ret = transport->push_refs(transport, remote_refs, flags);
918 err = push_had_errors(remote_refs);
919 ret = push_ret | err;
922 transport_print_push_status(transport->url, remote_refs,
923 verbose | porcelain, porcelain,
926 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
927 set_upstreams(transport, remote_refs, pretend);
929 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
931 for (ref = remote_refs; ref; ref = ref->next)
932 transport_update_tracking_ref(transport->remote, ref, verbose);
935 if (porcelain && !push_ret)
937 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
938 fprintf(stderr, "Everything up-to-date\n");
945 const struct ref *transport_get_remote_refs(struct transport *transport)
947 if (!transport->got_remote_refs) {
948 transport->remote_refs = transport->get_refs_list(transport, 0);
949 transport->got_remote_refs = 1;
952 return transport->remote_refs;
955 int transport_fetch_refs(struct transport *transport, struct ref *refs)
958 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
959 struct ref **heads = NULL;
962 for (rm = refs; rm; rm = rm->next) {
965 !is_null_oid(&rm->old_oid) &&
966 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
968 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
969 heads[nr_heads++] = rm;
974 * When deepening of a shallow repository is requested,
975 * then local and remote refs are likely to still be equal.
976 * Just feed them all to the fetch method in that case.
977 * This condition shouldn't be met in a non-deepening fetch
978 * (see builtin/fetch.c:quickfetch()).
980 heads = xmalloc(nr_refs * sizeof(*heads));
981 for (rm = refs; rm; rm = rm->next)
982 heads[nr_heads++] = rm;
985 rc = transport->fetch(transport, nr_heads, heads);
991 void transport_unlock_pack(struct transport *transport)
993 if (transport->pack_lockfile) {
994 unlink_or_warn(transport->pack_lockfile);
995 free(transport->pack_lockfile);
996 transport->pack_lockfile = NULL;
1000 int transport_connect(struct transport *transport, const char *name,
1001 const char *exec, int fd[2])
1003 if (transport->connect)
1004 return transport->connect(transport, name, exec, fd);
1006 die("Operation not supported by protocol");
1009 int transport_disconnect(struct transport *transport)
1012 if (transport->disconnect)
1013 ret = transport->disconnect(transport);
1019 * Strip username (and password) from a URL and return
1020 * it in a newly allocated string.
1022 char *transport_anonymize_url(const char *url)
1024 char *anon_url, *scheme_prefix, *anon_part;
1025 size_t anon_len, prefix_len = 0;
1027 anon_part = strchr(url, '@');
1028 if (url_is_local_not_ssh(url) || !anon_part)
1031 anon_len = strlen(++anon_part);
1032 scheme_prefix = strstr(url, "://");
1033 if (!scheme_prefix) {
1034 if (!strchr(anon_part, ':'))
1035 /* cannot be "me@there:/path/name" */
1039 /* make sure scheme is reasonable */
1040 for (cp = url; cp < scheme_prefix; cp++) {
1043 case '+': case '.': case '-':
1052 /* @ past the first slash does not count */
1053 cp = strchr(scheme_prefix + 3, '/');
1054 if (cp && cp < anon_part)
1056 prefix_len = scheme_prefix - url + 3;
1058 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1059 memcpy(anon_url, url, prefix_len);
1060 memcpy(anon_url + prefix_len, anon_part, anon_len);
1063 return xstrdup(url);
1066 struct alternate_refs_data {
1067 alternate_ref_fn *fn;
1071 static int refs_from_alternate_cb(struct alternate_object_database *e,
1076 struct remote *remote;
1077 struct transport *transport;
1078 const struct ref *extra;
1079 struct alternate_refs_data *cb = data;
1082 other = xstrdup(real_path(e->base));
1084 len = strlen(other);
1086 while (other[len-1] == '/')
1087 other[--len] = '\0';
1088 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1090 /* Is this a git repository with refs? */
1091 memcpy(other + len - 8, "/refs", 6);
1092 if (!is_directory(other))
1094 other[len - 8] = '\0';
1095 remote = remote_get(other);
1096 transport = transport_get(remote, other);
1097 for (extra = transport_get_remote_refs(transport);
1099 extra = extra->next)
1100 cb->fn(extra, cb->data);
1101 transport_disconnect(transport);
1107 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1109 struct alternate_refs_data cb;
1112 foreach_alt_odb(refs_from_alternate_cb, &cb);