]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/show-ref.c
builtin/show-ref: stop using global vars for `show_one()`
[thirdparty/git.git] / builtin / show-ref.c
CommitLineData
baffc0e7 1#include "builtin.h"
8e712ef6 2#include "config.h"
f394e093 3#include "gettext.h"
41771fa4 4#include "hex.h"
358ddb62 5#include "refs.h"
dabab1d6 6#include "object-name.h"
a034e910 7#include "object-store-ll.h"
358ddb62
LT
8#include "object.h"
9#include "tag.h"
c455c87c 10#include "string-list.h"
69932bc6 11#include "parse-options.h"
358ddb62 12
69932bc6 13static const char * const show_ref_usage[] = {
5af8b61c
ÆAB
14 N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference]\n"
15 " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags]\n"
16 " [--heads] [--] [<pattern>...]"),
33e8fc87 17 N_("git show-ref --exclude-existing[=<pattern>]"),
69932bc6
SB
18 NULL
19};
358ddb62 20
b0f0be93 21static int show_head, tags_only, heads_only, verify;
358ddb62 22
b0f0be93
PS
23struct show_one_options {
24 int quiet;
25 int hash_only;
26 int abbrev;
27 int deref_tags;
28};
29
30static void show_one(const struct show_one_options *opts,
31 const char *refname, const struct object_id *oid)
64fe031a 32{
f1627040
VP
33 const char *hex;
34 struct object_id peeled;
35
bc726bd0 36 if (!repo_has_object_file(the_repository, oid))
d01b8203
VP
37 die("git show-ref: bad ref %s (%s)", refname,
38 oid_to_hex(oid));
39
b0f0be93 40 if (opts->quiet)
14144d3b
VP
41 return;
42
b0f0be93
PS
43 hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
44 if (opts->hash_only)
64fe031a
JH
45 printf("%s\n", hex);
46 else
47 printf("%s %s\n", hex, refname);
f1627040 48
b0f0be93 49 if (!opts->deref_tags)
f1627040
VP
50 return;
51
36a31792 52 if (!peel_iterated_oid(oid, &peeled)) {
b0f0be93 53 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
f1627040
VP
54 printf("%s %s^{}\n", hex, refname);
55 }
64fe031a
JH
56}
57
ff546ebb 58struct show_ref_data {
b0f0be93 59 const struct show_one_options *show_one_opts;
ff546ebb 60 const char **patterns;
84650989 61 int found_match;
ff546ebb
PS
62};
63
f0a011fa 64static int show_ref(const char *refname, const struct object_id *oid,
ff546ebb 65 int flag UNUSED, void *cbdata)
358ddb62 66{
ff546ebb
PS
67 struct show_ref_data *data = cbdata;
68
3f3d0cea
DB
69 if (show_head && !strcmp(refname, "HEAD"))
70 goto match;
71
ff546ebb 72 if (data->patterns) {
358ddb62 73 int reflen = strlen(refname);
ff546ebb 74 const char **p = data->patterns, *m;
358ddb62
LT
75 while ((m = *p++) != NULL) {
76 int len = strlen(m);
77 if (len > reflen)
78 continue;
79 if (memcmp(m, refname + reflen - len, len))
80 continue;
81 if (len == reflen)
82 goto match;
358ddb62
LT
83 if (refname[reflen - len - 1] == '/')
84 goto match;
85 }
86 return 0;
87 }
88
89match:
84650989 90 data->found_match++;
cf0adba7 91
b0f0be93 92 show_one(data->show_one_opts, refname, oid);
cf0adba7 93
358ddb62
LT
94 return 0;
95}
96
63e14ee2 97static int add_existing(const char *refname,
5cf88fd8
ÆAB
98 const struct object_id *oid UNUSED,
99 int flag UNUSED, void *cbdata)
ed9f7c95 100{
c455c87c 101 struct string_list *list = (struct string_list *)cbdata;
78a395d3 102 string_list_insert(list, refname);
ed9f7c95
JH
103 return 0;
104}
105
7907fb0c
PS
106struct exclude_existing_options {
107 /*
108 * We need an explicit `enabled` field because it is perfectly valid
109 * for `pattern` to be `NULL` even if `--exclude-existing` was given.
110 */
111 int enabled;
112 const char *pattern;
113};
114
ed9f7c95
JH
115/*
116 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
117 * and
118 * (1) strip "^{}" at the end of line if any;
119 * (2) ignore if match is provided and does not head-match refname;
120 * (3) warn if refname is not a well-formed refname and skip;
121 * (4) ignore if refname is a ref that exists in the local repository;
122 * (5) otherwise output the line.
123 */
7907fb0c 124static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts)
ed9f7c95 125{
dbabd0b0 126 struct string_list existing_refs = STRING_LIST_INIT_DUP;
ed9f7c95 127 char buf[1024];
7907fb0c 128 int patternlen = opts->pattern ? strlen(opts->pattern) : 0;
ed9f7c95 129
f0a011fa 130 for_each_ref(add_existing, &existing_refs);
ed9f7c95 131 while (fgets(buf, sizeof(buf), stdin)) {
ed9f7c95 132 char *ref;
d8285af4
JH
133 int len = strlen(buf);
134
ed9f7c95
JH
135 if (len > 0 && buf[len - 1] == '\n')
136 buf[--len] = '\0';
d8285af4 137 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
ed9f7c95
JH
138 len -= 3;
139 buf[len] = '\0';
140 }
141 for (ref = buf + len; buf < ref; ref--)
142 if (isspace(ref[-1]))
143 break;
7907fb0c 144 if (opts->pattern) {
ed9f7c95 145 int reflen = buf + len - ref;
7907fb0c 146 if (reflen < patternlen)
ed9f7c95 147 continue;
7907fb0c 148 if (strncmp(ref, opts->pattern, patternlen))
ed9f7c95
JH
149 continue;
150 }
8d9c5010 151 if (check_refname_format(ref, 0)) {
5620e77e 152 warning("ref '%s' ignored", ref);
ed9f7c95
JH
153 continue;
154 }
c455c87c 155 if (!string_list_has_string(&existing_refs, ref)) {
ed9f7c95
JH
156 printf("%s\n", buf);
157 }
158 }
dbabd0b0
PS
159
160 string_list_clear(&existing_refs, 0);
ed9f7c95
JH
161 return 0;
162}
163
b0f0be93
PS
164static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
165 const char **refs)
b14cbae2
PS
166{
167 if (!refs || !*refs)
168 die("--verify requires a reference");
169
170 while (*refs) {
171 struct object_id oid;
172
173 if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) &&
174 !read_ref(*refs, &oid)) {
b0f0be93 175 show_one(show_one_opts, *refs, &oid);
b14cbae2 176 }
b0f0be93 177 else if (!show_one_opts->quiet)
b14cbae2
PS
178 die("'%s' - not a valid ref", *refs);
179 else
180 return 1;
181 refs++;
182 }
183
184 return 0;
185}
186
b0f0be93
PS
187static int cmd_show_ref__patterns(const struct show_one_options *show_one_opts,
188 const char **patterns)
b14cbae2 189{
b0f0be93
PS
190 struct show_ref_data show_ref_data = {
191 .show_one_opts = show_one_opts,
192 };
b14cbae2
PS
193
194 if (patterns && *patterns)
195 show_ref_data.patterns = patterns;
196
197 if (show_head)
198 head_ref(show_ref, &show_ref_data);
199 if (heads_only || tags_only) {
200 if (heads_only)
201 for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
202 if (tags_only)
203 for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
204 } else {
205 for_each_ref(show_ref, &show_ref_data);
206 }
84650989 207 if (!show_ref_data.found_match)
b14cbae2 208 return 1;
b14cbae2
PS
209
210 return 0;
211}
212
69932bc6
SB
213static int hash_callback(const struct option *opt, const char *arg, int unset)
214{
b0f0be93
PS
215 struct show_one_options *opts = opt->value;
216 struct option abbrev_opt = *opt;
217
218 opts->hash_only = 1;
69932bc6
SB
219 /* Use full length SHA1 if no argument */
220 if (!arg)
221 return 0;
b0f0be93
PS
222
223 abbrev_opt.value = &opts->abbrev;
224 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset);
69932bc6
SB
225}
226
227static int exclude_existing_callback(const struct option *opt, const char *arg,
228 int unset)
229{
7907fb0c 230 struct exclude_existing_options *opts = opt->value;
517fe807 231 BUG_ON_OPT_NEG(unset);
7907fb0c
PS
232 opts->enabled = 1;
233 opts->pattern = arg;
69932bc6
SB
234 return 0;
235}
236
358ddb62
LT
237int cmd_show_ref(int argc, const char **argv, const char *prefix)
238{
7907fb0c 239 struct exclude_existing_options exclude_existing_opts = {0};
b0f0be93 240 struct show_one_options show_one_opts = {0};
7907fb0c
PS
241 const struct option show_ref_options[] = {
242 OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
243 OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
244 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
245 "requires exact ref path")),
246 OPT_HIDDEN_BOOL('h', NULL, &show_head,
247 N_("show the HEAD reference, even if it would be filtered out")),
248 OPT_BOOL(0, "head", &show_head,
249 N_("show the HEAD reference, even if it would be filtered out")),
b0f0be93 250 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
7907fb0c 251 N_("dereference tags into object IDs")),
b0f0be93 252 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"),
7907fb0c
PS
253 N_("only show SHA1 hash using <n> digits"),
254 PARSE_OPT_OPTARG, &hash_callback),
b0f0be93
PS
255 OPT__ABBREV(&show_one_opts.abbrev),
256 OPT__QUIET(&show_one_opts.quiet,
7907fb0c
PS
257 N_("do not print results to stdout (useful with --verify)")),
258 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts,
259 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
260 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
261 OPT_END()
262 };
263
8e712ef6
EN
264 git_config(git_default_config, NULL);
265
69932bc6 266 argc = parse_options(argc, argv, prefix, show_ref_options,
42fdf86e 267 show_ref_usage, 0);
358ddb62 268
7907fb0c
PS
269 if (exclude_existing_opts.enabled)
270 return cmd_show_ref__exclude_existing(&exclude_existing_opts);
b14cbae2 271 else if (verify)
b0f0be93 272 return cmd_show_ref__verify(&show_one_opts, argv);
b14cbae2 273 else
b0f0be93 274 return cmd_show_ref__patterns(&show_one_opts, argv);
358ddb62 275}