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