The eleventh batch
[git/gitster.git] / builtin / show-ref.c
blob285cd3e43382cb94a5c40ef7f44715ecfc5f4adf
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "refs/refs-internal.h"
7 #include "object-name.h"
8 #include "object-store-ll.h"
9 #include "object.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>]] [--branches] [--tags]\n"
16 " [--] [<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(the_repository, 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 char *referent UNUSED, 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 char *referent UNUSED,
102 const struct object_id *oid UNUSED,
103 int flag UNUSED, void *cbdata)
105 struct string_list *list = (struct string_list *)cbdata;
106 string_list_insert(list, refname);
107 return 0;
110 struct exclude_existing_options {
112 * We need an explicit `enabled` field because it is perfectly valid
113 * for `pattern` to be `NULL` even if `--exclude-existing` was given.
115 int enabled;
116 const char *pattern;
120 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
121 * and
122 * (1) strip "^{}" at the end of line if any;
123 * (2) ignore if match is provided and does not head-match refname;
124 * (3) warn if refname is not a well-formed refname and skip;
125 * (4) ignore if refname is a ref that exists in the local repository;
126 * (5) otherwise output the line.
128 static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
130 struct string_list existing_refs = STRING_LIST_INIT_DUP;
131 char buf[1024];
132 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
134 refs_for_each_ref(get_main_ref_store(the_repository), add_existing,
135 &existing_refs);
136 while (fgets(buf, sizeof(buf), stdin)) {
137 char *ref;
138 int len = strlen(buf);
140 if (len > 0 && buf[len - 1] == '\n')
141 buf[--len] = '\0';
142 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
143 len -= 3;
144 buf[len] = '\0';
146 for (ref = buf + len; buf < ref; ref--)
147 if (isspace(ref[-1]))
148 break;
149 if (opts->pattern) {
150 int reflen = buf + len - ref;
151 if (reflen < patternlen)
152 continue;
153 if (strncmp(ref, opts->pattern, patternlen))
154 continue;
156 if (check_refname_format(ref, 0)) {
157 warning("ref '%s' ignored", ref);
158 continue;
160 if (!string_list_has_string(&existing_refs, ref)) {
161 printf("%s\n", buf);
165 string_list_clear(&existing_refs, 0);
166 return 0;
169 static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
170 const char **refs)
172 if (!refs || !*refs)
173 die("--verify requires a reference");
175 while (*refs) {
176 struct object_id oid;
178 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
179 !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
180 show_one(show_one_opts, *refs, &oid);
182 else if (!show_one_opts->quiet)
183 die("'%s' - not a valid ref", *refs);
184 else
185 return 1;
186 refs++;
189 return 0;
192 struct patterns_options {
193 int show_head;
194 int branches_only;
195 int tags_only;
198 static int cmd_show_ref__patterns(const struct patterns_options *opts,
199 const struct show_one_options *show_one_opts,
200 const char **patterns)
202 struct show_ref_data show_ref_data = {
203 .show_one_opts = show_one_opts,
204 .show_head = opts->show_head,
207 if (patterns && *patterns)
208 show_ref_data.patterns = patterns;
210 if (opts->show_head)
211 refs_head_ref(get_main_ref_store(the_repository), show_ref,
212 &show_ref_data);
213 if (opts->branches_only || opts->tags_only) {
214 if (opts->branches_only)
215 refs_for_each_fullref_in(get_main_ref_store(the_repository),
216 "refs/heads/", NULL,
217 show_ref, &show_ref_data);
218 if (opts->tags_only)
219 refs_for_each_fullref_in(get_main_ref_store(the_repository),
220 "refs/tags/", NULL, show_ref,
221 &show_ref_data);
222 } else {
223 refs_for_each_ref(get_main_ref_store(the_repository),
224 show_ref, &show_ref_data);
226 if (!show_ref_data.found_match)
227 return 1;
229 return 0;
232 static int cmd_show_ref__exists(const char **refs)
234 struct strbuf unused_referent = STRBUF_INIT;
235 struct object_id unused_oid;
236 unsigned int unused_type;
237 int failure_errno = 0;
238 const char *ref;
239 int ret = 0;
241 if (!refs || !*refs)
242 die("--exists requires a reference");
243 ref = *refs++;
244 if (*refs)
245 die("--exists requires exactly one reference");
247 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
248 &unused_oid, &unused_referent, &unused_type,
249 &failure_errno)) {
250 if (failure_errno == ENOENT || failure_errno == EISDIR) {
251 error(_("reference does not exist"));
252 ret = 2;
253 } else {
254 errno = failure_errno;
255 error_errno(_("failed to look up reference"));
256 ret = 1;
259 goto out;
262 out:
263 strbuf_release(&unused_referent);
264 return ret;
267 static int hash_callback(const struct option *opt, const char *arg, int unset)
269 struct show_one_options *opts = opt->value;
270 struct option abbrev_opt = *opt;
272 opts->hash_only = 1;
273 /* Use full length SHA1 if no argument */
274 if (!arg)
275 return 0;
277 abbrev_opt.value = &opts->abbrev;
278 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
281 static int exclude_existing_callback(const struct option *opt, const char *arg,
282 int unset)
284 struct exclude_existing_options *opts = opt->value;
285 BUG_ON_OPT_NEG(unset);
286 opts->enabled = 1;
287 opts->pattern = arg;
288 return 0;
291 int cmd_show_ref(int argc,
292 const char **argv,
293 const char *prefix,
294 struct repository *repo UNUSED)
296 struct exclude_existing_options exclude_existing_opts = {0};
297 struct patterns_options patterns_opts = {0};
298 struct show_one_options show_one_opts = {0};
299 int verify = 0, exists = 0;
300 const struct option show_ref_options[] = {
301 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with --branches)")),
302 OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with --tags)")),
303 OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only,
304 N_("deprecated synonym for --branches")),
305 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")),
306 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
307 "requires exact ref path")),
308 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
309 N_("show the HEAD reference, even if it would be filtered out")),
310 OPT_BOOL(0, "head", &patterns_opts.show_head,
311 N_("show the HEAD reference, even if it would be filtered out")),
312 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
313 N_("dereference tags into object IDs")),
314 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
315 N_("only show SHA1 hash using <n> digits"),
316 PARSE_OPT_OPTARG, &hash_callback),
317 OPT__ABBREV(&show_one_opts.abbrev),
318 OPT__QUIET(&show_one_opts.quiet,
319 N_("do not print results to stdout (useful with --verify)")),
320 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
321 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
322 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
323 OPT_END()
326 git_config(git_default_config, NULL);
328 argc = parse_options(argc, argv, prefix, show_ref_options,
329 show_ref_usage, 0);
331 die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing",
332 verify, "--verify",
333 exists, "--exists");
335 if (exclude_existing_opts.enabled)
336 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
337 else if (verify)
338 return cmd_show_ref__verify(&show_one_opts, argv);
339 else if (exists)
340 return cmd_show_ref__exists(argv);
341 else
342 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);