add: use advise_if_enabled for ADVICE_ADD_EMBEDDED_REPO
[alt-git.git] / builtin / show-ref.c
blob1c15421e6008e80b1983c168592998f17667bf58
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 "string-list.h"
10 #include "parse-options.h"
12 static const char * const show_ref_usage[] = {
13 N_("git show-ref [--head] [-d | --dereference]\n"
14 " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags]\n"
15 " [--heads] [--] [<pattern>...]"),
16 N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n"
17 " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n"
18 " [--] [<ref>...]"),
19 N_("git show-ref --exclude-existing[=<pattern>]"),
20 N_("git show-ref --exists <ref>"),
21 NULL
24 struct show_one_options {
25 int quiet;
26 int hash_only;
27 int abbrev;
28 int deref_tags;
31 static void show_one(const struct show_one_options *opts,
32 const char *refname, const struct object_id *oid)
34 const char *hex;
35 struct object_id peeled;
37 if (!repo_has_object_file(the_repository, oid))
38 die("git show-ref: bad ref %s (%s)", refname,
39 oid_to_hex(oid));
41 if (opts->quiet)
42 return;
44 hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
45 if (opts->hash_only)
46 printf("%s\n", hex);
47 else
48 printf("%s %s\n", hex, refname);
50 if (!opts->deref_tags)
51 return;
53 if (!peel_iterated_oid(oid, &peeled)) {
54 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
55 printf("%s %s^{}\n", hex, refname);
59 struct show_ref_data {
60 const struct show_one_options *show_one_opts;
61 const char **patterns;
62 int found_match;
63 int show_head;
66 static int show_ref(const char *refname, const struct object_id *oid,
67 int flag UNUSED, void *cbdata)
69 struct show_ref_data *data = cbdata;
71 if (data->show_head && !strcmp(refname, "HEAD"))
72 goto match;
74 if (data->patterns) {
75 int reflen = strlen(refname);
76 const char **p = data->patterns, *m;
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;
85 if (refname[reflen - len - 1] == '/')
86 goto match;
88 return 0;
91 match:
92 data->found_match++;
94 show_one(data->show_one_opts, refname, oid);
96 return 0;
99 static int add_existing(const char *refname,
100 const struct object_id *oid UNUSED,
101 int flag UNUSED, void *cbdata)
103 struct string_list *list = (struct string_list *)cbdata;
104 string_list_insert(list, refname);
105 return 0;
108 struct exclude_existing_options {
110 * We need an explicit `enabled` field because it is perfectly valid
111 * for `pattern` to be `NULL` even if `--exclude-existing` was given.
113 int enabled;
114 const char *pattern;
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.
126 static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
128 struct string_list existing_refs = STRING_LIST_INIT_DUP;
129 char buf[1024];
130 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
132 for_each_ref(add_existing, &existing_refs);
133 while (fgets(buf, sizeof(buf), stdin)) {
134 char *ref;
135 int len = strlen(buf);
137 if (len > 0 && buf[len - 1] == '\n')
138 buf[--len] = '\0';
139 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
140 len -= 3;
141 buf[len] = '\0';
143 for (ref = buf + len; buf < ref; ref--)
144 if (isspace(ref[-1]))
145 break;
146 if (opts->pattern) {
147 int reflen = buf + len - ref;
148 if (reflen < patternlen)
149 continue;
150 if (strncmp(ref, opts->pattern, patternlen))
151 continue;
153 if (check_refname_format(ref, 0)) {
154 warning("ref '%s' ignored", ref);
155 continue;
157 if (!string_list_has_string(&existing_refs, ref)) {
158 printf("%s\n", buf);
162 string_list_clear(&existing_refs, 0);
163 return 0;
166 static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
167 const char **refs)
169 if (!refs || !*refs)
170 die("--verify requires a reference");
172 while (*refs) {
173 struct object_id oid;
175 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
176 !read_ref(*refs, &oid)) {
177 show_one(show_one_opts, *refs, &oid);
179 else if (!show_one_opts->quiet)
180 die("'%s' - not a valid ref", *refs);
181 else
182 return 1;
183 refs++;
186 return 0;
189 struct patterns_options {
190 int show_head;
191 int heads_only;
192 int tags_only;
195 static int cmd_show_ref__patterns(const struct patterns_options *opts,
196 const struct show_one_options *show_one_opts,
197 const char **patterns)
199 struct show_ref_data show_ref_data = {
200 .show_one_opts = show_one_opts,
201 .show_head = opts->show_head,
204 if (patterns && *patterns)
205 show_ref_data.patterns = patterns;
207 if (opts->show_head)
208 head_ref(show_ref, &show_ref_data);
209 if (opts->heads_only || opts->tags_only) {
210 if (opts->heads_only)
211 for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
212 if (opts->tags_only)
213 for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
214 } else {
215 for_each_ref(show_ref, &show_ref_data);
217 if (!show_ref_data.found_match)
218 return 1;
220 return 0;
223 static int cmd_show_ref__exists(const char **refs)
225 struct strbuf unused_referent = STRBUF_INIT;
226 struct object_id unused_oid;
227 unsigned int unused_type;
228 int failure_errno = 0;
229 const char *ref;
230 int ret = 0;
232 if (!refs || !*refs)
233 die("--exists requires a reference");
234 ref = *refs++;
235 if (*refs)
236 die("--exists requires exactly one reference");
238 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
239 &unused_oid, &unused_referent, &unused_type,
240 &failure_errno)) {
241 if (failure_errno == ENOENT || failure_errno == EISDIR) {
242 error(_("reference does not exist"));
243 ret = 2;
244 } else {
245 errno = failure_errno;
246 error_errno(_("failed to look up reference"));
247 ret = 1;
250 goto out;
253 out:
254 strbuf_release(&unused_referent);
255 return ret;
258 static int hash_callback(const struct option *opt, const char *arg, int unset)
260 struct show_one_options *opts = opt->value;
261 struct option abbrev_opt = *opt;
263 opts->hash_only = 1;
264 /* Use full length SHA1 if no argument */
265 if (!arg)
266 return 0;
268 abbrev_opt.value = &opts->abbrev;
269 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
272 static int exclude_existing_callback(const struct option *opt, const char *arg,
273 int unset)
275 struct exclude_existing_options *opts = opt->value;
276 BUG_ON_OPT_NEG(unset);
277 opts->enabled = 1;
278 opts->pattern = arg;
279 return 0;
282 int cmd_show_ref(int argc, const char **argv, const char *prefix)
284 struct exclude_existing_options exclude_existing_opts = {0};
285 struct patterns_options patterns_opts = {0};
286 struct show_one_options show_one_opts = {0};
287 int verify = 0, exists = 0;
288 const struct option show_ref_options[] = {
289 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")),
290 OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")),
291 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
292 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
293 "requires exact ref path")),
294 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
295 N_("show the HEAD reference, even if it would be filtered out")),
296 OPT_BOOL(0, "head", &patterns_opts.show_head,
297 N_("show the HEAD reference, even if it would be filtered out")),
298 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
299 N_("dereference tags into object IDs")),
300 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
301 N_("only show SHA1 hash using <n> digits"),
302 PARSE_OPT_OPTARG, &hash_callback),
303 OPT__ABBREV(&show_one_opts.abbrev),
304 OPT__QUIET(&show_one_opts.quiet,
305 N_("do not print results to stdout (useful with --verify)")),
306 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
307 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
308 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
309 OPT_END()
312 git_config(git_default_config, NULL);
314 argc = parse_options(argc, argv, prefix, show_ref_options,
315 show_ref_usage, 0);
317 die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing",
318 verify, "--verify",
319 exists, "--exists");
321 if (exclude_existing_opts.enabled)
322 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
323 else if (verify)
324 return cmd_show_ref__verify(&show_one_opts, argv);
325 else if (exists)
326 return cmd_show_ref__exists(argv);
327 else
328 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);