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"
22 static void set_upstreams(struct transport *transport, struct ref *refs,
26 for (ref = refs; ref; ref = ref->next) {
27 const char *localname;
29 const char *remotename;
32 * Check suitability for tracking. Must be successful /
33 * already up-to-date ref create/modify (not delete).
35 if (ref->status != REF_STATUS_OK &&
36 ref->status != REF_STATUS_UPTODATE)
40 if (is_null_oid(&ref->new_oid))
43 /* Follow symbolic refs (mainly for HEAD). */
44 localname = ref->peer_ref->name;
45 remotename = ref->name;
46 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
48 if (tmp && flag & REF_ISSYMREF &&
49 starts_with(tmp, "refs/heads/"))
52 /* Both source and destination must be local branches. */
53 if (!localname || !starts_with(localname, "refs/heads/"))
55 if (!remotename || !starts_with(remotename, "refs/heads/"))
59 install_branch_config(BRANCH_CONFIG_VERBOSE,
60 localname + 11, transport->remote->name,
63 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
64 localname + 11, remotename + 11,
65 transport->remote->name);
69 struct bundle_transport_data {
71 struct bundle_header header;
74 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
76 struct bundle_transport_data *data = transport->data;
77 struct ref *result = NULL;
85 data->fd = read_bundle_header(transport->url, &data->header);
87 die ("Could not read bundle '%s'.", transport->url);
88 for (i = 0; i < data->header.references.nr; i++) {
89 struct ref_list_entry *e = data->header.references.list + i;
90 struct ref *ref = alloc_ref(e->name);
91 oidcpy(&ref->old_oid, &e->oid);
98 static int fetch_refs_from_bundle(struct transport *transport,
99 int nr_heads, struct ref **to_fetch)
101 struct bundle_transport_data *data = transport->data;
102 return unbundle(&data->header, data->fd,
103 transport->progress ? BUNDLE_VERBOSE : 0);
106 static int close_bundle(struct transport *transport)
108 struct bundle_transport_data *data = transport->data;
115 struct git_transport_data {
116 struct git_transport_options options;
117 struct child_process *conn;
119 unsigned got_remote_heads : 1;
120 struct oid_array extra_have;
121 struct oid_array shallow;
124 static int set_git_option(struct git_transport_options *opts,
125 const char *name, const char *value)
127 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
128 opts->uploadpack = value;
130 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
131 opts->receivepack = value;
133 } else if (!strcmp(name, TRANS_OPT_THIN)) {
134 opts->thin = !!value;
136 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
137 opts->followtags = !!value;
139 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
140 opts->keep = !!value;
142 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
143 opts->update_shallow = !!value;
145 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
150 opts->depth = strtol(value, &end, 0);
152 die(_("transport: invalid depth option '%s'"), value);
155 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
156 opts->deepen_since = value;
158 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
159 opts->deepen_not = (const struct string_list *)value;
161 } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
162 opts->deepen_relative = !!value;
164 } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
165 opts->from_promisor = !!value;
167 } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
168 opts->no_dependents = !!value;
170 } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
171 parse_list_objects_filter(&opts->filter_options, value);
177 static int connect_setup(struct transport *transport, int for_push)
179 struct git_transport_data *data = transport->data;
180 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
185 switch (transport->family) {
186 case TRANSPORT_FAMILY_ALL: break;
187 case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
188 case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
191 data->conn = git_connect(data->fd, transport->url,
192 for_push ? data->options.receivepack :
193 data->options.uploadpack,
199 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
201 struct git_transport_data *data = transport->data;
204 connect_setup(transport, for_push);
205 get_remote_heads(data->fd[0], NULL, 0, &refs,
206 for_push ? REF_NORMAL : 0,
209 data->got_remote_heads = 1;
214 static int fetch_refs_via_pack(struct transport *transport,
215 int nr_heads, struct ref **to_fetch)
218 struct git_transport_data *data = transport->data;
220 char *dest = xstrdup(transport->url);
221 struct fetch_pack_args args;
222 struct ref *refs_tmp = NULL;
224 memset(&args, 0, sizeof(args));
225 args.uploadpack = data->options.uploadpack;
226 args.keep_pack = data->options.keep;
228 args.use_thin_pack = data->options.thin;
229 args.include_tag = data->options.followtags;
230 args.verbose = (transport->verbose > 1);
231 args.quiet = (transport->verbose < 0);
232 args.no_progress = !transport->progress;
233 args.depth = data->options.depth;
234 args.deepen_since = data->options.deepen_since;
235 args.deepen_not = data->options.deepen_not;
236 args.deepen_relative = data->options.deepen_relative;
237 args.check_self_contained_and_connected =
238 data->options.check_self_contained_and_connected;
239 args.cloning = transport->cloning;
240 args.update_shallow = data->options.update_shallow;
241 args.from_promisor = data->options.from_promisor;
242 args.no_dependents = data->options.no_dependents;
243 args.filter_options = data->options.filter_options;
245 if (!data->got_remote_heads) {
246 connect_setup(transport, 0);
247 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
248 NULL, &data->shallow);
249 data->got_remote_heads = 1;
252 refs = fetch_pack(&args, data->fd, data->conn,
253 refs_tmp ? refs_tmp : transport->remote_refs,
254 dest, to_fetch, nr_heads, &data->shallow,
255 &transport->pack_lockfile);
258 if (finish_connect(data->conn))
261 data->got_remote_heads = 0;
262 data->options.self_contained_and_connected =
263 args.self_contained_and_connected;
267 if (report_unmatched_refs(to_fetch, nr_heads))
276 static int push_had_errors(struct ref *ref)
278 for (; ref; ref = ref->next) {
279 switch (ref->status) {
280 case REF_STATUS_NONE:
281 case REF_STATUS_UPTODATE:
291 int transport_refs_pushed(struct ref *ref)
293 for (; ref; ref = ref->next) {
294 switch(ref->status) {
295 case REF_STATUS_NONE:
296 case REF_STATUS_UPTODATE:
305 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
309 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
315 if (!remote_find_tracking(remote, &rs)) {
317 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
319 delete_ref(NULL, rs.dst, NULL, 0);
321 update_ref("update by push", rs.dst, &ref->new_oid,
327 static void print_ref_status(char flag, const char *summary,
328 struct ref *to, struct ref *from, const char *msg,
329 int porcelain, int summary_width)
333 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
335 fprintf(stdout, "%c\t:%s\t", flag, to->name);
337 fprintf(stdout, "%s (%s)\n", summary, msg);
339 fprintf(stdout, "%s\n", summary);
341 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
343 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
345 fputs(prettify_refname(to->name), stderr);
355 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
358 print_ref_status('-', "[deleted]", ref, NULL, NULL,
359 porcelain, summary_width);
360 else if (is_null_oid(&ref->old_oid))
361 print_ref_status('*',
362 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
364 ref, ref->peer_ref, NULL, porcelain, summary_width);
366 struct strbuf quickref = STRBUF_INIT;
370 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
372 if (ref->forced_update) {
373 strbuf_addstr(&quickref, "...");
375 msg = "forced update";
377 strbuf_addstr(&quickref, "..");
381 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
384 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
385 porcelain, summary_width);
386 strbuf_release(&quickref);
390 static int print_one_push_status(struct ref *ref, const char *dest, int count,
391 int porcelain, int summary_width)
394 char *url = transport_anonymize_url(dest);
395 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
399 switch(ref->status) {
400 case REF_STATUS_NONE:
401 print_ref_status('X', "[no match]", ref, NULL, NULL,
402 porcelain, summary_width);
404 case REF_STATUS_REJECT_NODELETE:
405 print_ref_status('!', "[rejected]", ref, NULL,
406 "remote does not support deleting refs",
407 porcelain, summary_width);
409 case REF_STATUS_UPTODATE:
410 print_ref_status('=', "[up to date]", ref,
411 ref->peer_ref, NULL, porcelain, summary_width);
413 case REF_STATUS_REJECT_NONFASTFORWARD:
414 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
415 "non-fast-forward", porcelain, summary_width);
417 case REF_STATUS_REJECT_ALREADY_EXISTS:
418 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
419 "already exists", porcelain, summary_width);
421 case REF_STATUS_REJECT_FETCH_FIRST:
422 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
423 "fetch first", porcelain, summary_width);
425 case REF_STATUS_REJECT_NEEDS_FORCE:
426 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
427 "needs force", porcelain, summary_width);
429 case REF_STATUS_REJECT_STALE:
430 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
431 "stale info", porcelain, summary_width);
433 case REF_STATUS_REJECT_SHALLOW:
434 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
435 "new shallow roots not allowed",
436 porcelain, summary_width);
438 case REF_STATUS_REMOTE_REJECT:
439 print_ref_status('!', "[remote rejected]", ref,
440 ref->deletion ? NULL : ref->peer_ref,
441 ref->remote_status, porcelain, summary_width);
443 case REF_STATUS_EXPECTING_REPORT:
444 print_ref_status('!', "[remote failure]", ref,
445 ref->deletion ? NULL : ref->peer_ref,
446 "remote failed to report status",
447 porcelain, summary_width);
449 case REF_STATUS_ATOMIC_PUSH_FAILED:
450 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
451 "atomic push failed", porcelain, summary_width);
454 print_ok_ref_status(ref, porcelain, summary_width);
461 static int measure_abbrev(const struct object_id *oid, int sofar)
463 char hex[GIT_MAX_HEXSZ + 1];
464 int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
466 return (w < sofar) ? sofar : w;
469 int transport_summary_width(const struct ref *refs)
473 for (; refs; refs = refs->next) {
474 maxw = measure_abbrev(&refs->old_oid, maxw);
475 maxw = measure_abbrev(&refs->new_oid, maxw);
478 maxw = FALLBACK_DEFAULT_ABBREV;
479 return (2 * maxw + 3);
482 void transport_print_push_status(const char *dest, struct ref *refs,
483 int verbose, int porcelain, unsigned int *reject_reasons)
488 int summary_width = transport_summary_width(refs);
490 head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
493 for (ref = refs; ref; ref = ref->next)
494 if (ref->status == REF_STATUS_UPTODATE)
495 n += print_one_push_status(ref, dest, n,
496 porcelain, summary_width);
499 for (ref = refs; ref; ref = ref->next)
500 if (ref->status == REF_STATUS_OK)
501 n += print_one_push_status(ref, dest, n,
502 porcelain, summary_width);
505 for (ref = refs; ref; ref = ref->next) {
506 if (ref->status != REF_STATUS_NONE &&
507 ref->status != REF_STATUS_UPTODATE &&
508 ref->status != REF_STATUS_OK)
509 n += print_one_push_status(ref, dest, n,
510 porcelain, summary_width);
511 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
512 if (head != NULL && !strcmp(head, ref->name))
513 *reject_reasons |= REJECT_NON_FF_HEAD;
515 *reject_reasons |= REJECT_NON_FF_OTHER;
516 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
517 *reject_reasons |= REJECT_ALREADY_EXISTS;
518 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
519 *reject_reasons |= REJECT_FETCH_FIRST;
520 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
521 *reject_reasons |= REJECT_NEEDS_FORCE;
527 void transport_verify_remote_names(int nr_heads, const char **heads)
531 for (i = 0; i < nr_heads; i++) {
532 const char *local = heads[i];
533 const char *remote = strrchr(heads[i], ':');
538 /* A matching refspec is okay. */
539 if (remote == local && remote[1] == '\0')
542 remote = remote ? (remote + 1) : local;
543 if (check_refname_format(remote,
544 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
545 die("remote part of refspec is not a valid name in %s",
550 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
552 struct git_transport_data *data = transport->data;
553 struct send_pack_args args;
556 if (!data->got_remote_heads) {
557 struct ref *tmp_refs;
558 connect_setup(transport, 1);
560 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
561 NULL, &data->shallow);
562 data->got_remote_heads = 1;
565 memset(&args, 0, sizeof(args));
566 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
567 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
568 args.use_thin_pack = data->options.thin;
569 args.verbose = (transport->verbose > 0);
570 args.quiet = (transport->verbose < 0);
571 args.progress = transport->progress;
572 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
573 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
574 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
575 args.push_options = transport->push_options;
576 args.url = transport->url;
578 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
579 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
580 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
581 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
583 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
585 ret = send_pack(&args, data->fd, data->conn, remote_refs,
590 ret |= finish_connect(data->conn);
592 data->got_remote_heads = 0;
597 static int connect_git(struct transport *transport, const char *name,
598 const char *executable, int fd[2])
600 struct git_transport_data *data = transport->data;
601 data->conn = git_connect(data->fd, transport->url,
608 static int disconnect_git(struct transport *transport)
610 struct git_transport_data *data = transport->data;
612 if (data->got_remote_heads)
613 packet_flush(data->fd[1]);
616 finish_connect(data->conn);
623 static struct transport_vtable taken_over_vtable = {
625 get_refs_via_connect,
632 void transport_take_over(struct transport *transport,
633 struct child_process *child)
635 struct git_transport_data *data;
637 if (!transport->smart_options)
638 die("BUG: taking over transport requires non-NULL "
639 "smart_options field.");
641 data = xcalloc(1, sizeof(*data));
642 data->options = *transport->smart_options;
644 data->fd[0] = data->conn->out;
645 data->fd[1] = data->conn->in;
646 data->got_remote_heads = 0;
647 transport->data = data;
649 transport->vtable = &taken_over_vtable;
650 transport->smart_options = &(data->options);
652 transport->cannot_reuse = 1;
655 static int is_file(const char *url)
660 return S_ISREG(buf.st_mode);
663 static int external_specification_len(const char *url)
665 return strchr(url, ':') - url;
668 static const struct string_list *protocol_whitelist(void)
670 static int enabled = -1;
671 static struct string_list allowed = STRING_LIST_INIT_DUP;
674 const char *v = getenv("GIT_ALLOW_PROTOCOL");
676 string_list_split(&allowed, v, ':', -1);
677 string_list_sort(&allowed);
684 return enabled ? &allowed : NULL;
687 enum protocol_allow_config {
688 PROTOCOL_ALLOW_NEVER = 0,
689 PROTOCOL_ALLOW_USER_ONLY,
690 PROTOCOL_ALLOW_ALWAYS
693 static enum protocol_allow_config parse_protocol_config(const char *key,
696 if (!strcasecmp(value, "always"))
697 return PROTOCOL_ALLOW_ALWAYS;
698 else if (!strcasecmp(value, "never"))
699 return PROTOCOL_ALLOW_NEVER;
700 else if (!strcasecmp(value, "user"))
701 return PROTOCOL_ALLOW_USER_ONLY;
703 die("unknown value for config '%s': %s", key, value);
706 static enum protocol_allow_config get_protocol_config(const char *type)
708 char *key = xstrfmt("protocol.%s.allow", type);
711 /* first check the per-protocol config */
712 if (!git_config_get_string(key, &value)) {
713 enum protocol_allow_config ret =
714 parse_protocol_config(key, value);
721 /* if defined, fallback to user-defined default for unknown protocols */
722 if (!git_config_get_string("protocol.allow", &value)) {
723 enum protocol_allow_config ret =
724 parse_protocol_config("protocol.allow", value);
729 /* fallback to built-in defaults */
731 if (!strcmp(type, "http") ||
732 !strcmp(type, "https") ||
733 !strcmp(type, "git") ||
734 !strcmp(type, "ssh") ||
735 !strcmp(type, "file"))
736 return PROTOCOL_ALLOW_ALWAYS;
738 /* known scary; err on the side of caution */
739 if (!strcmp(type, "ext"))
740 return PROTOCOL_ALLOW_NEVER;
742 /* unknown; by default let them be used only directly by the user */
743 return PROTOCOL_ALLOW_USER_ONLY;
746 int is_transport_allowed(const char *type, int from_user)
748 const struct string_list *whitelist = protocol_whitelist();
750 return string_list_has_string(whitelist, type);
752 switch (get_protocol_config(type)) {
753 case PROTOCOL_ALLOW_ALWAYS:
755 case PROTOCOL_ALLOW_NEVER:
757 case PROTOCOL_ALLOW_USER_ONLY:
759 from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
763 die("BUG: invalid protocol_allow_config type");
766 void transport_check_allowed(const char *type)
768 if (!is_transport_allowed(type, -1))
769 die("transport '%s' not allowed", type);
772 static struct transport_vtable bundle_vtable = {
774 get_refs_from_bundle,
775 fetch_refs_from_bundle,
781 static struct transport_vtable builtin_smart_vtable = {
783 get_refs_via_connect,
790 struct transport *transport_get(struct remote *remote, const char *url)
793 struct transport *ret = xcalloc(1, sizeof(*ret));
795 ret->progress = isatty(2);
798 die("No remote provided to transport_get()");
800 ret->got_remote_refs = 0;
801 ret->remote = remote;
802 helper = remote->foreign_vcs;
804 if (!url && remote->url)
805 url = remote->url[0];
808 /* maybe it is a foreign URL? */
812 while (is_urlschemechar(p == url, *p))
814 if (starts_with(p, "::"))
815 helper = xstrndup(url, p - url);
819 transport_helper_init(ret, helper);
820 } else if (starts_with(url, "rsync:")) {
821 die("git-over-rsync is no longer supported");
822 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
823 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
824 transport_check_allowed("file");
826 ret->vtable = &bundle_vtable;
827 ret->smart_options = NULL;
828 } else if (!is_url(url)
829 || starts_with(url, "file://")
830 || starts_with(url, "git://")
831 || starts_with(url, "ssh://")
832 || starts_with(url, "git+ssh://") /* deprecated - do not use */
833 || starts_with(url, "ssh+git://") /* deprecated - do not use */
836 * These are builtin smart transports; "allowed" transports
837 * will be checked individually in git_connect.
839 struct git_transport_data *data = xcalloc(1, sizeof(*data));
841 ret->vtable = &builtin_smart_vtable;
842 ret->smart_options = &(data->options);
845 data->got_remote_heads = 0;
847 /* Unknown protocol in URL. Pass to external handler. */
848 int len = external_specification_len(url);
849 char *handler = xmemdupz(url, len);
850 transport_helper_init(ret, handler);
853 if (ret->smart_options) {
854 ret->smart_options->thin = 1;
855 ret->smart_options->uploadpack = "git-upload-pack";
856 if (remote->uploadpack)
857 ret->smart_options->uploadpack = remote->uploadpack;
858 ret->smart_options->receivepack = "git-receive-pack";
859 if (remote->receivepack)
860 ret->smart_options->receivepack = remote->receivepack;
866 int transport_set_option(struct transport *transport,
867 const char *name, const char *value)
869 int git_reports = 1, protocol_reports = 1;
871 if (transport->smart_options)
872 git_reports = set_git_option(transport->smart_options,
875 if (transport->vtable->set_option)
876 protocol_reports = transport->vtable->set_option(transport,
879 /* If either report is 0, report 0 (success). */
880 if (!git_reports || !protocol_reports)
882 /* If either reports -1 (invalid value), report -1. */
883 if ((git_reports == -1) || (protocol_reports == -1))
885 /* Otherwise if both report unknown, report unknown. */
889 void transport_set_verbosity(struct transport *transport, int verbosity,
893 transport->verbose = verbosity <= 3 ? verbosity : 3;
895 transport->verbose = -1;
898 * Rules used to determine whether to report progress (processing aborts
899 * when a rule is satisfied):
901 * . Report progress, if force_progress is 1 (ie. --progress).
902 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
903 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
904 * . Report progress if isatty(2) is 1.
906 if (force_progress >= 0)
907 transport->progress = !!force_progress;
909 transport->progress = verbosity >= 0 && isatty(2);
912 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
916 fprintf(stderr, _("The following submodule paths contain changes that can\n"
917 "not be found on any remote:\n"));
918 for (i = 0; i < needs_pushing->nr; i++)
919 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
920 fprintf(stderr, _("\nPlease try\n\n"
921 " git push --recurse-submodules=on-demand\n\n"
922 "or cd to the path and use\n\n"
924 "to push them to a remote.\n\n"));
926 string_list_clear(needs_pushing, 0);
931 static int run_pre_push_hook(struct transport *transport,
932 struct ref *remote_refs)
936 struct child_process proc = CHILD_PROCESS_INIT;
940 if (!(argv[0] = find_hook("pre-push")))
943 argv[1] = transport->remote->name;
944 argv[2] = transport->url;
950 if (start_command(&proc)) {
951 finish_command(&proc);
955 sigchain_push(SIGPIPE, SIG_IGN);
957 strbuf_init(&buf, 256);
959 for (r = remote_refs; r; r = r->next) {
960 if (!r->peer_ref) continue;
961 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
962 if (r->status == REF_STATUS_REJECT_STALE) continue;
963 if (r->status == REF_STATUS_UPTODATE) continue;
966 strbuf_addf( &buf, "%s %s %s %s\n",
967 r->peer_ref->name, oid_to_hex(&r->new_oid),
968 r->name, oid_to_hex(&r->old_oid));
970 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
971 /* We do not mind if a hook does not read all refs. */
978 strbuf_release(&buf);
984 sigchain_pop(SIGPIPE);
986 x = finish_command(&proc);
993 int transport_push(struct transport *transport,
994 int refspec_nr, const char **refspec, int flags,
995 unsigned int *reject_reasons)
998 transport_verify_remote_names(refspec_nr, refspec);
1000 if (transport->vtable->push_refs) {
1001 struct ref *remote_refs;
1002 struct ref *local_refs = get_local_heads();
1003 int match_flags = MATCH_REFS_NONE;
1004 int verbose = (transport->verbose > 0);
1005 int quiet = (transport->verbose < 0);
1006 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1007 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1008 int push_ret, ret, err;
1010 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
1013 remote_refs = transport->vtable->get_refs_list(transport, 1);
1015 if (flags & TRANSPORT_PUSH_ALL)
1016 match_flags |= MATCH_REFS_ALL;
1017 if (flags & TRANSPORT_PUSH_MIRROR)
1018 match_flags |= MATCH_REFS_MIRROR;
1019 if (flags & TRANSPORT_PUSH_PRUNE)
1020 match_flags |= MATCH_REFS_PRUNE;
1021 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1022 match_flags |= MATCH_REFS_FOLLOW_TAGS;
1024 if (match_push_refs(local_refs, &remote_refs,
1025 refspec_nr, refspec, match_flags)) {
1029 if (transport->smart_options &&
1030 transport->smart_options->cas &&
1031 !is_empty_cas(transport->smart_options->cas))
1032 apply_push_cas(transport->smart_options->cas,
1033 transport->remote, remote_refs);
1035 set_ref_status_for_push(remote_refs,
1036 flags & TRANSPORT_PUSH_MIRROR,
1037 flags & TRANSPORT_PUSH_FORCE);
1039 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1040 if (run_pre_push_hook(transport, remote_refs))
1043 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1044 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1045 !is_bare_repository()) {
1046 struct ref *ref = remote_refs;
1047 struct oid_array commits = OID_ARRAY_INIT;
1049 for (; ref; ref = ref->next)
1050 if (!is_null_oid(&ref->new_oid))
1051 oid_array_append(&commits,
1054 if (!push_unpushed_submodules(&commits,
1056 refspec, refspec_nr,
1057 transport->push_options,
1059 oid_array_clear(&commits);
1060 die("Failed to push all needed submodules!");
1062 oid_array_clear(&commits);
1065 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1066 ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1067 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1068 !pretend)) && !is_bare_repository()) {
1069 struct ref *ref = remote_refs;
1070 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1071 struct oid_array commits = OID_ARRAY_INIT;
1073 for (; ref; ref = ref->next)
1074 if (!is_null_oid(&ref->new_oid))
1075 oid_array_append(&commits,
1078 if (find_unpushed_submodules(&commits, transport->remote->name,
1080 oid_array_clear(&commits);
1081 die_with_unpushed_submodules(&needs_pushing);
1083 string_list_clear(&needs_pushing, 0);
1084 oid_array_clear(&commits);
1087 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1088 push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1091 err = push_had_errors(remote_refs);
1092 ret = push_ret | err;
1095 transport_print_push_status(transport->url, remote_refs,
1096 verbose | porcelain, porcelain,
1099 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1100 set_upstreams(transport, remote_refs, pretend);
1102 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1103 TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1105 for (ref = remote_refs; ref; ref = ref->next)
1106 transport_update_tracking_ref(transport->remote, ref, verbose);
1109 if (porcelain && !push_ret)
1111 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1112 fprintf(stderr, "Everything up-to-date\n");
1119 const struct ref *transport_get_remote_refs(struct transport *transport)
1121 if (!transport->got_remote_refs) {
1122 transport->remote_refs = transport->vtable->get_refs_list(transport, 0);
1123 transport->got_remote_refs = 1;
1126 return transport->remote_refs;
1129 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1132 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1133 struct ref **heads = NULL;
1136 for (rm = refs; rm; rm = rm->next) {
1139 !is_null_oid(&rm->old_oid) &&
1140 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
1142 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1143 heads[nr_heads++] = rm;
1148 * When deepening of a shallow repository is requested,
1149 * then local and remote refs are likely to still be equal.
1150 * Just feed them all to the fetch method in that case.
1151 * This condition shouldn't be met in a non-deepening fetch
1152 * (see builtin/fetch.c:quickfetch()).
1154 ALLOC_ARRAY(heads, nr_refs);
1155 for (rm = refs; rm; rm = rm->next)
1156 heads[nr_heads++] = rm;
1159 rc = transport->vtable->fetch(transport, nr_heads, heads);
1165 void transport_unlock_pack(struct transport *transport)
1167 if (transport->pack_lockfile) {
1168 unlink_or_warn(transport->pack_lockfile);
1169 FREE_AND_NULL(transport->pack_lockfile);
1173 int transport_connect(struct transport *transport, const char *name,
1174 const char *exec, int fd[2])
1176 if (transport->vtable->connect)
1177 return transport->vtable->connect(transport, name, exec, fd);
1179 die("Operation not supported by protocol");
1182 int transport_disconnect(struct transport *transport)
1185 if (transport->vtable->disconnect)
1186 ret = transport->vtable->disconnect(transport);
1192 * Strip username (and password) from a URL and return
1193 * it in a newly allocated string.
1195 char *transport_anonymize_url(const char *url)
1197 char *scheme_prefix, *anon_part;
1198 size_t anon_len, prefix_len = 0;
1200 anon_part = strchr(url, '@');
1201 if (url_is_local_not_ssh(url) || !anon_part)
1204 anon_len = strlen(++anon_part);
1205 scheme_prefix = strstr(url, "://");
1206 if (!scheme_prefix) {
1207 if (!strchr(anon_part, ':'))
1208 /* cannot be "me@there:/path/name" */
1212 /* make sure scheme is reasonable */
1213 for (cp = url; cp < scheme_prefix; cp++) {
1216 case '+': case '.': case '-':
1225 /* @ past the first slash does not count */
1226 cp = strchr(scheme_prefix + 3, '/');
1227 if (cp && cp < anon_part)
1229 prefix_len = scheme_prefix - url + 3;
1231 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1232 (int)anon_len, anon_part);
1234 return xstrdup(url);
1237 static void read_alternate_refs(const char *path,
1238 alternate_ref_fn *cb,
1241 struct child_process cmd = CHILD_PROCESS_INIT;
1242 struct strbuf line = STRBUF_INIT;
1246 argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1247 argv_array_push(&cmd.args, "for-each-ref");
1248 argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1249 cmd.env = local_repo_env;
1252 if (start_command(&cmd))
1255 fh = xfdopen(cmd.out, "r");
1256 while (strbuf_getline_lf(&line, fh) != EOF) {
1257 struct object_id oid;
1259 if (get_oid_hex(line.buf, &oid) ||
1260 line.buf[GIT_SHA1_HEXSZ] != ' ') {
1261 warning("invalid line while parsing alternate refs: %s",
1266 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1270 finish_command(&cmd);
1273 struct alternate_refs_data {
1274 alternate_ref_fn *fn;
1278 static int refs_from_alternate_cb(struct alternate_object_database *e,
1281 struct strbuf path = STRBUF_INIT;
1283 struct alternate_refs_data *cb = data;
1285 if (!strbuf_realpath(&path, e->path, 0))
1287 if (!strbuf_strip_suffix(&path, "/objects"))
1289 base_len = path.len;
1291 /* Is this a git repository with refs? */
1292 strbuf_addstr(&path, "/refs");
1293 if (!is_directory(path.buf))
1295 strbuf_setlen(&path, base_len);
1297 read_alternate_refs(path.buf, cb->fn, cb->data);
1300 strbuf_release(&path);
1304 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1306 struct alternate_refs_data cb;
1309 foreach_alt_odb(refs_from_alternate_cb, &cb);