remote: show published status and help
[git] / transport.c
1 #include "cache.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
5 #include "fetch-pack.h"
6 #include "remote.h"
7 #include "connect.h"
8 #include "send-pack.h"
9 #include "walker.h"
10 #include "bundle.h"
11 #include "dir.h"
12 #include "refs.h"
13 #include "branch.h"
14 #include "url.h"
15 #include "submodule.h"
16 #include "string-list.h"
17 #include "sha1-array.h"
18 #include "sigchain.h"
19
20 static void set_tracking(struct transport *transport, struct ref *refs,
21         int pretend, int publish)
22 {
23         struct ref *ref;
24         for (ref = refs; ref; ref = ref->next) {
25                 const char *localname;
26                 const char *tmp;
27                 const char *remotename;
28                 unsigned char sha[20];
29                 int flag = 0;
30                 /*
31                  * Check suitability for tracking. Must be successful /
32                  * already up-to-date ref create/modify (not delete).
33                  */
34                 if (ref->status != REF_STATUS_OK &&
35                         ref->status != REF_STATUS_UPTODATE)
36                         continue;
37                 if (!ref->peer_ref)
38                         continue;
39                 if (is_null_oid(&ref->new_oid))
40                         continue;
41
42                 /* Follow symbolic refs (mainly for HEAD). */
43                 localname = ref->peer_ref->name;
44                 remotename = ref->name;
45                 tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING,
46                                          sha, &flag);
47                 if (tmp && flag & REF_ISSYMREF &&
48                         starts_with(tmp, "refs/heads/"))
49                         localname = tmp;
50
51                 /* Both source and destination must be local branches. */
52                 if (!localname || !starts_with(localname, "refs/heads/"))
53                         continue;
54                 if (!remotename || !starts_with(remotename, "refs/heads/"))
55                         continue;
56
57                 if (!pretend) {
58                         if (publish)
59                                 install_branch_publish(localname + 11,
60                                                 transport->remote->name,
61                                                 remotename);
62                         else
63                                 install_branch_config(BRANCH_CONFIG_VERBOSE,
64                                                 localname + 11, transport->remote->name,
65                                                 remotename);
66                 } else
67                         printf("Would set %s of '%s' to '%s' of '%s'\n",
68                                 publish ? "publish" : "upstream",
69                                 localname + 11, remotename + 11,
70                                 transport->remote->name);
71         }
72 }
73
74 struct bundle_transport_data {
75         int fd;
76         struct bundle_header header;
77 };
78
79 static struct ref *get_refs_from_bundle(struct transport *transport, int for_push)
80 {
81         struct bundle_transport_data *data = transport->data;
82         struct ref *result = NULL;
83         int i;
84
85         if (for_push)
86                 return NULL;
87
88         if (data->fd > 0)
89                 close(data->fd);
90         data->fd = read_bundle_header(transport->url, &data->header);
91         if (data->fd < 0)
92                 die ("Could not read bundle '%s'.", transport->url);
93         for (i = 0; i < data->header.references.nr; i++) {
94                 struct ref_list_entry *e = data->header.references.list + i;
95                 struct ref *ref = alloc_ref(e->name);
96                 hashcpy(ref->old_oid.hash, e->sha1);
97                 ref->next = result;
98                 result = ref;
99         }
100         return result;
101 }
102
103 static int fetch_refs_from_bundle(struct transport *transport,
104                                int nr_heads, struct ref **to_fetch)
105 {
106         struct bundle_transport_data *data = transport->data;
107         return unbundle(&data->header, data->fd,
108                         transport->progress ? BUNDLE_VERBOSE : 0);
109 }
110
111 static int close_bundle(struct transport *transport)
112 {
113         struct bundle_transport_data *data = transport->data;
114         if (data->fd > 0)
115                 close(data->fd);
116         free(data);
117         return 0;
118 }
119
120 struct git_transport_data {
121         struct git_transport_options options;
122         struct child_process *conn;
123         int fd[2];
124         unsigned got_remote_heads : 1;
125         struct sha1_array extra_have;
126         struct sha1_array shallow;
127 };
128
129 static int set_git_option(struct git_transport_options *opts,
130                           const char *name, const char *value)
131 {
132         if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
133                 opts->uploadpack = value;
134                 return 0;
135         } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
136                 opts->receivepack = value;
137                 return 0;
138         } else if (!strcmp(name, TRANS_OPT_THIN)) {
139                 opts->thin = !!value;
140                 return 0;
141         } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
142                 opts->followtags = !!value;
143                 return 0;
144         } else if (!strcmp(name, TRANS_OPT_KEEP)) {
145                 opts->keep = !!value;
146                 return 0;
147         } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) {
148                 opts->update_shallow = !!value;
149                 return 0;
150         } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
151                 if (!value)
152                         opts->depth = 0;
153                 else {
154                         char *end;
155                         opts->depth = strtol(value, &end, 0);
156                         if (*end)
157                                 die("transport: invalid depth option '%s'", value);
158                 }
159                 return 0;
160         }
161         return 1;
162 }
163
164 static int connect_setup(struct transport *transport, int for_push)
165 {
166         struct git_transport_data *data = transport->data;
167         int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0;
168
169         if (data->conn)
170                 return 0;
171
172         switch (transport->family) {
173         case TRANSPORT_FAMILY_ALL: break;
174         case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break;
175         case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break;
176         }
177
178         data->conn = git_connect(data->fd, transport->url,
179                                  for_push ? data->options.receivepack :
180                                  data->options.uploadpack,
181                                  flags);
182
183         return 0;
184 }
185
186 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
187 {
188         struct git_transport_data *data = transport->data;
189         struct ref *refs;
190
191         connect_setup(transport, for_push);
192         get_remote_heads(data->fd[0], NULL, 0, &refs,
193                          for_push ? REF_NORMAL : 0,
194                          &data->extra_have,
195                          &data->shallow);
196         data->got_remote_heads = 1;
197
198         return refs;
199 }
200
201 static int fetch_refs_via_pack(struct transport *transport,
202                                int nr_heads, struct ref **to_fetch)
203 {
204         struct git_transport_data *data = transport->data;
205         struct ref *refs;
206         char *dest = xstrdup(transport->url);
207         struct fetch_pack_args args;
208         struct ref *refs_tmp = NULL;
209
210         memset(&args, 0, sizeof(args));
211         args.uploadpack = data->options.uploadpack;
212         args.keep_pack = data->options.keep;
213         args.lock_pack = 1;
214         args.use_thin_pack = data->options.thin;
215         args.include_tag = data->options.followtags;
216         args.verbose = (transport->verbose > 1);
217         args.quiet = (transport->verbose < 0);
218         args.no_progress = !transport->progress;
219         args.depth = data->options.depth;
220         args.check_self_contained_and_connected =
221                 data->options.check_self_contained_and_connected;
222         args.cloning = transport->cloning;
223         args.update_shallow = data->options.update_shallow;
224
225         if (!data->got_remote_heads) {
226                 connect_setup(transport, 0);
227                 get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0,
228                                  NULL, &data->shallow);
229                 data->got_remote_heads = 1;
230         }
231
232         refs = fetch_pack(&args, data->fd, data->conn,
233                           refs_tmp ? refs_tmp : transport->remote_refs,
234                           dest, to_fetch, nr_heads, &data->shallow,
235                           &transport->pack_lockfile);
236         close(data->fd[0]);
237         close(data->fd[1]);
238         if (finish_connect(data->conn)) {
239                 free_refs(refs);
240                 refs = NULL;
241         }
242         data->conn = NULL;
243         data->got_remote_heads = 0;
244         data->options.self_contained_and_connected =
245                 args.self_contained_and_connected;
246
247         free_refs(refs_tmp);
248         free_refs(refs);
249         free(dest);
250         return (refs ? 0 : -1);
251 }
252
253 static int push_had_errors(struct ref *ref)
254 {
255         for (; ref; ref = ref->next) {
256                 switch (ref->status) {
257                 case REF_STATUS_NONE:
258                 case REF_STATUS_UPTODATE:
259                 case REF_STATUS_OK:
260                         break;
261                 default:
262                         return 1;
263                 }
264         }
265         return 0;
266 }
267
268 int transport_refs_pushed(struct ref *ref)
269 {
270         for (; ref; ref = ref->next) {
271                 switch(ref->status) {
272                 case REF_STATUS_NONE:
273                 case REF_STATUS_UPTODATE:
274                         break;
275                 default:
276                         return 1;
277                 }
278         }
279         return 0;
280 }
281
282 void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose)
283 {
284         struct refspec rs;
285
286         if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE)
287                 return;
288
289         rs.src = ref->name;
290         rs.dst = NULL;
291
292         if (!remote_find_tracking(remote, &rs)) {
293                 if (verbose)
294                         fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
295                 if (ref->deletion) {
296                         delete_ref(rs.dst, NULL, 0);
297                 } else
298                         update_ref("update by push", rs.dst,
299                                         ref->new_oid.hash, NULL, 0, 0);
300                 free(rs.dst);
301         }
302 }
303
304 static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain)
305 {
306         if (porcelain) {
307                 if (from)
308                         fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name);
309                 else
310                         fprintf(stdout, "%c\t:%s\t", flag, to->name);
311                 if (msg)
312                         fprintf(stdout, "%s (%s)\n", summary, msg);
313                 else
314                         fprintf(stdout, "%s\n", summary);
315         } else {
316                 fprintf(stderr, " %c %-*s ", flag, TRANSPORT_SUMMARY_WIDTH, summary);
317                 if (from)
318                         fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name));
319                 else
320                         fputs(prettify_refname(to->name), stderr);
321                 if (msg) {
322                         fputs(" (", stderr);
323                         fputs(msg, stderr);
324                         fputc(')', stderr);
325                 }
326                 fputc('\n', stderr);
327         }
328 }
329
330 static const char *status_abbrev(unsigned char sha1[20])
331 {
332         return find_unique_abbrev(sha1, DEFAULT_ABBREV);
333 }
334
335 static void print_ok_ref_status(struct ref *ref, int porcelain)
336 {
337         if (ref->deletion)
338                 print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain);
339         else if (is_null_oid(&ref->old_oid))
340                 print_ref_status('*',
341                         (starts_with(ref->name, "refs/tags/") ? "[new tag]" :
342                         "[new branch]"),
343                         ref, ref->peer_ref, NULL, porcelain);
344         else {
345                 struct strbuf quickref = STRBUF_INIT;
346                 char type;
347                 const char *msg;
348
349                 strbuf_addstr(&quickref, status_abbrev(ref->old_oid.hash));
350                 if (ref->forced_update) {
351                         strbuf_addstr(&quickref, "...");
352                         type = '+';
353                         msg = "forced update";
354                 } else {
355                         strbuf_addstr(&quickref, "..");
356                         type = ' ';
357                         msg = NULL;
358                 }
359                 strbuf_addstr(&quickref, status_abbrev(ref->new_oid.hash));
360
361                 print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, porcelain);
362                 strbuf_release(&quickref);
363         }
364 }
365
366 static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain)
367 {
368         if (!count)
369                 fprintf(porcelain ? stdout : stderr, "To %s\n", dest);
370
371         switch(ref->status) {
372         case REF_STATUS_NONE:
373                 print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain);
374                 break;
375         case REF_STATUS_REJECT_NODELETE:
376                 print_ref_status('!', "[rejected]", ref, NULL,
377                                                  "remote does not support deleting refs", porcelain);
378                 break;
379         case REF_STATUS_UPTODATE:
380                 print_ref_status('=', "[up to date]", ref,
381                                                  ref->peer_ref, NULL, porcelain);
382                 break;
383         case REF_STATUS_REJECT_NONFASTFORWARD:
384                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
385                                                  "non-fast-forward", porcelain);
386                 break;
387         case REF_STATUS_REJECT_ALREADY_EXISTS:
388                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
389                                                  "already exists", porcelain);
390                 break;
391         case REF_STATUS_REJECT_FETCH_FIRST:
392                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
393                                                  "fetch first", porcelain);
394                 break;
395         case REF_STATUS_REJECT_NEEDS_FORCE:
396                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
397                                                  "needs force", porcelain);
398                 break;
399         case REF_STATUS_REJECT_STALE:
400                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
401                                                  "stale info", porcelain);
402                 break;
403         case REF_STATUS_REJECT_SHALLOW:
404                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
405                                                  "new shallow roots not allowed", porcelain);
406                 break;
407         case REF_STATUS_REMOTE_REJECT:
408                 print_ref_status('!', "[remote rejected]", ref,
409                                                  ref->deletion ? NULL : ref->peer_ref,
410                                                  ref->remote_status, porcelain);
411                 break;
412         case REF_STATUS_EXPECTING_REPORT:
413                 print_ref_status('!', "[remote failure]", ref,
414                                                  ref->deletion ? NULL : ref->peer_ref,
415                                                  "remote failed to report status", porcelain);
416                 break;
417         case REF_STATUS_ATOMIC_PUSH_FAILED:
418                 print_ref_status('!', "[rejected]", ref, ref->peer_ref,
419                                                  "atomic push failed", porcelain);
420                 break;
421         case REF_STATUS_OK:
422                 print_ok_ref_status(ref, porcelain);
423                 break;
424         }
425
426         return 1;
427 }
428
429 void transport_print_push_status(const char *dest, struct ref *refs,
430                                   int verbose, int porcelain, unsigned int *reject_reasons)
431 {
432         struct ref *ref;
433         int n = 0;
434         unsigned char head_sha1[20];
435         char *head;
436
437         head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL);
438
439         if (verbose) {
440                 for (ref = refs; ref; ref = ref->next)
441                         if (ref->status == REF_STATUS_UPTODATE)
442                                 n += print_one_push_status(ref, dest, n, porcelain);
443         }
444
445         for (ref = refs; ref; ref = ref->next)
446                 if (ref->status == REF_STATUS_OK)
447                         n += print_one_push_status(ref, dest, n, porcelain);
448
449         *reject_reasons = 0;
450         for (ref = refs; ref; ref = ref->next) {
451                 if (ref->status != REF_STATUS_NONE &&
452                     ref->status != REF_STATUS_UPTODATE &&
453                     ref->status != REF_STATUS_OK)
454                         n += print_one_push_status(ref, dest, n, porcelain);
455                 if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) {
456                         if (head != NULL && !strcmp(head, ref->name))
457                                 *reject_reasons |= REJECT_NON_FF_HEAD;
458                         else
459                                 *reject_reasons |= REJECT_NON_FF_OTHER;
460                 } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) {
461                         *reject_reasons |= REJECT_ALREADY_EXISTS;
462                 } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) {
463                         *reject_reasons |= REJECT_FETCH_FIRST;
464                 } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) {
465                         *reject_reasons |= REJECT_NEEDS_FORCE;
466                 }
467         }
468         free(head);
469 }
470
471 void transport_verify_remote_names(int nr_heads, const char **heads)
472 {
473         int i;
474
475         for (i = 0; i < nr_heads; i++) {
476                 const char *local = heads[i];
477                 const char *remote = strrchr(heads[i], ':');
478
479                 if (*local == '+')
480                         local++;
481
482                 /* A matching refspec is okay.  */
483                 if (remote == local && remote[1] == '\0')
484                         continue;
485
486                 remote = remote ? (remote + 1) : local;
487                 if (check_refname_format(remote,
488                                 REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN))
489                         die("remote part of refspec is not a valid name in %s",
490                                 heads[i]);
491         }
492 }
493
494 static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags)
495 {
496         struct git_transport_data *data = transport->data;
497         struct send_pack_args args;
498         int ret;
499
500         if (!data->got_remote_heads) {
501                 struct ref *tmp_refs;
502                 connect_setup(transport, 1);
503
504                 get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL,
505                                  NULL, &data->shallow);
506                 data->got_remote_heads = 1;
507         }
508
509         memset(&args, 0, sizeof(args));
510         args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
511         args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
512         args.use_thin_pack = data->options.thin;
513         args.verbose = (transport->verbose > 0);
514         args.quiet = (transport->verbose < 0);
515         args.progress = transport->progress;
516         args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
517         args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN);
518         args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC);
519         args.url = transport->url;
520
521         if (flags & TRANSPORT_PUSH_CERT_ALWAYS)
522                 args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
523         else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED)
524                 args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
525         else
526                 args.push_cert = SEND_PACK_PUSH_CERT_NEVER;
527
528         ret = send_pack(&args, data->fd, data->conn, remote_refs,
529                         &data->extra_have);
530
531         close(data->fd[1]);
532         close(data->fd[0]);
533         ret |= finish_connect(data->conn);
534         data->conn = NULL;
535         data->got_remote_heads = 0;
536
537         return ret;
538 }
539
540 static int connect_git(struct transport *transport, const char *name,
541                        const char *executable, int fd[2])
542 {
543         struct git_transport_data *data = transport->data;
544         data->conn = git_connect(data->fd, transport->url,
545                                  executable, 0);
546         fd[0] = data->fd[0];
547         fd[1] = data->fd[1];
548         return 0;
549 }
550
551 static int disconnect_git(struct transport *transport)
552 {
553         struct git_transport_data *data = transport->data;
554         if (data->conn) {
555                 if (data->got_remote_heads)
556                         packet_flush(data->fd[1]);
557                 close(data->fd[0]);
558                 close(data->fd[1]);
559                 finish_connect(data->conn);
560         }
561
562         free(data);
563         return 0;
564 }
565
566 void transport_take_over(struct transport *transport,
567                          struct child_process *child)
568 {
569         struct git_transport_data *data;
570
571         if (!transport->smart_options)
572                 die("Bug detected: Taking over transport requires non-NULL "
573                     "smart_options field.");
574
575         data = xcalloc(1, sizeof(*data));
576         data->options = *transport->smart_options;
577         data->conn = child;
578         data->fd[0] = data->conn->out;
579         data->fd[1] = data->conn->in;
580         data->got_remote_heads = 0;
581         transport->data = data;
582
583         transport->set_option = NULL;
584         transport->get_refs_list = get_refs_via_connect;
585         transport->fetch = fetch_refs_via_pack;
586         transport->push = NULL;
587         transport->push_refs = git_transport_push;
588         transport->disconnect = disconnect_git;
589         transport->smart_options = &(data->options);
590
591         transport->cannot_reuse = 1;
592 }
593
594 static int is_file(const char *url)
595 {
596         struct stat buf;
597         if (stat(url, &buf))
598                 return 0;
599         return S_ISREG(buf.st_mode);
600 }
601
602 static int external_specification_len(const char *url)
603 {
604         return strchr(url, ':') - url;
605 }
606
607 static const struct string_list *protocol_whitelist(void)
608 {
609         static int enabled = -1;
610         static struct string_list allowed = STRING_LIST_INIT_DUP;
611
612         if (enabled < 0) {
613                 const char *v = getenv("GIT_ALLOW_PROTOCOL");
614                 if (v) {
615                         string_list_split(&allowed, v, ':', -1);
616                         string_list_sort(&allowed);
617                         enabled = 1;
618                 } else {
619                         enabled = 0;
620                 }
621         }
622
623         return enabled ? &allowed : NULL;
624 }
625
626 int is_transport_allowed(const char *type)
627 {
628         const struct string_list *allowed = protocol_whitelist();
629         return !allowed || string_list_has_string(allowed, type);
630 }
631
632 void transport_check_allowed(const char *type)
633 {
634         if (!is_transport_allowed(type))
635                 die("transport '%s' not allowed", type);
636 }
637
638 int transport_restrict_protocols(void)
639 {
640         return !!protocol_whitelist();
641 }
642
643 struct transport *transport_get(struct remote *remote, const char *url)
644 {
645         const char *helper;
646         struct transport *ret = xcalloc(1, sizeof(*ret));
647
648         ret->progress = isatty(2);
649
650         if (!remote)
651                 die("No remote provided to transport_get()");
652
653         ret->got_remote_refs = 0;
654         ret->remote = remote;
655         helper = remote->foreign_vcs;
656
657         if (!url && remote->url)
658                 url = remote->url[0];
659         ret->url = url;
660
661         /* maybe it is a foreign URL? */
662         if (url) {
663                 const char *p = url;
664
665                 while (is_urlschemechar(p == url, *p))
666                         p++;
667                 if (starts_with(p, "::"))
668                         helper = xstrndup(url, p - url);
669         }
670
671         if (helper) {
672                 transport_helper_init(ret, helper);
673         } else if (starts_with(url, "rsync:")) {
674                 die("git-over-rsync is no longer supported");
675         } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) {
676                 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
677                 transport_check_allowed("file");
678                 ret->data = data;
679                 ret->get_refs_list = get_refs_from_bundle;
680                 ret->fetch = fetch_refs_from_bundle;
681                 ret->disconnect = close_bundle;
682                 ret->smart_options = NULL;
683         } else if (!is_url(url)
684                 || starts_with(url, "file://")
685                 || starts_with(url, "git://")
686                 || starts_with(url, "ssh://")
687                 || starts_with(url, "git+ssh://") /* deprecated - do not use */
688                 || starts_with(url, "ssh+git://") /* deprecated - do not use */
689                 ) {
690                 /*
691                  * These are builtin smart transports; "allowed" transports
692                  * will be checked individually in git_connect.
693                  */
694                 struct git_transport_data *data = xcalloc(1, sizeof(*data));
695                 ret->data = data;
696                 ret->set_option = NULL;
697                 ret->get_refs_list = get_refs_via_connect;
698                 ret->fetch = fetch_refs_via_pack;
699                 ret->push_refs = git_transport_push;
700                 ret->connect = connect_git;
701                 ret->disconnect = disconnect_git;
702                 ret->smart_options = &(data->options);
703
704                 data->conn = NULL;
705                 data->got_remote_heads = 0;
706         } else {
707                 /* Unknown protocol in URL. Pass to external handler. */
708                 int len = external_specification_len(url);
709                 char *handler = xmemdupz(url, len);
710                 transport_helper_init(ret, handler);
711         }
712
713         if (ret->smart_options) {
714                 ret->smart_options->thin = 1;
715                 ret->smart_options->uploadpack = "git-upload-pack";
716                 if (remote->uploadpack)
717                         ret->smart_options->uploadpack = remote->uploadpack;
718                 ret->smart_options->receivepack = "git-receive-pack";
719                 if (remote->receivepack)
720                         ret->smart_options->receivepack = remote->receivepack;
721         }
722
723         return ret;
724 }
725
726 int transport_set_option(struct transport *transport,
727                          const char *name, const char *value)
728 {
729         int git_reports = 1, protocol_reports = 1;
730
731         if (transport->smart_options)
732                 git_reports = set_git_option(transport->smart_options,
733                                              name, value);
734
735         if (transport->set_option)
736                 protocol_reports = transport->set_option(transport, name,
737                                                         value);
738
739         /* If either report is 0, report 0 (success). */
740         if (!git_reports || !protocol_reports)
741                 return 0;
742         /* If either reports -1 (invalid value), report -1. */
743         if ((git_reports == -1) || (protocol_reports == -1))
744                 return -1;
745         /* Otherwise if both report unknown, report unknown. */
746         return 1;
747 }
748
749 void transport_set_verbosity(struct transport *transport, int verbosity,
750         int force_progress)
751 {
752         if (verbosity >= 1)
753                 transport->verbose = verbosity <= 3 ? verbosity : 3;
754         if (verbosity < 0)
755                 transport->verbose = -1;
756
757         /**
758          * Rules used to determine whether to report progress (processing aborts
759          * when a rule is satisfied):
760          *
761          *   . Report progress, if force_progress is 1 (ie. --progress).
762          *   . Don't report progress, if force_progress is 0 (ie. --no-progress).
763          *   . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
764          *   . Report progress if isatty(2) is 1.
765          **/
766         if (force_progress >= 0)
767                 transport->progress = !!force_progress;
768         else
769                 transport->progress = verbosity >= 0 && isatty(2);
770 }
771
772 static void die_with_unpushed_submodules(struct string_list *needs_pushing)
773 {
774         int i;
775
776         fprintf(stderr, "The following submodule paths contain changes that can\n"
777                         "not be found on any remote:\n");
778         for (i = 0; i < needs_pushing->nr; i++)
779                 printf("  %s\n", needs_pushing->items[i].string);
780         fprintf(stderr, "\nPlease try\n\n"
781                         "       git push --recurse-submodules=on-demand\n\n"
782                         "or cd to the path and use\n\n"
783                         "       git push\n\n"
784                         "to push them to a remote.\n\n");
785
786         string_list_clear(needs_pushing, 0);
787
788         die("Aborting.");
789 }
790
791 static int run_pre_push_hook(struct transport *transport,
792                              struct ref *remote_refs)
793 {
794         int ret = 0, x;
795         struct ref *r;
796         struct child_process proc = CHILD_PROCESS_INIT;
797         struct strbuf buf;
798         const char *argv[4];
799
800         if (!(argv[0] = find_hook("pre-push")))
801                 return 0;
802
803         argv[1] = transport->remote->name;
804         argv[2] = transport->url;
805         argv[3] = NULL;
806
807         proc.argv = argv;
808         proc.in = -1;
809
810         if (start_command(&proc)) {
811                 finish_command(&proc);
812                 return -1;
813         }
814
815         sigchain_push(SIGPIPE, SIG_IGN);
816
817         strbuf_init(&buf, 256);
818
819         for (r = remote_refs; r; r = r->next) {
820                 if (!r->peer_ref) continue;
821                 if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
822                 if (r->status == REF_STATUS_REJECT_STALE) continue;
823                 if (r->status == REF_STATUS_UPTODATE) continue;
824
825                 strbuf_reset(&buf);
826                 strbuf_addf( &buf, "%s %s %s %s\n",
827                          r->peer_ref->name, oid_to_hex(&r->new_oid),
828                          r->name, oid_to_hex(&r->old_oid));
829
830                 if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
831                         /* We do not mind if a hook does not read all refs. */
832                         if (errno != EPIPE)
833                                 ret = -1;
834                         break;
835                 }
836         }
837
838         strbuf_release(&buf);
839
840         x = close(proc.in);
841         if (!ret)
842                 ret = x;
843
844         sigchain_pop(SIGPIPE);
845
846         x = finish_command(&proc);
847         if (!ret)
848                 ret = x;
849
850         return ret;
851 }
852
853 int transport_push(struct transport *transport,
854                    int refspec_nr, const char **refspec, int flags,
855                    unsigned int *reject_reasons)
856 {
857         *reject_reasons = 0;
858         transport_verify_remote_names(refspec_nr, refspec);
859
860         if (transport->push) {
861                 /* Maybe FIXME. But no important transport uses this case. */
862                 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
863                         die("This transport does not support using --set-upstream");
864                 if (flags & TRANSPORT_PUSH_SET_PUBLISH)
865                         die("This transport does not support using --set-publish");
866
867                 return transport->push(transport, refspec_nr, refspec, flags);
868         } else if (transport->push_refs) {
869                 struct ref *remote_refs;
870                 struct ref *local_refs = get_local_heads();
871                 int match_flags = MATCH_REFS_NONE;
872                 int verbose = (transport->verbose > 0);
873                 int quiet = (transport->verbose < 0);
874                 int porcelain = flags & TRANSPORT_PUSH_PORCELAIN;
875                 int pretend = flags & TRANSPORT_PUSH_DRY_RUN;
876                 int push_ret, ret, err;
877
878                 if (check_push_refs(local_refs, refspec_nr, refspec) < 0)
879                         return -1;
880
881                 remote_refs = transport->get_refs_list(transport, 1);
882
883                 if (flags & TRANSPORT_PUSH_ALL)
884                         match_flags |= MATCH_REFS_ALL;
885                 if (flags & TRANSPORT_PUSH_MIRROR)
886                         match_flags |= MATCH_REFS_MIRROR;
887                 if (flags & TRANSPORT_PUSH_PRUNE)
888                         match_flags |= MATCH_REFS_PRUNE;
889                 if (flags & TRANSPORT_PUSH_FOLLOW_TAGS)
890                         match_flags |= MATCH_REFS_FOLLOW_TAGS;
891
892                 if (match_push_refs(local_refs, &remote_refs,
893                                     refspec_nr, refspec, match_flags)) {
894                         return -1;
895                 }
896
897                 if (transport->smart_options &&
898                     transport->smart_options->cas &&
899                     !is_empty_cas(transport->smart_options->cas))
900                         apply_push_cas(transport->smart_options->cas,
901                                        transport->remote, remote_refs);
902
903                 set_ref_status_for_push(remote_refs,
904                         flags & TRANSPORT_PUSH_MIRROR,
905                         flags & TRANSPORT_PUSH_FORCE);
906
907                 if (!(flags & TRANSPORT_PUSH_NO_HOOK))
908                         if (run_pre_push_hook(transport, remote_refs))
909                                 return -1;
910
911                 if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
912                         struct ref *ref = remote_refs;
913                         for (; ref; ref = ref->next)
914                                 if (!is_null_oid(&ref->new_oid) &&
915                                     !push_unpushed_submodules(ref->new_oid.hash,
916                                             transport->remote->name))
917                                     die ("Failed to push all needed submodules!");
918                 }
919
920                 if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
921                               TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
922                         struct ref *ref = remote_refs;
923                         struct string_list needs_pushing = STRING_LIST_INIT_DUP;
924
925                         for (; ref; ref = ref->next)
926                                 if (!is_null_oid(&ref->new_oid) &&
927                                     find_unpushed_submodules(ref->new_oid.hash,
928                                             transport->remote->name, &needs_pushing))
929                                         die_with_unpushed_submodules(&needs_pushing);
930                 }
931
932                 push_ret = transport->push_refs(transport, remote_refs, flags);
933                 err = push_had_errors(remote_refs);
934                 ret = push_ret | err;
935
936                 if (!quiet || err)
937                         transport_print_push_status(transport->url, remote_refs,
938                                         verbose | porcelain, porcelain,
939                                         reject_reasons);
940
941                 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
942                         set_tracking(transport, remote_refs, pretend, 0);
943                 if (flags & TRANSPORT_PUSH_SET_PUBLISH)
944                         set_tracking(transport, remote_refs, pretend, 1);
945
946                 if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
947                         struct ref *ref;
948                         for (ref = remote_refs; ref; ref = ref->next)
949                                 transport_update_tracking_ref(transport->remote, ref, verbose);
950                 }
951
952                 if (porcelain && !push_ret)
953                         puts("Done");
954                 else if (!quiet && !ret && !transport_refs_pushed(remote_refs))
955                         fprintf(stderr, "Everything up-to-date\n");
956
957                 return ret;
958         }
959         return 1;
960 }
961
962 const struct ref *transport_get_remote_refs(struct transport *transport)
963 {
964         if (!transport->got_remote_refs) {
965                 transport->remote_refs = transport->get_refs_list(transport, 0);
966                 transport->got_remote_refs = 1;
967         }
968
969         return transport->remote_refs;
970 }
971
972 int transport_fetch_refs(struct transport *transport, struct ref *refs)
973 {
974         int rc;
975         int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
976         struct ref **heads = NULL;
977         struct ref *rm;
978
979         for (rm = refs; rm; rm = rm->next) {
980                 nr_refs++;
981                 if (rm->peer_ref &&
982                     !is_null_oid(&rm->old_oid) &&
983                     !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
984                         continue;
985                 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
986                 heads[nr_heads++] = rm;
987         }
988
989         if (!nr_heads) {
990                 /*
991                  * When deepening of a shallow repository is requested,
992                  * then local and remote refs are likely to still be equal.
993                  * Just feed them all to the fetch method in that case.
994                  * This condition shouldn't be met in a non-deepening fetch
995                  * (see builtin/fetch.c:quickfetch()).
996                  */
997                 ALLOC_ARRAY(heads, nr_refs);
998                 for (rm = refs; rm; rm = rm->next)
999                         heads[nr_heads++] = rm;
1000         }
1001
1002         rc = transport->fetch(transport, nr_heads, heads);
1003
1004         free(heads);
1005         return rc;
1006 }
1007
1008 void transport_unlock_pack(struct transport *transport)
1009 {
1010         if (transport->pack_lockfile) {
1011                 unlink_or_warn(transport->pack_lockfile);
1012                 free(transport->pack_lockfile);
1013                 transport->pack_lockfile = NULL;
1014         }
1015 }
1016
1017 int transport_connect(struct transport *transport, const char *name,
1018                       const char *exec, int fd[2])
1019 {
1020         if (transport->connect)
1021                 return transport->connect(transport, name, exec, fd);
1022         else
1023                 die("Operation not supported by protocol");
1024 }
1025
1026 int transport_disconnect(struct transport *transport)
1027 {
1028         int ret = 0;
1029         if (transport->disconnect)
1030                 ret = transport->disconnect(transport);
1031         free(transport);
1032         return ret;
1033 }
1034
1035 /*
1036  * Strip username (and password) from a URL and return
1037  * it in a newly allocated string.
1038  */
1039 char *transport_anonymize_url(const char *url)
1040 {
1041         char *scheme_prefix, *anon_part;
1042         size_t anon_len, prefix_len = 0;
1043
1044         anon_part = strchr(url, '@');
1045         if (url_is_local_not_ssh(url) || !anon_part)
1046                 goto literal_copy;
1047
1048         anon_len = strlen(++anon_part);
1049         scheme_prefix = strstr(url, "://");
1050         if (!scheme_prefix) {
1051                 if (!strchr(anon_part, ':'))
1052                         /* cannot be "me@there:/path/name" */
1053                         goto literal_copy;
1054         } else {
1055                 const char *cp;
1056                 /* make sure scheme is reasonable */
1057                 for (cp = url; cp < scheme_prefix; cp++) {
1058                         switch (*cp) {
1059                                 /* RFC 1738 2.1 */
1060                         case '+': case '.': case '-':
1061                                 break; /* ok */
1062                         default:
1063                                 if (isalnum(*cp))
1064                                         break;
1065                                 /* it isn't */
1066                                 goto literal_copy;
1067                         }
1068                 }
1069                 /* @ past the first slash does not count */
1070                 cp = strchr(scheme_prefix + 3, '/');
1071                 if (cp && cp < anon_part)
1072                         goto literal_copy;
1073                 prefix_len = scheme_prefix - url + 3;
1074         }
1075         return xstrfmt("%.*s%.*s", (int)prefix_len, url,
1076                        (int)anon_len, anon_part);
1077 literal_copy:
1078         return xstrdup(url);
1079 }
1080
1081 struct alternate_refs_data {
1082         alternate_ref_fn *fn;
1083         void *data;
1084 };
1085
1086 static int refs_from_alternate_cb(struct alternate_object_database *e,
1087                                   void *data)
1088 {
1089         char *other;
1090         size_t len;
1091         struct remote *remote;
1092         struct transport *transport;
1093         const struct ref *extra;
1094         struct alternate_refs_data *cb = data;
1095
1096         e->name[-1] = '\0';
1097         other = xstrdup(real_path(e->base));
1098         e->name[-1] = '/';
1099         len = strlen(other);
1100
1101         while (other[len-1] == '/')
1102                 other[--len] = '\0';
1103         if (len < 8 || memcmp(other + len - 8, "/objects", 8))
1104                 goto out;
1105         /* Is this a git repository with refs? */
1106         memcpy(other + len - 8, "/refs", 6);
1107         if (!is_directory(other))
1108                 goto out;
1109         other[len - 8] = '\0';
1110         remote = remote_get(other);
1111         transport = transport_get(remote, other);
1112         for (extra = transport_get_remote_refs(transport);
1113              extra;
1114              extra = extra->next)
1115                 cb->fn(extra, cb->data);
1116         transport_disconnect(transport);
1117 out:
1118         free(other);
1119         return 0;
1120 }
1121
1122 void for_each_alternate_ref(alternate_ref_fn fn, void *data)
1123 {
1124         struct alternate_refs_data cb;
1125         cb.fn = fn;
1126         cb.data = data;
1127         foreach_alt_odb(refs_from_alternate_cb, &cb);
1128 }