builtin/show: do not prune by pathspec
[git/mjg.git] / builtin / ls-remote.c
blobdebf2d4f887ed2d24ad0dc8a717ac188c73d5bda
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 char **pattern, const char *path)
24 const char *p;
25 char *pathbuf;
27 if (!pattern)
28 return 1; /* no restriction */
30 pathbuf = xstrfmt("/%s", path);
31 while ((p = *(pattern++)) != NULL) {
32 if (!wildmatch(p, pathbuf, 0)) {
33 free(pathbuf);
34 return 1;
37 free(pathbuf);
38 return 0;
41 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
43 const char *dest = NULL;
44 unsigned flags = 0;
45 int get_url = 0;
46 int quiet = 0;
47 int status = 0;
48 int show_symref_target = 0;
49 const char *uploadpack = NULL;
50 const char **pattern = NULL;
51 struct transport_ls_refs_options transport_options =
52 TRANSPORT_LS_REFS_OPTIONS_INIT;
53 int i;
54 struct string_list server_options = STRING_LIST_INIT_DUP;
56 struct remote *remote;
57 struct transport *transport;
58 const struct ref *ref;
59 struct ref_array ref_array;
60 struct ref_sorting *sorting;
61 struct string_list sorting_options = STRING_LIST_INIT_DUP;
63 struct option options[] = {
64 OPT__QUIET(&quiet, N_("do not print remote URL")),
65 OPT_STRING(0, "upload-pack", &uploadpack, N_("exec"),
66 N_("path of git-upload-pack on the remote host")),
67 { OPTION_STRING, 0, "exec", &uploadpack, N_("exec"),
68 N_("path of git-upload-pack on the remote host"),
69 PARSE_OPT_HIDDEN },
70 OPT_BIT('t', "tags", &flags, N_("limit to tags"), REF_TAGS),
71 OPT_BIT('b', "branches", &flags, N_("limit to branches"), REF_BRANCHES),
72 OPT_BIT_F('h', "heads", &flags,
73 N_("deprecated synonym for --branches"), REF_BRANCHES,
74 PARSE_OPT_HIDDEN),
75 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
76 OPT_BOOL(0, "get-url", &get_url,
77 N_("take url.<base>.insteadOf into account")),
78 OPT_REF_SORT(&sorting_options),
79 OPT_SET_INT_F(0, "exit-code", &status,
80 N_("exit with exit code 2 if no matching refs are found"),
81 2, PARSE_OPT_NOCOMPLETE),
82 OPT_BOOL(0, "symref", &show_symref_target,
83 N_("show underlying ref in addition to the object pointed by it")),
84 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
85 OPT_END()
88 memset(&ref_array, 0, sizeof(ref_array));
90 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
91 PARSE_OPT_STOP_AT_NON_OPTION);
92 dest = argv[0];
94 packet_trace_identity("ls-remote");
96 if (argc > 1) {
97 int i;
98 CALLOC_ARRAY(pattern, argc);
99 for (i = 1; i < argc; i++) {
100 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
104 if (flags & REF_TAGS)
105 strvec_push(&transport_options.ref_prefixes, "refs/tags/");
106 if (flags & REF_BRANCHES)
107 strvec_push(&transport_options.ref_prefixes, "refs/heads/");
109 remote = remote_get(dest);
110 if (!remote) {
111 if (dest)
112 die("bad repository '%s'", dest);
113 die("No remote configured to list refs from.");
116 if (get_url) {
117 printf("%s\n", remote->url.v[0]);
118 return 0;
121 transport = transport_get(remote, NULL);
122 if (uploadpack)
123 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
124 if (server_options.nr)
125 transport->server_options = &server_options;
127 ref = transport_get_remote_refs(transport, &transport_options);
128 if (ref) {
129 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
130 repo_set_hash_algo(the_repository, hash_algo);
133 if (!dest && !quiet)
134 fprintf(stderr, "From %s\n", remote->url.v[0]);
135 for ( ; ref; ref = ref->next) {
136 struct ref_array_item *item;
137 if (!check_ref_type(ref, flags))
138 continue;
139 if (!tail_match(pattern, ref->name))
140 continue;
141 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
142 item->symref = xstrdup_or_null(ref->symref);
145 sorting = ref_sorting_options(&sorting_options);
146 ref_array_sort(sorting, &ref_array);
148 for (i = 0; i < ref_array.nr; i++) {
149 const struct ref_array_item *ref = ref_array.items[i];
150 if (show_symref_target && ref->symref)
151 printf("ref: %s\t%s\n", ref->symref, ref->refname);
152 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
153 status = 0; /* we found something */
156 ref_sorting_release(sorting);
157 ref_array_clear(&ref_array);
158 if (transport_disconnect(transport))
159 status = 1;
160 transport_ls_refs_options_release(&transport_options);
161 return status;