reftable/stack: fix stale lock when dying
[git.git] / builtin / show-ref.c
blob7aac525a878b3b3ae4959a831de4747d379e140f
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "refs/refs-internal.h"
6 #include "object-name.h"
7 #include "object-store-ll.h"
8 #include "object.h"
9 #include "tag.h"
10 #include "string-list.h"
11 #include "parse-options.h"
13 static const char * const show_ref_usage[] = {
14 N_("git show-ref [--head] [-d | --dereference]\n"
15 " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags]\n"
16 " [--heads] [--] [<pattern>...]"),
17 N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n"
18 " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n"
19 " [--] [<ref>...]"),
20 N_("git show-ref --exclude-existing[=<pattern>]"),
21 N_("git show-ref --exists <ref>"),
22 NULL
25 struct show_one_options {
26 int quiet;
27 int hash_only;
28 int abbrev;
29 int deref_tags;
32 static void show_one(const struct show_one_options *opts,
33 const char *refname, const struct object_id *oid)
35 const char *hex;
36 struct object_id peeled;
38 if (!repo_has_object_file(the_repository, oid))
39 die("git show-ref: bad ref %s (%s)", refname,
40 oid_to_hex(oid));
42 if (opts->quiet)
43 return;
45 hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
46 if (opts->hash_only)
47 printf("%s\n", hex);
48 else
49 printf("%s %s\n", hex, refname);
51 if (!opts->deref_tags)
52 return;
54 if (!peel_iterated_oid(oid, &peeled)) {
55 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
56 printf("%s %s^{}\n", hex, refname);
60 struct show_ref_data {
61 const struct show_one_options *show_one_opts;
62 const char **patterns;
63 int found_match;
64 int show_head;
67 static int show_ref(const char *refname, const struct object_id *oid,
68 int flag UNUSED, void *cbdata)
70 struct show_ref_data *data = cbdata;
72 if (data->show_head && !strcmp(refname, "HEAD"))
73 goto match;
75 if (data->patterns) {
76 int reflen = strlen(refname);
77 const char **p = data->patterns, *m;
78 while ((m = *p++) != NULL) {
79 int len = strlen(m);
80 if (len > reflen)
81 continue;
82 if (memcmp(m, refname + reflen - len, len))
83 continue;
84 if (len == reflen)
85 goto match;
86 if (refname[reflen - len - 1] == '/')
87 goto match;
89 return 0;
92 match:
93 data->found_match++;
95 show_one(data->show_one_opts, refname, oid);
97 return 0;
100 static int add_existing(const char *refname,
101 const struct object_id *oid UNUSED,
102 int flag UNUSED, void *cbdata)
104 struct string_list *list = (struct string_list *)cbdata;
105 string_list_insert(list, refname);
106 return 0;
109 struct exclude_existing_options {
111 * We need an explicit `enabled` field because it is perfectly valid
112 * for `pattern` to be `NULL` even if `--exclude-existing` was given.
114 int enabled;
115 const char *pattern;
119 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
120 * and
121 * (1) strip "^{}" at the end of line if any;
122 * (2) ignore if match is provided and does not head-match refname;
123 * (3) warn if refname is not a well-formed refname and skip;
124 * (4) ignore if refname is a ref that exists in the local repository;
125 * (5) otherwise output the line.
127 static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
129 struct string_list existing_refs = STRING_LIST_INIT_DUP;
130 char buf[1024];
131 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
133 for_each_ref(add_existing, &existing_refs);
134 while (fgets(buf, sizeof(buf), stdin)) {
135 char *ref;
136 int len = strlen(buf);
138 if (len > 0 && buf[len - 1] == '\n')
139 buf[--len] = '\0';
140 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
141 len -= 3;
142 buf[len] = '\0';
144 for (ref = buf + len; buf < ref; ref--)
145 if (isspace(ref[-1]))
146 break;
147 if (opts->pattern) {
148 int reflen = buf + len - ref;
149 if (reflen < patternlen)
150 continue;
151 if (strncmp(ref, opts->pattern, patternlen))
152 continue;
154 if (check_refname_format(ref, 0)) {
155 warning("ref '%s' ignored", ref);
156 continue;
158 if (!string_list_has_string(&existing_refs, ref)) {
159 printf("%s\n", buf);
163 string_list_clear(&existing_refs, 0);
164 return 0;
167 static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
168 const char **refs)
170 if (!refs || !*refs)
171 die("--verify requires a reference");
173 while (*refs) {
174 struct object_id oid;
176 if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
177 !read_ref(*refs, &oid)) {
178 show_one(show_one_opts, *refs, &oid);
180 else if (!show_one_opts->quiet)
181 die("'%s' - not a valid ref", *refs);
182 else
183 return 1;
184 refs++;
187 return 0;
190 struct patterns_options {
191 int show_head;
192 int heads_only;
193 int tags_only;
196 static int cmd_show_ref__patterns(const struct patterns_options *opts,
197 const struct show_one_options *show_one_opts,
198 const char **patterns)
200 struct show_ref_data show_ref_data = {
201 .show_one_opts = show_one_opts,
202 .show_head = opts->show_head,
205 if (patterns && *patterns)
206 show_ref_data.patterns = patterns;
208 if (opts->show_head)
209 head_ref(show_ref, &show_ref_data);
210 if (opts->heads_only || opts->tags_only) {
211 if (opts->heads_only)
212 for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
213 if (opts->tags_only)
214 for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
215 } else {
216 for_each_ref(show_ref, &show_ref_data);
218 if (!show_ref_data.found_match)
219 return 1;
221 return 0;
224 static int cmd_show_ref__exists(const char **refs)
226 struct strbuf unused_referent = STRBUF_INIT;
227 struct object_id unused_oid;
228 unsigned int unused_type;
229 int failure_errno = 0;
230 const char *ref;
231 int ret = 0;
233 if (!refs || !*refs)
234 die("--exists requires a reference");
235 ref = *refs++;
236 if (*refs)
237 die("--exists requires exactly one reference");
239 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
240 &unused_oid, &unused_referent, &unused_type,
241 &failure_errno)) {
242 if (failure_errno == ENOENT) {
243 error(_("reference does not exist"));
244 ret = 2;
245 } else {
246 errno = failure_errno;
247 error_errno(_("failed to look up reference"));
248 ret = 1;
251 goto out;
254 out:
255 strbuf_release(&unused_referent);
256 return ret;
259 static int hash_callback(const struct option *opt, const char *arg, int unset)
261 struct show_one_options *opts = opt->value;
262 struct option abbrev_opt = *opt;
264 opts->hash_only = 1;
265 /* Use full length SHA1 if no argument */
266 if (!arg)
267 return 0;
269 abbrev_opt.value = &opts->abbrev;
270 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
273 static int exclude_existing_callback(const struct option *opt, const char *arg,
274 int unset)
276 struct exclude_existing_options *opts = opt->value;
277 BUG_ON_OPT_NEG(unset);
278 opts->enabled = 1;
279 opts->pattern = arg;
280 return 0;
283 int cmd_show_ref(int argc, const char **argv, const char *prefix)
285 struct exclude_existing_options exclude_existing_opts = {0};
286 struct patterns_options patterns_opts = {0};
287 struct show_one_options show_one_opts = {0};
288 int verify = 0, exists = 0;
289 const struct option show_ref_options[] = {
290 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")),
291 OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")),
292 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
293 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
294 "requires exact ref path")),
295 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
296 N_("show the HEAD reference, even if it would be filtered out")),
297 OPT_BOOL(0, "head", &patterns_opts.show_head,
298 N_("show the HEAD reference, even if it would be filtered out")),
299 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
300 N_("dereference tags into object IDs")),
301 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
302 N_("only show SHA1 hash using <n> digits"),
303 PARSE_OPT_OPTARG, &hash_callback),
304 OPT__ABBREV(&show_one_opts.abbrev),
305 OPT__QUIET(&show_one_opts.quiet,
306 N_("do not print results to stdout (useful with --verify)")),
307 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
308 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
309 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
310 OPT_END()
313 git_config(git_default_config, NULL);
315 argc = parse_options(argc, argv, prefix, show_ref_options,
316 show_ref_usage, 0);
318 if ((!!exclude_existing_opts.enabled + !!verify + !!exists) > 1)
319 die(_("only one of '%s', '%s' or '%s' can be given"),
320 "--exclude-existing", "--verify", "--exists");
322 if (exclude_existing_opts.enabled)
323 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
324 else if (verify)
325 return cmd_show_ref__verify(&show_one_opts, argv);
326 else if (exists)
327 return cmd_show_ref__exists(argv);
328 else
329 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);