]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/show-ref.c
The seventh batch
[thirdparty/git.git] / builtin / show-ref.c
CommitLineData
baffc0e7 1#include "builtin.h"
8e712ef6 2#include "config.h"
f394e093 3#include "gettext.h"
41771fa4 4#include "hex.h"
9080a7f1 5#include "refs/refs-internal.h"
dabab1d6 6#include "object-name.h"
a034e910 7#include "object-store-ll.h"
358ddb62 8#include "object.h"
c455c87c 9#include "string-list.h"
69932bc6 10#include "parse-options.h"
358ddb62 11
69932bc6 12static const char * const show_ref_usage[] = {
1307d5e8 13 N_("git show-ref [--head] [-d | --dereference]\n"
5af8b61c
ÆAB
14 " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags]\n"
15 " [--heads] [--] [<pattern>...]"),
1307d5e8
PS
16 N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n"
17 " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n"
18 " [--] [<ref>...]"),
33e8fc87 19 N_("git show-ref --exclude-existing[=<pattern>]"),
9080a7f1 20 N_("git show-ref --exists <ref>"),
69932bc6
SB
21 NULL
22};
358ddb62 23
b0f0be93
PS
24struct show_one_options {
25 int quiet;
26 int hash_only;
27 int abbrev;
28 int deref_tags;
29};
30
31static void show_one(const struct show_one_options *opts,
32 const char *refname, const struct object_id *oid)
64fe031a 33{
f1627040
VP
34 const char *hex;
35 struct object_id peeled;
36
bc726bd0 37 if (!repo_has_object_file(the_repository, oid))
d01b8203
VP
38 die("git show-ref: bad ref %s (%s)", refname,
39 oid_to_hex(oid));
40
b0f0be93 41 if (opts->quiet)
14144d3b
VP
42 return;
43
b0f0be93
PS
44 hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
45 if (opts->hash_only)
64fe031a
JH
46 printf("%s\n", hex);
47 else
48 printf("%s %s\n", hex, refname);
f1627040 49
b0f0be93 50 if (!opts->deref_tags)
f1627040
VP
51 return;
52
36a31792 53 if (!peel_iterated_oid(oid, &peeled)) {
b0f0be93 54 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
f1627040
VP
55 printf("%s %s^{}\n", hex, refname);
56 }
64fe031a
JH
57}
58
ff546ebb 59struct show_ref_data {
b0f0be93 60 const struct show_one_options *show_one_opts;
ff546ebb 61 const char **patterns;
84650989 62 int found_match;
ee26f1e2 63 int show_head;
ff546ebb
PS
64};
65
f0a011fa 66static int show_ref(const char *refname, const struct object_id *oid,
ff546ebb 67 int flag UNUSED, void *cbdata)
358ddb62 68{
ff546ebb
PS
69 struct show_ref_data *data = cbdata;
70
ee26f1e2 71 if (data->show_head && !strcmp(refname, "HEAD"))
3f3d0cea
DB
72 goto match;
73
ff546ebb 74 if (data->patterns) {
358ddb62 75 int reflen = strlen(refname);
ff546ebb 76 const char **p = data->patterns, *m;
358ddb62
LT
77 while ((m = *p++) != NULL) {
78 int len = strlen(m);
79 if (len > reflen)
80 continue;
81 if (memcmp(m, refname + reflen - len, len))
82 continue;
83 if (len == reflen)
84 goto match;
358ddb62
LT
85 if (refname[reflen - len - 1] == '/')
86 goto match;
87 }
88 return 0;
89 }
90
91match:
84650989 92 data->found_match++;
cf0adba7 93
b0f0be93 94 show_one(data->show_one_opts, refname, oid);
cf0adba7 95
358ddb62
LT
96 return 0;
97}
98
63e14ee2 99static int add_existing(const char *refname,
5cf88fd8
ÆAB
100 const struct object_id *oid UNUSED,
101 int flag UNUSED, void *cbdata)
ed9f7c95 102{
c455c87c 103 struct string_list *list = (struct string_list *)cbdata;
78a395d3 104 string_list_insert(list, refname);
ed9f7c95
JH
105 return 0;
106}
107
7907fb0c
PS
108struct exclude_existing_options {
109 /*
110 * We need an explicit `enabled` field because it is perfectly valid
111 * for `pattern` to be `NULL` even if `--exclude-existing` was given.
112 */
113 int enabled;
114 const char *pattern;
115};
116
ed9f7c95
JH
117/*
118 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
119 * and
120 * (1) strip "^{}" at the end of line if any;
121 * (2) ignore if match is provided and does not head-match refname;
122 * (3) warn if refname is not a well-formed refname and skip;
123 * (4) ignore if refname is a ref that exists in the local repository;
124 * (5) otherwise output the line.
125 */
7907fb0c 126static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
ed9f7c95 127{
dbabd0b0 128 struct string_list existing_refs = STRING_LIST_INIT_DUP;
ed9f7c95 129 char buf[1024];
7907fb0c 130 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
ed9f7c95 131
2e5c4758
PS
132 refs_for_each_ref(get_main_ref_store(the_repository), add_existing,
133 &existing_refs);
ed9f7c95 134 while (fgets(buf, sizeof(buf), stdin)) {
ed9f7c95 135 char *ref;
d8285af4
JH
136 int len = strlen(buf);
137
ed9f7c95
JH
138 if (len > 0 && buf[len - 1] == '\n')
139 buf[--len] = '\0';
d8285af4 140 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
ed9f7c95
JH
141 len -= 3;
142 buf[len] = '\0';
143 }
144 for (ref = buf + len; buf < ref; ref--)
145 if (isspace(ref[-1]))
146 break;
7907fb0c 147 if (opts->pattern) {
ed9f7c95 148 int reflen = buf + len - ref;
7907fb0c 149 if (reflen < patternlen)
ed9f7c95 150 continue;
7907fb0c 151 if (strncmp(ref, opts->pattern, patternlen))
ed9f7c95
JH
152 continue;
153 }
8d9c5010 154 if (check_refname_format(ref, 0)) {
5620e77e 155 warning("ref '%s' ignored", ref);
ed9f7c95
JH
156 continue;
157 }
c455c87c 158 if (!string_list_has_string(&existing_refs, ref)) {
ed9f7c95
JH
159 printf("%s\n", buf);
160 }
161 }
dbabd0b0
PS
162
163 string_list_clear(&existing_refs, 0);
ed9f7c95
JH
164 return 0;
165}
166
b0f0be93
PS
167static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
168 const char **refs)
b14cbae2
PS
169{
170 if (!refs || !*refs)
171 die("--verify requires a reference");
172
173 while (*refs) {
174 struct object_id oid;
175
1dbe4015 176 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
2e5c4758 177 !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
b0f0be93 178 show_one(show_one_opts, *refs, &oid);
b14cbae2 179 }
b0f0be93 180 else if (!show_one_opts->quiet)
b14cbae2
PS
181 die("'%s' - not a valid ref", *refs);
182 else
183 return 1;
184 refs++;
185 }
186
187 return 0;
188}
189
ee26f1e2
PS
190struct patterns_options {
191 int show_head;
192 int heads_only;
193 int tags_only;
194};
195
196static int cmd_show_ref__patterns(const struct patterns_options *opts,
197 const struct show_one_options *show_one_opts,
b0f0be93 198 const char **patterns)
b14cbae2 199{
b0f0be93
PS
200 struct show_ref_data show_ref_data = {
201 .show_one_opts = show_one_opts,
ee26f1e2 202 .show_head = opts->show_head,
b0f0be93 203 };
b14cbae2
PS
204
205 if (patterns && *patterns)
206 show_ref_data.patterns = patterns;
207
ee26f1e2 208 if (opts->show_head)
2e5c4758
PS
209 refs_head_ref(get_main_ref_store(the_repository), show_ref,
210 &show_ref_data);
ee26f1e2
PS
211 if (opts->heads_only || opts->tags_only) {
212 if (opts->heads_only)
2e5c4758
PS
213 refs_for_each_fullref_in(get_main_ref_store(the_repository),
214 "refs/heads/", NULL,
215 show_ref, &show_ref_data);
ee26f1e2 216 if (opts->tags_only)
2e5c4758
PS
217 refs_for_each_fullref_in(get_main_ref_store(the_repository),
218 "refs/tags/", NULL, show_ref,
219 &show_ref_data);
b14cbae2 220 } else {
2e5c4758
PS
221 refs_for_each_ref(get_main_ref_store(the_repository),
222 show_ref, &show_ref_data);
b14cbae2 223 }
84650989 224 if (!show_ref_data.found_match)
b14cbae2 225 return 1;
b14cbae2
PS
226
227 return 0;
228}
229
9080a7f1
PS
230static int cmd_show_ref__exists(const char **refs)
231{
232 struct strbuf unused_referent = STRBUF_INIT;
233 struct object_id unused_oid;
234 unsigned int unused_type;
235 int failure_errno = 0;
236 const char *ref;
237 int ret = 0;
238
239 if (!refs || !*refs)
240 die("--exists requires a reference");
241 ref = *refs++;
242 if (*refs)
243 die("--exists requires exactly one reference");
244
245 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
246 &unused_oid, &unused_referent, &unused_type,
247 &failure_errno)) {
0aabeaa5 248 if (failure_errno == ENOENT || failure_errno == EISDIR) {
9080a7f1
PS
249 error(_("reference does not exist"));
250 ret = 2;
251 } else {
252 errno = failure_errno;
253 error_errno(_("failed to look up reference"));
254 ret = 1;
255 }
256
257 goto out;
258 }
259
260out:
261 strbuf_release(&unused_referent);
262 return ret;
263}
264
69932bc6
SB
265static int hash_callback(const struct option *opt, const char *arg, int unset)
266{
b0f0be93
PS
267 struct show_one_options *opts = opt->value;
268 struct option abbrev_opt = *opt;
269
270 opts->hash_only = 1;
69932bc6
SB
271 /* Use full length SHA1 if no argument */
272 if (!arg)
273 return 0;
b0f0be93
PS
274
275 abbrev_opt.value = &opts->abbrev;
276 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
69932bc6
SB
277}
278
279static int exclude_existing_callback(const struct option *opt, const char *arg,
280 int unset)
281{
7907fb0c 282 struct exclude_existing_options *opts = opt->value;
517fe807 283 BUG_ON_OPT_NEG(unset);
7907fb0c
PS
284 opts->enabled = 1;
285 opts->pattern = arg;
69932bc6
SB
286 return 0;
287}
288
358ddb62
LT
289int cmd_show_ref(int argc, const char **argv, const char *prefix)
290{
7907fb0c 291 struct exclude_existing_options exclude_existing_opts = {0};
ee26f1e2 292 struct patterns_options patterns_opts = {0};
b0f0be93 293 struct show_one_options show_one_opts = {0};
9080a7f1 294 int verify = 0, exists = 0;
7907fb0c 295 const struct option show_ref_options[] = {
ee26f1e2
PS
296 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")),
297 OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")),
9080a7f1 298 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
7907fb0c
PS
299 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
300 "requires exact ref path")),
ee26f1e2 301 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
7907fb0c 302 N_("show the HEAD reference, even if it would be filtered out")),
ee26f1e2 303 OPT_BOOL(0, "head", &patterns_opts.show_head,
7907fb0c 304 N_("show the HEAD reference, even if it would be filtered out")),
b0f0be93 305 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
7907fb0c 306 N_("dereference tags into object IDs")),
b0f0be93 307 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
7907fb0c
PS
308 N_("only show SHA1 hash using <n> digits"),
309 PARSE_OPT_OPTARG, &hash_callback),
b0f0be93
PS
310 OPT__ABBREV(&show_one_opts.abbrev),
311 OPT__QUIET(&show_one_opts.quiet,
7907fb0c
PS
312 N_("do not print results to stdout (useful with --verify)")),
313 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
314 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
315 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
316 OPT_END()
317 };
318
8e712ef6
EN
319 git_config(git_default_config, NULL);
320
69932bc6 321 argc = parse_options(argc, argv, prefix, show_ref_options,
42fdf86e 322 show_ref_usage, 0);
358ddb62 323
73824973
RS
324 die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing",
325 verify, "--verify",
326 exists, "--exists");
199970e7 327
7907fb0c
PS
328 if (exclude_existing_opts.enabled)
329 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
b14cbae2 330 else if (verify)
b0f0be93 331 return cmd_show_ref__verify(&show_one_opts, argv);
9080a7f1
PS
332 else if (exists)
333 return cmd_show_ref__exists(argv);
b14cbae2 334 else
ee26f1e2 335 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
358ddb62 336}