]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/show-ref.c
fsck: change functions to use object_id
[thirdparty/git.git] / builtin / show-ref.c
CommitLineData
baffc0e7 1#include "builtin.h"
358ddb62
LT
2#include "cache.h"
3#include "refs.h"
4#include "object.h"
5#include "tag.h"
c455c87c 6#include "string-list.h"
69932bc6 7#include "parse-options.h"
358ddb62 8
69932bc6 9static const char * const show_ref_usage[] = {
9c9b4f2f 10 N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]"),
c9120b1c 11 N_("git show-ref --exclude-existing[=pattern] < ref-list"),
69932bc6
SB
12 NULL
13};
358ddb62 14
69932bc6
SB
15static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
16 quiet, hash_only, abbrev, exclude_arg;
358ddb62 17static const char **pattern;
69932bc6 18static const char *exclude_existing_arg;
358ddb62 19
64fe031a
JH
20static void show_one(const char *refname, const unsigned char *sha1)
21{
22 const char *hex = find_unique_abbrev(sha1, abbrev);
23 if (hash_only)
24 printf("%s\n", hex);
25 else
26 printf("%s %s\n", hex, refname);
27}
28
eaf12a8c 29static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
358ddb62 30{
2eaf2224 31 const char *hex;
cf0adba7 32 unsigned char peeled[20];
358ddb62 33
3f3d0cea
DB
34 if (show_head && !strcmp(refname, "HEAD"))
35 goto match;
36
358ddb62
LT
37 if (tags_only || heads_only) {
38 int match;
39
59556548
CC
40 match = heads_only && starts_with(refname, "refs/heads/");
41 match |= tags_only && starts_with(refname, "refs/tags/");
358ddb62
LT
42 if (!match)
43 return 0;
44 }
45 if (pattern) {
46 int reflen = strlen(refname);
47 const char **p = pattern, *m;
48 while ((m = *p++) != NULL) {
49 int len = strlen(m);
50 if (len > reflen)
51 continue;
52 if (memcmp(m, refname + reflen - len, len))
53 continue;
54 if (len == reflen)
55 goto match;
56 /* "--verify" requires an exact match */
57 if (verify)
58 continue;
59 if (refname[reflen - len - 1] == '/')
60 goto match;
61 }
62 return 0;
63 }
64
65match:
66 found_match++;
cf0adba7
JH
67
68 /* This changes the semantics slightly that even under quiet we
69 * detect and return error if the repository is corrupt and
70 * ref points at a nonexistent object.
71 */
72 if (!has_sha1_file(sha1))
7e44c935 73 die("git show-ref: bad ref %s (%s)", refname,
cf0adba7
JH
74 sha1_to_hex(sha1));
75
358ddb62
LT
76 if (quiet)
77 return 0;
2eaf2224 78
64fe031a 79 show_one(refname, sha1);
cf0adba7
JH
80
81 if (!deref_tags)
82 return 0;
83
e6dbffa6
JK
84 if (!peel_ref(refname, peeled)) {
85 hex = find_unique_abbrev(peeled, abbrev);
86 printf("%s %s^{}\n", hex, refname);
cf0adba7 87 }
358ddb62
LT
88 return 0;
89}
90
ed9f7c95
JH
91static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
92{
c455c87c 93 struct string_list *list = (struct string_list *)cbdata;
78a395d3 94 string_list_insert(list, refname);
ed9f7c95
JH
95 return 0;
96}
97
98/*
99 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
100 * and
101 * (1) strip "^{}" at the end of line if any;
102 * (2) ignore if match is provided and does not head-match refname;
103 * (3) warn if refname is not a well-formed refname and skip;
104 * (4) ignore if refname is a ref that exists in the local repository;
105 * (5) otherwise output the line.
106 */
107static int exclude_existing(const char *match)
108{
66ce0366 109 static struct string_list existing_refs = STRING_LIST_INIT_DUP;
ed9f7c95
JH
110 char buf[1024];
111 int matchlen = match ? strlen(match) : 0;
2b2a5be3
MH
112 struct each_ref_fn_sha1_adapter wrapped_add_existing =
113 {add_existing, &existing_refs};
ed9f7c95 114
2b2a5be3 115 for_each_ref(each_ref_fn_adapter, &wrapped_add_existing);
ed9f7c95 116 while (fgets(buf, sizeof(buf), stdin)) {
ed9f7c95 117 char *ref;
d8285af4
JH
118 int len = strlen(buf);
119
ed9f7c95
JH
120 if (len > 0 && buf[len - 1] == '\n')
121 buf[--len] = '\0';
d8285af4 122 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
ed9f7c95
JH
123 len -= 3;
124 buf[len] = '\0';
125 }
126 for (ref = buf + len; buf < ref; ref--)
127 if (isspace(ref[-1]))
128 break;
129 if (match) {
130 int reflen = buf + len - ref;
131 if (reflen < matchlen)
132 continue;
133 if (strncmp(ref, match, matchlen))
134 continue;
135 }
8d9c5010 136 if (check_refname_format(ref, 0)) {
5620e77e 137 warning("ref '%s' ignored", ref);
ed9f7c95
JH
138 continue;
139 }
c455c87c 140 if (!string_list_has_string(&existing_refs, ref)) {
ed9f7c95
JH
141 printf("%s\n", buf);
142 }
143 }
144 return 0;
145}
146
69932bc6
SB
147static int hash_callback(const struct option *opt, const char *arg, int unset)
148{
149 hash_only = 1;
150 /* Use full length SHA1 if no argument */
151 if (!arg)
152 return 0;
153 return parse_opt_abbrev_cb(opt, arg, unset);
154}
155
156static int exclude_existing_callback(const struct option *opt, const char *arg,
157 int unset)
158{
159 exclude_arg = 1;
160 *(const char **)opt->value = arg;
161 return 0;
162}
163
164static int help_callback(const struct option *opt, const char *arg, int unset)
165{
166 return -1;
167}
168
169static const struct option show_ref_options[] = {
d5d09d47
SB
170 OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
171 OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
172 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
c9120b1c 173 "requires exact ref path")),
4741edd5
SB
174 OPT_HIDDEN_BOOL('h', NULL, &show_head,
175 N_("show the HEAD reference, even if it would be filtered out")),
d5d09d47 176 OPT_BOOL(0, "head", &show_head,
3f3d0cea 177 N_("show the HEAD reference, even if it would be filtered out")),
d5d09d47 178 OPT_BOOL('d', "dereference", &deref_tags,
c9120b1c
NTND
179 N_("dereference tags into object IDs")),
180 { OPTION_CALLBACK, 's', "hash", &abbrev, N_("n"),
181 N_("only show SHA1 hash using <n> digits"),
69932bc6
SB
182 PARSE_OPT_OPTARG, &hash_callback },
183 OPT__ABBREV(&abbrev),
8c839683 184 OPT__QUIET(&quiet,
c9120b1c 185 N_("do not print results to stdout (useful with --verify)")),
69932bc6 186 { OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
c9120b1c 187 N_("pattern"), N_("show refs from stdin that aren't in local repository"),
69932bc6 188 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
c9120b1c 189 { OPTION_CALLBACK, 0, "help-all", NULL, NULL, N_("show usage"),
69932bc6
SB
190 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
191 OPT_END()
192};
193
358ddb62
LT
194int cmd_show_ref(int argc, const char **argv, const char *prefix)
195{
2b2a5be3
MH
196 struct each_ref_fn_sha1_adapter wrapped_show_ref =
197 {show_ref, NULL};
198
e62b3935
JN
199 if (argc == 2 && !strcmp(argv[1], "-h"))
200 usage_with_options(show_ref_usage, show_ref_options);
201
69932bc6
SB
202 argc = parse_options(argc, argv, prefix, show_ref_options,
203 show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
358ddb62 204
69932bc6
SB
205 if (exclude_arg)
206 return exclude_existing(exclude_existing_arg);
207
208 pattern = argv;
209 if (!*pattern)
210 pattern = NULL;
26cdd1e7
JH
211
212 if (verify) {
8ab40a20
DL
213 if (!pattern)
214 die("--verify requires a reference");
26cdd1e7 215 while (*pattern) {
8ab40a20
DL
216 unsigned char sha1[20];
217
59556548 218 if (starts_with(*pattern, "refs/") &&
c6893323 219 !read_ref(*pattern, sha1)) {
dd914299 220 if (!quiet)
64fe031a 221 show_one(*pattern, sha1);
dd914299 222 }
26cdd1e7
JH
223 else if (!quiet)
224 die("'%s' - not a valid ref", *pattern);
225 else
226 return 1;
227 pattern++;
228 }
229 return 0;
230 }
231
358ddb62 232 if (show_head)
2b2a5be3
MH
233 head_ref(each_ref_fn_adapter, &wrapped_show_ref);
234 for_each_ref(each_ref_fn_adapter, &wrapped_show_ref);
358ddb62
LT
235 if (!found_match) {
236 if (verify && !quiet)
237 die("No match");
238 return 1;
239 }
240 return 0;
241}