]>
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 | |
b0f0be93 PS |
21 | struct show_one_options { |
22 | int quiet; | |
23 | int hash_only; | |
24 | int abbrev; | |
25 | int deref_tags; | |
26 | }; | |
27 | ||
28 | static void show_one(const struct show_one_options *opts, | |
29 | const char *refname, const struct object_id *oid) | |
64fe031a | 30 | { |
f1627040 VP |
31 | const char *hex; |
32 | struct object_id peeled; | |
33 | ||
bc726bd0 | 34 | if (!repo_has_object_file(the_repository, oid)) |
d01b8203 VP |
35 | die("git show-ref: bad ref %s (%s)", refname, |
36 | oid_to_hex(oid)); | |
37 | ||
b0f0be93 | 38 | if (opts->quiet) |
14144d3b VP |
39 | return; |
40 | ||
b0f0be93 PS |
41 | hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev); |
42 | if (opts->hash_only) | |
64fe031a JH |
43 | printf("%s\n", hex); |
44 | else | |
45 | printf("%s %s\n", hex, refname); | |
f1627040 | 46 | |
b0f0be93 | 47 | if (!opts->deref_tags) |
f1627040 VP |
48 | return; |
49 | ||
36a31792 | 50 | if (!peel_iterated_oid(oid, &peeled)) { |
b0f0be93 | 51 | hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); |
f1627040 VP |
52 | printf("%s %s^{}\n", hex, refname); |
53 | } | |
64fe031a JH |
54 | } |
55 | ||
ff546ebb | 56 | struct show_ref_data { |
b0f0be93 | 57 | const struct show_one_options *show_one_opts; |
ff546ebb | 58 | const char **patterns; |
84650989 | 59 | int found_match; |
ee26f1e2 | 60 | int show_head; |
ff546ebb PS |
61 | }; |
62 | ||
f0a011fa | 63 | static int show_ref(const char *refname, const struct object_id *oid, |
ff546ebb | 64 | int flag UNUSED, void *cbdata) |
358ddb62 | 65 | { |
ff546ebb PS |
66 | struct show_ref_data *data = cbdata; |
67 | ||
ee26f1e2 | 68 | if (data->show_head && !strcmp(refname, "HEAD")) |
3f3d0cea DB |
69 | goto match; |
70 | ||
ff546ebb | 71 | if (data->patterns) { |
358ddb62 | 72 | int reflen = strlen(refname); |
ff546ebb | 73 | const char **p = data->patterns, *m; |
358ddb62 LT |
74 | while ((m = *p++) != NULL) { |
75 | int len = strlen(m); | |
76 | if (len > reflen) | |
77 | continue; | |
78 | if (memcmp(m, refname + reflen - len, len)) | |
79 | continue; | |
80 | if (len == reflen) | |
81 | goto match; | |
358ddb62 LT |
82 | if (refname[reflen - len - 1] == '/') |
83 | goto match; | |
84 | } | |
85 | return 0; | |
86 | } | |
87 | ||
88 | match: | |
84650989 | 89 | data->found_match++; |
cf0adba7 | 90 | |
b0f0be93 | 91 | show_one(data->show_one_opts, refname, oid); |
cf0adba7 | 92 | |
358ddb62 LT |
93 | return 0; |
94 | } | |
95 | ||
63e14ee2 | 96 | static int add_existing(const char *refname, |
5cf88fd8 ÆAB |
97 | const struct object_id *oid UNUSED, |
98 | int flag UNUSED, void *cbdata) | |
ed9f7c95 | 99 | { |
c455c87c | 100 | struct string_list *list = (struct string_list *)cbdata; |
78a395d3 | 101 | string_list_insert(list, refname); |
ed9f7c95 JH |
102 | return 0; |
103 | } | |
104 | ||
7907fb0c PS |
105 | struct exclude_existing_options { |
106 | /* | |
107 | * We need an explicit `enabled` field because it is perfectly valid | |
108 | * for `pattern` to be `NULL` even if `--exclude-existing` was given. | |
109 | */ | |
110 | int enabled; | |
111 | const char *pattern; | |
112 | }; | |
113 | ||
ed9f7c95 JH |
114 | /* |
115 | * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input, | |
116 | * and | |
117 | * (1) strip "^{}" at the end of line if any; | |
118 | * (2) ignore if match is provided and does not head-match refname; | |
119 | * (3) warn if refname is not a well-formed refname and skip; | |
120 | * (4) ignore if refname is a ref that exists in the local repository; | |
121 | * (5) otherwise output the line. | |
122 | */ | |
7907fb0c | 123 | static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts) |
ed9f7c95 | 124 | { |
dbabd0b0 | 125 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
ed9f7c95 | 126 | char buf[1024]; |
7907fb0c | 127 | int patternlen = opts->pattern ? strlen(opts->pattern) : 0; |
ed9f7c95 | 128 | |
f0a011fa | 129 | for_each_ref(add_existing, &existing_refs); |
ed9f7c95 | 130 | while (fgets(buf, sizeof(buf), stdin)) { |
ed9f7c95 | 131 | char *ref; |
d8285af4 JH |
132 | int len = strlen(buf); |
133 | ||
ed9f7c95 JH |
134 | if (len > 0 && buf[len - 1] == '\n') |
135 | buf[--len] = '\0'; | |
d8285af4 | 136 | if (3 <= len && !strcmp(buf + len - 3, "^{}")) { |
ed9f7c95 JH |
137 | len -= 3; |
138 | buf[len] = '\0'; | |
139 | } | |
140 | for (ref = buf + len; buf < ref; ref--) | |
141 | if (isspace(ref[-1])) | |
142 | break; | |
7907fb0c | 143 | if (opts->pattern) { |
ed9f7c95 | 144 | int reflen = buf + len - ref; |
7907fb0c | 145 | if (reflen < patternlen) |
ed9f7c95 | 146 | continue; |
7907fb0c | 147 | if (strncmp(ref, opts->pattern, patternlen)) |
ed9f7c95 JH |
148 | continue; |
149 | } | |
8d9c5010 | 150 | if (check_refname_format(ref, 0)) { |
5620e77e | 151 | warning("ref '%s' ignored", ref); |
ed9f7c95 JH |
152 | continue; |
153 | } | |
c455c87c | 154 | if (!string_list_has_string(&existing_refs, ref)) { |
ed9f7c95 JH |
155 | printf("%s\n", buf); |
156 | } | |
157 | } | |
dbabd0b0 PS |
158 | |
159 | string_list_clear(&existing_refs, 0); | |
ed9f7c95 JH |
160 | return 0; |
161 | } | |
162 | ||
b0f0be93 PS |
163 | static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, |
164 | const char **refs) | |
b14cbae2 PS |
165 | { |
166 | if (!refs || !*refs) | |
167 | die("--verify requires a reference"); | |
168 | ||
169 | while (*refs) { | |
170 | struct object_id oid; | |
171 | ||
172 | if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) && | |
173 | !read_ref(*refs, &oid)) { | |
b0f0be93 | 174 | show_one(show_one_opts, *refs, &oid); |
b14cbae2 | 175 | } |
b0f0be93 | 176 | else if (!show_one_opts->quiet) |
b14cbae2 PS |
177 | die("'%s' - not a valid ref", *refs); |
178 | else | |
179 | return 1; | |
180 | refs++; | |
181 | } | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
ee26f1e2 PS |
186 | struct patterns_options { |
187 | int show_head; | |
188 | int heads_only; | |
189 | int tags_only; | |
190 | }; | |
191 | ||
192 | static int cmd_show_ref__patterns(const struct patterns_options *opts, | |
193 | const struct show_one_options *show_one_opts, | |
b0f0be93 | 194 | const char **patterns) |
b14cbae2 | 195 | { |
b0f0be93 PS |
196 | struct show_ref_data show_ref_data = { |
197 | .show_one_opts = show_one_opts, | |
ee26f1e2 | 198 | .show_head = opts->show_head, |
b0f0be93 | 199 | }; |
b14cbae2 PS |
200 | |
201 | if (patterns && *patterns) | |
202 | show_ref_data.patterns = patterns; | |
203 | ||
ee26f1e2 | 204 | if (opts->show_head) |
b14cbae2 | 205 | head_ref(show_ref, &show_ref_data); |
ee26f1e2 PS |
206 | if (opts->heads_only || opts->tags_only) { |
207 | if (opts->heads_only) | |
b14cbae2 | 208 | for_each_fullref_in("refs/heads/", show_ref, &show_ref_data); |
ee26f1e2 | 209 | if (opts->tags_only) |
b14cbae2 PS |
210 | for_each_fullref_in("refs/tags/", show_ref, &show_ref_data); |
211 | } else { | |
212 | for_each_ref(show_ref, &show_ref_data); | |
213 | } | |
84650989 | 214 | if (!show_ref_data.found_match) |
b14cbae2 | 215 | return 1; |
b14cbae2 PS |
216 | |
217 | return 0; | |
218 | } | |
219 | ||
69932bc6 SB |
220 | static int hash_callback(const struct option *opt, const char *arg, int unset) |
221 | { | |
b0f0be93 PS |
222 | struct show_one_options *opts = opt->value; |
223 | struct option abbrev_opt = *opt; | |
224 | ||
225 | opts->hash_only = 1; | |
69932bc6 SB |
226 | /* Use full length SHA1 if no argument */ |
227 | if (!arg) | |
228 | return 0; | |
b0f0be93 PS |
229 | |
230 | abbrev_opt.value = &opts->abbrev; | |
231 | return parse_opt_abbrev_cb(&abbrev_opt, arg, unset); | |
69932bc6 SB |
232 | } |
233 | ||
234 | static int exclude_existing_callback(const struct option *opt, const char *arg, | |
235 | int unset) | |
236 | { | |
7907fb0c | 237 | struct exclude_existing_options *opts = opt->value; |
517fe807 | 238 | BUG_ON_OPT_NEG(unset); |
7907fb0c PS |
239 | opts->enabled = 1; |
240 | opts->pattern = arg; | |
69932bc6 SB |
241 | return 0; |
242 | } | |
243 | ||
358ddb62 LT |
244 | int cmd_show_ref(int argc, const char **argv, const char *prefix) |
245 | { | |
7907fb0c | 246 | struct exclude_existing_options exclude_existing_opts = {0}; |
ee26f1e2 | 247 | struct patterns_options patterns_opts = {0}; |
b0f0be93 | 248 | struct show_one_options show_one_opts = {0}; |
ee26f1e2 | 249 | int verify = 0; |
7907fb0c | 250 | const struct option show_ref_options[] = { |
ee26f1e2 PS |
251 | OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")), |
252 | OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")), | |
7907fb0c PS |
253 | OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, " |
254 | "requires exact ref path")), | |
ee26f1e2 | 255 | OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head, |
7907fb0c | 256 | N_("show the HEAD reference, even if it would be filtered out")), |
ee26f1e2 | 257 | OPT_BOOL(0, "head", &patterns_opts.show_head, |
7907fb0c | 258 | N_("show the HEAD reference, even if it would be filtered out")), |
b0f0be93 | 259 | OPT_BOOL('d', "dereference", &show_one_opts.deref_tags, |
7907fb0c | 260 | N_("dereference tags into object IDs")), |
b0f0be93 | 261 | OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"), |
7907fb0c PS |
262 | N_("only show SHA1 hash using <n> digits"), |
263 | PARSE_OPT_OPTARG, &hash_callback), | |
b0f0be93 PS |
264 | OPT__ABBREV(&show_one_opts.abbrev), |
265 | OPT__QUIET(&show_one_opts.quiet, | |
7907fb0c PS |
266 | N_("do not print results to stdout (useful with --verify)")), |
267 | OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts, | |
268 | N_("pattern"), N_("show refs from stdin that aren't in local repository"), | |
269 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback), | |
270 | OPT_END() | |
271 | }; | |
272 | ||
8e712ef6 EN |
273 | git_config(git_default_config, NULL); |
274 | ||
69932bc6 | 275 | argc = parse_options(argc, argv, prefix, show_ref_options, |
42fdf86e | 276 | show_ref_usage, 0); |
358ddb62 | 277 | |
7907fb0c PS |
278 | if (exclude_existing_opts.enabled) |
279 | return cmd_show_ref__exclude_existing(&exclude_existing_opts); | |
b14cbae2 | 280 | else if (verify) |
b0f0be93 | 281 | return cmd_show_ref__verify(&show_one_opts, argv); |
b14cbae2 | 282 | else |
ee26f1e2 | 283 | return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv); |
358ddb62 | 284 | } |