Merge branch 'sg/subtree-signed-commits' into pu
[git] / builtin / ls-remote.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "transport.h"
4 #include "remote.h"
5
6 static const char * const ls_remote_usage[] = {
7         N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
8            "                     [-q | --quiet] [--exit-code] [--get-url]\n"
9            "                     [--symref] [<repository> [<refs>...]]"),
10         NULL
11 };
12
13 /*
14  * Is there one among the list of patterns that match the tail part
15  * of the path?
16  */
17 static int tail_match(const char **pattern, const char *path)
18 {
19         const char *p;
20         char *pathbuf;
21
22         if (!pattern)
23                 return 1; /* no restriction */
24
25         pathbuf = xstrfmt("/%s", path);
26         while ((p = *(pattern++)) != NULL) {
27                 if (!wildmatch(p, pathbuf, 0)) {
28                         free(pathbuf);
29                         return 1;
30                 }
31         }
32         free(pathbuf);
33         return 0;
34 }
35
36 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
37 {
38         const char *dest = NULL;
39         unsigned flags = 0;
40         int get_url = 0;
41         int quiet = 0;
42         int status = 0;
43         int show_symref_target = 0;
44         const char *uploadpack = NULL;
45         const char **pattern = NULL;
46         struct argv_array ref_patterns = ARGV_ARRAY_INIT;
47
48         struct remote *remote;
49         struct transport *transport;
50         const struct ref *ref;
51
52         struct option options[] = {
53                 OPT__QUIET(&quiet, N_("do not print remote URL")),
54                 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
55                            N_("path of git-upload-pack on the remote host")),
56                 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
57                            N_("path of git-upload-pack on the remote host"),
58                            PARSE_OPT_HIDDEN },
59                 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
60                 OPT_BIT('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
61                 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
62                 OPT_BOOL(0, "get-url", &get_url,
63                          N_("take url.<base>.insteadOf into account")),
64                 OPT_SET_INT_F(0, "exit-code", &status,
65                               N_("exit with exit code 2 if no matching refs are found"),
66                               2, PARSE_OPT_NOCOMPLETE),
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                         pattern[i - 1] = xstrfmt("*/%s", argv[i]);
81                         argv_array_push(&ref_patterns, argv[i]);
82                 }
83         }
84
85         remote = remote_get(dest);
86         if (!remote) {
87                 if (dest)
88                         die("bad repository '%s'", dest);
89                 die("No remote configured to list refs from.");
90         }
91         if (!remote->url_nr)
92                 die("remote %s has no configured URL", dest);
93
94         if (get_url) {
95                 printf("%s\n", *remote->url);
96                 return 0;
97         }
98
99         transport = transport_get(remote, NULL);
100         if (uploadpack != NULL)
101                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
102
103         ref = transport_get_remote_refs(transport, &ref_patterns);
104         if (transport_disconnect(transport))
105                 return 1;
106
107         if (!dest && !quiet)
108                 fprintf(stderr, "From %s\n", *remote->url);
109         for ( ; ref; ref = ref->next) {
110                 if (!check_ref_type(ref, flags))
111                         continue;
112                 if (!tail_match(pattern, ref->name))
113                         continue;
114                 if (show_symref_target && ref->symref)
115                         printf("ref: %s\t%s\n", ref->symref, ref->name);
116                 printf("%s\t%s\n", oid_to_hex(&ref->old_oid), ref->name);
117                 status = 0; /* we found something */
118         }
119         return status;
120 }