clone: define shallow clone boundary with --shallow-exclude
[git/raj.git] / transport.c
blob3e6f3aa1175f81a9cde32488c6d4be9b27dacbd3
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;
154 } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
155 opts->deepen_since = value;
156 return 0;
157 } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
158 opts->deepen_not = (const struct string_list *)value;
159 return 0;
161 return 1;
164 static int connect_setup(struct transport *transport, int for_push)
166 struct git_transport_data *data = transport->data;
167 int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
169 if (data->conn)
170 return 0;
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.deepen_since = data->options.deepen_since;
215 args.deepen_not = data->options.deepen_not;
216 args.check_self_contained_and_connected =
217 data->options.check_self_contained_and_connected;
218 args.cloning = transport->cloning;
219 args.update_shallow = data->options.update_shallow;
221 if (!data->got_remote_heads) {
222 connect_setup(transport, 0);
223 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
224 NULL, &data->shallow);
225 data->got_remote_heads = 1;
228 refs = fetch_pack(&args, data->fd, data->conn,
229 refs_tmp ? refs_tmp : transport->remote_refs,
230 dest, to_fetch, nr_heads, &data->shallow,
231 &transport->pack_lockfile);
232 close(data->fd[0]);
233 close(data->fd[1]);
234 if (finish_connect(data->conn)) {
235 free_refs(refs);
236 refs = NULL;
238 data->conn = NULL;
239 data->got_remote_heads = 0;
240 data->options.self_contained_and_connected =
241 args.self_contained_and_connected;
243 free_refs(refs_tmp);
244 free_refs(refs);
245 free(dest);
246 return (refs ? 0 : -1);
249 static int push_had_errors(struct ref *ref)
251 for (; ref; ref = ref->next) {
252 switch (ref->status) {
253 case REF_STATUS_NONE:
254 case REF_STATUS_UPTODATE:
255 case REF_STATUS_OK:
256 break;
257 default:
258 return 1;
261 return 0;
264 int transport_refs_pushed(struct ref *ref)
266 for (; ref; ref = ref->next) {
267 switch(ref->status) {
268 case REF_STATUS_NONE:
269 case REF_STATUS_UPTODATE:
270 break;
271 default:
272 return 1;
275 return 0;
278 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
280 struct refspec rs;
282 if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
283 return;
285 rs.src = ref->name;
286 rs.dst = NULL;
288 if (!remote_find_tracking(remote, &rs)) {
289 if (verbose)
290 fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
291 if (ref->deletion) {
292 delete_ref(rs.dst, NULL, 0);
293 } else
294 update_ref("update by push", rs.dst,
295 ref->new_oid.hash, NULL, 0, 0);
296 free(rs.dst);
300 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
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, TRANSPORT_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 const char *status_abbrev(unsigned char sha1[20])
328 return find_unique_abbrev(sha1, DEFAULT_ABBREV);
331 static void print_ok_ref_status(struct ref *ref, int porcelain)
333 if (ref->deletion)
334 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
335 else if (is_null_oid(&ref->old_oid))
336 print_ref_status('*',
337 (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
338 "[new branch]"),
339 ref, ref->peer_ref, NULL, porcelain);
340 else {
341 struct strbuf quickref = STRBUF_INIT;
342 char type;
343 const char *msg;
345 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
346 if (ref->forced_update) {
347 strbuf_addstr(&quickref, "...");
348 type = '+';
349 msg = "forced update";
350 } else {
351 strbuf_addstr(&quickref, "..");
352 type = ' ';
353 msg = NULL;
355 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
357 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
358 strbuf_release(&quickref);
362 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
364 if (!count)
365 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
367 switch(ref->status) {
368 case REF_STATUS_NONE:
369 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
370 break;
371 case REF_STATUS_REJECT_NODELETE:
372 print_ref_status('!', "[rejected]", ref, NULL,
373 "remote does not support deleting refs", porcelain);
374 break;
375 case REF_STATUS_UPTODATE:
376 print_ref_status('=', "[up to date]", ref,
377 ref->peer_ref, NULL, porcelain);
378 break;
379 case REF_STATUS_REJECT_NONFASTFORWARD:
380 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
381 "non-fast-forward", porcelain);
382 break;
383 case REF_STATUS_REJECT_ALREADY_EXISTS:
384 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
385 "already exists", porcelain);
386 break;
387 case REF_STATUS_REJECT_FETCH_FIRST:
388 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
389 "fetch first", porcelain);
390 break;
391 case REF_STATUS_REJECT_NEEDS_FORCE:
392 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
393 "needs force", porcelain);
394 break;
395 case REF_STATUS_REJECT_STALE:
396 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
397 "stale info", porcelain);
398 break;
399 case REF_STATUS_REJECT_SHALLOW:
400 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
401 "new shallow roots not allowed", porcelain);
402 break;
403 case REF_STATUS_REMOTE_REJECT:
404 print_ref_status('!', "[remote rejected]", ref,
405 ref->deletion ? NULL : ref->peer_ref,
406 ref->remote_status, porcelain);
407 break;
408 case REF_STATUS_EXPECTING_REPORT:
409 print_ref_status('!', "[remote failure]", ref,
410 ref->deletion ? NULL : ref->peer_ref,
411 "remote failed to report status", porcelain);
412 break;
413 case REF_STATUS_ATOMIC_PUSH_FAILED:
414 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
415 "atomic push failed", porcelain);
416 break;
417 case REF_STATUS_OK:
418 print_ok_ref_status(ref, porcelain);
419 break;
422 return 1;
425 void transport_print_push_status(const char *dest, struct ref *refs,
426 int verbose, int porcelain, unsigned int *reject_reasons)
428 struct ref *ref;
429 int n = 0;
430 unsigned char head_sha1[20];
431 char *head;
433 head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
435 if (verbose) {
436 for (ref = refs; ref; ref = ref->next)
437 if (ref->status == REF_STATUS_UPTODATE)
438 n += print_one_push_status(ref, dest, n, porcelain);
441 for (ref = refs; ref; ref = ref->next)
442 if (ref->status == REF_STATUS_OK)
443 n += print_one_push_status(ref, dest, n, porcelain);
445 *reject_reasons = 0;
446 for (ref = refs; ref; ref = ref->next) {
447 if (ref->status != REF_STATUS_NONE &&
448 ref->status != REF_STATUS_UPTODATE &&
449 ref->status != REF_STATUS_OK)
450 n += print_one_push_status(ref, dest, n, porcelain);
451 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
452 if (head != NULL && !strcmp(head, ref->name))
453 *reject_reasons |= REJECT_NON_FF_HEAD;
454 else
455 *reject_reasons |= REJECT_NON_FF_OTHER;
456 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
457 *reject_reasons |= REJECT_ALREADY_EXISTS;
458 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
459 *reject_reasons |= REJECT_FETCH_FIRST;
460 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
461 *reject_reasons |= REJECT_NEEDS_FORCE;
464 free(head);
467 void transport_verify_remote_names(int nr_heads, const char **heads)
469 int i;
471 for (i = 0; i < nr_heads; i++) {
472 const char *local = heads[i];
473 const char *remote = strrchr(heads[i], ':');
475 if (*local == '+')
476 local++;
478 /* A matching refspec is okay. */
479 if (remote == local && remote[1] == '\0')
480 continue;
482 remote = remote ? (remote + 1) : local;
483 if (check_refname_format(remote,
484 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
485 die("remote part of refspec is not a valid name in %s",
486 heads[i]);
490 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
492 struct git_transport_data *data = transport->data;
493 struct send_pack_args args;
494 int ret;
496 if (!data->got_remote_heads) {
497 struct ref *tmp_refs;
498 connect_setup(transport, 1);
500 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
501 NULL, &data->shallow);
502 data->got_remote_heads = 1;
505 memset(&args, 0, sizeof(args));
506 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
507 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
508 args.use_thin_pack = data->options.thin;
509 args.verbose = (transport->verbose > 0);
510 args.quiet = (transport->verbose < 0);
511 args.progress = transport->progress;
512 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
513 args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
514 args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
515 args.url = transport->url;
517 if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
518 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
519 else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
520 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
521 else
522 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
524 ret = send_pack(&args, data->fd, data->conn, remote_refs,
525 &data->extra_have);
527 close(data->fd[1]);
528 close(data->fd[0]);
529 ret |= finish_connect(data->conn);
530 data->conn = NULL;
531 data->got_remote_heads = 0;
533 return ret;
536 static int connect_git(struct transport *transport, const char *name,
537 const char *executable, int fd[2])
539 struct git_transport_data *data = transport->data;
540 data->conn = git_connect(data->fd, transport->url,
541 executable, 0);
542 fd[0] = data->fd[0];
543 fd[1] = data->fd[1];
544 return 0;
547 static int disconnect_git(struct transport *transport)
549 struct git_transport_data *data = transport->data;
550 if (data->conn) {
551 if (data->got_remote_heads)
552 packet_flush(data->fd[1]);
553 close(data->fd[0]);
554 close(data->fd[1]);
555 finish_connect(data->conn);
558 free(data);
559 return 0;
562 void transport_take_over(struct transport *transport,
563 struct child_process *child)
565 struct git_transport_data *data;
567 if (!transport->smart_options)
568 die("Bug detected: Taking over transport requires non-NULL "
569 "smart_options field.");
571 data = xcalloc(1, sizeof(*data));
572 data->options = *transport->smart_options;
573 data->conn = child;
574 data->fd[0] = data->conn->out;
575 data->fd[1] = data->conn->in;
576 data->got_remote_heads = 0;
577 transport->data = data;
579 transport->set_option = NULL;
580 transport->get_refs_list = get_refs_via_connect;
581 transport->fetch = fetch_refs_via_pack;
582 transport->push = NULL;
583 transport->push_refs = git_transport_push;
584 transport->disconnect = disconnect_git;
585 transport->smart_options = &(data->options);
587 transport->cannot_reuse = 1;
590 static int is_file(const char *url)
592 struct stat buf;
593 if (stat(url, &buf))
594 return 0;
595 return S_ISREG(buf.st_mode);
598 static int external_specification_len(const char *url)
600 return strchr(url, ':') - url;
603 static const struct string_list *protocol_whitelist(void)
605 static int enabled = -1;
606 static struct string_list allowed = STRING_LIST_INIT_DUP;
608 if (enabled < 0) {
609 const char *v = getenv("GIT_ALLOW_PROTOCOL");
610 if (v) {
611 string_list_split(&allowed, v, ':', -1);
612 string_list_sort(&allowed);
613 enabled = 1;
614 } else {
615 enabled = 0;
619 return enabled ? &allowed : NULL;
622 int is_transport_allowed(const char *type)
624 const struct string_list *allowed = protocol_whitelist();
625 return !allowed || string_list_has_string(allowed, type);
628 void transport_check_allowed(const char *type)
630 if (!is_transport_allowed(type))
631 die("transport '%s' not allowed", type);
634 int transport_restrict_protocols(void)
636 return !!protocol_whitelist();
639 struct transport *transport_get(struct remote *remote, const char *url)
641 const char *helper;
642 struct transport *ret = xcalloc(1, sizeof(*ret));
644 ret->progress = isatty(2);
646 if (!remote)
647 die("No remote provided to transport_get()");
649 ret->got_remote_refs = 0;
650 ret->remote = remote;
651 helper = remote->foreign_vcs;
653 if (!url && remote->url)
654 url = remote->url[0];
655 ret->url = url;
657 /* maybe it is a foreign URL? */
658 if (url) {
659 const char *p = url;
661 while (is_urlschemechar(p == url, *p))
662 p++;
663 if (starts_with(p, "::"))
664 helper = xstrndup(url, p - url);
667 if (helper) {
668 transport_helper_init(ret, helper);
669 } else if (starts_with(url, "rsync:")) {
670 die("git-over-rsync is no longer supported");
671 } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
672 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
673 transport_check_allowed("file");
674 ret->data = data;
675 ret->get_refs_list = get_refs_from_bundle;
676 ret->fetch = fetch_refs_from_bundle;
677 ret->disconnect = close_bundle;
678 ret->smart_options = NULL;
679 } else if (!is_url(url)
680 || starts_with(url, "file://")
681 || starts_with(url, "git://")
682 || starts_with(url, "ssh://")
683 || starts_with(url, "git+ssh://")
684 || starts_with(url, "ssh+git://")) {
686 * These are builtin smart transports; "allowed" transports
687 * will be checked individually in git_connect.
689 struct git_transport_data *data = xcalloc(1, sizeof(*data));
690 ret->data = data;
691 ret->set_option = NULL;
692 ret->get_refs_list = get_refs_via_connect;
693 ret->fetch = fetch_refs_via_pack;
694 ret->push_refs = git_transport_push;
695 ret->connect = connect_git;
696 ret->disconnect = disconnect_git;
697 ret->smart_options = &(data->options);
699 data->conn = NULL;
700 data->got_remote_heads = 0;
701 } else {
702 /* Unknown protocol in URL. Pass to external handler. */
703 int len = external_specification_len(url);
704 char *handler = xmemdupz(url, len);
705 transport_helper_init(ret, handler);
708 if (ret->smart_options) {
709 ret->smart_options->thin = 1;
710 ret->smart_options->uploadpack = "git-upload-pack";
711 if (remote->uploadpack)
712 ret->smart_options->uploadpack = remote->uploadpack;
713 ret->smart_options->receivepack = "git-receive-pack";
714 if (remote->receivepack)
715 ret->smart_options->receivepack = remote->receivepack;
718 return ret;
721 int transport_set_option(struct transport *transport,
722 const char *name, const char *value)
724 int git_reports = 1, protocol_reports = 1;
726 if (transport->smart_options)
727 git_reports = set_git_option(transport->smart_options,
728 name, value);
730 if (transport->set_option)
731 protocol_reports = transport->set_option(transport, name,
732 value);
734 /* If either report is 0, report 0 (success). */
735 if (!git_reports || !protocol_reports)
736 return 0;
737 /* If either reports -1 (invalid value), report -1. */
738 if ((git_reports == -1) || (protocol_reports == -1))
739 return -1;
740 /* Otherwise if both report unknown, report unknown. */
741 return 1;
744 void transport_set_verbosity(struct transport *transport, int verbosity,
745 int force_progress)
747 if (verbosity >= 1)
748 transport->verbose = verbosity <= 3 ? verbosity : 3;
749 if (verbosity < 0)
750 transport->verbose = -1;
753 * Rules used to determine whether to report progress (processing aborts
754 * when a rule is satisfied):
756 * . Report progress, if force_progress is 1 (ie. --progress).
757 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
758 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
759 * . Report progress if isatty(2) is 1.
761 if (force_progress >= 0)
762 transport->progress = !!force_progress;
763 else
764 transport->progress = verbosity >= 0 && isatty(2);
767 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
769 int i;
771 fprintf(stderr, "The following submodule paths contain changes that can\n"
772 "not be found on any remote:\n");
773 for (i = 0; i < needs_pushing->nr; i++)
774 printf(" %s\n", needs_pushing->items[i].string);
775 fprintf(stderr, "\nPlease try\n\n"
776 " git push --recurse-submodules=on-demand\n\n"
777 "or cd to the path and use\n\n"
778 " git push\n\n"
779 "to push them to a remote.\n\n");
781 string_list_clear(needs_pushing, 0);
783 die("Aborting.");
786 static int run_pre_push_hook(struct transport *transport,
787 struct ref *remote_refs)
789 int ret = 0, x;
790 struct ref *r;
791 struct child_process proc = CHILD_PROCESS_INIT;
792 struct strbuf buf;
793 const char *argv[4];
795 if (!(argv[0] = find_hook("pre-push")))
796 return 0;
798 argv[1] = transport->remote->name;
799 argv[2] = transport->url;
800 argv[3] = NULL;
802 proc.argv = argv;
803 proc.in = -1;
805 if (start_command(&proc)) {
806 finish_command(&proc);
807 return -1;
810 sigchain_push(SIGPIPE, SIG_IGN);
812 strbuf_init(&buf, 256);
814 for (r = remote_refs; r; r = r->next) {
815 if (!r->peer_ref) continue;
816 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
817 if (r->status == REF_STATUS_REJECT_STALE) continue;
818 if (r->status == REF_STATUS_UPTODATE) continue;
820 strbuf_reset(&buf);
821 strbuf_addf( &buf, "%s %s %s %s\n",
822 r->peer_ref->name, oid_to_hex(&r->new_oid),
823 r->name, oid_to_hex(&r->old_oid));
825 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
826 /* We do not mind if a hook does not read all refs. */
827 if (errno != EPIPE)
828 ret = -1;
829 break;
833 strbuf_release(&buf);
835 x = close(proc.in);
836 if (!ret)
837 ret = x;
839 sigchain_pop(SIGPIPE);
841 x = finish_command(&proc);
842 if (!ret)
843 ret = x;
845 return ret;
848 int transport_push(struct transport *transport,
849 int refspec_nr, const char **refspec, int flags,
850 unsigned int *reject_reasons)
852 *reject_reasons = 0;
853 transport_verify_remote_names(refspec_nr, refspec);
855 if (transport->push) {
856 /* Maybe FIXME. But no important transport uses this case. */
857 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
858 die("This transport does not support using --set-upstream");
860 return transport->push(transport, refspec_nr, refspec, flags);
861 } else if (transport->push_refs) {
862 struct ref *remote_refs;
863 struct ref *local_refs = get_local_heads();
864 int match_flags = MATCH_REFS_NONE;
865 int verbose = (transport->verbose > 0);
866 int quiet = (transport->verbose < 0);
867 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
868 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
869 int push_ret, ret, err;
871 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
872 return -1;
874 remote_refs = transport->get_refs_list(transport, 1);
876 if (flags & TRANSPORT_PUSH_ALL)
877 match_flags |= MATCH_REFS_ALL;
878 if (flags & TRANSPORT_PUSH_MIRROR)
879 match_flags |= MATCH_REFS_MIRROR;
880 if (flags & TRANSPORT_PUSH_PRUNE)
881 match_flags |= MATCH_REFS_PRUNE;
882 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
883 match_flags |= MATCH_REFS_FOLLOW_TAGS;
885 if (match_push_refs(local_refs, &remote_refs,
886 refspec_nr, refspec, match_flags)) {
887 return -1;
890 if (transport->smart_options &&
891 transport->smart_options->cas &&
892 !is_empty_cas(transport->smart_options->cas))
893 apply_push_cas(transport->smart_options->cas,
894 transport->remote, remote_refs);
896 set_ref_status_for_push(remote_refs,
897 flags & TRANSPORT_PUSH_MIRROR,
898 flags & TRANSPORT_PUSH_FORCE);
900 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
901 if (run_pre_push_hook(transport, remote_refs))
902 return -1;
904 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
905 struct ref *ref = remote_refs;
906 for (; ref; ref = ref->next)
907 if (!is_null_oid(&ref->new_oid) &&
908 !push_unpushed_submodules(ref->new_oid.hash,
909 transport->remote->name))
910 die ("Failed to push all needed submodules!");
913 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
914 TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
915 struct ref *ref = remote_refs;
916 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
918 for (; ref; ref = ref->next)
919 if (!is_null_oid(&ref->new_oid) &&
920 find_unpushed_submodules(ref->new_oid.hash,
921 transport->remote->name, &needs_pushing))
922 die_with_unpushed_submodules(&needs_pushing);
925 push_ret = transport->push_refs(transport, remote_refs, flags);
926 err = push_had_errors(remote_refs);
927 ret = push_ret | err;
929 if (!quiet || err)
930 transport_print_push_status(transport->url, remote_refs,
931 verbose | porcelain, porcelain,
932 reject_reasons);
934 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
935 set_upstreams(transport, remote_refs, pretend);
937 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
938 struct ref *ref;
939 for (ref = remote_refs; ref; ref = ref->next)
940 transport_update_tracking_ref(transport->remote, ref, verbose);
943 if (porcelain && !push_ret)
944 puts("Done");
945 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
946 fprintf(stderr, "Everything up-to-date\n");
948 return ret;
950 return 1;
953 const struct ref *transport_get_remote_refs(struct transport *transport)
955 if (!transport->got_remote_refs) {
956 transport->remote_refs = transport->get_refs_list(transport, 0);
957 transport->got_remote_refs = 1;
960 return transport->remote_refs;
963 int transport_fetch_refs(struct transport *transport, struct ref *refs)
965 int rc;
966 int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
967 struct ref **heads = NULL;
968 struct ref *rm;
970 for (rm = refs; rm; rm = rm->next) {
971 nr_refs++;
972 if (rm->peer_ref &&
973 !is_null_oid(&rm->old_oid) &&
974 !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
975 continue;
976 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
977 heads[nr_heads++] = rm;
980 if (!nr_heads) {
982 * When deepening of a shallow repository is requested,
983 * then local and remote refs are likely to still be equal.
984 * Just feed them all to the fetch method in that case.
985 * This condition shouldn't be met in a non-deepening fetch
986 * (see builtin/fetch.c:quickfetch()).
988 heads = xmalloc(nr_refs * sizeof(*heads));
989 for (rm = refs; rm; rm = rm->next)
990 heads[nr_heads++] = rm;
993 rc = transport->fetch(transport, nr_heads, heads);
995 free(heads);
996 return rc;
999 void transport_unlock_pack(struct transport *transport)
1001 if (transport->pack_lockfile) {
1002 unlink_or_warn(transport->pack_lockfile);
1003 free(transport->pack_lockfile);
1004 transport->pack_lockfile = NULL;
1008 int transport_connect(struct transport *transport, const char *name,
1009 const char *exec, int fd[2])
1011 if (transport->connect)
1012 return transport->connect(transport, name, exec, fd);
1013 else
1014 die("Operation not supported by protocol");
1017 int transport_disconnect(struct transport *transport)
1019 int ret = 0;
1020 if (transport->disconnect)
1021 ret = transport->disconnect(transport);
1022 free(transport);
1023 return ret;
1027 * Strip username (and password) from a URL and return
1028 * it in a newly allocated string.
1030 char *transport_anonymize_url(const char *url)
1032 char *anon_url, *scheme_prefix, *anon_part;
1033 size_t anon_len, prefix_len = 0;
1035 anon_part = strchr(url, '@');
1036 if (url_is_local_not_ssh(url) || !anon_part)
1037 goto literal_copy;
1039 anon_len = strlen(++anon_part);
1040 scheme_prefix = strstr(url, "://");
1041 if (!scheme_prefix) {
1042 if (!strchr(anon_part, ':'))
1043 /* cannot be "me@there:/path/name" */
1044 goto literal_copy;
1045 } else {
1046 const char *cp;
1047 /* make sure scheme is reasonable */
1048 for (cp = url; cp < scheme_prefix; cp++) {
1049 switch (*cp) {
1050 /* RFC 1738 2.1 */
1051 case '+': case '.': case '-':
1052 break; /* ok */
1053 default:
1054 if (isalnum(*cp))
1055 break;
1056 /* it isn't */
1057 goto literal_copy;
1060 /* @ past the first slash does not count */
1061 cp = strchr(scheme_prefix + 3, '/');
1062 if (cp && cp < anon_part)
1063 goto literal_copy;
1064 prefix_len = scheme_prefix - url + 3;
1066 anon_url = xcalloc(1, 1 + prefix_len + anon_len);
1067 memcpy(anon_url, url, prefix_len);
1068 memcpy(anon_url + prefix_len, anon_part, anon_len);
1069 return anon_url;
1070 literal_copy:
1071 return xstrdup(url);
1074 struct alternate_refs_data {
1075 alternate_ref_fn *fn;
1076 void *data;
1079 static int refs_from_alternate_cb(struct alternate_object_database *e,
1080 void *data)
1082 char *other;
1083 size_t len;
1084 struct remote *remote;
1085 struct transport *transport;
1086 const struct ref *extra;
1087 struct alternate_refs_data *cb = data;
1089 e->name[-1] = '\0';
1090 other = xstrdup(real_path(e->base));
1091 e->name[-1] = '/';
1092 len = strlen(other);
1094 while (other[len-1] == '/')
1095 other[--len] = '\0';
1096 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1097 goto out;
1098 /* Is this a git repository with refs? */
1099 memcpy(other + len - 8, "/refs", 6);
1100 if (!is_directory(other))
1101 goto out;
1102 other[len - 8] = '\0';
1103 remote = remote_get(other);
1104 transport = transport_get(remote, other);
1105 for (extra = transport_get_remote_refs(transport);
1106 extra;
1107 extra = extra->next)
1108 cb->fn(extra, cb->data);
1109 transport_disconnect(transport);
1110 out:
1111 free(other);
1112 return 0;
1115 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1117 struct alternate_refs_data cb;
1118 cb.fn = fn;
1119 cb.data = data;
1120 foreach_alt_odb(refs_from_alternate_cb, &cb);