Peter Hagervall | baffc0e | 2007-07-15 01:14:45 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Elijah Newren | 8e712ef | 2019-04-25 07:58:54 -0700 | [diff] [blame] | 2 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 3 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 4 | #include "hex.h" |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 5 | #include "refs/refs-internal.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 6 | #include "object-name.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 7 | #include "object-store-ll.h" |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 8 | #include "object.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 9 | #include "string-list.h" |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 10 | #include "parse-options.h" |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 11 | |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 12 | static const char * const show_ref_usage[] = { |
Patrick Steinhardt | 1307d5e | 2023-10-31 09:16:50 +0100 | [diff] [blame] | 13 | N_("git show-ref [--head] [-d | --dereference]\n" |
Junio C Hamano | 607c3d3 | 2024-06-04 15:01:45 -0700 | [diff] [blame] | 14 | " [-s | --hash[=<n>]] [--abbrev[=<n>]] [--branches] [--tags]\n" |
| 15 | " [--] [<pattern>...]"), |
Patrick Steinhardt | 1307d5e | 2023-10-31 09:16:50 +0100 | [diff] [blame] | 16 | N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n" |
| 17 | " [-s | --hash[=<n>]] [--abbrev[=<n>]]\n" |
| 18 | " [--] [<ref>...]"), |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 19 | N_("git show-ref --exclude-existing[=<pattern>]"), |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 20 | N_("git show-ref --exists <ref>"), |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 21 | NULL |
| 22 | }; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 23 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 24 | struct show_one_options { |
| 25 | int quiet; |
| 26 | int hash_only; |
| 27 | int abbrev; |
| 28 | int deref_tags; |
| 29 | }; |
| 30 | |
| 31 | static void show_one(const struct show_one_options *opts, |
| 32 | const char *refname, const struct object_id *oid) |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 33 | { |
Vladimir Panteleev | f162704 | 2017-01-23 18:00:56 +0000 | [diff] [blame] | 34 | const char *hex; |
| 35 | struct object_id peeled; |
| 36 | |
Ævar Arnfjörð Bjarmason | bc726bd | 2023-03-28 15:58:50 +0200 | [diff] [blame] | 37 | if (!repo_has_object_file(the_repository, oid)) |
Vladimir Panteleev | d01b820 | 2017-01-23 18:00:58 +0000 | [diff] [blame] | 38 | die("git show-ref: bad ref %s (%s)", refname, |
| 39 | oid_to_hex(oid)); |
| 40 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 41 | if (opts->quiet) |
Vladimir Panteleev | 14144d3 | 2017-01-23 18:00:57 +0000 | [diff] [blame] | 42 | return; |
| 43 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 44 | hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev); |
| 45 | if (opts->hash_only) |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 46 | printf("%s\n", hex); |
| 47 | else |
| 48 | printf("%s %s\n", hex, refname); |
Vladimir Panteleev | f162704 | 2017-01-23 18:00:56 +0000 | [diff] [blame] | 49 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 50 | if (!opts->deref_tags) |
Vladimir Panteleev | f162704 | 2017-01-23 18:00:56 +0000 | [diff] [blame] | 51 | return; |
| 52 | |
Patrick Steinhardt | 30aaff4 | 2024-05-17 10:19:04 +0200 | [diff] [blame] | 53 | if (!peel_iterated_oid(the_repository, oid, &peeled)) { |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 54 | hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); |
Vladimir Panteleev | f162704 | 2017-01-23 18:00:56 +0000 | [diff] [blame] | 55 | printf("%s %s^{}\n", hex, refname); |
| 56 | } |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 59 | struct show_ref_data { |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 60 | const struct show_one_options *show_one_opts; |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 61 | const char **patterns; |
Patrick Steinhardt | 8465098 | 2023-10-31 09:16:33 +0100 | [diff] [blame] | 62 | int found_match; |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 63 | int show_head; |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 64 | }; |
| 65 | |
Michael Haggerty | f0a011f | 2015-05-25 18:38:51 +0000 | [diff] [blame] | 66 | static int show_ref(const char *refname, const struct object_id *oid, |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 67 | int flag UNUSED, void *cbdata) |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 68 | { |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 69 | struct show_ref_data *data = cbdata; |
| 70 | |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 71 | if (data->show_head && !strcmp(refname, "HEAD")) |
Doug Bell | 3f3d0ce | 2013-07-16 19:05:14 -0500 | [diff] [blame] | 72 | goto match; |
| 73 | |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 74 | if (data->patterns) { |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 75 | int reflen = strlen(refname); |
Patrick Steinhardt | ff546eb | 2023-10-31 09:16:12 +0100 | [diff] [blame] | 76 | const char **p = data->patterns, *m; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 77 | while ((m = *p++) != NULL) { |
| 78 | int len = strlen(m); |
| 79 | if (len > reflen) |
| 80 | continue; |
| 81 | if (memcmp(m, refname + reflen - len, len)) |
| 82 | continue; |
| 83 | if (len == reflen) |
| 84 | goto match; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 85 | if (refname[reflen - len - 1] == '/') |
| 86 | goto match; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | match: |
Patrick Steinhardt | 8465098 | 2023-10-31 09:16:33 +0100 | [diff] [blame] | 92 | data->found_match++; |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 93 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 94 | show_one(data->show_one_opts, refname, oid); |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 95 | |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 99 | static int add_existing(const char *refname, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 100 | const struct object_id *oid UNUSED, |
| 101 | int flag UNUSED, void *cbdata) |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 102 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 103 | struct string_list *list = (struct string_list *)cbdata; |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 104 | string_list_insert(list, refname); |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 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 | |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 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 | */ |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 126 | static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts) |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 127 | { |
Patrick Steinhardt | dbabd0b | 2023-10-31 09:16:21 +0100 | [diff] [blame] | 128 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 129 | char buf[1024]; |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 130 | int patternlen = opts->pattern ? strlen(opts->pattern) : 0; |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 131 | |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 132 | refs_for_each_ref(get_main_ref_store(the_repository), add_existing, |
| 133 | &existing_refs); |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 134 | while (fgets(buf, sizeof(buf), stdin)) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 135 | char *ref; |
Junio C Hamano | d8285af | 2006-12-18 13:33:47 -0800 | [diff] [blame] | 136 | int len = strlen(buf); |
| 137 | |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 138 | if (len > 0 && buf[len - 1] == '\n') |
| 139 | buf[--len] = '\0'; |
Junio C Hamano | d8285af | 2006-12-18 13:33:47 -0800 | [diff] [blame] | 140 | if (3 <= len && !strcmp(buf + len - 3, "^{}")) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 141 | len -= 3; |
| 142 | buf[len] = '\0'; |
| 143 | } |
| 144 | for (ref = buf + len; buf < ref; ref--) |
| 145 | if (isspace(ref[-1])) |
| 146 | break; |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 147 | if (opts->pattern) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 148 | int reflen = buf + len - ref; |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 149 | if (reflen < patternlen) |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 150 | continue; |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 151 | if (strncmp(ref, opts->pattern, patternlen)) |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 152 | continue; |
| 153 | } |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 154 | if (check_refname_format(ref, 0)) { |
Miklos Vajna | 5620e77 | 2009-03-24 02:09:16 +0100 | [diff] [blame] | 155 | warning("ref '%s' ignored", ref); |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 156 | continue; |
| 157 | } |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 158 | if (!string_list_has_string(&existing_refs, ref)) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 159 | printf("%s\n", buf); |
| 160 | } |
| 161 | } |
Patrick Steinhardt | dbabd0b | 2023-10-31 09:16:21 +0100 | [diff] [blame] | 162 | |
| 163 | string_list_clear(&existing_refs, 0); |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 167 | static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, |
| 168 | const char **refs) |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 169 | { |
| 170 | if (!refs || !*refs) |
| 171 | die("--verify requires a reference"); |
| 172 | |
| 173 | while (*refs) { |
| 174 | struct object_id oid; |
| 175 | |
Phillip Wood | 1dbe401 | 2024-02-07 16:44:35 +0000 | [diff] [blame] | 176 | if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) && |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 177 | !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) { |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 178 | show_one(show_one_opts, *refs, &oid); |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 179 | } |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 180 | else if (!show_one_opts->quiet) |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 181 | die("'%s' - not a valid ref", *refs); |
| 182 | else |
| 183 | return 1; |
| 184 | refs++; |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 190 | struct patterns_options { |
| 191 | int show_head; |
Junio C Hamano | 607c3d3 | 2024-06-04 15:01:45 -0700 | [diff] [blame] | 192 | int branches_only; |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 193 | int tags_only; |
| 194 | }; |
| 195 | |
| 196 | static int cmd_show_ref__patterns(const struct patterns_options *opts, |
| 197 | const struct show_one_options *show_one_opts, |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 198 | const char **patterns) |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 199 | { |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 200 | struct show_ref_data show_ref_data = { |
| 201 | .show_one_opts = show_one_opts, |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 202 | .show_head = opts->show_head, |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 203 | }; |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 204 | |
| 205 | if (patterns && *patterns) |
| 206 | show_ref_data.patterns = patterns; |
| 207 | |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 208 | if (opts->show_head) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 209 | refs_head_ref(get_main_ref_store(the_repository), show_ref, |
| 210 | &show_ref_data); |
Junio C Hamano | 607c3d3 | 2024-06-04 15:01:45 -0700 | [diff] [blame] | 211 | if (opts->branches_only || opts->tags_only) { |
| 212 | if (opts->branches_only) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 213 | refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 214 | "refs/heads/", NULL, |
| 215 | show_ref, &show_ref_data); |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 216 | if (opts->tags_only) |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 217 | refs_for_each_fullref_in(get_main_ref_store(the_repository), |
| 218 | "refs/tags/", NULL, show_ref, |
| 219 | &show_ref_data); |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 220 | } else { |
Patrick Steinhardt | 2e5c475 | 2024-05-07 09:11:53 +0200 | [diff] [blame] | 221 | refs_for_each_ref(get_main_ref_store(the_repository), |
| 222 | show_ref, &show_ref_data); |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 223 | } |
Patrick Steinhardt | 8465098 | 2023-10-31 09:16:33 +0100 | [diff] [blame] | 224 | if (!show_ref_data.found_match) |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 225 | return 1; |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 230 | static int cmd_show_ref__exists(const char **refs) |
| 231 | { |
| 232 | struct strbuf unused_referent = STRBUF_INIT; |
| 233 | struct object_id unused_oid; |
| 234 | unsigned int unused_type; |
| 235 | int failure_errno = 0; |
| 236 | const char *ref; |
| 237 | int ret = 0; |
| 238 | |
| 239 | if (!refs || !*refs) |
| 240 | die("--exists requires a reference"); |
| 241 | ref = *refs++; |
| 242 | if (*refs) |
| 243 | die("--exists requires exactly one reference"); |
| 244 | |
| 245 | if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, |
| 246 | &unused_oid, &unused_referent, &unused_type, |
| 247 | &failure_errno)) { |
Toon Claes | 0aabeaa | 2024-01-10 15:15:59 +0100 | [diff] [blame] | 248 | if (failure_errno == ENOENT || failure_errno == EISDIR) { |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 249 | error(_("reference does not exist")); |
| 250 | ret = 2; |
| 251 | } else { |
| 252 | errno = failure_errno; |
| 253 | error_errno(_("failed to look up reference")); |
| 254 | ret = 1; |
| 255 | } |
| 256 | |
| 257 | goto out; |
| 258 | } |
| 259 | |
| 260 | out: |
| 261 | strbuf_release(&unused_referent); |
| 262 | return ret; |
| 263 | } |
| 264 | |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 265 | static int hash_callback(const struct option *opt, const char *arg, int unset) |
| 266 | { |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 267 | struct show_one_options *opts = opt->value; |
| 268 | struct option abbrev_opt = *opt; |
| 269 | |
| 270 | opts->hash_only = 1; |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 271 | /* Use full length SHA1 if no argument */ |
| 272 | if (!arg) |
| 273 | return 0; |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 274 | |
| 275 | abbrev_opt.value = &opts->abbrev; |
| 276 | return parse_opt_abbrev_cb(&abbrev_opt, arg, unset); |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | static int exclude_existing_callback(const struct option *opt, const char *arg, |
| 280 | int unset) |
| 281 | { |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 282 | struct exclude_existing_options *opts = opt->value; |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 283 | BUG_ON_OPT_NEG(unset); |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 284 | opts->enabled = 1; |
| 285 | opts->pattern = arg; |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 289 | int cmd_show_ref(int argc, const char **argv, const char *prefix) |
| 290 | { |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 291 | struct exclude_existing_options exclude_existing_opts = {0}; |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 292 | struct patterns_options patterns_opts = {0}; |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 293 | struct show_one_options show_one_opts = {0}; |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 294 | int verify = 0, exists = 0; |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 295 | const struct option show_ref_options[] = { |
Junio C Hamano | 607c3d3 | 2024-06-04 15:01:45 -0700 | [diff] [blame] | 296 | OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with branches)")), |
| 297 | OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with tags)")), |
| 298 | OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only, |
| 299 | N_("deprecated synonym for --branches")), |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 300 | OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")), |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 301 | OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, " |
| 302 | "requires exact ref path")), |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 303 | OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head, |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 304 | N_("show the HEAD reference, even if it would be filtered out")), |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 305 | OPT_BOOL(0, "head", &patterns_opts.show_head, |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 306 | N_("show the HEAD reference, even if it would be filtered out")), |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 307 | OPT_BOOL('d', "dereference", &show_one_opts.deref_tags, |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 308 | N_("dereference tags into object IDs")), |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 309 | OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"), |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 310 | N_("only show SHA1 hash using <n> digits"), |
| 311 | PARSE_OPT_OPTARG, &hash_callback), |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 312 | OPT__ABBREV(&show_one_opts.abbrev), |
| 313 | OPT__QUIET(&show_one_opts.quiet, |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 314 | N_("do not print results to stdout (useful with --verify)")), |
| 315 | OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts, |
| 316 | N_("pattern"), N_("show refs from stdin that aren't in local repository"), |
| 317 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback), |
| 318 | OPT_END() |
| 319 | }; |
| 320 | |
Elijah Newren | 8e712ef | 2019-04-25 07:58:54 -0700 | [diff] [blame] | 321 | git_config(git_default_config, NULL); |
| 322 | |
Stephen Boyd | 69932bc | 2009-06-20 21:40:46 -0700 | [diff] [blame] | 323 | argc = parse_options(argc, argv, prefix, show_ref_options, |
René Scharfe | 42fdf86 | 2015-11-17 11:26:05 +0100 | [diff] [blame] | 324 | show_ref_usage, 0); |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 325 | |
René Scharfe | 7382497 | 2023-12-11 09:09:28 +0100 | [diff] [blame] | 326 | die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing", |
| 327 | verify, "--verify", |
| 328 | exists, "--exists"); |
Patrick Steinhardt | 199970e | 2023-10-31 09:16:46 +0100 | [diff] [blame] | 329 | |
Patrick Steinhardt | 7907fb0 | 2023-10-31 09:16:29 +0100 | [diff] [blame] | 330 | if (exclude_existing_opts.enabled) |
| 331 | return cmd_show_ref__exclude_existing(&exclude_existing_opts); |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 332 | else if (verify) |
Patrick Steinhardt | b0f0be9 | 2023-10-31 09:16:38 +0100 | [diff] [blame] | 333 | return cmd_show_ref__verify(&show_one_opts, argv); |
Patrick Steinhardt | 9080a7f | 2023-10-31 09:16:54 +0100 | [diff] [blame] | 334 | else if (exists) |
| 335 | return cmd_show_ref__exists(argv); |
Patrick Steinhardt | b14cbae | 2023-10-31 09:16:17 +0100 | [diff] [blame] | 336 | else |
Patrick Steinhardt | ee26f1e | 2023-10-31 09:16:42 +0100 | [diff] [blame] | 337 | return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv); |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 338 | } |