]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-show-ref.c
show-ref: fix --verify --hash=length
[thirdparty/git.git] / builtin-show-ref.c
CommitLineData
358ddb62
LT
1#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "tag.h"
ed9f7c95 5#include "path-list.h"
358ddb62 6
ed9f7c95 7static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] | --filter-invalid < ref-list";
358ddb62 8
c40abef8 9static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
2eaf2224 10 found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
358ddb62
LT
11static const char **pattern;
12
64fe031a
JH
13static void show_one(const char *refname, const unsigned char *sha1)
14{
15 const char *hex = find_unique_abbrev(sha1, abbrev);
16 if (hash_only)
17 printf("%s\n", hex);
18 else
19 printf("%s %s\n", hex, refname);
20}
21
eaf12a8c 22static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
358ddb62
LT
23{
24 struct object *obj;
2eaf2224 25 const char *hex;
cf0adba7 26 unsigned char peeled[20];
358ddb62
LT
27
28 if (tags_only || heads_only) {
29 int match;
30
31 match = heads_only && !strncmp(refname, "refs/heads/", 11);
32 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
33 if (!match)
34 return 0;
35 }
36 if (pattern) {
37 int reflen = strlen(refname);
38 const char **p = pattern, *m;
39 while ((m = *p++) != NULL) {
40 int len = strlen(m);
41 if (len > reflen)
42 continue;
43 if (memcmp(m, refname + reflen - len, len))
44 continue;
45 if (len == reflen)
46 goto match;
47 /* "--verify" requires an exact match */
48 if (verify)
49 continue;
50 if (refname[reflen - len - 1] == '/')
51 goto match;
52 }
53 return 0;
54 }
55
56match:
57 found_match++;
cf0adba7
JH
58
59 /* This changes the semantics slightly that even under quiet we
60 * detect and return error if the repository is corrupt and
61 * ref points at a nonexistent object.
62 */
63 if (!has_sha1_file(sha1))
64 die("git-show-ref: bad ref %s (%s)", refname,
65 sha1_to_hex(sha1));
66
358ddb62
LT
67 if (quiet)
68 return 0;
2eaf2224 69
64fe031a 70 show_one(refname, sha1);
cf0adba7
JH
71
72 if (!deref_tags)
73 return 0;
74
75 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
f4204ab9
JH
76 if (!is_null_sha1(peeled)) {
77 hex = find_unique_abbrev(peeled, abbrev);
78 printf("%s %s^{}\n", hex, refname);
79 }
358ddb62 80 }
cf0adba7
JH
81 else {
82 obj = parse_object(sha1);
83 if (!obj)
84 die("git-show-ref: bad ref %s (%s)", refname,
85 sha1_to_hex(sha1));
86 if (obj->type == OBJ_TAG) {
87 obj = deref_tag(obj, refname, 0);
88 hex = find_unique_abbrev(obj->sha1, abbrev);
89 printf("%s %s^{}\n", hex, refname);
90 }
91 }
358ddb62
LT
92 return 0;
93}
94
ed9f7c95
JH
95static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
96{
97 struct path_list *list = (struct path_list *)cbdata;
98 path_list_insert(refname, list);
99 return 0;
100}
101
102/*
103 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
104 * and
105 * (1) strip "^{}" at the end of line if any;
106 * (2) ignore if match is provided and does not head-match refname;
107 * (3) warn if refname is not a well-formed refname and skip;
108 * (4) ignore if refname is a ref that exists in the local repository;
109 * (5) otherwise output the line.
110 */
111static int exclude_existing(const char *match)
112{
113 static struct path_list existing_refs = { NULL, 0, 0, 0 };
114 char buf[1024];
115 int matchlen = match ? strlen(match) : 0;
116
117 for_each_ref(add_existing, &existing_refs);
118 while (fgets(buf, sizeof(buf), stdin)) {
119 int len = strlen(buf);
120 char *ref;
121 if (len > 0 && buf[len - 1] == '\n')
122 buf[--len] = '\0';
123 if (!strcmp(buf + len - 3, "^{}")) {
124 len -= 3;
125 buf[len] = '\0';
126 }
127 for (ref = buf + len; buf < ref; ref--)
128 if (isspace(ref[-1]))
129 break;
130 if (match) {
131 int reflen = buf + len - ref;
132 if (reflen < matchlen)
133 continue;
134 if (strncmp(ref, match, matchlen))
135 continue;
136 }
137 if (check_ref_format(ref)) {
138 fprintf(stderr, "warning: ref '%s' ignored\n", ref);
139 continue;
140 }
141 if (!path_list_has_path(&existing_refs, ref)) {
142 printf("%s\n", buf);
143 }
144 }
145 return 0;
146}
147
358ddb62
LT
148int cmd_show_ref(int argc, const char **argv, const char *prefix)
149{
150 int i;
151
152 for (i = 1; i < argc; i++) {
153 const char *arg = argv[i];
154 if (*arg != '-') {
155 pattern = argv + i;
156 break;
157 }
158 if (!strcmp(arg, "--")) {
159 pattern = argv + i + 1;
160 if (!*pattern)
161 pattern = NULL;
162 break;
163 }
164 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
165 quiet = 1;
166 continue;
167 }
168 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
169 show_head = 1;
170 continue;
171 }
172 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
173 deref_tags = 1;
174 continue;
175 }
c40abef8
CC
176 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
177 hash_only = 1;
178 continue;
179 }
2eaf2224
JH
180 if (!strncmp(arg, "--hash=", 7) ||
181 (!strncmp(arg, "--abbrev", 8) &&
182 (arg[8] == '=' || arg[8] == '\0'))) {
64fe031a 183 if (arg[2] != 'h' && !arg[8])
2eaf2224
JH
184 /* --abbrev only */
185 abbrev = DEFAULT_ABBREV;
186 else {
187 /* --hash= or --abbrev= */
188 char *end;
64fe031a 189 if (arg[2] == 'h') {
2eaf2224
JH
190 hash_only = 1;
191 arg += 7;
192 }
193 else
194 arg += 9;
195 abbrev = strtoul(arg, &end, 10);
196 if (*end || abbrev > 40)
197 usage(show_ref_usage);
198 if (abbrev < MINIMUM_ABBREV)
199 abbrev = MINIMUM_ABBREV;
200 }
201 continue;
202 }
358ddb62
LT
203 if (!strcmp(arg, "--verify")) {
204 verify = 1;
205 continue;
206 }
207 if (!strcmp(arg, "--tags")) {
208 tags_only = 1;
209 continue;
210 }
211 if (!strcmp(arg, "--heads")) {
212 heads_only = 1;
213 continue;
214 }
ed9f7c95
JH
215 if (!strcmp(arg, "--exclude-existing"))
216 return exclude_existing(NULL);
217 if (!strncmp(arg, "--exclude-existing=", 19))
218 return exclude_existing(arg + 19);
358ddb62
LT
219 usage(show_ref_usage);
220 }
26cdd1e7
JH
221
222 if (verify) {
223 unsigned char sha1[20];
224
225 while (*pattern) {
64fe031a
JH
226 if (!strncmp(*pattern, "refs/", 5) &&
227 resolve_ref(*pattern, sha1, 1, NULL)) {
dd914299 228 if (!quiet)
64fe031a 229 show_one(*pattern, sha1);
dd914299 230 }
26cdd1e7
JH
231 else if (!quiet)
232 die("'%s' - not a valid ref", *pattern);
233 else
234 return 1;
235 pattern++;
236 }
237 return 0;
238 }
239
358ddb62 240 if (show_head)
eaf12a8c
JH
241 head_ref(show_ref, NULL);
242 for_each_ref(show_ref, NULL);
358ddb62
LT
243 if (!found_match) {
244 if (verify && !quiet)
245 die("No match");
246 return 1;
247 }
248 return 0;
249}