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