blob: 839a5c29f31439e104ef99daf3460709e3e45909 [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Elijah Newren8e712ef2019-04-25 07:58:54 -07002#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00004#include "hex.h"
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +01005#include "refs/refs-internal.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07006#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +00007#include "object-store-ll.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -07008#include "object.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01009#include "string-list.h"
Stephen Boyd69932bc2009-06-20 21:40:46 -070010#include "parse-options.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -070011
Stephen Boyd69932bc2009-06-20 21:40:46 -070012static const char * const show_ref_usage[] = {
Patrick Steinhardt1307d5e2023-10-31 09:16:50 +010013 N_("git show-ref [--head] [-d | --dereference]\n"
Junio C Hamano607c3d32024-06-04 15:01:45 -070014 " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--branches] [--tags]\n"
15 " [--] [<pattern>...]"),
Patrick Steinhardt1307d5e2023-10-31 09:16:50 +010016 N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n"
17 " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n"
18 " [--] [<ref>...]"),
Junio C Hamano33e8fc82015-10-16 11:27:42 -070019 N_("git show-ref --exclude-existing[=<pattern>]"),
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +010020 N_("git show-ref --exists <ref>"),
Stephen Boyd69932bc2009-06-20 21:40:46 -070021 NULL
22};
Linus Torvalds358ddb62006-09-15 11:19:32 -070023
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010024struct 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)
Junio C Hamano64fe0312006-12-17 19:27:49 -080033{
Vladimir Panteleevf1627042017-01-23 18:00:56 +000034 const char *hex;
35 struct object_id peeled;
36
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020037 if (!repo_has_object_file(the_repository, oid))
Vladimir Panteleevd01b8202017-01-23 18:00:58 +000038 die("git show-ref: bad ref %s (%s)", refname,
39 oid_to_hex(oid));
40
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010041 if (opts->quiet)
Vladimir Panteleev14144d32017-01-23 18:00:57 +000042 return;
43
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010044 hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
45 if (opts->hash_only)
Junio C Hamano64fe0312006-12-17 19:27:49 -080046 printf("%s\n", hex);
47 else
48 printf("%s %s\n", hex, refname);
Vladimir Panteleevf1627042017-01-23 18:00:56 +000049
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010050 if (!opts->deref_tags)
Vladimir Panteleevf1627042017-01-23 18:00:56 +000051 return;
52
Patrick Steinhardt30aaff42024-05-17 10:19:04 +020053 if (!peel_iterated_oid(the_repository, oid, &peeled)) {
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010054 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
Vladimir Panteleevf1627042017-01-23 18:00:56 +000055 printf("%s %s^{}\n", hex, refname);
56 }
Junio C Hamano64fe0312006-12-17 19:27:49 -080057}
58
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010059struct show_ref_data {
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010060 const struct show_one_options *show_one_opts;
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010061 const char **patterns;
Patrick Steinhardt84650982023-10-31 09:16:33 +010062 int found_match;
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +010063 int show_head;
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010064};
65
Michael Haggertyf0a011f2015-05-25 18:38:51 +000066static int show_ref(const char *refname, const struct object_id *oid,
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010067 int flag UNUSED, void *cbdata)
Linus Torvalds358ddb62006-09-15 11:19:32 -070068{
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010069 struct show_ref_data *data = cbdata;
70
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +010071 if (data->show_head && !strcmp(refname, "HEAD"))
Doug Bell3f3d0ce2013-07-16 19:05:14 -050072 goto match;
73
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010074 if (data->patterns) {
Linus Torvalds358ddb62006-09-15 11:19:32 -070075 int reflen = strlen(refname);
Patrick Steinhardtff546eb2023-10-31 09:16:12 +010076 const char **p = data->patterns, *m;
Linus Torvalds358ddb62006-09-15 11:19:32 -070077 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;
Linus Torvalds358ddb62006-09-15 11:19:32 -070085 if (refname[reflen - len - 1] == '/')
86 goto match;
87 }
88 return 0;
89 }
90
91match:
Patrick Steinhardt84650982023-10-31 09:16:33 +010092 data->found_match++;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080093
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +010094 show_one(data->show_one_opts, refname, oid);
Junio C Hamanocf0adba2006-11-19 13:22:44 -080095
Linus Torvalds358ddb62006-09-15 11:19:32 -070096 return 0;
97}
98
Jeff King63e14ee2022-08-19 06:08:32 -040099static int add_existing(const char *refname,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200100 const struct object_id *oid UNUSED,
101 int flag UNUSED, void *cbdata)
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800102{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100103 struct string_list *list = (struct string_list *)cbdata;
Julian Phillips78a395d2010-06-26 00:41:35 +0100104 string_list_insert(list, refname);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800105 return 0;
106}
107
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100108struct 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
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800117/*
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 */
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100126static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800127{
Patrick Steinhardtdbabd0b2023-10-31 09:16:21 +0100128 struct string_list existing_refs = STRING_LIST_INIT_DUP;
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800129 char buf[1024];
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100130 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800131
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200132 refs_for_each_ref(get_main_ref_store(the_repository), add_existing,
133 &existing_refs);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800134 while (fgets(buf, sizeof(buf), stdin)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800135 char *ref;
Junio C Hamanod8285af2006-12-18 13:33:47 -0800136 int len = strlen(buf);
137
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800138 if (len > 0 && buf[len - 1] == '\n')
139 buf[--len] = '\0';
Junio C Hamanod8285af2006-12-18 13:33:47 -0800140 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800141 len -= 3;
142 buf[len] = '\0';
143 }
144 for (ref = buf + len; buf < ref; ref--)
145 if (isspace(ref[-1]))
146 break;
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100147 if (opts->pattern) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800148 int reflen = buf + len - ref;
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100149 if (reflen < patternlen)
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800150 continue;
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100151 if (strncmp(ref, opts->pattern, patternlen))
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800152 continue;
153 }
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200154 if (check_refname_format(ref, 0)) {
Miklos Vajna5620e772009-03-24 02:09:16 +0100155 warning("ref '%s' ignored", ref);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800156 continue;
157 }
Johannes Schindelinc455c872008-07-21 19:03:49 +0100158 if (!string_list_has_string(&existing_refs, ref)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800159 printf("%s\n", buf);
160 }
161 }
Patrick Steinhardtdbabd0b2023-10-31 09:16:21 +0100162
163 string_list_clear(&existing_refs, 0);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800164 return 0;
165}
166
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100167static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
168 const char **refs)
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100169{
170 if (!refs || !*refs)
171 die("--verify requires a reference");
172
173 while (*refs) {
174 struct object_id oid;
175
Phillip Wood1dbe4012024-02-07 16:44:35 +0000176 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200177 !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100178 show_one(show_one_opts, *refs, &oid);
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100179 }
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100180 else if (!show_one_opts->quiet)
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100181 die("'%s' - not a valid ref", *refs);
182 else
183 return 1;
184 refs++;
185 }
186
187 return 0;
188}
189
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100190struct patterns_options {
191 int show_head;
Junio C Hamano607c3d32024-06-04 15:01:45 -0700192 int branches_only;
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100193 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,
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100198 const char **patterns)
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100199{
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100200 struct show_ref_data show_ref_data = {
201 .show_one_opts = show_one_opts,
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100202 .show_head = opts->show_head,
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100203 };
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100204
205 if (patterns && *patterns)
206 show_ref_data.patterns = patterns;
207
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100208 if (opts->show_head)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200209 refs_head_ref(get_main_ref_store(the_repository), show_ref,
210 &show_ref_data);
Junio C Hamano607c3d32024-06-04 15:01:45 -0700211 if (opts->branches_only || opts->tags_only) {
212 if (opts->branches_only)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200213 refs_for_each_fullref_in(get_main_ref_store(the_repository),
214 "refs/heads/", NULL,
215 show_ref, &show_ref_data);
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100216 if (opts->tags_only)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200217 refs_for_each_fullref_in(get_main_ref_store(the_repository),
218 "refs/tags/", NULL, show_ref,
219 &show_ref_data);
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100220 } else {
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200221 refs_for_each_ref(get_main_ref_store(the_repository),
222 show_ref, &show_ref_data);
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100223 }
Patrick Steinhardt84650982023-10-31 09:16:33 +0100224 if (!show_ref_data.found_match)
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100225 return 1;
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100226
227 return 0;
228}
229
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +0100230static 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)) {
Toon Claes0aabeaa2024-01-10 15:15:59 +0100248 if (failure_errno == ENOENT || failure_errno == EISDIR) {
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +0100249 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
Stephen Boyd69932bc2009-06-20 21:40:46 -0700265static int hash_callback(const struct option *opt, const char *arg, int unset)
266{
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100267 struct show_one_options *opts = opt->value;
268 struct option abbrev_opt = *opt;
269
270 opts->hash_only = 1;
Stephen Boyd69932bc2009-06-20 21:40:46 -0700271 /* Use full length SHA1 if no argument */
272 if (!arg)
273 return 0;
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100274
275 abbrev_opt.value = &opts->abbrev;
276 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
Stephen Boyd69932bc2009-06-20 21:40:46 -0700277}
278
279static int exclude_existing_callback(const struct option *opt, const char *arg,
280 int unset)
281{
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100282 struct exclude_existing_options *opts = opt->value;
Jeff King517fe802018-11-05 01:45:42 -0500283 BUG_ON_OPT_NEG(unset);
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100284 opts->enabled = 1;
285 opts->pattern = arg;
Stephen Boyd69932bc2009-06-20 21:40:46 -0700286 return 0;
287}
288
Linus Torvalds358ddb62006-09-15 11:19:32 -0700289int cmd_show_ref(int argc, const char **argv, const char *prefix)
290{
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100291 struct exclude_existing_options exclude_existing_opts = {0};
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100292 struct patterns_options patterns_opts = {0};
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100293 struct show_one_options show_one_opts = {0};
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +0100294 int verify = 0, exists = 0;
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100295 const struct option show_ref_options[] = {
Junio C Hamano607c3d32024-06-04 15:01:45 -0700296 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with branches)")),
297 OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with tags)")),
298 OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only,
299 N_("deprecated synonym for --branches")),
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +0100300 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100301 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
302 "requires exact ref path")),
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100303 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100304 N_("show the HEAD reference, even if it would be filtered out")),
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100305 OPT_BOOL(0, "head", &patterns_opts.show_head,
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100306 N_("show the HEAD reference, even if it would be filtered out")),
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100307 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100308 N_("dereference tags into object IDs")),
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100309 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100310 N_("only show SHA1 hash using <n> digits"),
311 PARSE_OPT_OPTARG, &hash_callback),
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100312 OPT__ABBREV(&show_one_opts.abbrev),
313 OPT__QUIET(&show_one_opts.quiet,
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100314 N_("do not print results to stdout (useful with --verify)")),
315 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
316 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
317 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
318 OPT_END()
319 };
320
Elijah Newren8e712ef2019-04-25 07:58:54 -0700321 git_config(git_default_config, NULL);
322
Stephen Boyd69932bc2009-06-20 21:40:46 -0700323 argc = parse_options(argc, argv, prefix, show_ref_options,
René Scharfe42fdf862015-11-17 11:26:05 +0100324 show_ref_usage, 0);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700325
René Scharfe73824972023-12-11 09:09:28 +0100326 die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing",
327 verify, "--verify",
328 exists, "--exists");
Patrick Steinhardt199970e2023-10-31 09:16:46 +0100329
Patrick Steinhardt7907fb02023-10-31 09:16:29 +0100330 if (exclude_existing_opts.enabled)
331 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100332 else if (verify)
Patrick Steinhardtb0f0be92023-10-31 09:16:38 +0100333 return cmd_show_ref__verify(&show_one_opts, argv);
Patrick Steinhardt9080a7f2023-10-31 09:16:54 +0100334 else if (exists)
335 return cmd_show_ref__exists(argv);
Patrick Steinhardtb14cbae2023-10-31 09:16:17 +0100336 else
Patrick Steinhardtee26f1e2023-10-31 09:16:42 +0100337 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700338}