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