]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/ls-remote.c
parse-options: simplify positivation handling
[thirdparty/git.git] / builtin / ls-remote.c
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 "refs.h"
9 #include "parse-options.h"
10 #include "wildmatch.h"
11
12 static const char * const ls_remote_usage[] = {
13 N_("git ls-remote [--heads] [--tags] [--refs] [--upload-pack=<exec>]\n"
14 " [-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]\n"
15 " [--symref] [<repository> [<patterns>...]]"),
16 NULL
17 };
18
19 /*
20 * Is there one among the list of patterns that match the tail part
21 * of the path?
22 */
23 static int tail_match(const char **pattern, const char *path)
24 {
25 const char *p;
26 char *pathbuf;
27
28 if (!pattern)
29 return 1; /* no restriction */
30
31 pathbuf = xstrfmt("/%s", path);
32 while ((p = *(pattern++)) != NULL) {
33 if (!wildmatch(p, pathbuf, 0)) {
34 free(pathbuf);
35 return 1;
36 }
37 }
38 free(pathbuf);
39 return 0;
40 }
41
42 int cmd_ls_remote(int argc, const char **argv, const char *prefix)
43 {
44 const char *dest = NULL;
45 unsigned flags = 0;
46 int get_url = 0;
47 int quiet = 0;
48 int status = 0;
49 int show_symref_target = 0;
50 const char *uploadpack = NULL;
51 const char **pattern = NULL;
52 struct transport_ls_refs_options transport_options =
53 TRANSPORT_LS_REFS_OPTIONS_INIT;
54 int i;
55 struct string_list server_options = STRING_LIST_INIT_DUP;
56
57 struct remote *remote;
58 struct transport *transport;
59 const struct ref *ref;
60 struct ref_array ref_array;
61 struct string_list sorting_options = STRING_LIST_INIT_DUP;
62
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('h', "heads", &flags, N_("limit to heads"), REF_HEADS),
72 OPT_BIT(0, "refs", &flags, N_("do not show peeled tags"), REF_NORMAL),
73 OPT_BOOL(0, "get-url", &get_url,
74 N_("take url.<base>.insteadOf into account")),
75 OPT_REF_SORT(&sorting_options),
76 OPT_SET_INT_F(0, "exit-code", &status,
77 N_("exit with exit code 2 if no matching refs are found"),
78 2, PARSE_OPT_NOCOMPLETE),
79 OPT_BOOL(0, "symref", &show_symref_target,
80 N_("show underlying ref in addition to the object pointed by it")),
81 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
82 OPT_END()
83 };
84
85 memset(&ref_array, 0, sizeof(ref_array));
86
87 argc = parse_options(argc, argv, prefix, options, ls_remote_usage,
88 PARSE_OPT_STOP_AT_NON_OPTION);
89 dest = argv[0];
90
91 packet_trace_identity("ls-remote");
92
93 if (argc > 1) {
94 int i;
95 CALLOC_ARRAY(pattern, argc);
96 for (i = 1; i < argc; i++) {
97 pattern[i - 1] = xstrfmt("*/%s", argv[i]);
98 }
99 }
100
101 if (flags & REF_TAGS)
102 strvec_push(&transport_options.ref_prefixes, "refs/tags/");
103 if (flags & REF_HEADS)
104 strvec_push(&transport_options.ref_prefixes, "refs/heads/");
105
106 remote = remote_get(dest);
107 if (!remote) {
108 if (dest)
109 die("bad repository '%s'", dest);
110 die("No remote configured to list refs from.");
111 }
112 if (!remote->url_nr)
113 die("remote %s has no configured URL", dest);
114
115 if (get_url) {
116 printf("%s\n", *remote->url);
117 return 0;
118 }
119
120 transport = transport_get(remote, NULL);
121 if (uploadpack)
122 transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack);
123 if (server_options.nr)
124 transport->server_options = &server_options;
125
126 ref = transport_get_remote_refs(transport, &transport_options);
127 if (ref) {
128 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
129 repo_set_hash_algo(the_repository, hash_algo);
130 }
131
132 if (!dest && !quiet)
133 fprintf(stderr, "From %s\n", *remote->url);
134 for ( ; ref; ref = ref->next) {
135 struct ref_array_item *item;
136 if (!check_ref_type(ref, flags))
137 continue;
138 if (!tail_match(pattern, ref->name))
139 continue;
140 item = ref_array_push(&ref_array, ref->name, &ref->old_oid);
141 item->symref = xstrdup_or_null(ref->symref);
142 }
143
144 if (sorting_options.nr) {
145 struct ref_sorting *sorting;
146
147 sorting = ref_sorting_options(&sorting_options);
148 ref_array_sort(sorting, &ref_array);
149 ref_sorting_release(sorting);
150 }
151
152 for (i = 0; i < ref_array.nr; i++) {
153 const struct ref_array_item *ref = ref_array.items[i];
154 if (show_symref_target && ref->symref)
155 printf("ref: %s\t%s\n", ref->symref, ref->refname);
156 printf("%s\t%s\n", oid_to_hex(&ref->objectname), ref->refname);
157 status = 0; /* we found something */
158 }
159
160 ref_array_clear(&ref_array);
161 if (transport_disconnect(transport))
162 status = 1;
163 transport_ls_refs_options_release(&transport_options);
164 return status;
165 }