]>
| Commit | Line | Data |
|---|---|---|
| 03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
| baffc0e7 | 2 | #include "builtin.h" |
| 8e712ef6 | 3 | #include "config.h" |
| 08b77586 | 4 | #include "environment.h" |
| f394e093 | 5 | #include "gettext.h" |
| 41771fa4 | 6 | #include "hex.h" |
| 9080a7f1 | 7 | #include "refs/refs-internal.h" |
| dabab1d6 | 8 | #include "object-name.h" |
| 8f491517 | 9 | #include "odb.h" |
| 358ddb62 | 10 | #include "object.h" |
| c455c87c | 11 | #include "string-list.h" |
| 69932bc6 | 12 | #include "parse-options.h" |
| 358ddb62 | 13 | |
| 69932bc6 | 14 | static const char * const show_ref_usage[] = { |
| 1307d5e8 | 15 | N_("git show-ref [--head] [-d | --dereference]\n" |
| 607c3d37 JH |
16 | " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--branches] [--tags]\n" |
| 17 | " [--] [<pattern>...]"), | |
| 1307d5e8 PS |
18 | N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n" |
| 19 | " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n" | |
| 20 | " [--] [<ref>...]"), | |
| 33e8fc87 | 21 | N_("git show-ref --exclude-existing[=<pattern>]"), |
| 9080a7f1 | 22 | N_("git show-ref --exists <ref>"), |
| 69932bc6 SB |
23 | NULL |
| 24 | }; | |
| 358ddb62 | 25 | |
| b0f0be93 PS |
26 | struct show_one_options { |
| 27 | int quiet; | |
| 28 | int hash_only; | |
| 29 | int abbrev; | |
| 30 | int deref_tags; | |
| 31 | }; | |
| 32 | ||
| 33 | static void show_one(const struct show_one_options *opts, | |
| feaaea4c | 34 | const struct reference *ref) |
| 64fe031a | 35 | { |
| f1627040 VP |
36 | const char *hex; |
| 37 | struct object_id peeled; | |
| 38 | ||
| feaaea4c | 39 | if (!odb_has_object(the_repository->objects, ref->oid, |
| fcf8e3e1 | 40 | HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR)) |
| feaaea4c PS |
41 | die("git show-ref: bad ref %s (%s)", ref->name, |
| 42 | oid_to_hex(ref->oid)); | |
| d01b8203 | 43 | |
| b0f0be93 | 44 | if (opts->quiet) |
| 14144d3b VP |
45 | return; |
| 46 | ||
| feaaea4c | 47 | hex = repo_find_unique_abbrev(the_repository, ref->oid, opts->abbrev); |
| b0f0be93 | 48 | if (opts->hash_only) |
| 64fe031a JH |
49 | printf("%s\n", hex); |
| 50 | else | |
| feaaea4c | 51 | printf("%s %s\n", hex, ref->name); |
| f1627040 | 52 | |
| b0f0be93 | 53 | if (!opts->deref_tags) |
| f1627040 VP |
54 | return; |
| 55 | ||
| feaaea4c | 56 | if (!reference_get_peeled_oid(the_repository, ref, &peeled)) { |
| b0f0be93 | 57 | hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); |
| feaaea4c | 58 | printf("%s %s^{}\n", hex, ref->name); |
| f1627040 | 59 | } |
| 64fe031a JH |
60 | } |
| 61 | ||
| ff546ebb | 62 | struct show_ref_data { |
| b0f0be93 | 63 | const struct show_one_options *show_one_opts; |
| ff546ebb | 64 | const char **patterns; |
| 84650989 | 65 | int found_match; |
| ee26f1e2 | 66 | int show_head; |
| ff546ebb PS |
67 | }; |
| 68 | ||
| bdbebe57 | 69 | static int show_ref(const struct reference *ref, void *cbdata) |
| 358ddb62 | 70 | { |
| ff546ebb PS |
71 | struct show_ref_data *data = cbdata; |
| 72 | ||
| bdbebe57 | 73 | if (data->show_head && !strcmp(ref->name, "HEAD")) |
| 3f3d0cea DB |
74 | goto match; |
| 75 | ||
| ff546ebb | 76 | if (data->patterns) { |
| bdbebe57 | 77 | int reflen = strlen(ref->name); |
| ff546ebb | 78 | const char **p = data->patterns, *m; |
| 358ddb62 LT |
79 | while ((m = *p++) != NULL) { |
| 80 | int len = strlen(m); | |
| 81 | if (len > reflen) | |
| 82 | continue; | |
| bdbebe57 | 83 | if (memcmp(m, ref->name + reflen - len, len)) |
| 358ddb62 LT |
84 | continue; |
| 85 | if (len == reflen) | |
| 86 | goto match; | |
| bdbebe57 | 87 | if (ref->name[reflen - len - 1] == '/') |
| 358ddb62 LT |
88 | goto match; |
| 89 | } | |
| 90 | return 0; | |
| 91 | } | |
| 92 | ||
| 93 | match: | |
| 84650989 | 94 | data->found_match++; |
| cf0adba7 | 95 | |
| feaaea4c | 96 | show_one(data->show_one_opts, ref); |
| cf0adba7 | 97 | |
| 358ddb62 LT |
98 | return 0; |
| 99 | } | |
| 100 | ||
| bdbebe57 | 101 | static int add_existing(const struct reference *ref, void *cbdata) |
| ed9f7c95 | 102 | { |
| c455c87c | 103 | struct string_list *list = (struct string_list *)cbdata; |
| bdbebe57 | 104 | string_list_insert(list, ref->name); |
| ed9f7c95 JH |
105 | return 0; |
| 106 | } | |
| 107 | ||
| 7907fb0c PS |
108 | struct exclude_existing_options { |
| 109 | /* | |
| 110 | * We need an explicit `enabled` field because it is perfectly valid | |
| 111 | * for `pattern` to be `NULL` even if `--exclude-existing` was given. | |
| 112 | */ | |
| 113 | int enabled; | |
| 114 | const char *pattern; | |
| 115 | }; | |
| 116 | ||
| ed9f7c95 JH |
117 | /* |
| 118 | * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input, | |
| 119 | * and | |
| 120 | * (1) strip "^{}" at the end of line if any; | |
| 121 | * (2) ignore if match is provided and does not head-match refname; | |
| 122 | * (3) warn if refname is not a well-formed refname and skip; | |
| 123 | * (4) ignore if refname is a ref that exists in the local repository; | |
| 124 | * (5) otherwise output the line. | |
| 125 | */ | |
| 7907fb0c | 126 | static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts) |
| ed9f7c95 | 127 | { |
| dbabd0b0 | 128 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
| ed9f7c95 | 129 | char buf[1024]; |
| 7907fb0c | 130 | int patternlen = opts->pattern ? strlen(opts->pattern) : 0; |
| ed9f7c95 | 131 | |
| 2e5c4758 PS |
132 | refs_for_each_ref(get_main_ref_store(the_repository), add_existing, |
| 133 | &existing_refs); | |
| ed9f7c95 | 134 | while (fgets(buf, sizeof(buf), stdin)) { |
| ed9f7c95 | 135 | char *ref; |
| d8285af4 JH |
136 | int len = strlen(buf); |
| 137 | ||
| ed9f7c95 JH |
138 | if (len > 0 && buf[len - 1] == '\n') |
| 139 | buf[--len] = '\0'; | |
| d8285af4 | 140 | if (3 <= len && !strcmp(buf + len - 3, "^{}")) { |
| ed9f7c95 JH |
141 | len -= 3; |
| 142 | buf[len] = '\0'; | |
| 143 | } | |
| 144 | for (ref = buf + len; buf < ref; ref--) | |
| 145 | if (isspace(ref[-1])) | |
| 146 | break; | |
| 7907fb0c | 147 | if (opts->pattern) { |
| ed9f7c95 | 148 | int reflen = buf + len - ref; |
| 7907fb0c | 149 | if (reflen < patternlen) |
| ed9f7c95 | 150 | continue; |
| 7907fb0c | 151 | if (strncmp(ref, opts->pattern, patternlen)) |
| ed9f7c95 JH |
152 | continue; |
| 153 | } | |
| 8d9c5010 | 154 | if (check_refname_format(ref, 0)) { |
| 5620e77e | 155 | warning("ref '%s' ignored", ref); |
| ed9f7c95 JH |
156 | continue; |
| 157 | } | |
| c455c87c | 158 | if (!string_list_has_string(&existing_refs, ref)) { |
| ed9f7c95 JH |
159 | printf("%s\n", buf); |
| 160 | } | |
| 161 | } | |
| dbabd0b0 PS |
162 | |
| 163 | string_list_clear(&existing_refs, 0); | |
| ed9f7c95 JH |
164 | return 0; |
| 165 | } | |
| 166 | ||
| b0f0be93 PS |
167 | static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, |
| 168 | const char **refs) | |
| b14cbae2 PS |
169 | { |
| 170 | if (!refs || !*refs) | |
| 171 | die("--verify requires a reference"); | |
| 172 | ||
| 173 | while (*refs) { | |
| 174 | struct object_id oid; | |
| 175 | ||
| 1dbe4015 | 176 | if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) && |
| 2e5c4758 | 177 | !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) { |
| feaaea4c PS |
178 | struct reference ref = { |
| 179 | .name = *refs, | |
| 180 | .oid = &oid, | |
| 181 | }; | |
| 182 | ||
| 183 | show_one(show_one_opts, &ref); | |
| 184 | } else if (!show_one_opts->quiet) { | |
| b14cbae2 | 185 | die("'%s' - not a valid ref", *refs); |
| feaaea4c | 186 | } else { |
| b14cbae2 | 187 | return 1; |
| feaaea4c PS |
188 | } |
| 189 | ||
| b14cbae2 PS |
190 | refs++; |
| 191 | } | |
| 192 | ||
| 193 | return 0; | |
| 194 | } | |
| 195 | ||
| ee26f1e2 PS |
196 | struct patterns_options { |
| 197 | int show_head; | |
| 607c3d37 | 198 | int branches_only; |
| ee26f1e2 PS |
199 | int tags_only; |
| 200 | }; | |
| 201 | ||
| 202 | static int cmd_show_ref__patterns(const struct patterns_options *opts, | |
| 203 | const struct show_one_options *show_one_opts, | |
| b0f0be93 | 204 | const char **patterns) |
| b14cbae2 | 205 | { |
| b0f0be93 PS |
206 | struct show_ref_data show_ref_data = { |
| 207 | .show_one_opts = show_one_opts, | |
| ee26f1e2 | 208 | .show_head = opts->show_head, |
| b0f0be93 | 209 | }; |
| b14cbae2 PS |
210 | |
| 211 | if (patterns && *patterns) | |
| 212 | show_ref_data.patterns = patterns; | |
| 213 | ||
| ee26f1e2 | 214 | if (opts->show_head) |
| 2e5c4758 PS |
215 | refs_head_ref(get_main_ref_store(the_repository), show_ref, |
| 216 | &show_ref_data); | |
| 607c3d37 JH |
217 | if (opts->branches_only || opts->tags_only) { |
| 218 | if (opts->branches_only) | |
| 2e5c4758 PS |
219 | refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 220 | "refs/heads/", NULL, | |
| 221 | show_ref, &show_ref_data); | |
| ee26f1e2 | 222 | if (opts->tags_only) |
| 2e5c4758 PS |
223 | refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 224 | "refs/tags/", NULL, show_ref, | |
| 225 | &show_ref_data); | |
| b14cbae2 | 226 | } else { |
| 2e5c4758 PS |
227 | refs_for_each_ref(get_main_ref_store(the_repository), |
| 228 | show_ref, &show_ref_data); | |
| b14cbae2 | 229 | } |
| 84650989 | 230 | if (!show_ref_data.found_match) |
| b14cbae2 | 231 | return 1; |
| b14cbae2 PS |
232 | |
| 233 | return 0; | |
| 234 | } | |
| 235 | ||
| 9080a7f1 PS |
236 | static int cmd_show_ref__exists(const char **refs) |
| 237 | { | |
| 238 | struct strbuf unused_referent = STRBUF_INIT; | |
| 239 | struct object_id unused_oid; | |
| 240 | unsigned int unused_type; | |
| 241 | int failure_errno = 0; | |
| 242 | const char *ref; | |
| 243 | int ret = 0; | |
| 244 | ||
| 245 | if (!refs || !*refs) | |
| 246 | die("--exists requires a reference"); | |
| 247 | ref = *refs++; | |
| 248 | if (*refs) | |
| 249 | die("--exists requires exactly one reference"); | |
| 250 | ||
| 251 | if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, | |
| 252 | &unused_oid, &unused_referent, &unused_type, | |
| 253 | &failure_errno)) { | |
| 0aabeaa5 | 254 | if (failure_errno == ENOENT || failure_errno == EISDIR) { |
| 9080a7f1 PS |
255 | error(_("reference does not exist")); |
| 256 | ret = 2; | |
| 257 | } else { | |
| 258 | errno = failure_errno; | |
| 259 | error_errno(_("failed to look up reference")); | |
| 260 | ret = 1; | |
| 261 | } | |
| 262 | ||
| 263 | goto out; | |
| 264 | } | |
| 265 | ||
| 266 | out: | |
| 267 | strbuf_release(&unused_referent); | |
| 268 | return ret; | |
| 269 | } | |
| 270 | ||
| 69932bc6 SB |
271 | static int hash_callback(const struct option *opt, const char *arg, int unset) |
| 272 | { | |
| b0f0be93 PS |
273 | struct show_one_options *opts = opt->value; |
| 274 | struct option abbrev_opt = *opt; | |
| 275 | ||
| 276 | opts->hash_only = 1; | |
| 69932bc6 SB |
277 | /* Use full length SHA1 if no argument */ |
| 278 | if (!arg) | |
| 279 | return 0; | |
| b0f0be93 PS |
280 | |
| 281 | abbrev_opt.value = &opts->abbrev; | |
| 282 | return parse_opt_abbrev_cb(&abbrev_opt, arg, unset); | |
| 69932bc6 SB |
283 | } |
| 284 | ||
| 285 | static int exclude_existing_callback(const struct option *opt, const char *arg, | |
| 286 | int unset) | |
| 287 | { | |
| 7907fb0c | 288 | struct exclude_existing_options *opts = opt->value; |
| 517fe807 | 289 | BUG_ON_OPT_NEG(unset); |
| 7907fb0c PS |
290 | opts->enabled = 1; |
| 291 | opts->pattern = arg; | |
| 69932bc6 SB |
292 | return 0; |
| 293 | } | |
| 294 | ||
| 9b1cb507 JC |
295 | int cmd_show_ref(int argc, |
| 296 | const char **argv, | |
| 297 | const char *prefix, | |
| 298 | struct repository *repo UNUSED) | |
| 358ddb62 | 299 | { |
| 7907fb0c | 300 | struct exclude_existing_options exclude_existing_opts = {0}; |
| ee26f1e2 | 301 | struct patterns_options patterns_opts = {0}; |
| b0f0be93 | 302 | struct show_one_options show_one_opts = {0}; |
| 9080a7f1 | 303 | int verify = 0, exists = 0; |
| 7907fb0c | 304 | const struct option show_ref_options[] = { |
| 98858712 AS |
305 | OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with --branches)")), |
| 306 | OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with --tags)")), | |
| 607c3d37 JH |
307 | OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only, |
| 308 | N_("deprecated synonym for --branches")), | |
| 9080a7f1 | 309 | OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")), |
| 7907fb0c PS |
310 | OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, " |
| 311 | "requires exact ref path")), | |
| ee26f1e2 | 312 | OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head, |
| 7907fb0c | 313 | N_("show the HEAD reference, even if it would be filtered out")), |
| ee26f1e2 | 314 | OPT_BOOL(0, "head", &patterns_opts.show_head, |
| 7907fb0c | 315 | N_("show the HEAD reference, even if it would be filtered out")), |
| b0f0be93 | 316 | OPT_BOOL('d', "dereference", &show_one_opts.deref_tags, |
| 7907fb0c | 317 | N_("dereference tags into object IDs")), |
| b0f0be93 | 318 | OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"), |
| 7907fb0c PS |
319 | N_("only show SHA1 hash using <n> digits"), |
| 320 | PARSE_OPT_OPTARG, &hash_callback), | |
| b0f0be93 PS |
321 | OPT__ABBREV(&show_one_opts.abbrev), |
| 322 | OPT__QUIET(&show_one_opts.quiet, | |
| 7907fb0c PS |
323 | N_("do not print results to stdout (useful with --verify)")), |
| 324 | OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts, | |
| 325 | N_("pattern"), N_("show refs from stdin that aren't in local repository"), | |
| 326 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback), | |
| 327 | OPT_END() | |
| 328 | }; | |
| 329 | ||
| 9ce196e8 | 330 | repo_config(the_repository, git_default_config, NULL); |
| 8e712ef6 | 331 | |
| 69932bc6 | 332 | argc = parse_options(argc, argv, prefix, show_ref_options, |
| 42fdf86e | 333 | show_ref_usage, 0); |
| 358ddb62 | 334 | |
| 73824973 RS |
335 | die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing", |
| 336 | verify, "--verify", | |
| 337 | exists, "--exists"); | |
| 199970e7 | 338 | |
| 7907fb0c PS |
339 | if (exclude_existing_opts.enabled) |
| 340 | return cmd_show_ref__exclude_existing(&exclude_existing_opts); | |
| b14cbae2 | 341 | else if (verify) |
| b0f0be93 | 342 | return cmd_show_ref__verify(&show_one_opts, argv); |
| 9080a7f1 PS |
343 | else if (exists) |
| 344 | return cmd_show_ref__exists(argv); | |
| b14cbae2 | 345 | else |
| ee26f1e2 | 346 | return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv); |
| 358ddb62 | 347 | } |