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