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,
299 struct ref *to, struct ref *from, const char *msg,
300 int porcelain, int summary_width)
304 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
306 fprintf(stdout, "%c\t:%s\t", flag, to->name);
308 fprintf(stdout, "%s (%s)\n", summary, msg);
310 fprintf(stdout, "%s\n", summary);
312 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
314 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
316 fputs(prettify_refname(to->name), stderr);
326 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
329 print_ref_status('-', "[deleted]", ref, NULL, NULL,
330 porcelain, summary_width);
331 else if (is_null_oid(&ref->old_oid))
332 print_ref_status('*',
333 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
335 ref, ref->peer_ref, NULL, porcelain, summary_width);
337 struct strbuf quickref = STRBUF_INIT;
341 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
343 if (ref->forced_update) {
344 strbuf_addstr(&quickref, "...");
346 msg = "forced update";
348 strbuf_addstr(&quickref, "..");
352 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
355 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
356 porcelain, summary_width);
357 strbuf_release(&quickref);
361 static int print_one_push_status(struct ref *ref, const char *dest, int count,
362 int porcelain, int summary_width)
365 char *url = transport_anonymize_url(dest);
366 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
370 switch(ref->status) {
371 case REF_STATUS_NONE:
372 print_ref_status('X', "[no match]", ref, NULL, NULL,
373 porcelain, summary_width);
375 case REF_STATUS_REJECT_NODELETE:
376 print_ref_status('!', "[rejected]", ref, NULL,
377 "remote does not support deleting refs",
378 porcelain, summary_width);
380 case REF_STATUS_UPTODATE:
381 print_ref_status('=', "[up to date]", ref,
382 ref->peer_ref, NULL, porcelain, summary_width);
384 case REF_STATUS_REJECT_NONFASTFORWARD:
385 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
386 "non-fast-forward", porcelain, summary_width);
388 case REF_STATUS_REJECT_ALREADY_EXISTS:
389 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
390 "already exists", porcelain, summary_width);
392 case REF_STATUS_REJECT_FETCH_FIRST:
393 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
394 "fetch first", porcelain, summary_width);
396 case REF_STATUS_REJECT_NEEDS_FORCE:
397 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
398 "needs force", porcelain, summary_width);
400 case REF_STATUS_REJECT_STALE:
401 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
402 "stale info", porcelain, summary_width);
404 case REF_STATUS_REJECT_SHALLOW:
405 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
406 "new shallow roots not allowed",
407 porcelain, summary_width);
409 case REF_STATUS_REMOTE_REJECT:
410 print_ref_status('!', "[remote rejected]", ref,
411 ref->deletion ? NULL : ref->peer_ref,
412 ref->remote_status, porcelain, summary_width);
414 case REF_STATUS_EXPECTING_REPORT:
415 print_ref_status('!', "[remote failure]", ref,
416 ref->deletion ? NULL : ref->peer_ref,
417 "remote failed to report status",
418 porcelain, summary_width);
420 case REF_STATUS_ATOMIC_PUSH_FAILED:
421 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
422 "atomic push failed", porcelain, summary_width);
425 print_ok_ref_status(ref, porcelain, summary_width);
432 static int measure_abbrev(const struct object_id *oid, int sofar)
434 char hex[GIT_SHA1_HEXSZ + 1];
435 int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
437 return (w < sofar) ? sofar : w;
440 int transport_summary_width(const struct ref *refs)
444 for (; refs; refs = refs->next) {
445 maxw = measure_abbrev(&refs->old_oid, maxw);
446 maxw = measure_abbrev(&refs->new_oid, maxw);
449 maxw = FALLBACK_DEFAULT_ABBREV;
450 return (2 * maxw + 3);
453 void transport_print_push_status(const char *dest, struct ref *refs,
454 int verbose, int porcelain, unsigned int *reject_reasons)
458 unsigned char head_sha1[20];
460 int summary_width = transport_summary_width(refs);
462 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
465 for (ref = refs; ref; ref = ref->next)
466 if (ref->status == REF_STATUS_UPTODATE)
467 n += print_one_push_status(ref, dest, n,
468 porcelain, summary_width);
471 for (ref = refs; ref; ref = ref->next)
472 if (ref->status == REF_STATUS_OK)
473 n += print_one_push_status(ref, dest, n,
474 porcelain, summary_width);
477 for (ref = refs; ref; ref = ref->next) {
478 if (ref->status != REF_STATUS_NONE &&
479 ref->status != REF_STATUS_UPTODATE &&
480 ref->status != REF_STATUS_OK)
481 n += print_one_push_status(ref, dest, n,
482 porcelain, summary_width);
483 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
484 if (head != NULL && !strcmp(head, ref->name))
485 *reject_reasons |= REJECT_NON_FF_HEAD;
487 *reject_reasons |= REJECT_NON_FF_OTHER;
488 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
489 *reject_reasons |= REJECT_ALREADY_EXISTS;
490 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
491 *reject_reasons |= REJECT_FETCH_FIRST;
492 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
493 *reject_reasons |= REJECT_NEEDS_FORCE;
499 void transport_verify_remote_names(int nr_heads, const char **heads)
503 for (i = 0; i < nr_heads; i++) {
504 const char *local = heads[i];
505 const char *remote = strrchr(heads[i], ':');
510 /* A matching refspec is okay. */
511 if (remote == local && remote[1] == '\0')
514 remote = remote ? (remote + 1) : local;
515 if (check_refname_format(remote,
516 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
517 die("remote part of refspec is not a valid name in %s",
522 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
524 struct git_transport_data *data = transport->data;
525 struct send_pack_args args;
528 if (!data->got_remote_heads) {
529 struct ref *tmp_refs;
530 connect_setup(transport, 1);
532 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
533 NULL, &data->shallow);
534 data->got_remote_heads = 1;
537 memset(&args, 0, sizeof(args));
538 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
539 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
540 args.use_thin_pack = data->options.thin;
541 args.verbose = (transport->verbose > 0);
542 args.quiet = (transport->verbose < 0);
543 args.progress = transport->progress;
544 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
545 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
546 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
547 args.push_options = transport->push_options;
548 args.url = transport->url;
550 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
551 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
552 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
553 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
555 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
557 ret = send_pack(&args, data->fd, data->conn, remote_refs,
562 ret |= finish_connect(data->conn);
564 data->got_remote_heads = 0;
569 static int connect_git(struct transport *transport, const char *name,
570 const char *executable, int fd[2])
572 struct git_transport_data *data = transport->data;
573 data->conn = git_connect(data->fd, transport->url,
580 static int disconnect_git(struct transport *transport)
582 struct git_transport_data *data = transport->data;
584 if (data->got_remote_heads)
585 packet_flush(data->fd[1]);
588 finish_connect(data->conn);
595 void transport_take_over(struct transport *transport,
596 struct child_process *child)
598 struct git_transport_data *data;
600 if (!transport->smart_options)
601 die("BUG: taking over transport requires non-NULL "
602 "smart_options field.");
604 data = xcalloc(1, sizeof(*data));
605 data->options = *transport->smart_options;
607 data->fd[0] = data->conn->out;
608 data->fd[1] = data->conn->in;
609 data->got_remote_heads = 0;
610 transport->data = data;
612 transport->set_option = NULL;
613 transport->get_refs_list = get_refs_via_connect;
614 transport->fetch = fetch_refs_via_pack;
615 transport->push = NULL;
616 transport->push_refs = git_transport_push;
617 transport->disconnect = disconnect_git;
618 transport->smart_options = &(data->options);
620 transport->cannot_reuse = 1;
623 static int is_file(const char *url)
628 return S_ISREG(buf.st_mode);
631 static int external_specification_len(const char *url)
633 return strchr(url, ':') - url;
636 static const struct string_list *protocol_whitelist(void)
638 static int enabled = -1;
639 static struct string_list allowed = STRING_LIST_INIT_DUP;
642 const char *v = getenv("GIT_ALLOW_PROTOCOL");
644 string_list_split(&allowed, v, ':', -1);
645 string_list_sort(&allowed);
652 return enabled ? &allowed : NULL;
655 int is_transport_allowed(const char *type)
657 const struct string_list *allowed = protocol_whitelist();
658 return !allowed || string_list_has_string(allowed, type);
661 void transport_check_allowed(const char *type)
663 if (!is_transport_allowed(type))
664 die("transport '%s' not allowed", type);
667 int transport_restrict_protocols(void)
669 return !!protocol_whitelist();
672 struct transport *transport_get(struct remote *remote, const char *url)
675 struct transport *ret = xcalloc(1, sizeof(*ret));
677 ret->progress = isatty(2);
680 die("No remote provided to transport_get()");
682 ret->got_remote_refs = 0;
683 ret->remote = remote;
684 helper = remote->foreign_vcs;
686 if (!url && remote->url)
687 url = remote->url[0];
690 /* maybe it is a foreign URL? */
694 while (is_urlschemechar(p == url, *p))
696 if (starts_with(p, "::"))
697 helper = xstrndup(url, p - url);
701 transport_helper_init(ret, helper);
702 } else if (starts_with(url, "rsync:")) {
703 die("git-over-rsync is no longer supported");
704 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
705 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
706 transport_check_allowed("file");
708 ret->get_refs_list = get_refs_from_bundle;
709 ret->fetch = fetch_refs_from_bundle;
710 ret->disconnect = close_bundle;
711 ret->smart_options = NULL;
712 } else if (!is_url(url)
713 || starts_with(url, "file://")
714 || starts_with(url, "git://")
715 || starts_with(url, "ssh://")
716 || starts_with(url, "git+ssh://") /* deprecated - do not use */
717 || starts_with(url, "ssh+git://") /* deprecated - do not use */
720 * These are builtin smart transports; "allowed" transports
721 * will be checked individually in git_connect.
723 struct git_transport_data *data = xcalloc(1, sizeof(*data));
725 ret->set_option = NULL;
726 ret->get_refs_list = get_refs_via_connect;
727 ret->fetch = fetch_refs_via_pack;
728 ret->push_refs = git_transport_push;
729 ret->connect = connect_git;
730 ret->disconnect = disconnect_git;
731 ret->smart_options = &(data->options);
734 data->got_remote_heads = 0;
736 /* Unknown protocol in URL. Pass to external handler. */
737 int len = external_specification_len(url);
738 char *handler = xmemdupz(url, len);
739 transport_helper_init(ret, handler);
742 if (ret->smart_options) {
743 ret->smart_options->thin = 1;
744 ret->smart_options->uploadpack = "git-upload-pack";
745 if (remote->uploadpack)
746 ret->smart_options->uploadpack = remote->uploadpack;
747 ret->smart_options->receivepack = "git-receive-pack";
748 if (remote->receivepack)
749 ret->smart_options->receivepack = remote->receivepack;
755 int transport_set_option(struct transport *transport,
756 const char *name, const char *value)
758 int git_reports = 1, protocol_reports = 1;
760 if (transport->smart_options)
761 git_reports = set_git_option(transport->smart_options,
764 if (transport->set_option)
765 protocol_reports = transport->set_option(transport, name,
768 /* If either report is 0, report 0 (success). */
769 if (!git_reports || !protocol_reports)
771 /* If either reports -1 (invalid value), report -1. */
772 if ((git_reports == -1) || (protocol_reports == -1))
774 /* Otherwise if both report unknown, report unknown. */
778 void transport_set_verbosity(struct transport *transport, int verbosity,
782 transport->verbose = verbosity <= 3 ? verbosity : 3;
784 transport->verbose = -1;
787 * Rules used to determine whether to report progress (processing aborts
788 * when a rule is satisfied):
790 * . Report progress, if force_progress is 1 (ie. --progress).
791 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
792 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
793 * . Report progress if isatty(2) is 1.
795 if (force_progress >= 0)
796 transport->progress = !!force_progress;
798 transport->progress = verbosity >= 0 && isatty(2);
801 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
805 fprintf(stderr, _("The following submodule paths contain changes that can\n"
806 "not be found on any remote:\n"));
807 for (i = 0; i < needs_pushing->nr; i++)
808 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
809 fprintf(stderr, _("\nPlease try\n\n"
810 " git push --recurse-submodules=on-demand\n\n"
811 "or cd to the path and use\n\n"
813 "to push them to a remote.\n\n"));
815 string_list_clear(needs_pushing, 0);
820 static int run_pre_push_hook(struct transport *transport,
821 struct ref *remote_refs)
825 struct child_process proc = CHILD_PROCESS_INIT;
829 if (!(argv[0] = find_hook("pre-push")))
832 argv[1] = transport->remote->name;
833 argv[2] = transport->url;
839 if (start_command(&proc)) {
840 finish_command(&proc);
844 sigchain_push(SIGPIPE, SIG_IGN);
846 strbuf_init(&buf, 256);
848 for (r = remote_refs; r; r = r->next) {
849 if (!r->peer_ref) continue;
850 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
851 if (r->status == REF_STATUS_REJECT_STALE) continue;
852 if (r->status == REF_STATUS_UPTODATE) continue;
855 strbuf_addf( &buf, "%s %s %s %s\n",
856 r->peer_ref->name, oid_to_hex(&r->new_oid),
857 r->name, oid_to_hex(&r->old_oid));
859 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
860 /* We do not mind if a hook does not read all refs. */
867 strbuf_release(&buf);
873 sigchain_pop(SIGPIPE);
875 x = finish_command(&proc);
882 int transport_push(struct transport *transport,
883 int refspec_nr, const char **refspec, int flags,
884 unsigned int *reject_reasons)
887 transport_verify_remote_names(refspec_nr, refspec);
889 if (transport->push) {
890 /* Maybe FIXME. But no important transport uses this case. */
891 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
892 die("This transport does not support using --set-upstream");
894 return transport->push(transport, refspec_nr, refspec, flags);
895 } else if (transport->push_refs) {
896 struct ref *remote_refs;
897 struct ref *local_refs = get_local_heads();
898 int match_flags = MATCH_REFS_NONE;
899 int verbose = (transport->verbose > 0);
900 int quiet = (transport->verbose < 0);
901 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
902 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
903 int push_ret, ret, err;
905 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
908 remote_refs = transport->get_refs_list(transport, 1);
910 if (flags & TRANSPORT_PUSH_ALL)
911 match_flags |= MATCH_REFS_ALL;
912 if (flags & TRANSPORT_PUSH_MIRROR)
913 match_flags |= MATCH_REFS_MIRROR;
914 if (flags & TRANSPORT_PUSH_PRUNE)
915 match_flags |= MATCH_REFS_PRUNE;
916 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
917 match_flags |= MATCH_REFS_FOLLOW_TAGS;
919 if (match_push_refs(local_refs, &remote_refs,
920 refspec_nr, refspec, match_flags)) {
924 if (transport->smart_options &&
925 transport->smart_options->cas &&
926 !is_empty_cas(transport->smart_options->cas))
927 apply_push_cas(transport->smart_options->cas,
928 transport->remote, remote_refs);
930 set_ref_status_for_push(remote_refs,
931 flags & TRANSPORT_PUSH_MIRROR,
932 flags & TRANSPORT_PUSH_FORCE);
934 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
935 if (run_pre_push_hook(transport, remote_refs))
938 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
939 struct ref *ref = remote_refs;
940 for (; ref; ref = ref->next)
941 if (!is_null_oid(&ref->new_oid) &&
942 !push_unpushed_submodules(ref->new_oid.hash,
943 transport->remote->name))
944 die ("Failed to push all needed submodules!");
947 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
948 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
949 struct ref *ref = remote_refs;
950 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
952 for (; ref; ref = ref->next)
953 if (!is_null_oid(&ref->new_oid) &&
954 find_unpushed_submodules(ref->new_oid.hash,
955 transport->remote->name, &needs_pushing))
956 die_with_unpushed_submodules(&needs_pushing);
959 push_ret = transport->push_refs(transport, remote_refs, flags);
960 err = push_had_errors(remote_refs);
961 ret = push_ret | err;
964 transport_print_push_status(transport->url, remote_refs,
965 verbose | porcelain, porcelain,
968 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
969 set_upstreams(transport, remote_refs, pretend);
971 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
973 for (ref = remote_refs; ref; ref = ref->next)
974 transport_update_tracking_ref(transport->remote, ref, verbose);
977 if (porcelain && !push_ret)
979 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
980 fprintf(stderr, "Everything up-to-date\n");
987 const struct ref *transport_get_remote_refs(struct transport *transport)
989 if (!transport->got_remote_refs) {
990 transport->remote_refs = transport->get_refs_list(transport, 0);
991 transport->got_remote_refs = 1;
994 return transport->remote_refs;
997 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1000 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1001 struct ref **heads = NULL;
1004 for (rm = refs; rm; rm = rm->next) {
1007 !is_null_oid(&rm->old_oid) &&
1008 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1010 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1011 heads[nr_heads++] = rm;
1016 * When deepening of a shallow repository is requested,
1017 * then local and remote refs are likely to still be equal.
1018 * Just feed them all to the fetch method in that case.
1019 * This condition shouldn't be met in a non-deepening fetch
1020 * (see builtin/fetch.c:quickfetch()).
1022 ALLOC_ARRAY(heads, nr_refs);
1023 for (rm = refs; rm; rm = rm->next)
1024 heads[nr_heads++] = rm;
1027 rc = transport->fetch(transport, nr_heads, heads);
1033 void transport_unlock_pack(struct transport *transport)
1035 if (transport->pack_lockfile) {
1036 unlink_or_warn(transport->pack_lockfile);
1037 free(transport->pack_lockfile);
1038 transport->pack_lockfile = NULL;
1042 int transport_connect(struct transport *transport, const char *name,
1043 const char *exec, int fd[2])
1045 if (transport->connect)
1046 return transport->connect(transport, name, exec, fd);
1048 die("Operation not supported by protocol");
1051 int transport_disconnect(struct transport *transport)
1054 if (transport->disconnect)
1055 ret = transport->disconnect(transport);
1061 * Strip username (and password) from a URL and return
1062 * it in a newly allocated string.
1064 char *transport_anonymize_url(const char *url)
1066 char *scheme_prefix, *anon_part;
1067 size_t anon_len, prefix_len = 0;
1069 anon_part = strchr(url, '@');
1070 if (url_is_local_not_ssh(url) || !anon_part)
1073 anon_len = strlen(++anon_part);
1074 scheme_prefix = strstr(url, "://");
1075 if (!scheme_prefix) {
1076 if (!strchr(anon_part, ':'))
1077 /* cannot be "me@there:/path/name" */
1081 /* make sure scheme is reasonable */
1082 for (cp = url; cp < scheme_prefix; cp++) {
1085 case '+': case '.': case '-':
1094 /* @ past the first slash does not count */
1095 cp = strchr(scheme_prefix + 3, '/');
1096 if (cp && cp < anon_part)
1098 prefix_len = scheme_prefix - url + 3;
1100 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1101 (int)anon_len, anon_part);
1103 return xstrdup(url);
1106 struct alternate_refs_data {
1107 alternate_ref_fn *fn;
1111 static int refs_from_alternate_cb(struct alternate_object_database *e,
1116 struct remote *remote;
1117 struct transport *transport;
1118 const struct ref *extra;
1119 struct alternate_refs_data *cb = data;
1122 other = xstrdup(real_path(e->base));
1124 len = strlen(other);
1126 while (other[len-1] == '/')
1127 other[--len] = '\0';
1128 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1130 /* Is this a git repository with refs? */
1131 memcpy(other + len - 8, "/refs", 6);
1132 if (!is_directory(other))
1134 other[len - 8] = '\0';
1135 remote = remote_get(other);
1136 transport = transport_get(remote, other);
1137 for (extra = transport_get_remote_refs(transport);
1139 extra = extra->next)
1140 cb->fn(extra, cb->data);
1141 transport_disconnect(transport);
1147 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1149 struct alternate_refs_data cb;
1152 foreach_alt_odb(refs_from_alternate_cb, &cb);