4 #include "run-command.h"
6 #include "fetch-pack.h"
16 #include "submodule.h"
17 #include "string-list.h"
18 #include "sha1-array.h"
20 #include "transport-internal.h"
23 static void set_upstreams(struct transport *transport, struct ref *refs,
27 for (ref = refs; ref; ref = ref->next) {
28 const char *localname;
30 const char *remotename;
33 * Check suitability for tracking. Must be successful /
34 * already up-to-date ref create/modify (not delete).
36 if (ref->status != REF_STATUS_OK &&
37 ref->status != REF_STATUS_UPTODATE)
41 if (is_null_oid(&ref->new_oid))
44 /* Follow symbolic refs (mainly for HEAD). */
45 localname = ref->peer_ref->name;
46 remotename = ref->name;
47 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
49 if (tmp && flag & REF_ISSYMREF &&
50 starts_with(tmp, "refs/heads/"))
53 /* Both source and destination must be local branches. */
54 if (!localname || !starts_with(localname, "refs/heads/"))
56 if (!remotename || !starts_with(remotename, "refs/heads/"))
60 install_branch_config(BRANCH_CONFIG_VERBOSE,
61 localname + 11, transport->remote->name,
64 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
65 localname + 11, remotename + 11,
66 transport->remote->name);
70 struct bundle_transport_data {
72 struct bundle_header header;
75 static struct ref *get_refs_from_bundle(struct transport *transport,
77 const struct argv_array *ref_prefixes)
79 struct bundle_transport_data *data = transport->data;
80 struct ref *result = NULL;
88 data->fd = read_bundle_header(transport->url, &data->header);
90 die ("Could not read bundle '%s'.", transport->url);
91 for (i = 0; i < data->header.references.nr; i++) {
92 struct ref_list_entry *e = data->header.references.list + i;
93 struct ref *ref = alloc_ref(e->name);
94 oidcpy(&ref->old_oid, &e->oid);
101 static int fetch_refs_from_bundle(struct transport *transport,
102 int nr_heads, struct ref **to_fetch)
104 struct bundle_transport_data *data = transport->data;
105 return unbundle(&data->header, data->fd,
106 transport->progress ? BUNDLE_VERBOSE : 0);
109 static int close_bundle(struct transport *transport)
111 struct bundle_transport_data *data = transport->data;
118 struct git_transport_data {
119 struct git_transport_options options;
120 struct child_process *conn;
122 unsigned got_remote_heads : 1;
123 enum protocol_version version;
124 struct oid_array extra_have;
125 struct oid_array shallow;
128 static int set_git_option(struct git_transport_options *opts,
129 const char *name, const char *value)
131 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
132 opts->uploadpack = value;
134 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
135 opts->receivepack = value;
137 } else if (!strcmp(name, TRANS_OPT_THIN)) {
138 opts->thin = !!value;
140 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
141 opts->followtags = !!value;
143 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
144 opts->keep = !!value;
146 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
147 opts->update_shallow = !!value;
149 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
154 opts->depth = strtol(value, &end, 0);
156 die(_("transport: invalid depth option '%s'"), value);
159 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
160 opts->deepen_since = value;
162 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
163 opts->deepen_not = (const struct string_list *)value;
165 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
166 opts->deepen_relative = !!value;
172 static int connect_setup(struct transport *transport, int for_push)
174 struct git_transport_data *data = transport->data;
175 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
180 switch (transport->family) {
181 case TRANSPORT_FAMILY_ALL: break;
182 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
183 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
186 data->conn = git_connect(data->fd, transport->url,
187 for_push ? data->options.receivepack :
188 data->options.uploadpack,
194 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
195 const struct argv_array *ref_prefixes)
197 struct git_transport_data *data = transport->data;
198 struct ref *refs = NULL;
199 struct packet_reader reader;
201 connect_setup(transport, for_push);
203 packet_reader_init(&reader, data->fd[0], NULL, 0,
204 PACKET_READ_CHOMP_NEWLINE |
205 PACKET_READ_GENTLE_ON_EOF);
207 data->version = discover_version(&reader);
208 switch (data->version) {
210 get_remote_refs(data->fd[1], &reader, &refs, for_push,
215 get_remote_heads(&reader, &refs,
216 for_push ? REF_NORMAL : 0,
220 case protocol_unknown_version:
221 BUG("unknown protocol version");
223 data->got_remote_heads = 1;
228 static int fetch_refs_via_pack(struct transport *transport,
229 int nr_heads, struct ref **to_fetch)
232 struct git_transport_data *data = transport->data;
233 struct ref *refs = NULL;
234 char *dest = xstrdup(transport->url);
235 struct fetch_pack_args args;
236 struct ref *refs_tmp = NULL;
238 memset(&args, 0, sizeof(args));
239 args.uploadpack = data->options.uploadpack;
240 args.keep_pack = data->options.keep;
242 args.use_thin_pack = data->options.thin;
243 args.include_tag = data->options.followtags;
244 args.verbose = (transport->verbose > 1);
245 args.quiet = (transport->verbose < 0);
246 args.no_progress = !transport->progress;
247 args.depth = data->options.depth;
248 args.deepen_since = data->options.deepen_since;
249 args.deepen_not = data->options.deepen_not;
250 args.deepen_relative = data->options.deepen_relative;
251 args.check_self_contained_and_connected =
252 data->options.check_self_contained_and_connected;
253 args.cloning = transport->cloning;
254 args.update_shallow = data->options.update_shallow;
256 if (!data->got_remote_heads)
257 refs_tmp = get_refs_via_connect(transport, 0, NULL);
259 switch (data->version) {
261 die("support for protocol v2 not implemented yet");
265 refs = fetch_pack(&args, data->fd, data->conn,
266 refs_tmp ? refs_tmp : transport->remote_refs,
267 dest, to_fetch, nr_heads, &data->shallow,
268 &transport->pack_lockfile);
270 case protocol_unknown_version:
271 BUG("unknown protocol version");
276 if (finish_connect(data->conn))
279 data->got_remote_heads = 0;
280 data->options.self_contained_and_connected =
281 args.self_contained_and_connected;
285 if (report_unmatched_refs(to_fetch, nr_heads))
294 static int push_had_errors(struct ref *ref)
296 for (; ref; ref = ref->next) {
297 switch (ref->status) {
298 case REF_STATUS_NONE:
299 case REF_STATUS_UPTODATE:
309 int transport_refs_pushed(struct ref *ref)
311 for (; ref; ref = ref->next) {
312 switch(ref->status) {
313 case REF_STATUS_NONE:
314 case REF_STATUS_UPTODATE:
323 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
327 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
333 if (!remote_find_tracking(remote, &rs)) {
335 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
337 delete_ref(NULL, rs.dst, NULL, 0);
339 update_ref("update by push", rs.dst, &ref->new_oid,
345 static void print_ref_status(char flag, const char *summary,
346 struct ref *to, struct ref *from, const char *msg,
347 int porcelain, int summary_width)
351 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
353 fprintf(stdout, "%c\t:%s\t", flag, to->name);
355 fprintf(stdout, "%s (%s)\n", summary, msg);
357 fprintf(stdout, "%s\n", summary);
359 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
361 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
363 fputs(prettify_refname(to->name), stderr);
373 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
376 print_ref_status('-', "[deleted]", ref, NULL, NULL,
377 porcelain, summary_width);
378 else if (is_null_oid(&ref->old_oid))
379 print_ref_status('*',
380 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
382 ref, ref->peer_ref, NULL, porcelain, summary_width);
384 struct strbuf quickref = STRBUF_INIT;
388 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
390 if (ref->forced_update) {
391 strbuf_addstr(&quickref, "...");
393 msg = "forced update";
395 strbuf_addstr(&quickref, "..");
399 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
402 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
403 porcelain, summary_width);
404 strbuf_release(&quickref);
408 static int print_one_push_status(struct ref *ref, const char *dest, int count,
409 int porcelain, int summary_width)
412 char *url = transport_anonymize_url(dest);
413 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
417 switch(ref->status) {
418 case REF_STATUS_NONE:
419 print_ref_status('X', "[no match]", ref, NULL, NULL,
420 porcelain, summary_width);
422 case REF_STATUS_REJECT_NODELETE:
423 print_ref_status('!', "[rejected]", ref, NULL,
424 "remote does not support deleting refs",
425 porcelain, summary_width);
427 case REF_STATUS_UPTODATE:
428 print_ref_status('=', "[up to date]", ref,
429 ref->peer_ref, NULL, porcelain, summary_width);
431 case REF_STATUS_REJECT_NONFASTFORWARD:
432 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
433 "non-fast-forward", porcelain, summary_width);
435 case REF_STATUS_REJECT_ALREADY_EXISTS:
436 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
437 "already exists", porcelain, summary_width);
439 case REF_STATUS_REJECT_FETCH_FIRST:
440 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
441 "fetch first", porcelain, summary_width);
443 case REF_STATUS_REJECT_NEEDS_FORCE:
444 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
445 "needs force", porcelain, summary_width);
447 case REF_STATUS_REJECT_STALE:
448 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
449 "stale info", porcelain, summary_width);
451 case REF_STATUS_REJECT_SHALLOW:
452 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
453 "new shallow roots not allowed",
454 porcelain, summary_width);
456 case REF_STATUS_REMOTE_REJECT:
457 print_ref_status('!', "[remote rejected]", ref,
458 ref->deletion ? NULL : ref->peer_ref,
459 ref->remote_status, porcelain, summary_width);
461 case REF_STATUS_EXPECTING_REPORT:
462 print_ref_status('!', "[remote failure]", ref,
463 ref->deletion ? NULL : ref->peer_ref,
464 "remote failed to report status",
465 porcelain, summary_width);
467 case REF_STATUS_ATOMIC_PUSH_FAILED:
468 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
469 "atomic push failed", porcelain, summary_width);
472 print_ok_ref_status(ref, porcelain, summary_width);
479 static int measure_abbrev(const struct object_id *oid, int sofar)
481 char hex[GIT_MAX_HEXSZ + 1];
482 int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV);
484 return (w < sofar) ? sofar : w;
487 int transport_summary_width(const struct ref *refs)
491 for (; refs; refs = refs->next) {
492 maxw = measure_abbrev(&refs->old_oid, maxw);
493 maxw = measure_abbrev(&refs->new_oid, maxw);
496 maxw = FALLBACK_DEFAULT_ABBREV;
497 return (2 * maxw + 3);
500 void transport_print_push_status(const char *dest, struct ref *refs,
501 int verbose, int porcelain, unsigned int *reject_reasons)
506 int summary_width = transport_summary_width(refs);
508 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
511 for (ref = refs; ref; ref = ref->next)
512 if (ref->status == REF_STATUS_UPTODATE)
513 n += print_one_push_status(ref, dest, n,
514 porcelain, summary_width);
517 for (ref = refs; ref; ref = ref->next)
518 if (ref->status == REF_STATUS_OK)
519 n += print_one_push_status(ref, dest, n,
520 porcelain, summary_width);
523 for (ref = refs; ref; ref = ref->next) {
524 if (ref->status != REF_STATUS_NONE &&
525 ref->status != REF_STATUS_UPTODATE &&
526 ref->status != REF_STATUS_OK)
527 n += print_one_push_status(ref, dest, n,
528 porcelain, summary_width);
529 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
530 if (head != NULL && !strcmp(head, ref->name))
531 *reject_reasons |= REJECT_NON_FF_HEAD;
533 *reject_reasons |= REJECT_NON_FF_OTHER;
534 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
535 *reject_reasons |= REJECT_ALREADY_EXISTS;
536 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
537 *reject_reasons |= REJECT_FETCH_FIRST;
538 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
539 *reject_reasons |= REJECT_NEEDS_FORCE;
545 void transport_verify_remote_names(int nr_heads, const char **heads)
549 for (i = 0; i < nr_heads; i++) {
550 const char *local = heads[i];
551 const char *remote = strrchr(heads[i], ':');
556 /* A matching refspec is okay. */
557 if (remote == local && remote[1] == '\0')
560 remote = remote ? (remote + 1) : local;
561 if (check_refname_format(remote,
562 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
563 die("remote part of refspec is not a valid name in %s",
568 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
570 struct git_transport_data *data = transport->data;
571 struct send_pack_args args;
574 if (!data->got_remote_heads)
575 get_refs_via_connect(transport, 1, NULL);
577 memset(&args, 0, sizeof(args));
578 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
579 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
580 args.use_thin_pack = data->options.thin;
581 args.verbose = (transport->verbose > 0);
582 args.quiet = (transport->verbose < 0);
583 args.progress = transport->progress;
584 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
585 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
586 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
587 args.push_options = transport->push_options;
588 args.url = transport->url;
590 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
591 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
592 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
593 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
595 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
597 switch (data->version) {
599 die("support for protocol v2 not implemented yet");
603 ret = send_pack(&args, data->fd, data->conn, remote_refs,
606 case protocol_unknown_version:
607 BUG("unknown protocol version");
612 ret |= finish_connect(data->conn);
614 data->got_remote_heads = 0;
619 static int connect_git(struct transport *transport, const char *name,
620 const char *executable, int fd[2])
622 struct git_transport_data *data = transport->data;
623 data->conn = git_connect(data->fd, transport->url,
630 static int disconnect_git(struct transport *transport)
632 struct git_transport_data *data = transport->data;
634 if (data->got_remote_heads)
635 packet_flush(data->fd[1]);
638 finish_connect(data->conn);
645 static struct transport_vtable taken_over_vtable = {
647 get_refs_via_connect,
654 void transport_take_over(struct transport *transport,
655 struct child_process *child)
657 struct git_transport_data *data;
659 if (!transport->smart_options)
660 die("BUG: taking over transport requires non-NULL "
661 "smart_options field.");
663 data = xcalloc(1, sizeof(*data));
664 data->options = *transport->smart_options;
666 data->fd[0] = data->conn->out;
667 data->fd[1] = data->conn->in;
668 data->got_remote_heads = 0;
669 transport->data = data;
671 transport->vtable = &taken_over_vtable;
672 transport->smart_options = &(data->options);
674 transport->cannot_reuse = 1;
677 static int is_file(const char *url)
682 return S_ISREG(buf.st_mode);
685 static int external_specification_len(const char *url)
687 return strchr(url, ':') - url;
690 static const struct string_list *protocol_whitelist(void)
692 static int enabled = -1;
693 static struct string_list allowed = STRING_LIST_INIT_DUP;
696 const char *v = getenv("GIT_ALLOW_PROTOCOL");
698 string_list_split(&allowed, v, ':', -1);
699 string_list_sort(&allowed);
706 return enabled ? &allowed : NULL;
709 enum protocol_allow_config {
710 PROTOCOL_ALLOW_NEVER = 0,
711 PROTOCOL_ALLOW_USER_ONLY,
712 PROTOCOL_ALLOW_ALWAYS
715 static enum protocol_allow_config parse_protocol_config(const char *key,
718 if (!strcasecmp(value, "always"))
719 return PROTOCOL_ALLOW_ALWAYS;
720 else if (!strcasecmp(value, "never"))
721 return PROTOCOL_ALLOW_NEVER;
722 else if (!strcasecmp(value, "user"))
723 return PROTOCOL_ALLOW_USER_ONLY;
725 die("unknown value for config '%s': %s", key, value);
728 static enum protocol_allow_config get_protocol_config(const char *type)
730 char *key = xstrfmt("protocol.%s.allow", type);
733 /* first check the per-protocol config */
734 if (!git_config_get_string(key, &value)) {
735 enum protocol_allow_config ret =
736 parse_protocol_config(key, value);
743 /* if defined, fallback to user-defined default for unknown protocols */
744 if (!git_config_get_string("protocol.allow", &value)) {
745 enum protocol_allow_config ret =
746 parse_protocol_config("protocol.allow", value);
751 /* fallback to built-in defaults */
753 if (!strcmp(type, "http") ||
754 !strcmp(type, "https") ||
755 !strcmp(type, "git") ||
756 !strcmp(type, "ssh") ||
757 !strcmp(type, "file"))
758 return PROTOCOL_ALLOW_ALWAYS;
760 /* known scary; err on the side of caution */
761 if (!strcmp(type, "ext"))
762 return PROTOCOL_ALLOW_NEVER;
764 /* unknown; by default let them be used only directly by the user */
765 return PROTOCOL_ALLOW_USER_ONLY;
768 int is_transport_allowed(const char *type, int from_user)
770 const struct string_list *whitelist = protocol_whitelist();
772 return string_list_has_string(whitelist, type);
774 switch (get_protocol_config(type)) {
775 case PROTOCOL_ALLOW_ALWAYS:
777 case PROTOCOL_ALLOW_NEVER:
779 case PROTOCOL_ALLOW_USER_ONLY:
781 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
785 die("BUG: invalid protocol_allow_config type");
788 void transport_check_allowed(const char *type)
790 if (!is_transport_allowed(type, -1))
791 die("transport '%s' not allowed", type);
794 static struct transport_vtable bundle_vtable = {
796 get_refs_from_bundle,
797 fetch_refs_from_bundle,
803 static struct transport_vtable builtin_smart_vtable = {
805 get_refs_via_connect,
812 struct transport *transport_get(struct remote *remote, const char *url)
815 struct transport *ret = xcalloc(1, sizeof(*ret));
817 ret->progress = isatty(2);
820 die("No remote provided to transport_get()");
822 ret->got_remote_refs = 0;
823 ret->remote = remote;
824 helper = remote->foreign_vcs;
826 if (!url && remote->url)
827 url = remote->url[0];
830 /* maybe it is a foreign URL? */
834 while (is_urlschemechar(p == url, *p))
836 if (starts_with(p, "::"))
837 helper = xstrndup(url, p - url);
841 transport_helper_init(ret, helper);
842 } else if (starts_with(url, "rsync:")) {
843 die("git-over-rsync is no longer supported");
844 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
845 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
846 transport_check_allowed("file");
848 ret->vtable = &bundle_vtable;
849 ret->smart_options = NULL;
850 } else if (!is_url(url)
851 || starts_with(url, "file://")
852 || starts_with(url, "git://")
853 || starts_with(url, "ssh://")
854 || starts_with(url, "git+ssh://") /* deprecated - do not use */
855 || starts_with(url, "ssh+git://") /* deprecated - do not use */
858 * These are builtin smart transports; "allowed" transports
859 * will be checked individually in git_connect.
861 struct git_transport_data *data = xcalloc(1, sizeof(*data));
863 ret->vtable = &builtin_smart_vtable;
864 ret->smart_options = &(data->options);
867 data->got_remote_heads = 0;
869 /* Unknown protocol in URL. Pass to external handler. */
870 int len = external_specification_len(url);
871 char *handler = xmemdupz(url, len);
872 transport_helper_init(ret, handler);
875 if (ret->smart_options) {
876 ret->smart_options->thin = 1;
877 ret->smart_options->uploadpack = "git-upload-pack";
878 if (remote->uploadpack)
879 ret->smart_options->uploadpack = remote->uploadpack;
880 ret->smart_options->receivepack = "git-receive-pack";
881 if (remote->receivepack)
882 ret->smart_options->receivepack = remote->receivepack;
888 int transport_set_option(struct transport *transport,
889 const char *name, const char *value)
891 int git_reports = 1, protocol_reports = 1;
893 if (transport->smart_options)
894 git_reports = set_git_option(transport->smart_options,
897 if (transport->vtable->set_option)
898 protocol_reports = transport->vtable->set_option(transport,
901 /* If either report is 0, report 0 (success). */
902 if (!git_reports || !protocol_reports)
904 /* If either reports -1 (invalid value), report -1. */
905 if ((git_reports == -1) || (protocol_reports == -1))
907 /* Otherwise if both report unknown, report unknown. */
911 void transport_set_verbosity(struct transport *transport, int verbosity,
915 transport->verbose = verbosity <= 3 ? verbosity : 3;
917 transport->verbose = -1;
920 * Rules used to determine whether to report progress (processing aborts
921 * when a rule is satisfied):
923 * . Report progress, if force_progress is 1 (ie. --progress).
924 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
925 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
926 * . Report progress if isatty(2) is 1.
928 if (force_progress >= 0)
929 transport->progress = !!force_progress;
931 transport->progress = verbosity >= 0 && isatty(2);
934 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
938 fprintf(stderr, _("The following submodule paths contain changes that can\n"
939 "not be found on any remote:\n"));
940 for (i = 0; i < needs_pushing->nr; i++)
941 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
942 fprintf(stderr, _("\nPlease try\n\n"
943 " git push --recurse-submodules=on-demand\n\n"
944 "or cd to the path and use\n\n"
946 "to push them to a remote.\n\n"));
948 string_list_clear(needs_pushing, 0);
953 static int run_pre_push_hook(struct transport *transport,
954 struct ref *remote_refs)
958 struct child_process proc = CHILD_PROCESS_INIT;
962 if (!(argv[0] = find_hook("pre-push")))
965 argv[1] = transport->remote->name;
966 argv[2] = transport->url;
972 if (start_command(&proc)) {
973 finish_command(&proc);
977 sigchain_push(SIGPIPE, SIG_IGN);
979 strbuf_init(&buf, 256);
981 for (r = remote_refs; r; r = r->next) {
982 if (!r->peer_ref) continue;
983 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
984 if (r->status == REF_STATUS_REJECT_STALE) continue;
985 if (r->status == REF_STATUS_UPTODATE) continue;
988 strbuf_addf( &buf, "%s %s %s %s\n",
989 r->peer_ref->name, oid_to_hex(&r->new_oid),
990 r->name, oid_to_hex(&r->old_oid));
992 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
993 /* We do not mind if a hook does not read all refs. */
1000 strbuf_release(&buf);
1006 sigchain_pop(SIGPIPE);
1008 x = finish_command(&proc);
1015 int transport_push(struct transport *transport,
1016 int refspec_nr, const char **refspec, int flags,
1017 unsigned int *reject_reasons)
1019 *reject_reasons = 0;
1020 transport_verify_remote_names(refspec_nr, refspec);
1022 if (transport->vtable->push_refs) {
1023 struct ref *remote_refs;
1024 struct ref *local_refs = get_local_heads();
1025 int match_flags = MATCH_REFS_NONE;
1026 int verbose = (transport->verbose > 0);
1027 int quiet = (transport->verbose < 0);
1028 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1029 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1030 int push_ret, ret, err;
1032 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1035 remote_refs = transport->vtable->get_refs_list(transport, 1, NULL);
1037 if (flags & TRANSPORT_PUSH_ALL)
1038 match_flags |= MATCH_REFS_ALL;
1039 if (flags & TRANSPORT_PUSH_MIRROR)
1040 match_flags |= MATCH_REFS_MIRROR;
1041 if (flags & TRANSPORT_PUSH_PRUNE)
1042 match_flags |= MATCH_REFS_PRUNE;
1043 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1044 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1046 if (match_push_refs(local_refs, &remote_refs,
1047 refspec_nr, refspec, match_flags)) {
1051 if (transport->smart_options &&
1052 transport->smart_options->cas &&
1053 !is_empty_cas(transport->smart_options->cas))
1054 apply_push_cas(transport->smart_options->cas,
1055 transport->remote, remote_refs);
1057 set_ref_status_for_push(remote_refs,
1058 flags & TRANSPORT_PUSH_MIRROR,
1059 flags & TRANSPORT_PUSH_FORCE);
1061 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1062 if (run_pre_push_hook(transport, remote_refs))
1065 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1066 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1067 !is_bare_repository()) {
1068 struct ref *ref = remote_refs;
1069 struct oid_array commits = OID_ARRAY_INIT;
1071 for (; ref; ref = ref->next)
1072 if (!is_null_oid(&ref->new_oid))
1073 oid_array_append(&commits,
1076 if (!push_unpushed_submodules(&commits,
1078 refspec, refspec_nr,
1079 transport->push_options,
1081 oid_array_clear(&commits);
1082 die("Failed to push all needed submodules!");
1084 oid_array_clear(&commits);
1087 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1088 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1089 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1090 !pretend)) && !is_bare_repository()) {
1091 struct ref *ref = remote_refs;
1092 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1093 struct oid_array commits = OID_ARRAY_INIT;
1095 for (; ref; ref = ref->next)
1096 if (!is_null_oid(&ref->new_oid))
1097 oid_array_append(&commits,
1100 if (find_unpushed_submodules(&commits, transport->remote->name,
1102 oid_array_clear(&commits);
1103 die_with_unpushed_submodules(&needs_pushing);
1105 string_list_clear(&needs_pushing, 0);
1106 oid_array_clear(&commits);
1109 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1110 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1113 err = push_had_errors(remote_refs);
1114 ret = push_ret | err;
1117 transport_print_push_status(transport->url, remote_refs,
1118 verbose | porcelain, porcelain,
1121 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1122 set_upstreams(transport, remote_refs, pretend);
1124 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1125 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1127 for (ref = remote_refs; ref; ref = ref->next)
1128 transport_update_tracking_ref(transport->remote, ref, verbose);
1131 if (porcelain && !push_ret)
1133 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1134 fprintf(stderr, "Everything up-to-date\n");
1141 const struct ref *transport_get_remote_refs(struct transport *transport,
1142 const struct argv_array *ref_prefixes)
1144 if (!transport->got_remote_refs) {
1145 transport->remote_refs =
1146 transport->vtable->get_refs_list(transport, 0,
1148 transport->got_remote_refs = 1;
1151 return transport->remote_refs;
1154 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1157 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1158 struct ref **heads = NULL;
1161 for (rm = refs; rm; rm = rm->next) {
1164 !is_null_oid(&rm->old_oid) &&
1165 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1167 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1168 heads[nr_heads++] = rm;
1173 * When deepening of a shallow repository is requested,
1174 * then local and remote refs are likely to still be equal.
1175 * Just feed them all to the fetch method in that case.
1176 * This condition shouldn't be met in a non-deepening fetch
1177 * (see builtin/fetch.c:quickfetch()).
1179 ALLOC_ARRAY(heads, nr_refs);
1180 for (rm = refs; rm; rm = rm->next)
1181 heads[nr_heads++] = rm;
1184 rc = transport->vtable->fetch(transport, nr_heads, heads);
1190 void transport_unlock_pack(struct transport *transport)
1192 if (transport->pack_lockfile) {
1193 unlink_or_warn(transport->pack_lockfile);
1194 FREE_AND_NULL(transport->pack_lockfile);
1198 int transport_connect(struct transport *transport, const char *name,
1199 const char *exec, int fd[2])
1201 if (transport->vtable->connect)
1202 return transport->vtable->connect(transport, name, exec, fd);
1204 die("Operation not supported by protocol");
1207 int transport_disconnect(struct transport *transport)
1210 if (transport->vtable->disconnect)
1211 ret = transport->vtable->disconnect(transport);
1217 * Strip username (and password) from a URL and return
1218 * it in a newly allocated string.
1220 char *transport_anonymize_url(const char *url)
1222 char *scheme_prefix, *anon_part;
1223 size_t anon_len, prefix_len = 0;
1225 anon_part = strchr(url, '@');
1226 if (url_is_local_not_ssh(url) || !anon_part)
1229 anon_len = strlen(++anon_part);
1230 scheme_prefix = strstr(url, "://");
1231 if (!scheme_prefix) {
1232 if (!strchr(anon_part, ':'))
1233 /* cannot be "me@there:/path/name" */
1237 /* make sure scheme is reasonable */
1238 for (cp = url; cp < scheme_prefix; cp++) {
1241 case '+': case '.': case '-':
1250 /* @ past the first slash does not count */
1251 cp = strchr(scheme_prefix + 3, '/');
1252 if (cp && cp < anon_part)
1254 prefix_len = scheme_prefix - url + 3;
1256 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1257 (int)anon_len, anon_part);
1259 return xstrdup(url);
1262 static void read_alternate_refs(const char *path,
1263 alternate_ref_fn *cb,
1266 struct child_process cmd = CHILD_PROCESS_INIT;
1267 struct strbuf line = STRBUF_INIT;
1271 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1272 argv_array_push(&cmd.args, "for-each-ref");
1273 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1274 cmd.env = local_repo_env;
1277 if (start_command(&cmd))
1280 fh = xfdopen(cmd.out, "r");
1281 while (strbuf_getline_lf(&line, fh) != EOF) {
1282 struct object_id oid;
1284 if (get_oid_hex(line.buf, &oid) ||
1285 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1286 warning("invalid line while parsing alternate refs: %s",
1291 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1295 finish_command(&cmd);
1298 struct alternate_refs_data {
1299 alternate_ref_fn *fn;
1303 static int refs_from_alternate_cb(struct alternate_object_database *e,
1306 struct strbuf path = STRBUF_INIT;
1308 struct alternate_refs_data *cb = data;
1310 if (!strbuf_realpath(&path, e->path, 0))
1312 if (!strbuf_strip_suffix(&path, "/objects"))
1314 base_len = path.len;
1316 /* Is this a git repository with refs? */
1317 strbuf_addstr(&path, "/refs");
1318 if (!is_directory(path.buf))
1320 strbuf_setlen(&path, base_len);
1322 read_alternate_refs(path.buf, cb->fn, cb->data);
1325 strbuf_release(&path);
1329 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1331 struct alternate_refs_data cb;
1334 foreach_alt_odb(refs_from_alternate_cb, &cb);