Merge branch 'nd/attr-pathspec-fix'
[git] / transport.c
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 #include "run-command.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "send-pack.h"
10 #include "walker.h"
11 #include "bundle.h"
12 #include "dir.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "branch.h"
16 #include "url.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "sha1-array.h"
20 #include "sigchain.h"
21 #include "transport-internal.h"
22 #include "protocol.h"
23 #include "object-store.h"
24 #include "color.h"
25
26 static int transport_use_color = -1;
27 static char transport_colors[][COLOR_MAXLEN] = {
28         GIT_COLOR_RESET,
29         GIT_COLOR_RED           /* REJECTED */
30 };
31
32 enum color_transport {
33         TRANSPORT_COLOR_RESET = 0,
34         TRANSPORT_COLOR_REJECTED = 1
35 };
36
37 static int transport_color_config(void)
38 {
39         const char *keys[] = {
40                 "color.transport.reset",
41                 "color.transport.rejected"
42         }, *key = "color.transport";
43         char *value;
44         int i;
45         static int initialized;
46
47         if (initialized)
48                 return 0;
49         initialized = 1;
50
51         if (!git_config_get_string(key, &value))
52                 transport_use_color = git_config_colorbool(key, value);
53
54         if (!want_color_stderr(transport_use_color))
55                 return 0;
56
57         for (i = 0; i < ARRAY_SIZE(keys); i++)
58                 if (!git_config_get_string(keys[i], &value)) {
59                         if (!value)
60                                 return config_error_nonbool(keys[i]);
61                         if (color_parse(value, transport_colors[i]) < 0)
62                                 return -1;
63                 }
64
65         return 0;
66 }
67
68 static const char *transport_get_color(enum color_transport ix)
69 {
70         if (want_color_stderr(transport_use_color))
71                 return transport_colors[ix];
72         return "";
73 }
74
75 static void set_upstreams(struct transport *transport, struct ref *refs,
76         int pretend)
77 {
78         struct ref *ref;
79         for (ref = refs; ref; ref = ref->next) {
80                 const char *localname;
81                 const char *tmp;
82                 const char *remotename;
83                 int flag = 0;
84                 /*
85                  * Check suitability for tracking. Must be successful /
86                  * already up-to-date ref create/modify (not delete).
87                  */
88                 if (ref->status != REF_STATUS_OK &&
89                         ref->status != REF_STATUS_UPTODATE)
90                         continue;
91                 if (!ref->peer_ref)
92                         continue;
93                 if (is_null_oid(&ref->new_oid))
94                         continue;
95
96                 /* Follow symbolic refs (mainly for HEAD). */
97                 localname = ref->peer_ref->name;
98                 remotename = ref->name;
99                 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
100                                          NULL, &flag);
101                 if (tmp && flag & REF_ISSYMREF &&
102                         starts_with(tmp, "refs/heads/"))
103                         localname = tmp;
104
105                 /* Both source and destination must be local branches. */
106                 if (!localname || !starts_with(localname, "refs/heads/"))
107                         continue;
108                 if (!remotename || !starts_with(remotename, "refs/heads/"))
109                         continue;
110
111                 if (!pretend)
112                         install_branch_config(BRANCH_CONFIG_VERBOSE,
113                                 localname + 11, transport->remote->name,
114                                 remotename);
115                 else
116                         printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117                                 localname + 11, remotename + 11,
118                                 transport->remote->name);
119         }
120 }
121
122 struct bundle_transport_data {
123         int fd;
124         struct bundle_header header;
125 };
126
127 static struct ref *get_refs_from_bundle(struct transport *transport,
128                                         int for_push,
129                                         const struct argv_array *ref_prefixes)
130 {
131         struct bundle_transport_data *data = transport->data;
132         struct ref *result = NULL;
133         int i;
134
135         if (for_push)
136                 return NULL;
137
138         if (data->fd > 0)
139                 close(data->fd);
140         data->fd = read_bundle_header(transport->url, &data->header);
141         if (data->fd < 0)
142                 die(_("could not read bundle '%s'"), transport->url);
143         for (i = 0; i < data->header.references.nr; i++) {
144                 struct ref_list_entry *e = data->header.references.list + i;
145                 struct ref *ref = alloc_ref(e->name);
146                 oidcpy(&ref->old_oid, &e->oid);
147                 ref->next = result;
148                 result = ref;
149         }
150         return result;
151 }
152
153 static int fetch_refs_from_bundle(struct transport *transport,
154                                int nr_heads, struct ref **to_fetch)
155 {
156         struct bundle_transport_data *data = transport->data;
157         return unbundle(&data->header, data->fd,
158                         transport->progress ? BUNDLE_VERBOSE : 0);
159 }
160
161 static int close_bundle(struct transport *transport)
162 {
163         struct bundle_transport_data *data = transport->data;
164         if (data->fd > 0)
165                 close(data->fd);
166         free(data);
167         return 0;
168 }
169
170 struct git_transport_data {
171         struct git_transport_options options;
172         struct child_process *conn;
173         int fd[2];
174         unsigned got_remote_heads : 1;
175         enum protocol_version version;
176         struct oid_array extra_have;
177         struct oid_array shallow;
178 };
179
180 static int set_git_option(struct git_transport_options *opts,
181                           const char *name, const char *value)
182 {
183         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
184                 opts->uploadpack = value;
185                 return 0;
186         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
187                 opts->receivepack = value;
188                 return 0;
189         } else if (!strcmp(name, TRANS_OPT_THIN)) {
190                 opts->thin = !!value;
191                 return 0;
192         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
193                 opts->followtags = !!value;
194                 return 0;
195         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
196                 opts->keep = !!value;
197                 return 0;
198         } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
199                 opts->update_shallow = !!value;
200                 return 0;
201         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
202                 if (!value)
203                         opts->depth = 0;
204                 else {
205                         char *end;
206                         opts->depth = strtol(value, &end, 0);
207                         if (*end)
208                                 die(_("transport: invalid depth option '%s'"), value);
209                 }
210                 return 0;
211         } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) {
212                 opts->deepen_since = value;
213                 return 0;
214         } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) {
215                 opts->deepen_not = (const struct string_list *)value;
216                 return 0;
217         } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) {
218                 opts->deepen_relative = !!value;
219                 return 0;
220         } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) {
221                 opts->from_promisor = !!value;
222                 return 0;
223         } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) {
224                 opts->no_dependents = !!value;
225                 return 0;
226         } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) {
227                 parse_list_objects_filter(&opts->filter_options, value);
228                 return 0;
229         }
230         return 1;
231 }
232
233 static int connect_setup(struct transport *transport, int for_push)
234 {
235         struct git_transport_data *data = transport->data;
236         int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
237
238         if (data->conn)
239                 return 0;
240
241         switch (transport->family) {
242         case TRANSPORT_FAMILY_ALL: break;
243         case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
244         case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
245         }
246
247         data->conn = git_connect(data->fd, transport->url,
248                                  for_push ? data->options.receivepack :
249                                  data->options.uploadpack,
250                                  flags);
251
252         return 0;
253 }
254
255 static struct ref *get_refs_via_connect(struct transport *transport, int for_push,
256                                         const struct argv_array *ref_prefixes)
257 {
258         struct git_transport_data *data = transport->data;
259         struct ref *refs = NULL;
260         struct packet_reader reader;
261
262         connect_setup(transport, for_push);
263
264         packet_reader_init(&reader, data->fd[0], NULL, 0,
265                            PACKET_READ_CHOMP_NEWLINE |
266                            PACKET_READ_GENTLE_ON_EOF);
267
268         data->version = discover_version(&reader);
269         switch (data->version) {
270         case protocol_v2:
271                 get_remote_refs(data->fd[1], &reader, &refs, for_push,
272                                 ref_prefixes, transport->server_options);
273                 break;
274         case protocol_v1:
275         case protocol_v0:
276                 get_remote_heads(&reader, &refs,
277                                  for_push ? REF_NORMAL : 0,
278                                  &data->extra_have,
279                                  &data->shallow);
280                 break;
281         case protocol_unknown_version:
282                 BUG("unknown protocol version");
283         }
284         data->got_remote_heads = 1;
285
286         return refs;
287 }
288
289 static int fetch_refs_via_pack(struct transport *transport,
290                                int nr_heads, struct ref **to_fetch)
291 {
292         int ret = 0;
293         struct git_transport_data *data = transport->data;
294         struct ref *refs = NULL;
295         char *dest = xstrdup(transport->url);
296         struct fetch_pack_args args;
297         struct ref *refs_tmp = NULL;
298
299         memset(&args, 0, sizeof(args));
300         args.uploadpack = data->options.uploadpack;
301         args.keep_pack = data->options.keep;
302         args.lock_pack = 1;
303         args.use_thin_pack = data->options.thin;
304         args.include_tag = data->options.followtags;
305         args.verbose = (transport->verbose > 1);
306         args.quiet = (transport->verbose < 0);
307         args.no_progress = !transport->progress;
308         args.depth = data->options.depth;
309         args.deepen_since = data->options.deepen_since;
310         args.deepen_not = data->options.deepen_not;
311         args.deepen_relative = data->options.deepen_relative;
312         args.check_self_contained_and_connected =
313                 data->options.check_self_contained_and_connected;
314         args.cloning = transport->cloning;
315         args.update_shallow = data->options.update_shallow;
316         args.from_promisor = data->options.from_promisor;
317         args.no_dependents = data->options.no_dependents;
318         args.filter_options = data->options.filter_options;
319         args.stateless_rpc = transport->stateless_rpc;
320         args.server_options = transport->server_options;
321         args.negotiation_tips = data->options.negotiation_tips;
322
323         if (!data->got_remote_heads)
324                 refs_tmp = get_refs_via_connect(transport, 0, NULL);
325
326         switch (data->version) {
327         case protocol_v2:
328                 refs = fetch_pack(&args, data->fd, data->conn,
329                                   refs_tmp ? refs_tmp : transport->remote_refs,
330                                   dest, to_fetch, nr_heads, &data->shallow,
331                                   &transport->pack_lockfile, data->version);
332                 break;
333         case protocol_v1:
334         case protocol_v0:
335                 refs = fetch_pack(&args, data->fd, data->conn,
336                                   refs_tmp ? refs_tmp : transport->remote_refs,
337                                   dest, to_fetch, nr_heads, &data->shallow,
338                                   &transport->pack_lockfile, data->version);
339                 break;
340         case protocol_unknown_version:
341                 BUG("unknown protocol version");
342         }
343
344         close(data->fd[0]);
345         close(data->fd[1]);
346         if (finish_connect(data->conn))
347                 ret = -1;
348         data->conn = NULL;
349         data->got_remote_heads = 0;
350         data->options.self_contained_and_connected =
351                 args.self_contained_and_connected;
352         data->options.connectivity_checked = args.connectivity_checked;
353
354         if (refs == NULL)
355                 ret = -1;
356         if (report_unmatched_refs(to_fetch, nr_heads))
357                 ret = -1;
358
359         free_refs(refs_tmp);
360         free_refs(refs);
361         free(dest);
362         return ret;
363 }
364
365 static int push_had_errors(struct ref *ref)
366 {
367         for (; ref; ref = ref->next) {
368                 switch (ref->status) {
369                 case REF_STATUS_NONE:
370                 case REF_STATUS_UPTODATE:
371                 case REF_STATUS_OK:
372                         break;
373                 default:
374                         return 1;
375                 }
376         }
377         return 0;
378 }
379
380 int transport_refs_pushed(struct ref *ref)
381 {
382         for (; ref; ref = ref->next) {
383                 switch(ref->status) {
384                 case REF_STATUS_NONE:
385                 case REF_STATUS_UPTODATE:
386                         break;
387                 default:
388                         return 1;
389                 }
390         }
391         return 0;
392 }
393
394 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
395 {
396         struct refspec_item rs;
397
398         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
399                 return;
400
401         rs.src = ref->name;
402         rs.dst = NULL;
403
404         if (!remote_find_tracking(remote, &rs)) {
405                 if (verbose)
406                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
407                 if (ref->deletion) {
408                         delete_ref(NULL, rs.dst, NULL, 0);
409                 } else
410                         update_ref("update by push", rs.dst, &ref->new_oid,
411                                    NULL, 0, 0);
412                 free(rs.dst);
413         }
414 }
415
416 static void print_ref_status(char flag, const char *summary,
417                              struct ref *to, struct ref *from, const char *msg,
418                              int porcelain, int summary_width)
419 {
420         if (porcelain) {
421                 if (from)
422                         fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
423                 else
424                         fprintf(stdout, "%c\t:%s\t", flag, to->name);
425                 if (msg)
426                         fprintf(stdout, "%s (%s)\n", summary, msg);
427                 else
428                         fprintf(stdout, "%s\n", summary);
429         } else {
430                 const char *red = "", *reset = "";
431                 if (push_had_errors(to)) {
432                         red = transport_get_color(TRANSPORT_COLOR_REJECTED);
433                         reset = transport_get_color(TRANSPORT_COLOR_RESET);
434                 }
435                 fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width,
436                         summary, reset);
437                 if (from)
438                         fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
439                 else
440                         fputs(prettify_refname(to->name), stderr);
441                 if (msg) {
442                         fputs(" (", stderr);
443                         fputs(msg, stderr);
444                         fputc(')', stderr);
445                 }
446                 fputc('\n', stderr);
447         }
448 }
449
450 static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width)
451 {
452         if (ref->deletion)
453                 print_ref_status('-', "[deleted]", ref, NULL, NULL,
454                                  porcelain, summary_width);
455         else if (is_null_oid(&ref->old_oid))
456                 print_ref_status('*',
457                         (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
458                         "[new branch]"),
459                         ref, ref->peer_ref, NULL, porcelain, summary_width);
460         else {
461                 struct strbuf quickref = STRBUF_INIT;
462                 char type;
463                 const char *msg;
464
465                 strbuf_add_unique_abbrev(&quickref, &ref->old_oid,
466                                          DEFAULT_ABBREV);
467                 if (ref->forced_update) {
468                         strbuf_addstr(&quickref, "...");
469                         type = '+';
470                         msg = "forced update";
471                 } else {
472                         strbuf_addstr(&quickref, "..");
473                         type = ' ';
474                         msg = NULL;
475                 }
476                 strbuf_add_unique_abbrev(&quickref, &ref->new_oid,
477                                          DEFAULT_ABBREV);
478
479                 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg,
480                                  porcelain, summary_width);
481                 strbuf_release(&quickref);
482         }
483 }
484
485 static int print_one_push_status(struct ref *ref, const char *dest, int count,
486                                  int porcelain, int summary_width)
487 {
488         if (!count) {
489                 char *url = transport_anonymize_url(dest);
490                 fprintf(porcelain ? stdout : stderr, "To %s\n", url);
491                 free(url);
492         }
493
494         switch(ref->status) {
495         case REF_STATUS_NONE:
496                 print_ref_status('X', "[no match]", ref, NULL, NULL,
497                                  porcelain, summary_width);
498                 break;
499         case REF_STATUS_REJECT_NODELETE:
500                 print_ref_status('!', "[rejected]", ref, NULL,
501                                  "remote does not support deleting refs",
502                                  porcelain, summary_width);
503                 break;
504         case REF_STATUS_UPTODATE:
505                 print_ref_status('=', "[up to date]", ref,
506                                  ref->peer_ref, NULL, porcelain, summary_width);
507                 break;
508         case REF_STATUS_REJECT_NONFASTFORWARD:
509                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
510                                  "non-fast-forward", porcelain, summary_width);
511                 break;
512         case REF_STATUS_REJECT_ALREADY_EXISTS:
513                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
514                                  "already exists", porcelain, summary_width);
515                 break;
516         case REF_STATUS_REJECT_FETCH_FIRST:
517                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
518                                  "fetch first", porcelain, summary_width);
519                 break;
520         case REF_STATUS_REJECT_NEEDS_FORCE:
521                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
522                                  "needs force", porcelain, summary_width);
523                 break;
524         case REF_STATUS_REJECT_STALE:
525                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
526                                  "stale info", porcelain, summary_width);
527                 break;
528         case REF_STATUS_REJECT_SHALLOW:
529                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
530                                  "new shallow roots not allowed",
531                                  porcelain, summary_width);
532                 break;
533         case REF_STATUS_REMOTE_REJECT:
534                 print_ref_status('!', "[remote rejected]", ref,
535                                  ref->deletion ? NULL : ref->peer_ref,
536                                  ref->remote_status, porcelain, summary_width);
537                 break;
538         case REF_STATUS_EXPECTING_REPORT:
539                 print_ref_status('!', "[remote failure]", ref,
540                                  ref->deletion ? NULL : ref->peer_ref,
541                                  "remote failed to report status",
542                                  porcelain, summary_width);
543                 break;
544         case REF_STATUS_ATOMIC_PUSH_FAILED:
545                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
546                                  "atomic push failed", porcelain, summary_width);
547                 break;
548         case REF_STATUS_OK:
549                 print_ok_ref_status(ref, porcelain, summary_width);
550                 break;
551         }
552
553         return 1;
554 }
555
556 static int measure_abbrev(const struct object_id *oid, int sofar)
557 {
558         char hex[GIT_MAX_HEXSZ + 1];
559         int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV);
560
561         return (w < sofar) ? sofar : w;
562 }
563
564 int transport_summary_width(const struct ref *refs)
565 {
566         int maxw = -1;
567
568         for (; refs; refs = refs->next) {
569                 maxw = measure_abbrev(&refs->old_oid, maxw);
570                 maxw = measure_abbrev(&refs->new_oid, maxw);
571         }
572         if (maxw < 0)
573                 maxw = FALLBACK_DEFAULT_ABBREV;
574         return (2 * maxw + 3);
575 }
576
577 void transport_print_push_status(const char *dest, struct ref *refs,
578                                   int verbose, int porcelain, unsigned int *reject_reasons)
579 {
580         struct ref *ref;
581         int n = 0;
582         char *head;
583         int summary_width = transport_summary_width(refs);
584
585         if (transport_color_config() < 0)
586                 warning(_("could not parse transport.color.* config"));
587
588         head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
589
590         if (verbose) {
591                 for (ref = refs; ref; ref = ref->next)
592                         if (ref->status == REF_STATUS_UPTODATE)
593                                 n += print_one_push_status(ref, dest, n,
594                                                            porcelain, summary_width);
595         }
596
597         for (ref = refs; ref; ref = ref->next)
598                 if (ref->status == REF_STATUS_OK)
599                         n += print_one_push_status(ref, dest, n,
600                                                    porcelain, summary_width);
601
602         *reject_reasons = 0;
603         for (ref = refs; ref; ref = ref->next) {
604                 if (ref->status != REF_STATUS_NONE &&
605                     ref->status != REF_STATUS_UPTODATE &&
606                     ref->status != REF_STATUS_OK)
607                         n += print_one_push_status(ref, dest, n,
608                                                    porcelain, summary_width);
609                 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
610                         if (head != NULL && !strcmp(head, ref->name))
611                                 *reject_reasons |= REJECT_NON_FF_HEAD;
612                         else
613                                 *reject_reasons |= REJECT_NON_FF_OTHER;
614                 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
615                         *reject_reasons |= REJECT_ALREADY_EXISTS;
616                 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
617                         *reject_reasons |= REJECT_FETCH_FIRST;
618                 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
619                         *reject_reasons |= REJECT_NEEDS_FORCE;
620                 }
621         }
622         free(head);
623 }
624
625 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
626 {
627         struct git_transport_data *data = transport->data;
628         struct send_pack_args args;
629         int ret = 0;
630
631         if (transport_color_config() < 0)
632                 return -1;
633
634         if (!data->got_remote_heads)
635                 get_refs_via_connect(transport, 1, NULL);
636
637         memset(&args, 0, sizeof(args));
638         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
639         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
640         args.use_thin_pack = data->options.thin;
641         args.verbose = (transport->verbose > 0);
642         args.quiet = (transport->verbose < 0);
643         args.progress = transport->progress;
644         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
645         args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
646         args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
647         args.push_options = transport->push_options;
648         args.url = transport->url;
649
650         if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
651                 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
652         else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
653                 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
654         else
655                 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
656
657         switch (data->version) {
658         case protocol_v2:
659                 die(_("support for protocol v2 not implemented yet"));
660                 break;
661         case protocol_v1:
662         case protocol_v0:
663                 ret = send_pack(&args, data->fd, data->conn, remote_refs,
664                                 &data->extra_have);
665                 break;
666         case protocol_unknown_version:
667                 BUG("unknown protocol version");
668         }
669
670         close(data->fd[1]);
671         close(data->fd[0]);
672         ret |= finish_connect(data->conn);
673         data->conn = NULL;
674         data->got_remote_heads = 0;
675
676         return ret;
677 }
678
679 static int connect_git(struct transport *transport, const char *name,
680                        const char *executable, int fd[2])
681 {
682         struct git_transport_data *data = transport->data;
683         data->conn = git_connect(data->fd, transport->url,
684                                  executable, 0);
685         fd[0] = data->fd[0];
686         fd[1] = data->fd[1];
687         return 0;
688 }
689
690 static int disconnect_git(struct transport *transport)
691 {
692         struct git_transport_data *data = transport->data;
693         if (data->conn) {
694                 if (data->got_remote_heads)
695                         packet_flush(data->fd[1]);
696                 close(data->fd[0]);
697                 close(data->fd[1]);
698                 finish_connect(data->conn);
699         }
700
701         free(data);
702         return 0;
703 }
704
705 static struct transport_vtable taken_over_vtable = {
706         NULL,
707         get_refs_via_connect,
708         fetch_refs_via_pack,
709         git_transport_push,
710         NULL,
711         disconnect_git
712 };
713
714 void transport_take_over(struct transport *transport,
715                          struct child_process *child)
716 {
717         struct git_transport_data *data;
718
719         if (!transport->smart_options)
720                 BUG("taking over transport requires non-NULL "
721                     "smart_options field.");
722
723         data = xcalloc(1, sizeof(*data));
724         data->options = *transport->smart_options;
725         data->conn = child;
726         data->fd[0] = data->conn->out;
727         data->fd[1] = data->conn->in;
728         data->got_remote_heads = 0;
729         transport->data = data;
730
731         transport->vtable = &taken_over_vtable;
732         transport->smart_options = &(data->options);
733
734         transport->cannot_reuse = 1;
735 }
736
737 static int is_file(const char *url)
738 {
739         struct stat buf;
740         if (stat(url, &buf))
741                 return 0;
742         return S_ISREG(buf.st_mode);
743 }
744
745 static int external_specification_len(const char *url)
746 {
747         return strchr(url, ':') - url;
748 }
749
750 static const struct string_list *protocol_whitelist(void)
751 {
752         static int enabled = -1;
753         static struct string_list allowed = STRING_LIST_INIT_DUP;
754
755         if (enabled < 0) {
756                 const char *v = getenv("GIT_ALLOW_PROTOCOL");
757                 if (v) {
758                         string_list_split(&allowed, v, ':', -1);
759                         string_list_sort(&allowed);
760                         enabled = 1;
761                 } else {
762                         enabled = 0;
763                 }
764         }
765
766         return enabled ? &allowed : NULL;
767 }
768
769 enum protocol_allow_config {
770         PROTOCOL_ALLOW_NEVER = 0,
771         PROTOCOL_ALLOW_USER_ONLY,
772         PROTOCOL_ALLOW_ALWAYS
773 };
774
775 static enum protocol_allow_config parse_protocol_config(const char *key,
776                                                         const char *value)
777 {
778         if (!strcasecmp(value, "always"))
779                 return PROTOCOL_ALLOW_ALWAYS;
780         else if (!strcasecmp(value, "never"))
781                 return PROTOCOL_ALLOW_NEVER;
782         else if (!strcasecmp(value, "user"))
783                 return PROTOCOL_ALLOW_USER_ONLY;
784
785         die(_("unknown value for config '%s': %s"), key, value);
786 }
787
788 static enum protocol_allow_config get_protocol_config(const char *type)
789 {
790         char *key = xstrfmt("protocol.%s.allow", type);
791         char *value;
792
793         /* first check the per-protocol config */
794         if (!git_config_get_string(key, &value)) {
795                 enum protocol_allow_config ret =
796                         parse_protocol_config(key, value);
797                 free(key);
798                 free(value);
799                 return ret;
800         }
801         free(key);
802
803         /* if defined, fallback to user-defined default for unknown protocols */
804         if (!git_config_get_string("protocol.allow", &value)) {
805                 enum protocol_allow_config ret =
806                         parse_protocol_config("protocol.allow", value);
807                 free(value);
808                 return ret;
809         }
810
811         /* fallback to built-in defaults */
812         /* known safe */
813         if (!strcmp(type, "http") ||
814             !strcmp(type, "https") ||
815             !strcmp(type, "git") ||
816             !strcmp(type, "ssh") ||
817             !strcmp(type, "file"))
818                 return PROTOCOL_ALLOW_ALWAYS;
819
820         /* known scary; err on the side of caution */
821         if (!strcmp(type, "ext"))
822                 return PROTOCOL_ALLOW_NEVER;
823
824         /* unknown; by default let them be used only directly by the user */
825         return PROTOCOL_ALLOW_USER_ONLY;
826 }
827
828 int is_transport_allowed(const char *type, int from_user)
829 {
830         const struct string_list *whitelist = protocol_whitelist();
831         if (whitelist)
832                 return string_list_has_string(whitelist, type);
833
834         switch (get_protocol_config(type)) {
835         case PROTOCOL_ALLOW_ALWAYS:
836                 return 1;
837         case PROTOCOL_ALLOW_NEVER:
838                 return 0;
839         case PROTOCOL_ALLOW_USER_ONLY:
840                 if (from_user < 0)
841                         from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
842                 return from_user;
843         }
844
845         BUG("invalid protocol_allow_config type");
846 }
847
848 void transport_check_allowed(const char *type)
849 {
850         if (!is_transport_allowed(type, -1))
851                 die(_("transport '%s' not allowed"), type);
852 }
853
854 static struct transport_vtable bundle_vtable = {
855         NULL,
856         get_refs_from_bundle,
857         fetch_refs_from_bundle,
858         NULL,
859         NULL,
860         close_bundle
861 };
862
863 static struct transport_vtable builtin_smart_vtable = {
864         NULL,
865         get_refs_via_connect,
866         fetch_refs_via_pack,
867         git_transport_push,
868         connect_git,
869         disconnect_git
870 };
871
872 struct transport *transport_get(struct remote *remote, const char *url)
873 {
874         const char *helper;
875         struct transport *ret = xcalloc(1, sizeof(*ret));
876
877         ret->progress = isatty(2);
878
879         if (!remote)
880                 BUG("No remote provided to transport_get()");
881
882         ret->got_remote_refs = 0;
883         ret->remote = remote;
884         helper = remote->foreign_vcs;
885
886         if (!url && remote->url)
887                 url = remote->url[0];
888         ret->url = url;
889
890         /* maybe it is a foreign URL? */
891         if (url) {
892                 const char *p = url;
893
894                 while (is_urlschemechar(p == url, *p))
895                         p++;
896                 if (starts_with(p, "::"))
897                         helper = xstrndup(url, p - url);
898         }
899
900         if (helper) {
901                 transport_helper_init(ret, helper);
902         } else if (starts_with(url, "rsync:")) {
903                 die(_("git-over-rsync is no longer supported"));
904         } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
905                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
906                 transport_check_allowed("file");
907                 ret->data = data;
908                 ret->vtable = &bundle_vtable;
909                 ret->smart_options = NULL;
910         } else if (!is_url(url)
911                 || starts_with(url, "file://")
912                 || starts_with(url, "git://")
913                 || starts_with(url, "ssh://")
914                 || starts_with(url, "git+ssh://") /* deprecated - do not use */
915                 || starts_with(url, "ssh+git://") /* deprecated - do not use */
916                 ) {
917                 /*
918                  * These are builtin smart transports; "allowed" transports
919                  * will be checked individually in git_connect.
920                  */
921                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
922                 ret->data = data;
923                 ret->vtable = &builtin_smart_vtable;
924                 ret->smart_options = &(data->options);
925
926                 data->conn = NULL;
927                 data->got_remote_heads = 0;
928         } else {
929                 /* Unknown protocol in URL. Pass to external handler. */
930                 int len = external_specification_len(url);
931                 char *handler = xmemdupz(url, len);
932                 transport_helper_init(ret, handler);
933         }
934
935         if (ret->smart_options) {
936                 ret->smart_options->thin = 1;
937                 ret->smart_options->uploadpack = "git-upload-pack";
938                 if (remote->uploadpack)
939                         ret->smart_options->uploadpack = remote->uploadpack;
940                 ret->smart_options->receivepack = "git-receive-pack";
941                 if (remote->receivepack)
942                         ret->smart_options->receivepack = remote->receivepack;
943         }
944
945         return ret;
946 }
947
948 int transport_set_option(struct transport *transport,
949                          const char *name, const char *value)
950 {
951         int git_reports = 1, protocol_reports = 1;
952
953         if (transport->smart_options)
954                 git_reports = set_git_option(transport->smart_options,
955                                              name, value);
956
957         if (transport->vtable->set_option)
958                 protocol_reports = transport->vtable->set_option(transport,
959                                                                  name, value);
960
961         /* If either report is 0, report 0 (success). */
962         if (!git_reports || !protocol_reports)
963                 return 0;
964         /* If either reports -1 (invalid value), report -1. */
965         if ((git_reports == -1) || (protocol_reports == -1))
966                 return -1;
967         /* Otherwise if both report unknown, report unknown. */
968         return 1;
969 }
970
971 void transport_set_verbosity(struct transport *transport, int verbosity,
972         int force_progress)
973 {
974         if (verbosity >= 1)
975                 transport->verbose = verbosity <= 3 ? verbosity : 3;
976         if (verbosity < 0)
977                 transport->verbose = -1;
978
979         /**
980          * Rules used to determine whether to report progress (processing aborts
981          * when a rule is satisfied):
982          *
983          *   . Report progress, if force_progress is 1 (ie. --progress).
984          *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
985          *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
986          *   . Report progress if isatty(2) is 1.
987          **/
988         if (force_progress >= 0)
989                 transport->progress = !!force_progress;
990         else
991                 transport->progress = verbosity >= 0 && isatty(2);
992 }
993
994 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
995 {
996         int i;
997
998         fprintf(stderr, _("The following submodule paths contain changes that can\n"
999                         "not be found on any remote:\n"));
1000         for (i = 0; i < needs_pushing->nr; i++)
1001                 fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
1002         fprintf(stderr, _("\nPlease try\n\n"
1003                           "     git push --recurse-submodules=on-demand\n\n"
1004                           "or cd to the path and use\n\n"
1005                           "     git push\n\n"
1006                           "to push them to a remote.\n\n"));
1007
1008         string_list_clear(needs_pushing, 0);
1009
1010         die(_("Aborting."));
1011 }
1012
1013 static int run_pre_push_hook(struct transport *transport,
1014                              struct ref *remote_refs)
1015 {
1016         int ret = 0, x;
1017         struct ref *r;
1018         struct child_process proc = CHILD_PROCESS_INIT;
1019         struct strbuf buf;
1020         const char *argv[4];
1021
1022         if (!(argv[0] = find_hook("pre-push")))
1023                 return 0;
1024
1025         argv[1] = transport->remote->name;
1026         argv[2] = transport->url;
1027         argv[3] = NULL;
1028
1029         proc.argv = argv;
1030         proc.in = -1;
1031
1032         if (start_command(&proc)) {
1033                 finish_command(&proc);
1034                 return -1;
1035         }
1036
1037         sigchain_push(SIGPIPE, SIG_IGN);
1038
1039         strbuf_init(&buf, 256);
1040
1041         for (r = remote_refs; r; r = r->next) {
1042                 if (!r->peer_ref) continue;
1043                 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1044                 if (r->status == REF_STATUS_REJECT_STALE) continue;
1045                 if (r->status == REF_STATUS_UPTODATE) continue;
1046
1047                 strbuf_reset(&buf);
1048                 strbuf_addf( &buf, "%s %s %s %s\n",
1049                          r->peer_ref->name, oid_to_hex(&r->new_oid),
1050                          r->name, oid_to_hex(&r->old_oid));
1051
1052                 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
1053                         /* We do not mind if a hook does not read all refs. */
1054                         if (errno != EPIPE)
1055                                 ret = -1;
1056                         break;
1057                 }
1058         }
1059
1060         strbuf_release(&buf);
1061
1062         x = close(proc.in);
1063         if (!ret)
1064                 ret = x;
1065
1066         sigchain_pop(SIGPIPE);
1067
1068         x = finish_command(&proc);
1069         if (!ret)
1070                 ret = x;
1071
1072         return ret;
1073 }
1074
1075 int transport_push(struct transport *transport,
1076                    struct refspec *rs, int flags,
1077                    unsigned int *reject_reasons)
1078 {
1079         *reject_reasons = 0;
1080
1081         if (transport_color_config() < 0)
1082                 return -1;
1083
1084         if (transport->vtable->push_refs) {
1085                 struct ref *remote_refs;
1086                 struct ref *local_refs = get_local_heads();
1087                 int match_flags = MATCH_REFS_NONE;
1088                 int verbose = (transport->verbose > 0);
1089                 int quiet = (transport->verbose < 0);
1090                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1091                 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1092                 int push_ret, ret, err;
1093                 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1094
1095                 if (check_push_refs(local_refs, rs) < 0)
1096                         return -1;
1097
1098                 refspec_ref_prefixes(rs, &ref_prefixes);
1099
1100                 remote_refs = transport->vtable->get_refs_list(transport, 1,
1101                                                                &ref_prefixes);
1102
1103                 argv_array_clear(&ref_prefixes);
1104
1105                 if (flags & TRANSPORT_PUSH_ALL)
1106                         match_flags |= MATCH_REFS_ALL;
1107                 if (flags & TRANSPORT_PUSH_MIRROR)
1108                         match_flags |= MATCH_REFS_MIRROR;
1109                 if (flags & TRANSPORT_PUSH_PRUNE)
1110                         match_flags |= MATCH_REFS_PRUNE;
1111                 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1112                         match_flags |= MATCH_REFS_FOLLOW_TAGS;
1113
1114                 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1115                         return -1;
1116
1117                 if (transport->smart_options &&
1118                     transport->smart_options->cas &&
1119                     !is_empty_cas(transport->smart_options->cas))
1120                         apply_push_cas(transport->smart_options->cas,
1121                                        transport->remote, remote_refs);
1122
1123                 set_ref_status_for_push(remote_refs,
1124                         flags & TRANSPORT_PUSH_MIRROR,
1125                         flags & TRANSPORT_PUSH_FORCE);
1126
1127                 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1128                         if (run_pre_push_hook(transport, remote_refs))
1129                                 return -1;
1130
1131                 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1132                               TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1133                     !is_bare_repository()) {
1134                         struct ref *ref = remote_refs;
1135                         struct oid_array commits = OID_ARRAY_INIT;
1136
1137                         for (; ref; ref = ref->next)
1138                                 if (!is_null_oid(&ref->new_oid))
1139                                         oid_array_append(&commits,
1140                                                           &ref->new_oid);
1141
1142                         if (!push_unpushed_submodules(&commits,
1143                                                       transport->remote,
1144                                                       rs,
1145                                                       transport->push_options,
1146                                                       pretend)) {
1147                                 oid_array_clear(&commits);
1148                                 die(_("failed to push all needed submodules"));
1149                         }
1150                         oid_array_clear(&commits);
1151                 }
1152
1153                 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1154                      ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1155                                 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1156                       !pretend)) && !is_bare_repository()) {
1157                         struct ref *ref = remote_refs;
1158                         struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1159                         struct oid_array commits = OID_ARRAY_INIT;
1160
1161                         for (; ref; ref = ref->next)
1162                                 if (!is_null_oid(&ref->new_oid))
1163                                         oid_array_append(&commits,
1164                                                           &ref->new_oid);
1165
1166                         if (find_unpushed_submodules(&commits, transport->remote->name,
1167                                                 &needs_pushing)) {
1168                                 oid_array_clear(&commits);
1169                                 die_with_unpushed_submodules(&needs_pushing);
1170                         }
1171                         string_list_clear(&needs_pushing, 0);
1172                         oid_array_clear(&commits);
1173                 }
1174
1175                 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
1176                         push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1177                 else
1178                         push_ret = 0;
1179                 err = push_had_errors(remote_refs);
1180                 ret = push_ret | err;
1181
1182                 if (!quiet || err)
1183                         transport_print_push_status(transport->url, remote_refs,
1184                                         verbose | porcelain, porcelain,
1185                                         reject_reasons);
1186
1187                 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1188                         set_upstreams(transport, remote_refs, pretend);
1189
1190                 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1191                                TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1192                         struct ref *ref;
1193                         for (ref = remote_refs; ref; ref = ref->next)
1194                                 transport_update_tracking_ref(transport->remote, ref, verbose);
1195                 }
1196
1197                 if (porcelain && !push_ret)
1198                         puts("Done");
1199                 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1200                         fprintf(stderr, "Everything up-to-date\n");
1201
1202                 return ret;
1203         }
1204         return 1;
1205 }
1206
1207 const struct ref *transport_get_remote_refs(struct transport *transport,
1208                                             const struct argv_array *ref_prefixes)
1209 {
1210         if (!transport->got_remote_refs) {
1211                 transport->remote_refs =
1212                         transport->vtable->get_refs_list(transport, 0,
1213                                                          ref_prefixes);
1214                 transport->got_remote_refs = 1;
1215         }
1216
1217         return transport->remote_refs;
1218 }
1219
1220 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1221 {
1222         int rc;
1223         int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1224         struct ref **heads = NULL;
1225         struct ref *rm;
1226
1227         for (rm = refs; rm; rm = rm->next) {
1228                 nr_refs++;
1229                 if (rm->peer_ref &&
1230                     !is_null_oid(&rm->old_oid) &&
1231                     oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1232                         continue;
1233                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1234                 heads[nr_heads++] = rm;
1235         }
1236
1237         if (!nr_heads) {
1238                 /*
1239                  * When deepening of a shallow repository is requested,
1240                  * then local and remote refs are likely to still be equal.
1241                  * Just feed them all to the fetch method in that case.
1242                  * This condition shouldn't be met in a non-deepening fetch
1243                  * (see builtin/fetch.c:quickfetch()).
1244                  */
1245                 ALLOC_ARRAY(heads, nr_refs);
1246                 for (rm = refs; rm; rm = rm->next)
1247                         heads[nr_heads++] = rm;
1248         }
1249
1250         rc = transport->vtable->fetch(transport, nr_heads, heads);
1251
1252         free(heads);
1253         return rc;
1254 }
1255
1256 void transport_unlock_pack(struct transport *transport)
1257 {
1258         if (transport->pack_lockfile) {
1259                 unlink_or_warn(transport->pack_lockfile);
1260                 FREE_AND_NULL(transport->pack_lockfile);
1261         }
1262 }
1263
1264 int transport_connect(struct transport *transport, const char *name,
1265                       const char *exec, int fd[2])
1266 {
1267         if (transport->vtable->connect)
1268                 return transport->vtable->connect(transport, name, exec, fd);
1269         else
1270                 die(_("operation not supported by protocol"));
1271 }
1272
1273 int transport_disconnect(struct transport *transport)
1274 {
1275         int ret = 0;
1276         if (transport->vtable->disconnect)
1277                 ret = transport->vtable->disconnect(transport);
1278         free(transport);
1279         return ret;
1280 }
1281
1282 /*
1283  * Strip username (and password) from a URL and return
1284  * it in a newly allocated string.
1285  */
1286 char *transport_anonymize_url(const char *url)
1287 {
1288         char *scheme_prefix, *anon_part;
1289         size_t anon_len, prefix_len = 0;
1290
1291         anon_part = strchr(url, '@');
1292         if (url_is_local_not_ssh(url) || !anon_part)
1293                 goto literal_copy;
1294
1295         anon_len = strlen(++anon_part);
1296         scheme_prefix = strstr(url, "://");
1297         if (!scheme_prefix) {
1298                 if (!strchr(anon_part, ':'))
1299                         /* cannot be "me@there:/path/name" */
1300                         goto literal_copy;
1301         } else {
1302                 const char *cp;
1303                 /* make sure scheme is reasonable */
1304                 for (cp = url; cp < scheme_prefix; cp++) {
1305                         switch (*cp) {
1306                                 /* RFC 1738 2.1 */
1307                         case '+': case '.': case '-':
1308                                 break; /* ok */
1309                         default:
1310                                 if (isalnum(*cp))
1311                                         break;
1312                                 /* it isn't */
1313                                 goto literal_copy;
1314                         }
1315                 }
1316                 /* @ past the first slash does not count */
1317                 cp = strchr(scheme_prefix + 3, '/');
1318                 if (cp && cp < anon_part)
1319                         goto literal_copy;
1320                 prefix_len = scheme_prefix - url + 3;
1321         }
1322         return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1323                        (int)anon_len, anon_part);
1324 literal_copy:
1325         return xstrdup(url);
1326 }
1327
1328 static void read_alternate_refs(const char *path,
1329                                 alternate_ref_fn *cb,
1330                                 void *data)
1331 {
1332         struct child_process cmd = CHILD_PROCESS_INIT;
1333         struct strbuf line = STRBUF_INIT;
1334         FILE *fh;
1335
1336         cmd.git_cmd = 1;
1337         argv_array_pushf(&cmd.args, "--git-dir=%s", path);
1338         argv_array_push(&cmd.args, "for-each-ref");
1339         argv_array_push(&cmd.args, "--format=%(objectname) %(refname)");
1340         cmd.env = local_repo_env;
1341         cmd.out = -1;
1342
1343         if (start_command(&cmd))
1344                 return;
1345
1346         fh = xfdopen(cmd.out, "r");
1347         while (strbuf_getline_lf(&line, fh) != EOF) {
1348                 struct object_id oid;
1349
1350                 if (get_oid_hex(line.buf, &oid) ||
1351                     line.buf[GIT_SHA1_HEXSZ] != ' ') {
1352                         warning(_("invalid line while parsing alternate refs: %s"),
1353                                 line.buf);
1354                         break;
1355                 }
1356
1357                 cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data);
1358         }
1359
1360         fclose(fh);
1361         finish_command(&cmd);
1362 }
1363
1364 struct alternate_refs_data {
1365         alternate_ref_fn *fn;
1366         void *data;
1367 };
1368
1369 static int refs_from_alternate_cb(struct alternate_object_database *e,
1370                                   void *data)
1371 {
1372         struct strbuf path = STRBUF_INIT;
1373         size_t base_len;
1374         struct alternate_refs_data *cb = data;
1375
1376         if (!strbuf_realpath(&path, e->path, 0))
1377                 goto out;
1378         if (!strbuf_strip_suffix(&path, "/objects"))
1379                 goto out;
1380         base_len = path.len;
1381
1382         /* Is this a git repository with refs? */
1383         strbuf_addstr(&path, "/refs");
1384         if (!is_directory(path.buf))
1385                 goto out;
1386         strbuf_setlen(&path, base_len);
1387
1388         read_alternate_refs(path.buf, cb->fn, cb->data);
1389
1390 out:
1391         strbuf_release(&path);
1392         return 0;
1393 }
1394
1395 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1396 {
1397         struct alternate_refs_data cb;
1398         cb.fn = fn;
1399         cb.data = data;
1400         foreach_alt_odb(refs_from_alternate_cb, &cb);
1401 }