show-ref: fix --verify --hash=length
[git/gitweb.git] / builtin-show-ref.c
blob296070628c0d631ea5af1124415ce71e71c11c08
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
5 #include "path-list.h"
7 static 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";
9 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
10 found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
11 static const char **pattern;
13 static void show_one(const char *refname, const unsigned char *sha1)
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);
22 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
24 struct object *obj;
25 const char *hex;
26 unsigned char peeled[20];
28 if (tags_only || heads_only) {
29 int match;
31 match = heads_only && !strncmp(refname, "refs/heads/", 11);
32 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
33 if (!match)
34 return 0;
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;
53 return 0;
56 match:
57 found_match++;
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.
63 if (!has_sha1_file(sha1))
64 die("git-show-ref: bad ref %s (%s)", refname,
65 sha1_to_hex(sha1));
67 if (quiet)
68 return 0;
70 show_one(refname, sha1);
72 if (!deref_tags)
73 return 0;
75 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
76 if (!is_null_sha1(peeled)) {
77 hex = find_unique_abbrev(peeled, abbrev);
78 printf("%s %s^{}\n", hex, refname);
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);
92 return 0;
95 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
97 struct path_list *list = (struct path_list *)cbdata;
98 path_list_insert(refname, list);
99 return 0;
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.
111 static int exclude_existing(const char *match)
113 static struct path_list existing_refs = { NULL, 0, 0, 0 };
114 char buf[1024];
115 int matchlen = match ? strlen(match) : 0;
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';
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;
137 if (check_ref_format(ref)) {
138 fprintf(stderr, "warning: ref '%s' ignored\n", ref);
139 continue;
141 if (!path_list_has_path(&existing_refs, ref)) {
142 printf("%s\n", buf);
145 return 0;
148 int cmd_show_ref(int argc, const char **argv, const char *prefix)
150 int i;
152 for (i = 1; i < argc; i++) {
153 const char *arg = argv[i];
154 if (*arg != '-') {
155 pattern = argv + i;
156 break;
158 if (!strcmp(arg, "--")) {
159 pattern = argv + i + 1;
160 if (!*pattern)
161 pattern = NULL;
162 break;
164 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
165 quiet = 1;
166 continue;
168 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
169 show_head = 1;
170 continue;
172 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
173 deref_tags = 1;
174 continue;
176 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
177 hash_only = 1;
178 continue;
180 if (!strncmp(arg, "--hash=", 7) ||
181 (!strncmp(arg, "--abbrev", 8) &&
182 (arg[8] == '=' || arg[8] == '\0'))) {
183 if (arg[2] != 'h' && !arg[8])
184 /* --abbrev only */
185 abbrev = DEFAULT_ABBREV;
186 else {
187 /* --hash= or --abbrev= */
188 char *end;
189 if (arg[2] == 'h') {
190 hash_only = 1;
191 arg += 7;
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;
201 continue;
203 if (!strcmp(arg, "--verify")) {
204 verify = 1;
205 continue;
207 if (!strcmp(arg, "--tags")) {
208 tags_only = 1;
209 continue;
211 if (!strcmp(arg, "--heads")) {
212 heads_only = 1;
213 continue;
215 if (!strcmp(arg, "--exclude-existing"))
216 return exclude_existing(NULL);
217 if (!strncmp(arg, "--exclude-existing=", 19))
218 return exclude_existing(arg + 19);
219 usage(show_ref_usage);
222 if (verify) {
223 unsigned char sha1[20];
225 while (*pattern) {
226 if (!strncmp(*pattern, "refs/", 5) &&
227 resolve_ref(*pattern, sha1, 1, NULL)) {
228 if (!quiet)
229 show_one(*pattern, sha1);
231 else if (!quiet)
232 die("'%s' - not a valid ref", *pattern);
233 else
234 return 1;
235 pattern++;
237 return 0;
240 if (show_head)
241 head_ref(show_ref, NULL);
242 for_each_ref(show_ref, NULL);
243 if (!found_match) {
244 if (verify && !quiet)
245 die("No match");
246 return 1;
248 return 0;