Merge branch 'js/nedmalloc-gcc6-warnings'
[git/debian.git] / transport.c
blob4ba48b05960203f9c5ed3360e99382bc945ae7e4
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
5 #include "fetch-pack.h"
6 #include "remote.h"
7 #include "connect.h"
8 #include "send-pack.h"
9 #include "walker.h"
10 #include "bundle.h"
11 #include "dir.h"
12 #include "refs.h"
13 #include "branch.h"
14 #include "url.h"
15 #include "submodule.h"
16 #include "string-list.h"
17 #include "sha1-array.h"
18 #include "sigchain.h"
20 static void set_upstreams(struct transport *transport, struct ref *refs,
21 int pretend)
23 struct ref *ref;
24 for (ref = refs; ref; ref = ref->next) {
25 const char *localname;
26 const char *tmp;
27 const char *remotename;
28 unsigned char sha[20];
29 int flag = 0;
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)
36 continue;
37 if (!ref->peer_ref)
38 continue;
39 if (is_null_oid(&ref->new_oid))
40 continue;
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,
46 sha, &flag);
47 if (tmp && flag & REF_ISSYMREF &&
48 starts_with(tmp, "refs/heads/"))
49 localname = tmp;
51 /* Both source and destination must be local branches. */
52 if (!localname || !starts_with(localname, "refs/heads/"))
53 continue;
54 if (!remotename || !starts_with(remotename, "refs/heads/"))
55 continue;
57 if (!pretend)
58 install_branch_config(BRANCH_CONFIG_VERBOSE,
59 localname + 11, transport->remote->name,
60 remotename);
61 else
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 {
69 int fd;
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;
77 int i;
79 if (for_push)
80 return NULL;
82 if (data->fd > 0)
83 close(data->fd);
84 data->fd = read_bundle_header(transport->url, &data->header);
85 if (data->fd < 0)
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);
91 ref->next = result;
92 result = ref;
94 return result;
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;
108 if (data->fd > 0)
109 close(data->fd);
110 free(data);
111 return 0;
114 struct git_transport_data {
115 struct git_transport_options options;
116 struct child_process *conn;
117 int fd[2];
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;
128 return 0;
129 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
130 opts->receivepack = value;
131 return 0;
132 } else if (!strcmp(name, TRANS_OPT_THIN)) {
133 opts->thin = !!value;
134 return 0;
135 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
136 opts->followtags = !!value;
137 return 0;
138 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
139 opts->keep = !!value;
140 return 0;
141 } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
142 opts->update_shallow = !!value;
143 return 0;
144 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
145 if (!value)
146 opts->depth = 0;
147 else {
148 char *end;
149 opts->depth = strtol(value, &end, 0);
150 if (*end)
151 die(_("transport: invalid depth option '%s'"), value);
153 return 0;
155 return 1;
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;
163 if (data->conn)
164 return 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,
175 flags);
177 return 0;
180 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
182 struct git_transport_data *data = transport->data;
183 struct ref *refs;
185 connect_setup(transport, for_push);
186 get_remote_heads(data->fd[0], NULL, 0, &refs,
187 for_push ? REF_NORMAL : 0,
188 &data->extra_have,
189 &data->shallow);
190 data->got_remote_heads = 1;
192 return refs;
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;
199 struct ref *refs;
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;
207 args.lock_pack = 1;
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);
230 close(data->fd[0]);
231 close(data->fd[1]);
232 if (finish_connect(data->conn)) {
233 free_refs(refs);
234 refs = NULL;
236 data->conn = NULL;
237 data->got_remote_heads = 0;
238 data->options.self_contained_and_connected =
239 args.self_contained_and_connected;
241 free_refs(refs_tmp);
242 free_refs(refs);
243 free(dest);
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:
253 case REF_STATUS_OK:
254 break;
255 default:
256 return 1;
259 return 0;
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:
268 break;
269 default:
270 return 1;
273 return 0;
276 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
278 struct refspec rs;
280 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
281 return;
283 rs.src = ref->name;
284 rs.dst = NULL;
286 if (!remote_find_tracking(remote, &rs)) {
287 if (verbose)
288 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
289 if (ref->deletion) {
290 delete_ref(rs.dst, NULL, 0);
291 } else
292 update_ref("update by push", rs.dst,
293 ref->new_oid.hash, NULL, 0, 0);
294 free(rs.dst);
298 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
300 if (porcelain) {
301 if (from)
302 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
303 else
304 fprintf(stdout, "%c\t:%s\t", flag, to->name);
305 if (msg)
306 fprintf(stdout, "%s (%s)\n", summary, msg);
307 else
308 fprintf(stdout, "%s\n", summary);
309 } else {
310 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
311 if (from)
312 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
313 else
314 fputs(prettify_refname(to->name), stderr);
315 if (msg) {
316 fputs(" (", stderr);
317 fputs(msg, stderr);
318 fputc(')', stderr);
320 fputc('\n', stderr);
324 static const char *status_abbrev(unsigned char sha1[20])
326 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
329 static void print_ok_ref_status(struct ref *ref, int porcelain)
331 if (ref->deletion)
332 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
333 else if (is_null_oid(&ref->old_oid))
334 print_ref_status('*',
335 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
336 "[new branch]"),
337 ref, ref->peer_ref, NULL, porcelain);
338 else {
339 struct strbuf quickref = STRBUF_INIT;
340 char type;
341 const char *msg;
343 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
344 if (ref->forced_update) {
345 strbuf_addstr(&quickref, "...");
346 type = '+';
347 msg = "forced update";
348 } else {
349 strbuf_addstr(&quickref, "..");
350 type = ' ';
351 msg = NULL;
353 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
355 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
356 strbuf_release(&quickref);
360 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
362 if (!count) {
363 char *url = transport_anonymize_url(dest);
364 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
365 free(url);
368 switch(ref->status) {
369 case REF_STATUS_NONE:
370 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
371 break;
372 case REF_STATUS_REJECT_NODELETE:
373 print_ref_status('!', "[rejected]", ref, NULL,
374 "remote does not support deleting refs", porcelain);
375 break;
376 case REF_STATUS_UPTODATE:
377 print_ref_status('=', "[up to date]", ref,
378 ref->peer_ref, NULL, porcelain);
379 break;
380 case REF_STATUS_REJECT_NONFASTFORWARD:
381 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
382 "non-fast-forward", porcelain);
383 break;
384 case REF_STATUS_REJECT_ALREADY_EXISTS:
385 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
386 "already exists", porcelain);
387 break;
388 case REF_STATUS_REJECT_FETCH_FIRST:
389 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
390 "fetch first", porcelain);
391 break;
392 case REF_STATUS_REJECT_NEEDS_FORCE:
393 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
394 "needs force", porcelain);
395 break;
396 case REF_STATUS_REJECT_STALE:
397 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
398 "stale info", porcelain);
399 break;
400 case REF_STATUS_REJECT_SHALLOW:
401 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
402 "new shallow roots not allowed", porcelain);
403 break;
404 case REF_STATUS_REMOTE_REJECT:
405 print_ref_status('!', "[remote rejected]", ref,
406 ref->deletion ? NULL : ref->peer_ref,
407 ref->remote_status, porcelain);
408 break;
409 case REF_STATUS_EXPECTING_REPORT:
410 print_ref_status('!', "[remote failure]", ref,
411 ref->deletion ? NULL : ref->peer_ref,
412 "remote failed to report status", porcelain);
413 break;
414 case REF_STATUS_ATOMIC_PUSH_FAILED:
415 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
416 "atomic push failed", porcelain);
417 break;
418 case REF_STATUS_OK:
419 print_ok_ref_status(ref, porcelain);
420 break;
423 return 1;
426 void transport_print_push_status(const char *dest, struct ref *refs,
427 int verbose, int porcelain, unsigned int *reject_reasons)
429 struct ref *ref;
430 int n = 0;
431 unsigned char head_sha1[20];
432 char *head;
434 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
436 if (verbose) {
437 for (ref = refs; ref; ref = ref->next)
438 if (ref->status == REF_STATUS_UPTODATE)
439 n += print_one_push_status(ref, dest, n, porcelain);
442 for (ref = refs; ref; ref = ref->next)
443 if (ref->status == REF_STATUS_OK)
444 n += print_one_push_status(ref, dest, n, porcelain);
446 *reject_reasons = 0;
447 for (ref = refs; ref; ref = ref->next) {
448 if (ref->status != REF_STATUS_NONE &&
449 ref->status != REF_STATUS_UPTODATE &&
450 ref->status != REF_STATUS_OK)
451 n += print_one_push_status(ref, dest, n, porcelain);
452 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
453 if (head != NULL && !strcmp(head, ref->name))
454 *reject_reasons |= REJECT_NON_FF_HEAD;
455 else
456 *reject_reasons |= REJECT_NON_FF_OTHER;
457 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
458 *reject_reasons |= REJECT_ALREADY_EXISTS;
459 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
460 *reject_reasons |= REJECT_FETCH_FIRST;
461 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
462 *reject_reasons |= REJECT_NEEDS_FORCE;
465 free(head);
468 void transport_verify_remote_names(int nr_heads, const char **heads)
470 int i;
472 for (i = 0; i < nr_heads; i++) {
473 const char *local = heads[i];
474 const char *remote = strrchr(heads[i], ':');
476 if (*local == '+')
477 local++;
479 /* A matching refspec is okay. */
480 if (remote == local && remote[1] == '\0')
481 continue;
483 remote = remote ? (remote + 1) : local;
484 if (check_refname_format(remote,
485 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
486 die("remote part of refspec is not a valid name in %s",
487 heads[i]);
491 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
493 struct git_transport_data *data = transport->data;
494 struct send_pack_args args;
495 int ret;
497 if (!data->got_remote_heads) {
498 struct ref *tmp_refs;
499 connect_setup(transport, 1);
501 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
502 NULL, &data->shallow);
503 data->got_remote_heads = 1;
506 memset(&args, 0, sizeof(args));
507 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
508 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
509 args.use_thin_pack = data->options.thin;
510 args.verbose = (transport->verbose > 0);
511 args.quiet = (transport->verbose < 0);
512 args.progress = transport->progress;
513 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
514 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
515 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
516 args.push_options = transport->push_options;
517 args.url = transport->url;
519 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
520 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
521 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
522 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
523 else
524 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
526 ret = send_pack(&args, data->fd, data->conn, remote_refs,
527 &data->extra_have);
529 close(data->fd[1]);
530 close(data->fd[0]);
531 ret |= finish_connect(data->conn);
532 data->conn = NULL;
533 data->got_remote_heads = 0;
535 return ret;
538 static int connect_git(struct transport *transport, const char *name,
539 const char *executable, int fd[2])
541 struct git_transport_data *data = transport->data;
542 data->conn = git_connect(data->fd, transport->url,
543 executable, 0);
544 fd[0] = data->fd[0];
545 fd[1] = data->fd[1];
546 return 0;
549 static int disconnect_git(struct transport *transport)
551 struct git_transport_data *data = transport->data;
552 if (data->conn) {
553 if (data->got_remote_heads)
554 packet_flush(data->fd[1]);
555 close(data->fd[0]);
556 close(data->fd[1]);
557 finish_connect(data->conn);
560 free(data);
561 return 0;
564 void transport_take_over(struct transport *transport,
565 struct child_process *child)
567 struct git_transport_data *data;
569 if (!transport->smart_options)
570 die("Bug detected: Taking over transport requires non-NULL "
571 "smart_options field.");
573 data = xcalloc(1, sizeof(*data));
574 data->options = *transport->smart_options;
575 data->conn = child;
576 data->fd[0] = data->conn->out;
577 data->fd[1] = data->conn->in;
578 data->got_remote_heads = 0;
579 transport->data = data;
581 transport->set_option = NULL;
582 transport->get_refs_list = get_refs_via_connect;
583 transport->fetch = fetch_refs_via_pack;
584 transport->push = NULL;
585 transport->push_refs = git_transport_push;
586 transport->disconnect = disconnect_git;
587 transport->smart_options = &(data->options);
589 transport->cannot_reuse = 1;
592 static int is_file(const char *url)
594 struct stat buf;
595 if (stat(url, &buf))
596 return 0;
597 return S_ISREG(buf.st_mode);
600 static int external_specification_len(const char *url)
602 return strchr(url, ':') - url;
605 static const struct string_list *protocol_whitelist(void)
607 static int enabled = -1;
608 static struct string_list allowed = STRING_LIST_INIT_DUP;
610 if (enabled < 0) {
611 const char *v = getenv("GIT_ALLOW_PROTOCOL");
612 if (v) {
613 string_list_split(&allowed, v, ':', -1);
614 string_list_sort(&allowed);
615 enabled = 1;
616 } else {
617 enabled = 0;
621 return enabled ? &allowed : NULL;
624 int is_transport_allowed(const char *type)
626 const struct string_list *allowed = protocol_whitelist();
627 return !allowed || string_list_has_string(allowed, type);
630 void transport_check_allowed(const char *type)
632 if (!is_transport_allowed(type))
633 die("transport '%s' not allowed", type);
636 int transport_restrict_protocols(void)
638 return !!protocol_whitelist();
641 struct transport *transport_get(struct remote *remote, const char *url)
643 const char *helper;
644 struct transport *ret = xcalloc(1, sizeof(*ret));
646 ret->progress = isatty(2);
648 if (!remote)
649 die("No remote provided to transport_get()");
651 ret->got_remote_refs = 0;
652 ret->remote = remote;
653 helper = remote->foreign_vcs;
655 if (!url && remote->url)
656 url = remote->url[0];
657 ret->url = url;
659 /* maybe it is a foreign URL? */
660 if (url) {
661 const char *p = url;
663 while (is_urlschemechar(p == url, *p))
664 p++;
665 if (starts_with(p, "::"))
666 helper = xstrndup(url, p - url);
669 if (helper) {
670 transport_helper_init(ret, helper);
671 } else if (starts_with(url, "rsync:")) {
672 die("git-over-rsync is no longer supported");
673 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
674 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
675 transport_check_allowed("file");
676 ret->data = data;
677 ret->get_refs_list = get_refs_from_bundle;
678 ret->fetch = fetch_refs_from_bundle;
679 ret->disconnect = close_bundle;
680 ret->smart_options = NULL;
681 } else if (!is_url(url)
682 || starts_with(url, "file://")
683 || starts_with(url, "git://")
684 || starts_with(url, "ssh://")
685 || starts_with(url, "git+ssh://") /* deprecated - do not use */
686 || starts_with(url, "ssh+git://") /* deprecated - do not use */
689 * These are builtin smart transports; "allowed" transports
690 * will be checked individually in git_connect.
692 struct git_transport_data *data = xcalloc(1, sizeof(*data));
693 ret->data = data;
694 ret->set_option = NULL;
695 ret->get_refs_list = get_refs_via_connect;
696 ret->fetch = fetch_refs_via_pack;
697 ret->push_refs = git_transport_push;
698 ret->connect = connect_git;
699 ret->disconnect = disconnect_git;
700 ret->smart_options = &(data->options);
702 data->conn = NULL;
703 data->got_remote_heads = 0;
704 } else {
705 /* Unknown protocol in URL. Pass to external handler. */
706 int len = external_specification_len(url);
707 char *handler = xmemdupz(url, len);
708 transport_helper_init(ret, handler);
711 if (ret->smart_options) {
712 ret->smart_options->thin = 1;
713 ret->smart_options->uploadpack = "git-upload-pack";
714 if (remote->uploadpack)
715 ret->smart_options->uploadpack = remote->uploadpack;
716 ret->smart_options->receivepack = "git-receive-pack";
717 if (remote->receivepack)
718 ret->smart_options->receivepack = remote->receivepack;
721 return ret;
724 int transport_set_option(struct transport *transport,
725 const char *name, const char *value)
727 int git_reports = 1, protocol_reports = 1;
729 if (transport->smart_options)
730 git_reports = set_git_option(transport->smart_options,
731 name, value);
733 if (transport->set_option)
734 protocol_reports = transport->set_option(transport, name,
735 value);
737 /* If either report is 0, report 0 (success). */
738 if (!git_reports || !protocol_reports)
739 return 0;
740 /* If either reports -1 (invalid value), report -1. */
741 if ((git_reports == -1) || (protocol_reports == -1))
742 return -1;
743 /* Otherwise if both report unknown, report unknown. */
744 return 1;
747 void transport_set_verbosity(struct transport *transport, int verbosity,
748 int force_progress)
750 if (verbosity >= 1)
751 transport->verbose = verbosity <= 3 ? verbosity : 3;
752 if (verbosity < 0)
753 transport->verbose = -1;
756 * Rules used to determine whether to report progress (processing aborts
757 * when a rule is satisfied):
759 * . Report progress, if force_progress is 1 (ie. --progress).
760 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
761 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
762 * . Report progress if isatty(2) is 1.
764 if (force_progress >= 0)
765 transport->progress = !!force_progress;
766 else
767 transport->progress = verbosity >= 0 && isatty(2);
770 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
772 int i;
774 fprintf(stderr, _("The following submodule paths contain changes that can\n"
775 "not be found on any remote:\n"));
776 for (i = 0; i < needs_pushing->nr; i++)
777 printf(" %s\n", needs_pushing->items[i].string);
778 fprintf(stderr, _("\nPlease try\n\n"
779 " git push --recurse-submodules=on-demand\n\n"
780 "or cd to the path and use\n\n"
781 " git push\n\n"
782 "to push them to a remote.\n\n"));
784 string_list_clear(needs_pushing, 0);
786 die(_("Aborting."));
789 static int run_pre_push_hook(struct transport *transport,
790 struct ref *remote_refs)
792 int ret = 0, x;
793 struct ref *r;
794 struct child_process proc = CHILD_PROCESS_INIT;
795 struct strbuf buf;
796 const char *argv[4];
798 if (!(argv[0] = find_hook("pre-push")))
799 return 0;
801 argv[1] = transport->remote->name;
802 argv[2] = transport->url;
803 argv[3] = NULL;
805 proc.argv = argv;
806 proc.in = -1;
808 if (start_command(&proc)) {
809 finish_command(&proc);
810 return -1;
813 sigchain_push(SIGPIPE, SIG_IGN);
815 strbuf_init(&buf, 256);
817 for (r = remote_refs; r; r = r->next) {
818 if (!r->peer_ref) continue;
819 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
820 if (r->status == REF_STATUS_REJECT_STALE) continue;
821 if (r->status == REF_STATUS_UPTODATE) continue;
823 strbuf_reset(&buf);
824 strbuf_addf( &buf, "%s %s %s %s\n",
825 r->peer_ref->name, oid_to_hex(&r->new_oid),
826 r->name, oid_to_hex(&r->old_oid));
828 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
829 /* We do not mind if a hook does not read all refs. */
830 if (errno != EPIPE)
831 ret = -1;
832 break;
836 strbuf_release(&buf);
838 x = close(proc.in);
839 if (!ret)
840 ret = x;
842 sigchain_pop(SIGPIPE);
844 x = finish_command(&proc);
845 if (!ret)
846 ret = x;
848 return ret;
851 int transport_push(struct transport *transport,
852 int refspec_nr, const char **refspec, int flags,
853 unsigned int *reject_reasons)
855 *reject_reasons = 0;
856 transport_verify_remote_names(refspec_nr, refspec);
858 if (transport->push) {
859 /* Maybe FIXME. But no important transport uses this case. */
860 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
861 die("This transport does not support using --set-upstream");
863 return transport->push(transport, refspec_nr, refspec, flags);
864 } else if (transport->push_refs) {
865 struct ref *remote_refs;
866 struct ref *local_refs = get_local_heads();
867 int match_flags = MATCH_REFS_NONE;
868 int verbose = (transport->verbose > 0);
869 int quiet = (transport->verbose < 0);
870 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
871 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
872 int push_ret, ret, err;
874 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
875 return -1;
877 remote_refs = transport->get_refs_list(transport, 1);
879 if (flags & TRANSPORT_PUSH_ALL)
880 match_flags |= MATCH_REFS_ALL;
881 if (flags & TRANSPORT_PUSH_MIRROR)
882 match_flags |= MATCH_REFS_MIRROR;
883 if (flags & TRANSPORT_PUSH_PRUNE)
884 match_flags |= MATCH_REFS_PRUNE;
885 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
886 match_flags |= MATCH_REFS_FOLLOW_TAGS;
888 if (match_push_refs(local_refs, &remote_refs,
889 refspec_nr, refspec, match_flags)) {
890 return -1;
893 if (transport->smart_options &&
894 transport->smart_options->cas &&
895 !is_empty_cas(transport->smart_options->cas))
896 apply_push_cas(transport->smart_options->cas,
897 transport->remote, remote_refs);
899 set_ref_status_for_push(remote_refs,
900 flags & TRANSPORT_PUSH_MIRROR,
901 flags & TRANSPORT_PUSH_FORCE);
903 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
904 if (run_pre_push_hook(transport, remote_refs))
905 return -1;
907 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
908 struct ref *ref = remote_refs;
909 for (; ref; ref = ref->next)
910 if (!is_null_oid(&ref->new_oid) &&
911 !push_unpushed_submodules(ref->new_oid.hash,
912 transport->remote->name))
913 die ("Failed to push all needed submodules!");
916 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
917 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
918 struct ref *ref = remote_refs;
919 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
921 for (; ref; ref = ref->next)
922 if (!is_null_oid(&ref->new_oid) &&
923 find_unpushed_submodules(ref->new_oid.hash,
924 transport->remote->name, &needs_pushing))
925 die_with_unpushed_submodules(&needs_pushing);
928 push_ret = transport->push_refs(transport, remote_refs, flags);
929 err = push_had_errors(remote_refs);
930 ret = push_ret | err;
932 if (!quiet || err)
933 transport_print_push_status(transport->url, remote_refs,
934 verbose | porcelain, porcelain,
935 reject_reasons);
937 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
938 set_upstreams(transport, remote_refs, pretend);
940 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
941 struct ref *ref;
942 for (ref = remote_refs; ref; ref = ref->next)
943 transport_update_tracking_ref(transport->remote, ref, verbose);
946 if (porcelain && !push_ret)
947 puts("Done");
948 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
949 fprintf(stderr, "Everything up-to-date\n");
951 return ret;
953 return 1;
956 const struct ref *transport_get_remote_refs(struct transport *transport)
958 if (!transport->got_remote_refs) {
959 transport->remote_refs = transport->get_refs_list(transport, 0);
960 transport->got_remote_refs = 1;
963 return transport->remote_refs;
966 int transport_fetch_refs(struct transport *transport, struct ref *refs)
968 int rc;
969 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
970 struct ref **heads = NULL;
971 struct ref *rm;
973 for (rm = refs; rm; rm = rm->next) {
974 nr_refs++;
975 if (rm->peer_ref &&
976 !is_null_oid(&rm->old_oid) &&
977 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
978 continue;
979 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
980 heads[nr_heads++] = rm;
983 if (!nr_heads) {
985 * When deepening of a shallow repository is requested,
986 * then local and remote refs are likely to still be equal.
987 * Just feed them all to the fetch method in that case.
988 * This condition shouldn't be met in a non-deepening fetch
989 * (see builtin/fetch.c:quickfetch()).
991 ALLOC_ARRAY(heads, nr_refs);
992 for (rm = refs; rm; rm = rm->next)
993 heads[nr_heads++] = rm;
996 rc = transport->fetch(transport, nr_heads, heads);
998 free(heads);
999 return rc;
1002 void transport_unlock_pack(struct transport *transport)
1004 if (transport->pack_lockfile) {
1005 unlink_or_warn(transport->pack_lockfile);
1006 free(transport->pack_lockfile);
1007 transport->pack_lockfile = NULL;
1011 int transport_connect(struct transport *transport, const char *name,
1012 const char *exec, int fd[2])
1014 if (transport->connect)
1015 return transport->connect(transport, name, exec, fd);
1016 else
1017 die("Operation not supported by protocol");
1020 int transport_disconnect(struct transport *transport)
1022 int ret = 0;
1023 if (transport->disconnect)
1024 ret = transport->disconnect(transport);
1025 free(transport);
1026 return ret;
1030 * Strip username (and password) from a URL and return
1031 * it in a newly allocated string.
1033 char *transport_anonymize_url(const char *url)
1035 char *scheme_prefix, *anon_part;
1036 size_t anon_len, prefix_len = 0;
1038 anon_part = strchr(url, '@');
1039 if (url_is_local_not_ssh(url) || !anon_part)
1040 goto literal_copy;
1042 anon_len = strlen(++anon_part);
1043 scheme_prefix = strstr(url, "://");
1044 if (!scheme_prefix) {
1045 if (!strchr(anon_part, ':'))
1046 /* cannot be "me@there:/path/name" */
1047 goto literal_copy;
1048 } else {
1049 const char *cp;
1050 /* make sure scheme is reasonable */
1051 for (cp = url; cp < scheme_prefix; cp++) {
1052 switch (*cp) {
1053 /* RFC 1738 2.1 */
1054 case '+': case '.': case '-':
1055 break; /* ok */
1056 default:
1057 if (isalnum(*cp))
1058 break;
1059 /* it isn't */
1060 goto literal_copy;
1063 /* @ past the first slash does not count */
1064 cp = strchr(scheme_prefix + 3, '/');
1065 if (cp && cp < anon_part)
1066 goto literal_copy;
1067 prefix_len = scheme_prefix - url + 3;
1069 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1070 (int)anon_len, anon_part);
1071 literal_copy:
1072 return xstrdup(url);
1075 struct alternate_refs_data {
1076 alternate_ref_fn *fn;
1077 void *data;
1080 static int refs_from_alternate_cb(struct alternate_object_database *e,
1081 void *data)
1083 char *other;
1084 size_t len;
1085 struct remote *remote;
1086 struct transport *transport;
1087 const struct ref *extra;
1088 struct alternate_refs_data *cb = data;
1090 e->name[-1] = '\0';
1091 other = xstrdup(real_path(e->base));
1092 e->name[-1] = '/';
1093 len = strlen(other);
1095 while (other[len-1] == '/')
1096 other[--len] = '\0';
1097 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1098 goto out;
1099 /* Is this a git repository with refs? */
1100 memcpy(other + len - 8, "/refs", 6);
1101 if (!is_directory(other))
1102 goto out;
1103 other[len - 8] = '\0';
1104 remote = remote_get(other);
1105 transport = transport_get(remote, other);
1106 for (extra = transport_get_remote_refs(transport);
1107 extra;
1108 extra = extra->next)
1109 cb->fn(extra, cb->data);
1110 transport_disconnect(transport);
1111 out:
1112 free(other);
1113 return 0;
1116 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1118 struct alternate_refs_data cb;
1119 cb.fn = fn;
1120 cb.data = data;
1121 foreach_alt_odb(refs_from_alternate_cb, &cb);