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