transport: pass summary_width down the callchain
[git/git-svn.git] / transport.c
blobec02b789245d08044e07afc148fd7352cb4383f2
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,
299 struct ref *to, struct ref *from, const char *msg,
300 int porcelain, int summary_width)
302 if (porcelain) {
303 if (from)
304 fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
305 else
306 fprintf(stdout, "%c\t:%s\t", flag, to->name);
307 if (msg)
308 fprintf(stdout, "%s (%s)\n", summary, msg);
309 else
310 fprintf(stdout, "%s\n", summary);
311 } else {
312 fprintf(stderr, " %c %-*s ", flag, summary_width, summary);
313 if (from)
314 fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
315 else
316 fputs(prettify_refname(to->name), stderr);
317 if (msg) {
318 fputs(" (", stderr);
319 fputs(msg, stderr);
320 fputc(')', stderr);
322 fputc('\n', stderr);
326 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
328 if (ref->deletion)
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]" :
334 "[new branch]"),
335 ref, ref->peer_ref, NULL, porcelain, summary_width);
336 else {
337 struct strbuf quickref = STRBUF_INIT;
338 char type;
339 const char *msg;
341 strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash,
342 DEFAULT_ABBREV);
343 if (ref->forced_update) {
344 strbuf_addstr(&quickref, "...");
345 type = '+';
346 msg = "forced update";
347 } else {
348 strbuf_addstr(&quickref, "..");
349 type = ' ';
350 msg = NULL;
352 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash,
353 DEFAULT_ABBREV);
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)
364 if (!count) {
365 char *url = transport_anonymize_url(dest);
366 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
367 free(url);
370 switch(ref->status) {
371 case REF_STATUS_NONE:
372 print_ref_status('X', "[no match]", ref, NULL, NULL,
373 porcelain, summary_width);
374 break;
375 case REF_STATUS_REJECT_NODELETE:
376 print_ref_status('!', "[rejected]", ref, NULL,
377 "remote does not support deleting refs",
378 porcelain, summary_width);
379 break;
380 case REF_STATUS_UPTODATE:
381 print_ref_status('=', "[up to date]", ref,
382 ref->peer_ref, NULL, porcelain, summary_width);
383 break;
384 case REF_STATUS_REJECT_NONFASTFORWARD:
385 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
386 "non-fast-forward", porcelain, summary_width);
387 break;
388 case REF_STATUS_REJECT_ALREADY_EXISTS:
389 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
390 "already exists", porcelain, summary_width);
391 break;
392 case REF_STATUS_REJECT_FETCH_FIRST:
393 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
394 "fetch first", porcelain, summary_width);
395 break;
396 case REF_STATUS_REJECT_NEEDS_FORCE:
397 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
398 "needs force", porcelain, summary_width);
399 break;
400 case REF_STATUS_REJECT_STALE:
401 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
402 "stale info", porcelain, summary_width);
403 break;
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);
408 break;
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);
413 break;
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);
419 break;
420 case REF_STATUS_ATOMIC_PUSH_FAILED:
421 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
422 "atomic push failed", porcelain, summary_width);
423 break;
424 case REF_STATUS_OK:
425 print_ok_ref_status(ref, porcelain, summary_width);
426 break;
429 return 1;
432 void transport_print_push_status(const char *dest, struct ref *refs,
433 int verbose, int porcelain, unsigned int *reject_reasons)
435 struct ref *ref;
436 int n = 0;
437 unsigned char head_sha1[20];
438 char *head;
439 int summary_width = TRANSPORT_SUMMARY_WIDTH;
441 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
443 if (verbose) {
444 for (ref = refs; ref; ref = ref->next)
445 if (ref->status == REF_STATUS_UPTODATE)
446 n += print_one_push_status(ref, dest, n,
447 porcelain, summary_width);
450 for (ref = refs; ref; ref = ref->next)
451 if (ref->status == REF_STATUS_OK)
452 n += print_one_push_status(ref, dest, n,
453 porcelain, summary_width);
455 *reject_reasons = 0;
456 for (ref = refs; ref; ref = ref->next) {
457 if (ref->status != REF_STATUS_NONE &&
458 ref->status != REF_STATUS_UPTODATE &&
459 ref->status != REF_STATUS_OK)
460 n += print_one_push_status(ref, dest, n,
461 porcelain, summary_width);
462 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
463 if (head != NULL && !strcmp(head, ref->name))
464 *reject_reasons |= REJECT_NON_FF_HEAD;
465 else
466 *reject_reasons |= REJECT_NON_FF_OTHER;
467 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
468 *reject_reasons |= REJECT_ALREADY_EXISTS;
469 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
470 *reject_reasons |= REJECT_FETCH_FIRST;
471 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
472 *reject_reasons |= REJECT_NEEDS_FORCE;
475 free(head);
478 void transport_verify_remote_names(int nr_heads, const char **heads)
480 int i;
482 for (i = 0; i < nr_heads; i++) {
483 const char *local = heads[i];
484 const char *remote = strrchr(heads[i], ':');
486 if (*local == '+')
487 local++;
489 /* A matching refspec is okay. */
490 if (remote == local && remote[1] == '\0')
491 continue;
493 remote = remote ? (remote + 1) : local;
494 if (check_refname_format(remote,
495 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
496 die("remote part of refspec is not a valid name in %s",
497 heads[i]);
501 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
503 struct git_transport_data *data = transport->data;
504 struct send_pack_args args;
505 int ret;
507 if (!data->got_remote_heads) {
508 struct ref *tmp_refs;
509 connect_setup(transport, 1);
511 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
512 NULL, &data->shallow);
513 data->got_remote_heads = 1;
516 memset(&args, 0, sizeof(args));
517 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
518 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
519 args.use_thin_pack = data->options.thin;
520 args.verbose = (transport->verbose > 0);
521 args.quiet = (transport->verbose < 0);
522 args.progress = transport->progress;
523 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
524 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
525 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
526 args.push_options = transport->push_options;
527 args.url = transport->url;
529 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
530 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
531 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
532 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
533 else
534 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
536 ret = send_pack(&args, data->fd, data->conn, remote_refs,
537 &data->extra_have);
539 close(data->fd[1]);
540 close(data->fd[0]);
541 ret |= finish_connect(data->conn);
542 data->conn = NULL;
543 data->got_remote_heads = 0;
545 return ret;
548 static int connect_git(struct transport *transport, const char *name,
549 const char *executable, int fd[2])
551 struct git_transport_data *data = transport->data;
552 data->conn = git_connect(data->fd, transport->url,
553 executable, 0);
554 fd[0] = data->fd[0];
555 fd[1] = data->fd[1];
556 return 0;
559 static int disconnect_git(struct transport *transport)
561 struct git_transport_data *data = transport->data;
562 if (data->conn) {
563 if (data->got_remote_heads)
564 packet_flush(data->fd[1]);
565 close(data->fd[0]);
566 close(data->fd[1]);
567 finish_connect(data->conn);
570 free(data);
571 return 0;
574 void transport_take_over(struct transport *transport,
575 struct child_process *child)
577 struct git_transport_data *data;
579 if (!transport->smart_options)
580 die("BUG: taking over transport requires non-NULL "
581 "smart_options field.");
583 data = xcalloc(1, sizeof(*data));
584 data->options = *transport->smart_options;
585 data->conn = child;
586 data->fd[0] = data->conn->out;
587 data->fd[1] = data->conn->in;
588 data->got_remote_heads = 0;
589 transport->data = data;
591 transport->set_option = NULL;
592 transport->get_refs_list = get_refs_via_connect;
593 transport->fetch = fetch_refs_via_pack;
594 transport->push = NULL;
595 transport->push_refs = git_transport_push;
596 transport->disconnect = disconnect_git;
597 transport->smart_options = &(data->options);
599 transport->cannot_reuse = 1;
602 static int is_file(const char *url)
604 struct stat buf;
605 if (stat(url, &buf))
606 return 0;
607 return S_ISREG(buf.st_mode);
610 static int external_specification_len(const char *url)
612 return strchr(url, ':') - url;
615 static const struct string_list *protocol_whitelist(void)
617 static int enabled = -1;
618 static struct string_list allowed = STRING_LIST_INIT_DUP;
620 if (enabled < 0) {
621 const char *v = getenv("GIT_ALLOW_PROTOCOL");
622 if (v) {
623 string_list_split(&allowed, v, ':', -1);
624 string_list_sort(&allowed);
625 enabled = 1;
626 } else {
627 enabled = 0;
631 return enabled ? &allowed : NULL;
634 int is_transport_allowed(const char *type)
636 const struct string_list *allowed = protocol_whitelist();
637 return !allowed || string_list_has_string(allowed, type);
640 void transport_check_allowed(const char *type)
642 if (!is_transport_allowed(type))
643 die("transport '%s' not allowed", type);
646 int transport_restrict_protocols(void)
648 return !!protocol_whitelist();
651 struct transport *transport_get(struct remote *remote, const char *url)
653 const char *helper;
654 struct transport *ret = xcalloc(1, sizeof(*ret));
656 ret->progress = isatty(2);
658 if (!remote)
659 die("No remote provided to transport_get()");
661 ret->got_remote_refs = 0;
662 ret->remote = remote;
663 helper = remote->foreign_vcs;
665 if (!url && remote->url)
666 url = remote->url[0];
667 ret->url = url;
669 /* maybe it is a foreign URL? */
670 if (url) {
671 const char *p = url;
673 while (is_urlschemechar(p == url, *p))
674 p++;
675 if (starts_with(p, "::"))
676 helper = xstrndup(url, p - url);
679 if (helper) {
680 transport_helper_init(ret, helper);
681 } else if (starts_with(url, "rsync:")) {
682 die("git-over-rsync is no longer supported");
683 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
684 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
685 transport_check_allowed("file");
686 ret->data = data;
687 ret->get_refs_list = get_refs_from_bundle;
688 ret->fetch = fetch_refs_from_bundle;
689 ret->disconnect = close_bundle;
690 ret->smart_options = NULL;
691 } else if (!is_url(url)
692 || starts_with(url, "file://")
693 || starts_with(url, "git://")
694 || starts_with(url, "ssh://")
695 || starts_with(url, "git+ssh://") /* deprecated - do not use */
696 || starts_with(url, "ssh+git://") /* deprecated - do not use */
699 * These are builtin smart transports; "allowed" transports
700 * will be checked individually in git_connect.
702 struct git_transport_data *data = xcalloc(1, sizeof(*data));
703 ret->data = data;
704 ret->set_option = NULL;
705 ret->get_refs_list = get_refs_via_connect;
706 ret->fetch = fetch_refs_via_pack;
707 ret->push_refs = git_transport_push;
708 ret->connect = connect_git;
709 ret->disconnect = disconnect_git;
710 ret->smart_options = &(data->options);
712 data->conn = NULL;
713 data->got_remote_heads = 0;
714 } else {
715 /* Unknown protocol in URL. Pass to external handler. */
716 int len = external_specification_len(url);
717 char *handler = xmemdupz(url, len);
718 transport_helper_init(ret, handler);
721 if (ret->smart_options) {
722 ret->smart_options->thin = 1;
723 ret->smart_options->uploadpack = "git-upload-pack";
724 if (remote->uploadpack)
725 ret->smart_options->uploadpack = remote->uploadpack;
726 ret->smart_options->receivepack = "git-receive-pack";
727 if (remote->receivepack)
728 ret->smart_options->receivepack = remote->receivepack;
731 return ret;
734 int transport_set_option(struct transport *transport,
735 const char *name, const char *value)
737 int git_reports = 1, protocol_reports = 1;
739 if (transport->smart_options)
740 git_reports = set_git_option(transport->smart_options,
741 name, value);
743 if (transport->set_option)
744 protocol_reports = transport->set_option(transport, name,
745 value);
747 /* If either report is 0, report 0 (success). */
748 if (!git_reports || !protocol_reports)
749 return 0;
750 /* If either reports -1 (invalid value), report -1. */
751 if ((git_reports == -1) || (protocol_reports == -1))
752 return -1;
753 /* Otherwise if both report unknown, report unknown. */
754 return 1;
757 void transport_set_verbosity(struct transport *transport, int verbosity,
758 int force_progress)
760 if (verbosity >= 1)
761 transport->verbose = verbosity <= 3 ? verbosity : 3;
762 if (verbosity < 0)
763 transport->verbose = -1;
766 * Rules used to determine whether to report progress (processing aborts
767 * when a rule is satisfied):
769 * . Report progress, if force_progress is 1 (ie. --progress).
770 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
771 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
772 * . Report progress if isatty(2) is 1.
774 if (force_progress >= 0)
775 transport->progress = !!force_progress;
776 else
777 transport->progress = verbosity >= 0 && isatty(2);
780 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
782 int i;
784 fprintf(stderr, _("The following submodule paths contain changes that can\n"
785 "not be found on any remote:\n"));
786 for (i = 0; i < needs_pushing->nr; i++)
787 fprintf(stderr, " %s\n", needs_pushing->items[i].string);
788 fprintf(stderr, _("\nPlease try\n\n"
789 " git push --recurse-submodules=on-demand\n\n"
790 "or cd to the path and use\n\n"
791 " git push\n\n"
792 "to push them to a remote.\n\n"));
794 string_list_clear(needs_pushing, 0);
796 die(_("Aborting."));
799 static int run_pre_push_hook(struct transport *transport,
800 struct ref *remote_refs)
802 int ret = 0, x;
803 struct ref *r;
804 struct child_process proc = CHILD_PROCESS_INIT;
805 struct strbuf buf;
806 const char *argv[4];
808 if (!(argv[0] = find_hook("pre-push")))
809 return 0;
811 argv[1] = transport->remote->name;
812 argv[2] = transport->url;
813 argv[3] = NULL;
815 proc.argv = argv;
816 proc.in = -1;
818 if (start_command(&proc)) {
819 finish_command(&proc);
820 return -1;
823 sigchain_push(SIGPIPE, SIG_IGN);
825 strbuf_init(&buf, 256);
827 for (r = remote_refs; r; r = r->next) {
828 if (!r->peer_ref) continue;
829 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
830 if (r->status == REF_STATUS_REJECT_STALE) continue;
831 if (r->status == REF_STATUS_UPTODATE) continue;
833 strbuf_reset(&buf);
834 strbuf_addf( &buf, "%s %s %s %s\n",
835 r->peer_ref->name, oid_to_hex(&r->new_oid),
836 r->name, oid_to_hex(&r->old_oid));
838 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
839 /* We do not mind if a hook does not read all refs. */
840 if (errno != EPIPE)
841 ret = -1;
842 break;
846 strbuf_release(&buf);
848 x = close(proc.in);
849 if (!ret)
850 ret = x;
852 sigchain_pop(SIGPIPE);
854 x = finish_command(&proc);
855 if (!ret)
856 ret = x;
858 return ret;
861 int transport_push(struct transport *transport,
862 int refspec_nr, const char **refspec, int flags,
863 unsigned int *reject_reasons)
865 *reject_reasons = 0;
866 transport_verify_remote_names(refspec_nr, refspec);
868 if (transport->push) {
869 /* Maybe FIXME. But no important transport uses this case. */
870 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
871 die("This transport does not support using --set-upstream");
873 return transport->push(transport, refspec_nr, refspec, flags);
874 } else if (transport->push_refs) {
875 struct ref *remote_refs;
876 struct ref *local_refs = get_local_heads();
877 int match_flags = MATCH_REFS_NONE;
878 int verbose = (transport->verbose > 0);
879 int quiet = (transport->verbose < 0);
880 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
881 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
882 int push_ret, ret, err;
884 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
885 return -1;
887 remote_refs = transport->get_refs_list(transport, 1);
889 if (flags & TRANSPORT_PUSH_ALL)
890 match_flags |= MATCH_REFS_ALL;
891 if (flags & TRANSPORT_PUSH_MIRROR)
892 match_flags |= MATCH_REFS_MIRROR;
893 if (flags & TRANSPORT_PUSH_PRUNE)
894 match_flags |= MATCH_REFS_PRUNE;
895 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
896 match_flags |= MATCH_REFS_FOLLOW_TAGS;
898 if (match_push_refs(local_refs, &remote_refs,
899 refspec_nr, refspec, match_flags)) {
900 return -1;
903 if (transport->smart_options &&
904 transport->smart_options->cas &&
905 !is_empty_cas(transport->smart_options->cas))
906 apply_push_cas(transport->smart_options->cas,
907 transport->remote, remote_refs);
909 set_ref_status_for_push(remote_refs,
910 flags & TRANSPORT_PUSH_MIRROR,
911 flags & TRANSPORT_PUSH_FORCE);
913 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
914 if (run_pre_push_hook(transport, remote_refs))
915 return -1;
917 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
918 struct ref *ref = remote_refs;
919 for (; ref; ref = ref->next)
920 if (!is_null_oid(&ref->new_oid) &&
921 !push_unpushed_submodules(ref->new_oid.hash,
922 transport->remote->name))
923 die ("Failed to push all needed submodules!");
926 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
927 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
928 struct ref *ref = remote_refs;
929 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
931 for (; ref; ref = ref->next)
932 if (!is_null_oid(&ref->new_oid) &&
933 find_unpushed_submodules(ref->new_oid.hash,
934 transport->remote->name, &needs_pushing))
935 die_with_unpushed_submodules(&needs_pushing);
938 push_ret = transport->push_refs(transport, remote_refs, flags);
939 err = push_had_errors(remote_refs);
940 ret = push_ret | err;
942 if (!quiet || err)
943 transport_print_push_status(transport->url, remote_refs,
944 verbose | porcelain, porcelain,
945 reject_reasons);
947 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
948 set_upstreams(transport, remote_refs, pretend);
950 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
951 struct ref *ref;
952 for (ref = remote_refs; ref; ref = ref->next)
953 transport_update_tracking_ref(transport->remote, ref, verbose);
956 if (porcelain && !push_ret)
957 puts("Done");
958 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
959 fprintf(stderr, "Everything up-to-date\n");
961 return ret;
963 return 1;
966 const struct ref *transport_get_remote_refs(struct transport *transport)
968 if (!transport->got_remote_refs) {
969 transport->remote_refs = transport->get_refs_list(transport, 0);
970 transport->got_remote_refs = 1;
973 return transport->remote_refs;
976 int transport_fetch_refs(struct transport *transport, struct ref *refs)
978 int rc;
979 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
980 struct ref **heads = NULL;
981 struct ref *rm;
983 for (rm = refs; rm; rm = rm->next) {
984 nr_refs++;
985 if (rm->peer_ref &&
986 !is_null_oid(&rm->old_oid) &&
987 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
988 continue;
989 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
990 heads[nr_heads++] = rm;
993 if (!nr_heads) {
995 * When deepening of a shallow repository is requested,
996 * then local and remote refs are likely to still be equal.
997 * Just feed them all to the fetch method in that case.
998 * This condition shouldn't be met in a non-deepening fetch
999 * (see builtin/fetch.c:quickfetch()).
1001 ALLOC_ARRAY(heads, nr_refs);
1002 for (rm = refs; rm; rm = rm->next)
1003 heads[nr_heads++] = rm;
1006 rc = transport->fetch(transport, nr_heads, heads);
1008 free(heads);
1009 return rc;
1012 void transport_unlock_pack(struct transport *transport)
1014 if (transport->pack_lockfile) {
1015 unlink_or_warn(transport->pack_lockfile);
1016 free(transport->pack_lockfile);
1017 transport->pack_lockfile = NULL;
1021 int transport_connect(struct transport *transport, const char *name,
1022 const char *exec, int fd[2])
1024 if (transport->connect)
1025 return transport->connect(transport, name, exec, fd);
1026 else
1027 die("Operation not supported by protocol");
1030 int transport_disconnect(struct transport *transport)
1032 int ret = 0;
1033 if (transport->disconnect)
1034 ret = transport->disconnect(transport);
1035 free(transport);
1036 return ret;
1040 * Strip username (and password) from a URL and return
1041 * it in a newly allocated string.
1043 char *transport_anonymize_url(const char *url)
1045 char *scheme_prefix, *anon_part;
1046 size_t anon_len, prefix_len = 0;
1048 anon_part = strchr(url, '@');
1049 if (url_is_local_not_ssh(url) || !anon_part)
1050 goto literal_copy;
1052 anon_len = strlen(++anon_part);
1053 scheme_prefix = strstr(url, "://");
1054 if (!scheme_prefix) {
1055 if (!strchr(anon_part, ':'))
1056 /* cannot be "me@there:/path/name" */
1057 goto literal_copy;
1058 } else {
1059 const char *cp;
1060 /* make sure scheme is reasonable */
1061 for (cp = url; cp < scheme_prefix; cp++) {
1062 switch (*cp) {
1063 /* RFC 1738 2.1 */
1064 case '+': case '.': case '-':
1065 break; /* ok */
1066 default:
1067 if (isalnum(*cp))
1068 break;
1069 /* it isn't */
1070 goto literal_copy;
1073 /* @ past the first slash does not count */
1074 cp = strchr(scheme_prefix + 3, '/');
1075 if (cp && cp < anon_part)
1076 goto literal_copy;
1077 prefix_len = scheme_prefix - url + 3;
1079 return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1080 (int)anon_len, anon_part);
1081 literal_copy:
1082 return xstrdup(url);
1085 struct alternate_refs_data {
1086 alternate_ref_fn *fn;
1087 void *data;
1090 static int refs_from_alternate_cb(struct alternate_object_database *e,
1091 void *data)
1093 char *other;
1094 size_t len;
1095 struct remote *remote;
1096 struct transport *transport;
1097 const struct ref *extra;
1098 struct alternate_refs_data *cb = data;
1100 e->name[-1] = '\0';
1101 other = xstrdup(real_path(e->base));
1102 e->name[-1] = '/';
1103 len = strlen(other);
1105 while (other[len-1] == '/')
1106 other[--len] = '\0';
1107 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1108 goto out;
1109 /* Is this a git repository with refs? */
1110 memcpy(other + len - 8, "/refs", 6);
1111 if (!is_directory(other))
1112 goto out;
1113 other[len - 8] = '\0';
1114 remote = remote_get(other);
1115 transport = transport_get(remote, other);
1116 for (extra = transport_get_remote_refs(transport);
1117 extra;
1118 extra = extra->next)
1119 cb->fn(extra, cb->data);
1120 transport_disconnect(transport);
1121 out:
1122 free(other);
1123 return 0;
1126 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1128 struct alternate_refs_data cb;
1129 cb.fn = fn;
1130 cb.data = data;
1131 foreach_alt_odb(refs_from_alternate_cb, &cb);