ls-remote: pass ref prefixes when requesting a remote's refs
[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
49         struct remote *remote;
50         struct transport *transport;
51         const struct ref *ref;
52
53         struct option options[] = {
54                 OPT__QUIET(&quiet, N_("do not print remote URL")),
55                 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
56                            N_("path of git-upload-pack on the remote host")),
57                 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
58                            N_("path of git-upload-pack on the remote host"),
59                            PARSE_OPT_HIDDEN },
60                 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
61                 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
62                 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
63                 OPT_BOOL(0, "get-url", &get_url,
64                          N_("take url.<base>.insteadOf into account")),
65                 OPT_SET_INT(0, "exit-code", &status,
66                             N_("exit with exit code 2 if no matching refs are found"), 2),
67                 OPT_BOOL(0, "symref", &show_symref_target,
68                          N_("show underlying ref in addition to the object pointed by it")),
69                 OPT_END()
70         };
71
72         argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
73                              PARSE_OPT_STOP_AT_NON_OPTION);
74         dest = argv[0];
75
76         if (argc > 1) {
77                 int i;
78                 pattern = xcalloc(argc, sizeof(const char *));
79                 for (i = 1; i < argc; i++) {
80                         const char *glob;
81                         pattern[i - 1] = xstrfmt("*/%s", argv[i]);
82
83                         glob = strchr(argv[i], '*');
84                         if (glob)
85                                 argv_array_pushf(&ref_prefixes, "%.*s",
86                                                  (int)(glob - argv[i]), argv[i]);
87                         else
88                                 expand_ref_prefix(&ref_prefixes, argv[i]);
89                 }
90         }
91
92         remote = remote_get(dest);
93         if (!remote) {
94                 if (dest)
95                         die("bad repository '%s'", dest);
96                 die("No remote configured to list refs from.");
97         }
98         if (!remote->url_nr)
99                 die("remote %s has no configured URL", dest);
100
101         if (get_url) {
102                 printf("%s\n", *remote->url);
103                 return 0;
104         }
105
106         transport = transport_get(remote, NULL);
107         if (uploadpack != NULL)
108                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
109
110         ref = transport_get_remote_refs(transport, &ref_prefixes);
111         if (transport_disconnect(transport))
112                 return 1;
113
114         if (!dest && !quiet)
115                 fprintf(stderr, "From %s\n", *remote->url);
116         for ( ; ref; ref = ref->next) {
117                 if (!check_ref_type(ref, flags))
118                         continue;
119                 if (!tail_match(pattern, ref->name))
120                         continue;
121                 if (show_symref_target && ref->symref)
122                         printf("ref: %s\t%s\n", ref->symref, ref->name);
123                 printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
124                 status = 0; /* we found something */
125         }
126         return status;
127 }