Sync with 'maint'
[git/gitster.git] / builtin / ls-remote.c
blob0a491595ca831c3caeb9721efd322f3027c1983d
1 #include "builtin.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "transport.h"
5 #include "pkt-line.h"
6 #include "ref-filter.h"
7 #include "remote.h"
8 #include "parse-options.h"
9 #include "wildmatch.h"
11 static const char * const ls_remote_usage[] = {
12 N_("git ls-remote [--branches] [--tags] [--refs] [--upload-pack=<exec>]\n"
13 " [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n"
14 " [--symref] [<repository> [<patterns>...]]"),
15 NULL
19 * Is there one among the list of patterns that match the tail part
20 * of the path?
22 static int tail_match(const struct strvec *pattern, const char *path)
24 char *pathbuf;
26 if (!pattern->nr)
27 return 1; /* no restriction */
29 pathbuf = xstrfmt("/%s", path);
30 for (size_t i = 0; i < pattern->nr; i++) {
31 if (!wildmatch(pattern->v[i], pathbuf, 0)) {
32 free(pathbuf);
33 return 1;
36 free(pathbuf);
37 return 0;
40 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
42 const char *dest = NULL;
43 unsigned flags = 0;
44 int get_url = 0;
45 int quiet = 0;
46 int status = 0;
47 int show_symref_target = 0;
48 const char *uploadpack = NULL;
49 struct strvec pattern = STRVEC_INIT;
50 struct transport_ls_refs_options transport_options =
51 TRANSPORT_LS_REFS_OPTIONS_INIT;
52 int i;
53 struct string_list server_options = STRING_LIST_INIT_DUP;
55 struct remote *remote;
56 struct transport *transport;
57 const struct ref *ref;
58 struct ref_array ref_array;
59 struct ref_sorting *sorting;
60 struct string_list sorting_options = STRING_LIST_INIT_DUP;
62 struct option options[] = {
63 OPT__QUIET(&quiet, N_("do not print remote URL")),
64 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
65 N_("path of git-upload-pack on the remote host")),
66 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
67 N_("path of git-upload-pack on the remote host"),
68 PARSE_OPT_HIDDEN },
69 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
70 OPT_BIT('b', "branches", &flags, N_("limit to branches"), REF_BRANCHES),
71 OPT_BIT_F('h', "heads", &flags,
72 N_("deprecated synonym for --branches"), REF_BRANCHES,
73 PARSE_OPT_HIDDEN),
74 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
75 OPT_BOOL(0, "get-url", &get_url,
76 N_("take url.<base>.insteadOf into account")),
77 OPT_REF_SORT(&sorting_options),
78 OPT_SET_INT_F(0, "exit-code", &status,
79 N_("exit with exit code 2 if no matching refs are found"),
80 2, PARSE_OPT_NOCOMPLETE),
81 OPT_BOOL(0, "symref", &show_symref_target,
82 N_("show underlying ref in addition to the object pointed by it")),
83 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
84 OPT_END()
87 memset(&ref_array, 0, sizeof(ref_array));
89 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
90 PARSE_OPT_STOP_AT_NON_OPTION);
91 dest = argv[0];
94 * TODO: This is buggy, but required for transport helpers. When a
95 * transport helper advertises a "refspec", then we'd add that to a
96 * list of refspecs via `refspec_append()`, which transitively depends
97 * on `the_hash_algo`. Thus, when the hash algorithm isn't properly set
98 * up, this would lead to a segfault.
100 * We really should fix this in the transport helper logic such that we
101 * lazily parse refspec capabilities _after_ we have learned about the
102 * remote's object format. Otherwise, we may end up misparsing refspecs
103 * depending on what object hash the remote uses.
105 if (!the_repository->hash_algo)
106 repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
108 packet_trace_identity("ls-remote");
110 for (int i = 1; i < argc; i++)
111 strvec_pushf(&pattern, "*/%s", argv[i]);
113 if (flags & REF_TAGS)
114 strvec_push(&transport_options.ref_prefixes, "refs/tags/");
115 if (flags & REF_BRANCHES)
116 strvec_push(&transport_options.ref_prefixes, "refs/heads/");
118 remote = remote_get(dest);
119 if (!remote) {
120 if (dest)
121 die("bad repository '%s'", dest);
122 die("No remote configured to list refs from.");
125 if (get_url) {
126 printf("%s\n", remote->url.v[0]);
127 return 0;
130 transport = transport_get(remote, NULL);
131 if (uploadpack)
132 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
133 if (server_options.nr)
134 transport->server_options = &server_options;
136 ref = transport_get_remote_refs(transport, &transport_options);
137 if (ref) {
138 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
139 repo_set_hash_algo(the_repository, hash_algo);
142 if (!dest && !quiet)
143 fprintf(stderr, "From %s\n", remote->url.v[0]);
144 for ( ; ref; ref = ref->next) {
145 struct ref_array_item *item;
146 if (!check_ref_type(ref, flags))
147 continue;
148 if (!tail_match(&pattern, ref->name))
149 continue;
150 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
151 item->symref = xstrdup_or_null(ref->symref);
154 sorting = ref_sorting_options(&sorting_options);
155 ref_array_sort(sorting, &ref_array);
157 for (i = 0; i < ref_array.nr; i++) {
158 const struct ref_array_item *ref = ref_array.items[i];
159 if (show_symref_target && ref->symref)
160 printf("ref: %s\t%s\n", ref->symref, ref->refname);
161 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
162 status = 0; /* we found something */
165 ref_sorting_release(sorting);
166 ref_array_clear(&ref_array);
167 if (transport_disconnect(transport))
168 status = 1;
169 transport_ls_refs_options_release(&transport_options);
171 strvec_clear(&pattern);
172 return status;