]>
Commit | Line | Data |
---|---|---|
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 | 13 | static 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 | |
69932bc6 SB |
21 | static int deref_tags, show_head, tags_only, heads_only, found_match, verify, |
22 | quiet, hash_only, abbrev, exclude_arg; | |
69932bc6 | 23 | static const char *exclude_existing_arg; |
358ddb62 | 24 | |
f0a011fa | 25 | static void show_one(const char *refname, const struct object_id *oid) |
64fe031a | 26 | { |
f1627040 VP |
27 | const char *hex; |
28 | struct object_id peeled; | |
29 | ||
bc726bd0 | 30 | if (!repo_has_object_file(the_repository, oid)) |
d01b8203 VP |
31 | die("git show-ref: bad ref %s (%s)", refname, |
32 | oid_to_hex(oid)); | |
33 | ||
14144d3b VP |
34 | if (quiet) |
35 | return; | |
36 | ||
d850b7a5 | 37 | hex = repo_find_unique_abbrev(the_repository, oid, abbrev); |
64fe031a JH |
38 | if (hash_only) |
39 | printf("%s\n", hex); | |
40 | else | |
41 | printf("%s %s\n", hex, refname); | |
f1627040 VP |
42 | |
43 | if (!deref_tags) | |
44 | return; | |
45 | ||
36a31792 | 46 | if (!peel_iterated_oid(oid, &peeled)) { |
d850b7a5 | 47 | hex = repo_find_unique_abbrev(the_repository, &peeled, abbrev); |
f1627040 VP |
48 | printf("%s %s^{}\n", hex, refname); |
49 | } | |
64fe031a JH |
50 | } |
51 | ||
ff546ebb PS |
52 | struct show_ref_data { |
53 | const char **patterns; | |
54 | }; | |
55 | ||
f0a011fa | 56 | static int show_ref(const char *refname, const struct object_id *oid, |
ff546ebb | 57 | int flag UNUSED, void *cbdata) |
358ddb62 | 58 | { |
ff546ebb PS |
59 | struct show_ref_data *data = cbdata; |
60 | ||
3f3d0cea DB |
61 | if (show_head && !strcmp(refname, "HEAD")) |
62 | goto match; | |
63 | ||
ff546ebb | 64 | if (data->patterns) { |
358ddb62 | 65 | int reflen = strlen(refname); |
ff546ebb | 66 | const char **p = data->patterns, *m; |
358ddb62 LT |
67 | while ((m = *p++) != NULL) { |
68 | int len = strlen(m); | |
69 | if (len > reflen) | |
70 | continue; | |
71 | if (memcmp(m, refname + reflen - len, len)) | |
72 | continue; | |
73 | if (len == reflen) | |
74 | goto match; | |
358ddb62 LT |
75 | if (refname[reflen - len - 1] == '/') |
76 | goto match; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | match: | |
82 | found_match++; | |
cf0adba7 | 83 | |
f0a011fa | 84 | show_one(refname, oid); |
cf0adba7 | 85 | |
358ddb62 LT |
86 | return 0; |
87 | } | |
88 | ||
63e14ee2 | 89 | static int add_existing(const char *refname, |
5cf88fd8 ÆAB |
90 | const struct object_id *oid UNUSED, |
91 | int flag UNUSED, void *cbdata) | |
ed9f7c95 | 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 | */ | |
b14cbae2 | 107 | static int cmd_show_ref__exclude_existing(const char *match) |
ed9f7c95 | 108 | { |
dbabd0b0 | 109 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
ed9f7c95 JH |
110 | char buf[1024]; |
111 | int matchlen = match ? strlen(match) : 0; | |
112 | ||
f0a011fa | 113 | for_each_ref(add_existing, &existing_refs); |
ed9f7c95 | 114 | while (fgets(buf, sizeof(buf), stdin)) { |
ed9f7c95 | 115 | char *ref; |
d8285af4 JH |
116 | int len = strlen(buf); |
117 | ||
ed9f7c95 JH |
118 | if (len > 0 && buf[len - 1] == '\n') |
119 | buf[--len] = '\0'; | |
d8285af4 | 120 | if (3 <= len && !strcmp(buf + len - 3, "^{}")) { |
ed9f7c95 JH |
121 | len -= 3; |
122 | buf[len] = '\0'; | |
123 | } | |
124 | for (ref = buf + len; buf < ref; ref--) | |
125 | if (isspace(ref[-1])) | |
126 | break; | |
127 | if (match) { | |
128 | int reflen = buf + len - ref; | |
129 | if (reflen < matchlen) | |
130 | continue; | |
131 | if (strncmp(ref, match, matchlen)) | |
132 | continue; | |
133 | } | |
8d9c5010 | 134 | if (check_refname_format(ref, 0)) { |
5620e77e | 135 | warning("ref '%s' ignored", ref); |
ed9f7c95 JH |
136 | continue; |
137 | } | |
c455c87c | 138 | if (!string_list_has_string(&existing_refs, ref)) { |
ed9f7c95 JH |
139 | printf("%s\n", buf); |
140 | } | |
141 | } | |
dbabd0b0 PS |
142 | |
143 | string_list_clear(&existing_refs, 0); | |
ed9f7c95 JH |
144 | return 0; |
145 | } | |
146 | ||
b14cbae2 PS |
147 | static int cmd_show_ref__verify(const char **refs) |
148 | { | |
149 | if (!refs || !*refs) | |
150 | die("--verify requires a reference"); | |
151 | ||
152 | while (*refs) { | |
153 | struct object_id oid; | |
154 | ||
155 | if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) && | |
156 | !read_ref(*refs, &oid)) { | |
157 | show_one(*refs, &oid); | |
158 | } | |
159 | else if (!quiet) | |
160 | die("'%s' - not a valid ref", *refs); | |
161 | else | |
162 | return 1; | |
163 | refs++; | |
164 | } | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
169 | static int cmd_show_ref__patterns(const char **patterns) | |
170 | { | |
171 | struct show_ref_data show_ref_data = {0}; | |
172 | ||
173 | if (patterns && *patterns) | |
174 | show_ref_data.patterns = patterns; | |
175 | ||
176 | if (show_head) | |
177 | head_ref(show_ref, &show_ref_data); | |
178 | if (heads_only || tags_only) { | |
179 | if (heads_only) | |
180 | for_each_fullref_in("refs/heads/", show_ref, &show_ref_data); | |
181 | if (tags_only) | |
182 | for_each_fullref_in("refs/tags/", show_ref, &show_ref_data); | |
183 | } else { | |
184 | for_each_ref(show_ref, &show_ref_data); | |
185 | } | |
186 | if (!found_match) { | |
187 | if (verify && !quiet) | |
188 | die("No match"); | |
189 | return 1; | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
69932bc6 SB |
195 | static int hash_callback(const struct option *opt, const char *arg, int unset) |
196 | { | |
197 | hash_only = 1; | |
198 | /* Use full length SHA1 if no argument */ | |
199 | if (!arg) | |
200 | return 0; | |
201 | return parse_opt_abbrev_cb(opt, arg, unset); | |
202 | } | |
203 | ||
204 | static int exclude_existing_callback(const struct option *opt, const char *arg, | |
205 | int unset) | |
206 | { | |
517fe807 | 207 | BUG_ON_OPT_NEG(unset); |
69932bc6 SB |
208 | exclude_arg = 1; |
209 | *(const char **)opt->value = arg; | |
210 | return 0; | |
211 | } | |
212 | ||
69932bc6 | 213 | static const struct option show_ref_options[] = { |
d5d09d47 SB |
214 | OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")), |
215 | OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")), | |
216 | OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, " | |
c9120b1c | 217 | "requires exact ref path")), |
4741edd5 SB |
218 | OPT_HIDDEN_BOOL('h', NULL, &show_head, |
219 | N_("show the HEAD reference, even if it would be filtered out")), | |
d5d09d47 | 220 | OPT_BOOL(0, "head", &show_head, |
3f3d0cea | 221 | N_("show the HEAD reference, even if it would be filtered out")), |
d5d09d47 | 222 | OPT_BOOL('d', "dereference", &deref_tags, |
c9120b1c | 223 | N_("dereference tags into object IDs")), |
203c8533 DL |
224 | OPT_CALLBACK_F('s', "hash", &abbrev, N_("n"), |
225 | N_("only show SHA1 hash using <n> digits"), | |
226 | PARSE_OPT_OPTARG, &hash_callback), | |
69932bc6 | 227 | OPT__ABBREV(&abbrev), |
8c839683 | 228 | OPT__QUIET(&quiet, |
c9120b1c | 229 | N_("do not print results to stdout (useful with --verify)")), |
203c8533 DL |
230 | OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_arg, |
231 | N_("pattern"), N_("show refs from stdin that aren't in local repository"), | |
232 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback), | |
69932bc6 SB |
233 | OPT_END() |
234 | }; | |
235 | ||
358ddb62 LT |
236 | int cmd_show_ref(int argc, const char **argv, const char *prefix) |
237 | { | |
8e712ef6 EN |
238 | git_config(git_default_config, NULL); |
239 | ||
69932bc6 | 240 | argc = parse_options(argc, argv, prefix, show_ref_options, |
42fdf86e | 241 | show_ref_usage, 0); |
358ddb62 | 242 | |
69932bc6 | 243 | if (exclude_arg) |
b14cbae2 PS |
244 | return cmd_show_ref__exclude_existing(exclude_existing_arg); |
245 | else if (verify) | |
246 | return cmd_show_ref__verify(argv); | |
247 | else | |
248 | return cmd_show_ref__patterns(argv); | |
358ddb62 | 249 | } |