Merge branch 'ab/config-based-hooks-base' into seen
[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         .get_refs_list  = get_refs_via_connect,
885         .fetch_refs     = fetch_refs_via_pack,
886         .push_refs      = git_transport_push,
887         .disconnect     = disconnect_git
888 };
889
890 void transport_take_over(struct transport *transport,
891                          struct child_process *child)
892 {
893         struct git_transport_data *data;
894
895         if (!transport->smart_options)
896                 BUG("taking over transport requires non-NULL "
897                     "smart_options field.");
898
899         CALLOC_ARRAY(data, 1);
900         data->options = *transport->smart_options;
901         data->conn = child;
902         data->fd[0] = data->conn->out;
903         data->fd[1] = data->conn->in;
904         data->got_remote_heads = 0;
905         transport->data = data;
906
907         transport->vtable = &taken_over_vtable;
908         transport->smart_options = &(data->options);
909
910         transport->cannot_reuse = 1;
911 }
912
913 static int is_file(const char *url)
914 {
915         struct stat buf;
916         if (stat(url, &buf))
917                 return 0;
918         return S_ISREG(buf.st_mode);
919 }
920
921 static int external_specification_len(const char *url)
922 {
923         return strchr(url, ':') - url;
924 }
925
926 static const struct string_list *protocol_whitelist(void)
927 {
928         static int enabled = -1;
929         static struct string_list allowed = STRING_LIST_INIT_DUP;
930
931         if (enabled < 0) {
932                 const char *v = getenv("GIT_ALLOW_PROTOCOL");
933                 if (v) {
934                         string_list_split(&allowed, v, ':', -1);
935                         string_list_sort(&allowed);
936                         enabled = 1;
937                 } else {
938                         enabled = 0;
939                 }
940         }
941
942         return enabled ? &allowed : NULL;
943 }
944
945 enum protocol_allow_config {
946         PROTOCOL_ALLOW_NEVER = 0,
947         PROTOCOL_ALLOW_USER_ONLY,
948         PROTOCOL_ALLOW_ALWAYS
949 };
950
951 static enum protocol_allow_config parse_protocol_config(const char *key,
952                                                         const char *value)
953 {
954         if (!strcasecmp(value, "always"))
955                 return PROTOCOL_ALLOW_ALWAYS;
956         else if (!strcasecmp(value, "never"))
957                 return PROTOCOL_ALLOW_NEVER;
958         else if (!strcasecmp(value, "user"))
959                 return PROTOCOL_ALLOW_USER_ONLY;
960
961         die(_("unknown value for config '%s': %s"), key, value);
962 }
963
964 static enum protocol_allow_config get_protocol_config(const char *type)
965 {
966         char *key = xstrfmt("protocol.%s.allow", type);
967         char *value;
968
969         /* first check the per-protocol config */
970         if (!git_config_get_string(key, &value)) {
971                 enum protocol_allow_config ret =
972                         parse_protocol_config(key, value);
973                 free(key);
974                 free(value);
975                 return ret;
976         }
977         free(key);
978
979         /* if defined, fallback to user-defined default for unknown protocols */
980         if (!git_config_get_string("protocol.allow", &value)) {
981                 enum protocol_allow_config ret =
982                         parse_protocol_config("protocol.allow", value);
983                 free(value);
984                 return ret;
985         }
986
987         /* fallback to built-in defaults */
988         /* known safe */
989         if (!strcmp(type, "http") ||
990             !strcmp(type, "https") ||
991             !strcmp(type, "git") ||
992             !strcmp(type, "ssh") ||
993             !strcmp(type, "file"))
994                 return PROTOCOL_ALLOW_ALWAYS;
995
996         /* known scary; err on the side of caution */
997         if (!strcmp(type, "ext"))
998                 return PROTOCOL_ALLOW_NEVER;
999
1000         /* unknown; by default let them be used only directly by the user */
1001         return PROTOCOL_ALLOW_USER_ONLY;
1002 }
1003
1004 int is_transport_allowed(const char *type, int from_user)
1005 {
1006         const struct string_list *whitelist = protocol_whitelist();
1007         if (whitelist)
1008                 return string_list_has_string(whitelist, type);
1009
1010         switch (get_protocol_config(type)) {
1011         case PROTOCOL_ALLOW_ALWAYS:
1012                 return 1;
1013         case PROTOCOL_ALLOW_NEVER:
1014                 return 0;
1015         case PROTOCOL_ALLOW_USER_ONLY:
1016                 if (from_user < 0)
1017                         from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1018                 return from_user;
1019         }
1020
1021         BUG("invalid protocol_allow_config type");
1022 }
1023
1024 void transport_check_allowed(const char *type)
1025 {
1026         if (!is_transport_allowed(type, -1))
1027                 die(_("transport '%s' not allowed"), type);
1028 }
1029
1030 static struct transport_vtable bundle_vtable = {
1031         .get_refs_list  = get_refs_from_bundle,
1032         .fetch_refs     = fetch_refs_from_bundle,
1033         .disconnect     = close_bundle
1034 };
1035
1036 static struct transport_vtable builtin_smart_vtable = {
1037         .get_refs_list  = get_refs_via_connect,
1038         .fetch_refs     = fetch_refs_via_pack,
1039         .push_refs      = git_transport_push,
1040         .connect        = connect_git,
1041         .disconnect     = disconnect_git
1042 };
1043
1044 struct transport *transport_get(struct remote *remote, const char *url)
1045 {
1046         const char *helper;
1047         struct transport *ret = xcalloc(1, sizeof(*ret));
1048
1049         ret->progress = isatty(2);
1050         string_list_init(&ret->pack_lockfiles, 1);
1051
1052         if (!remote)
1053                 BUG("No remote provided to transport_get()");
1054
1055         ret->got_remote_refs = 0;
1056         ret->remote = remote;
1057         helper = remote->foreign_vcs;
1058
1059         if (!url && remote->url)
1060                 url = remote->url[0];
1061         ret->url = url;
1062
1063         /* maybe it is a foreign URL? */
1064         if (url) {
1065                 const char *p = url;
1066
1067                 while (is_urlschemechar(p == url, *p))
1068                         p++;
1069                 if (starts_with(p, "::"))
1070                         helper = xstrndup(url, p - url);
1071         }
1072
1073         if (helper) {
1074                 transport_helper_init(ret, helper);
1075         } else if (starts_with(url, "rsync:")) {
1076                 die(_("git-over-rsync is no longer supported"));
1077         } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
1078                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
1079                 transport_check_allowed("file");
1080                 ret->data = data;
1081                 ret->vtable = &bundle_vtable;
1082                 ret->smart_options = NULL;
1083         } else if (!is_url(url)
1084                 || starts_with(url, "file://")
1085                 || starts_with(url, "git://")
1086                 || starts_with(url, "ssh://")
1087                 || starts_with(url, "git+ssh://") /* deprecated - do not use */
1088                 || starts_with(url, "ssh+git://") /* deprecated - do not use */
1089                 ) {
1090                 /*
1091                  * These are builtin smart transports; "allowed" transports
1092                  * will be checked individually in git_connect.
1093                  */
1094                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
1095                 ret->data = data;
1096                 ret->vtable = &builtin_smart_vtable;
1097                 ret->smart_options = &(data->options);
1098
1099                 data->conn = NULL;
1100                 data->got_remote_heads = 0;
1101         } else {
1102                 /* Unknown protocol in URL. Pass to external handler. */
1103                 int len = external_specification_len(url);
1104                 char *handler = xmemdupz(url, len);
1105                 transport_helper_init(ret, handler);
1106         }
1107
1108         if (ret->smart_options) {
1109                 ret->smart_options->thin = 1;
1110                 ret->smart_options->uploadpack = "git-upload-pack";
1111                 if (remote->uploadpack)
1112                         ret->smart_options->uploadpack = remote->uploadpack;
1113                 ret->smart_options->receivepack = "git-receive-pack";
1114                 if (remote->receivepack)
1115                         ret->smart_options->receivepack = remote->receivepack;
1116         }
1117
1118         ret->hash_algo = &hash_algos[GIT_HASH_SHA1];
1119
1120         return ret;
1121 }
1122
1123 const struct git_hash_algo *transport_get_hash_algo(struct transport *transport)
1124 {
1125         return transport->hash_algo;
1126 }
1127
1128 int transport_set_option(struct transport *transport,
1129                          const char *name, const char *value)
1130 {
1131         int git_reports = 1, protocol_reports = 1;
1132
1133         if (transport->smart_options)
1134                 git_reports = set_git_option(transport->smart_options,
1135                                              name, value);
1136
1137         if (transport->vtable->set_option)
1138                 protocol_reports = transport->vtable->set_option(transport,
1139                                                                  name, value);
1140
1141         /* If either report is 0, report 0 (success). */
1142         if (!git_reports || !protocol_reports)
1143                 return 0;
1144         /* If either reports -1 (invalid value), report -1. */
1145         if ((git_reports == -1) || (protocol_reports == -1))
1146                 return -1;
1147         /* Otherwise if both report unknown, report unknown. */
1148         return 1;
1149 }
1150
1151 void transport_set_verbosity(struct transport *transport, int verbosity,
1152         int force_progress)
1153 {
1154         if (verbosity >= 1)
1155                 transport->verbose = verbosity <= 3 ? verbosity : 3;
1156         if (verbosity < 0)
1157                 transport->verbose = -1;
1158
1159         /**
1160          * Rules used to determine whether to report progress (processing aborts
1161          * when a rule is satisfied):
1162          *
1163          *   . Report progress, if force_progress is 1 (ie. --progress).
1164          *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
1165          *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1166          *   . Report progress if isatty(2) is 1.
1167          **/
1168         if (force_progress >= 0)
1169                 transport->progress = !!force_progress;
1170         else
1171                 transport->progress = verbosity >= 0 && isatty(2);
1172 }
1173
1174 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
1175 {
1176         int i;
1177
1178         fprintf(stderr, _("The following submodule paths contain changes that can\n"
1179                         "not be found on any remote:\n"));
1180         for (i = 0; i < needs_pushing->nr; i++)
1181                 fprintf(stderr, "  %s\n", needs_pushing->items[i].string);
1182         fprintf(stderr, _("\nPlease try\n\n"
1183                           "     git push --recurse-submodules=on-demand\n\n"
1184                           "or cd to the path and use\n\n"
1185                           "     git push\n\n"
1186                           "to push them to a remote.\n\n"));
1187
1188         string_list_clear(needs_pushing, 0);
1189
1190         die(_("Aborting."));
1191 }
1192
1193 static int run_pre_push_hook(struct transport *transport,
1194                              struct ref *remote_refs)
1195 {
1196         int ret = 0;
1197         struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1198         struct strbuf tmp = STRBUF_INIT;
1199         struct ref *r;
1200         struct string_list to_stdin = STRING_LIST_INIT_DUP;
1201
1202         strvec_push(&opt.args, transport->remote->name);
1203         strvec_push(&opt.args, transport->url);
1204
1205         for (r = remote_refs; r; r = r->next) {
1206                 if (!r->peer_ref) continue;
1207                 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
1208                 if (r->status == REF_STATUS_REJECT_STALE) continue;
1209                 if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
1210                 if (r->status == REF_STATUS_UPTODATE) continue;
1211
1212                 strbuf_reset(&tmp);
1213                 strbuf_addf(&tmp, "%s %s %s %s",
1214                          r->peer_ref->name, oid_to_hex(&r->new_oid),
1215                          r->name, oid_to_hex(&r->old_oid));
1216                 string_list_append(&to_stdin, tmp.buf);
1217         }
1218
1219         opt.feed_pipe = pipe_from_string_list;
1220         opt.feed_pipe_ctx = &to_stdin;
1221
1222         ret = run_hooks("pre-push", &opt);
1223         run_hooks_opt_clear(&opt);
1224         strbuf_release(&tmp);
1225         string_list_clear(&to_stdin, 0);
1226
1227         return ret;
1228 }
1229
1230 int transport_push(struct repository *r,
1231                    struct transport *transport,
1232                    struct refspec *rs, int flags,
1233                    unsigned int *reject_reasons)
1234 {
1235         *reject_reasons = 0;
1236
1237         if (transport_color_config() < 0)
1238                 return -1;
1239
1240         if (transport->vtable->push_refs) {
1241                 struct ref *remote_refs;
1242                 struct ref *local_refs = get_local_heads();
1243                 int match_flags = MATCH_REFS_NONE;
1244                 int verbose = (transport->verbose > 0);
1245                 int quiet = (transport->verbose < 0);
1246                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
1247                 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
1248                 int push_ret, ret, err;
1249                 struct transport_ls_refs_options transport_options =
1250                         TRANSPORT_LS_REFS_OPTIONS_INIT;
1251
1252                 if (check_push_refs(local_refs, rs) < 0)
1253                         return -1;
1254
1255                 refspec_ref_prefixes(rs, &transport_options.ref_prefixes);
1256
1257                 trace2_region_enter("transport_push", "get_refs_list", r);
1258                 remote_refs = transport->vtable->get_refs_list(transport, 1,
1259                                                                &transport_options);
1260                 trace2_region_leave("transport_push", "get_refs_list", r);
1261
1262                 strvec_clear(&transport_options.ref_prefixes);
1263
1264                 if (flags & TRANSPORT_PUSH_ALL)
1265                         match_flags |= MATCH_REFS_ALL;
1266                 if (flags & TRANSPORT_PUSH_MIRROR)
1267                         match_flags |= MATCH_REFS_MIRROR;
1268                 if (flags & TRANSPORT_PUSH_PRUNE)
1269                         match_flags |= MATCH_REFS_PRUNE;
1270                 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
1271                         match_flags |= MATCH_REFS_FOLLOW_TAGS;
1272
1273                 if (match_push_refs(local_refs, &remote_refs, rs, match_flags))
1274                         return -1;
1275
1276                 if (transport->smart_options &&
1277                     transport->smart_options->cas &&
1278                     !is_empty_cas(transport->smart_options->cas))
1279                         apply_push_cas(transport->smart_options->cas,
1280                                        transport->remote, remote_refs);
1281
1282                 set_ref_status_for_push(remote_refs,
1283                         flags & TRANSPORT_PUSH_MIRROR,
1284                         flags & TRANSPORT_PUSH_FORCE);
1285
1286                 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
1287                         if (run_pre_push_hook(transport, remote_refs))
1288                                 return -1;
1289
1290                 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1291                               TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1292                     !is_bare_repository()) {
1293                         struct ref *ref = remote_refs;
1294                         struct oid_array commits = OID_ARRAY_INIT;
1295
1296                         trace2_region_enter("transport_push", "push_submodules", r);
1297                         for (; ref; ref = ref->next)
1298                                 if (!is_null_oid(&ref->new_oid))
1299                                         oid_array_append(&commits,
1300                                                           &ref->new_oid);
1301
1302                         if (!push_unpushed_submodules(r,
1303                                                       &commits,
1304                                                       transport->remote,
1305                                                       rs,
1306                                                       transport->push_options,
1307                                                       pretend)) {
1308                                 oid_array_clear(&commits);
1309                                 trace2_region_leave("transport_push", "push_submodules", r);
1310                                 die(_("failed to push all needed submodules"));
1311                         }
1312                         oid_array_clear(&commits);
1313                         trace2_region_leave("transport_push", "push_submodules", r);
1314                 }
1315
1316                 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
1317                      ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
1318                                 TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
1319                       !pretend)) && !is_bare_repository()) {
1320                         struct ref *ref = remote_refs;
1321                         struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1322                         struct oid_array commits = OID_ARRAY_INIT;
1323
1324                         trace2_region_enter("transport_push", "check_submodules", r);
1325                         for (; ref; ref = ref->next)
1326                                 if (!is_null_oid(&ref->new_oid))
1327                                         oid_array_append(&commits,
1328                                                           &ref->new_oid);
1329
1330                         if (find_unpushed_submodules(r,
1331                                                      &commits,
1332                                                      transport->remote->name,
1333                                                      &needs_pushing)) {
1334                                 oid_array_clear(&commits);
1335                                 trace2_region_leave("transport_push", "check_submodules", r);
1336                                 die_with_unpushed_submodules(&needs_pushing);
1337                         }
1338                         string_list_clear(&needs_pushing, 0);
1339                         oid_array_clear(&commits);
1340                         trace2_region_leave("transport_push", "check_submodules", r);
1341                 }
1342
1343                 if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) {
1344                         trace2_region_enter("transport_push", "push_refs", r);
1345                         push_ret = transport->vtable->push_refs(transport, remote_refs, flags);
1346                         trace2_region_leave("transport_push", "push_refs", r);
1347                 } else
1348                         push_ret = 0;
1349                 err = push_had_errors(remote_refs);
1350                 ret = push_ret | err;
1351
1352                 if (!quiet || err)
1353                         transport_print_push_status(transport->url, remote_refs,
1354                                         verbose | porcelain, porcelain,
1355                                         reject_reasons);
1356
1357                 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1358                         set_upstreams(transport, remote_refs, pretend);
1359
1360                 if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1361                                TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1362                         struct ref *ref;
1363                         for (ref = remote_refs; ref; ref = ref->next)
1364                                 transport_update_tracking_ref(transport->remote, ref, verbose);
1365                 }
1366
1367                 if (porcelain && !push_ret)
1368                         puts("Done");
1369                 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
1370                         fprintf(stderr, "Everything up-to-date\n");
1371
1372                 return ret;
1373         }
1374         return 1;
1375 }
1376
1377 const struct ref *transport_get_remote_refs(struct transport *transport,
1378                                             struct transport_ls_refs_options *transport_options)
1379 {
1380         if (!transport->got_remote_refs) {
1381                 transport->remote_refs =
1382                         transport->vtable->get_refs_list(transport, 0,
1383                                                          transport_options);
1384                 transport->got_remote_refs = 1;
1385         }
1386
1387         return transport->remote_refs;
1388 }
1389
1390 int transport_fetch_refs(struct transport *transport, struct ref *refs)
1391 {
1392         int rc;
1393         int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
1394         struct ref **heads = NULL;
1395         struct ref *rm;
1396
1397         for (rm = refs; rm; rm = rm->next) {
1398                 nr_refs++;
1399                 if (rm->peer_ref &&
1400                     !is_null_oid(&rm->old_oid) &&
1401                     oideq(&rm->peer_ref->old_oid, &rm->old_oid))
1402                         continue;
1403                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
1404                 heads[nr_heads++] = rm;
1405         }
1406
1407         if (!nr_heads) {
1408                 /*
1409                  * When deepening of a shallow repository is requested,
1410                  * then local and remote refs are likely to still be equal.
1411                  * Just feed them all to the fetch method in that case.
1412                  * This condition shouldn't be met in a non-deepening fetch
1413                  * (see builtin/fetch.c:quickfetch()).
1414                  */
1415                 ALLOC_ARRAY(heads, nr_refs);
1416                 for (rm = refs; rm; rm = rm->next)
1417                         heads[nr_heads++] = rm;
1418         }
1419
1420         rc = transport->vtable->fetch_refs(transport, nr_heads, heads);
1421
1422         free(heads);
1423         return rc;
1424 }
1425
1426 void transport_unlock_pack(struct transport *transport)
1427 {
1428         int i;
1429
1430         for (i = 0; i < transport->pack_lockfiles.nr; i++)
1431                 unlink_or_warn(transport->pack_lockfiles.items[i].string);
1432         string_list_clear(&transport->pack_lockfiles, 0);
1433 }
1434
1435 int transport_connect(struct transport *transport, const char *name,
1436                       const char *exec, int fd[2])
1437 {
1438         if (transport->vtable->connect)
1439                 return transport->vtable->connect(transport, name, exec, fd);
1440         else
1441                 die(_("operation not supported by protocol"));
1442 }
1443
1444 int transport_disconnect(struct transport *transport)
1445 {
1446         int ret = 0;
1447         if (transport->vtable->disconnect)
1448                 ret = transport->vtable->disconnect(transport);
1449         if (transport->got_remote_refs)
1450                 free_refs((void *)transport->remote_refs);
1451         free(transport);
1452         return ret;
1453 }
1454
1455 /*
1456  * Strip username (and password) from a URL and return
1457  * it in a newly allocated string.
1458  */
1459 char *transport_anonymize_url(const char *url)
1460 {
1461         char *scheme_prefix, *anon_part;
1462         size_t anon_len, prefix_len = 0;
1463
1464         anon_part = strchr(url, '@');
1465         if (url_is_local_not_ssh(url) || !anon_part)
1466                 goto literal_copy;
1467
1468         anon_len = strlen(++anon_part);
1469         scheme_prefix = strstr(url, "://");
1470         if (!scheme_prefix) {
1471                 if (!strchr(anon_part, ':'))
1472                         /* cannot be "me@there:/path/name" */
1473                         goto literal_copy;
1474         } else {
1475                 const char *cp;
1476                 /* make sure scheme is reasonable */
1477                 for (cp = url; cp < scheme_prefix; cp++) {
1478                         switch (*cp) {
1479                                 /* RFC 1738 2.1 */
1480                         case '+': case '.': case '-':
1481                                 break; /* ok */
1482                         default:
1483                                 if (isalnum(*cp))
1484                                         break;
1485                                 /* it isn't */
1486                                 goto literal_copy;
1487                         }
1488                 }
1489                 /* @ past the first slash does not count */
1490                 cp = strchr(scheme_prefix + 3, '/');
1491                 if (cp && cp < anon_part)
1492                         goto literal_copy;
1493                 prefix_len = scheme_prefix - url + 3;
1494         }
1495         return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1496                        (int)anon_len, anon_part);
1497 literal_copy:
1498         return xstrdup(url);
1499 }