fetch: send server options when using protocol v2
[git] / builtin / ls-remote.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "refs.h"
6
7 static const char * const ls_remote_usage[] = {
8         N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
9            "                     [-q | --quiet] [--exit-code] [--get-url]\n"
10            "                     [--symref] [<repository> [<refs>...]]"),
11         NULL
12 };
13
14 /*
15  * Is there one among the list of patterns that match the tail part
16  * of the path?
17  */
18 static int tail_match(const char **pattern, const char *path)
19 {
20         const char *p;
21         char *pathbuf;
22
23         if (!pattern)
24                 return 1; /* no restriction */
25
26         pathbuf = xstrfmt("/%s", path);
27         while ((p = *(pattern++)) != NULL) {
28                 if (!wildmatch(p, pathbuf, 0)) {
29                         free(pathbuf);
30                         return 1;
31                 }
32         }
33         free(pathbuf);
34         return 0;
35 }
36
37 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
38 {
39         const char *dest = NULL;
40         unsigned flags = 0;
41         int get_url = 0;
42         int quiet = 0;
43         int status = 0;
44         int show_symref_target = 0;
45         const char *uploadpack = NULL;
46         const char **pattern = NULL;
47         struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
48         struct string_list server_options = STRING_LIST_INIT_DUP;
49
50         struct remote *remote;
51         struct transport *transport;
52         const struct ref *ref;
53
54         struct option options[] = {
55                 OPT__QUIET(&quiet, N_("do not print remote URL")),
56                 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
57                            N_("path of git-upload-pack on the remote host")),
58                 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
59                            N_("path of git-upload-pack on the remote host"),
60                            PARSE_OPT_HIDDEN },
61                 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
62                 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
63                 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
64                 OPT_BOOL(0, "get-url", &get_url,
65                          N_("take url.<base>.insteadOf into account")),
66                 OPT_SET_INT_F(0, "exit-code", &status,
67                               N_("exit with exit code 2 if no matching refs are found"),
68                               2, PARSE_OPT_NOCOMPLETE),
69                 OPT_BOOL(0, "symref", &show_symref_target,
70                          N_("show underlying ref in addition to the object pointed by it")),
71                 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
72                 OPT_END()
73         };
74
75         argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
76                              PARSE_OPT_STOP_AT_NON_OPTION);
77         dest = argv[0];
78
79         if (argc > 1) {
80                 int i;
81                 pattern = xcalloc(argc, sizeof(const char *));
82                 for (i = 1; i < argc; i++) {
83                         const char *glob;
84                         pattern[i - 1] = xstrfmt("*/%s", argv[i]);
85
86                         glob = strchr(argv[i], '*');
87                         if (glob)
88                                 argv_array_pushf(&ref_prefixes, "%.*s",
89                                                  (int)(glob - argv[i]), argv[i]);
90                         else
91                                 expand_ref_prefix(&ref_prefixes, argv[i]);
92                 }
93         }
94
95         remote = remote_get(dest);
96         if (!remote) {
97                 if (dest)
98                         die("bad repository '%s'", dest);
99                 die("No remote configured to list refs from.");
100         }
101         if (!remote->url_nr)
102                 die("remote %s has no configured URL", dest);
103
104         if (get_url) {
105                 printf("%s\n", *remote->url);
106                 return 0;
107         }
108
109         transport = transport_get(remote, NULL);
110         if (uploadpack != NULL)
111                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
112         if (server_options.nr)
113                 transport->server_options = &server_options;
114
115         ref = transport_get_remote_refs(transport, &ref_prefixes);
116         if (transport_disconnect(transport))
117                 return 1;
118
119         if (!dest && !quiet)
120                 fprintf(stderr, "From %s\n", *remote->url);
121         for ( ; ref; ref = ref->next) {
122                 if (!check_ref_type(ref, flags))
123                         continue;
124                 if (!tail_match(pattern, ref->name))
125                         continue;
126                 if (show_symref_target && ref->symref)
127                         printf("ref: %s\t%s\n", ref->symref, ref->name);
128                 printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
129                 status = 0; /* we found something */
130         }
131         return status;
132 }