Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/show-ref.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / show-ref.c` 352 lines ยท 9604 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #include "builtin.h" 3 #include "config.h" 4 #include "environment.h" 5 #include "gettext.h" 6 #include "hex.h" 7 #include "refs/refs-internal.h" 8 #include "object-name.h" 9 #include "odb.h" 10 #include "object.h" 11 #include "string-list.h" 12 #include "parse-options.h" 13 14 static const char * const show_ref_usage[] = { 15 N_("git show-ref [--head] [-d | --dereference]\n" 16 " [-s | --hash[=]] [--abbrev[=]] [--branches] [--tags]\n" 17 " [--] [...]"), 18 N_("git show-ref --verify [-q | --quiet] [-d | --dereference]\n" 19 " [-s | --hash[=]] [--abbrev[=]]\n" 20 " [--] [...]"), 21 N_("git show-ref --exclude-existing[=]"), 22 N_("git show-ref --exists "), 23 NULL 24 }; 25 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, 34 const struct reference *ref) 35 { 36 const char *hex; 37 struct object_id peeled; 38 39 if (!odb_has_object(the_repository->objects, ref->oid, 40 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) 41 die("git show-ref: bad ref %s (%s)", ref->name, 42 oid_to_hex(ref->oid)); 43 44 if (opts->quiet) 45 return; 46 47 hex = repo_find_unique_abbrev(the_repository, ref->oid, opts->abbrev); 48 if (opts->hash_only) 49 printf("%s\n", hex); 50 else 51 printf("%s %s\n", hex, ref->name); 52 53 if (!opts->deref_tags) 54 return; 55 56 if (!reference_get_peeled_oid(the_repository, ref, &peeled)) { 57 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); 58 printf("%s %s^{}\n", hex, ref->name); 59 } 60 } 61 62 struct show_ref_data { 63 const struct show_one_options *show_one_opts; 64 const char **patterns; 65 int found_match; 66 int show_head; 67 }; 68 69 static int show_ref(const struct reference *ref, void *cbdata) 70 { 71 struct show_ref_data *data = cbdata; 72 73 if (data->show_head && !strcmp(ref->name, "HEAD")) 74 goto match; 75 76 if (data->patterns) { 77 int reflen = strlen(ref->name); 78 const char **p = data->patterns, *m; 79 while ((m = *p++) != NULL) { 80 int len = strlen(m); 81 if (len > reflen) 82 continue; 83 if (memcmp(m, ref->name + reflen - len, len)) 84 continue; 85 if (len == reflen) 86 goto match; 87 if (ref->name[reflen - len - 1] == '/') 88 goto match; 89 } 90 return 0; 91 } 92 93 match: 94 data->found_match++; 95 96 show_one(data->show_one_opts, ref); 97 98 return 0; 99 } 100 101 static int add_existing(const struct reference *ref, void *cbdata) 102 { 103 struct string_list *list = (struct string_list *)cbdata; 104 string_list_insert(list, ref->name); 105 return 0; 106 } 107 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 117 /* 118 * read "^(?:\s)?(?:\^\{\})?$" 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 */ 126 static int cmd_show_ref__exclude_existing(const struct exclude_existing_options *opts) 127 { 128 struct string_list existing_refs = STRING_LIST_INIT_DUP; 129 char buf[1024]; 130 int patternlen = opts->pattern ? strlen(opts->pattern) : 0; 131 132 refs_for_each_ref(get_main_ref_store(the_repository), add_existing, 133 &existing_refs); 134 while (fgets(buf, sizeof(buf), stdin)) { 135 char *ref; 136 int len = strlen(buf); 137 138 if (len > 0 && buf[len - 1] == '\n') 139 buf[--len] = '\0'; 140 if (3 <= len && !strcmp(buf + len - 3, "^{}")) { 141 len -= 3; 142 buf[len] = '\0'; 143 } 144 for (ref = buf + len; buf < ref; ref--) 145 if (isspace(ref[-1])) 146 break; 147 if (opts->pattern) { 148 int reflen = buf + len - ref; 149 if (reflen < patternlen) 150 continue; 151 if (strncmp(ref, opts->pattern, patternlen)) 152 continue; 153 } 154 if (check_refname_format(ref, 0)) { 155 warning("ref '%s' ignored", ref); 156 continue; 157 } 158 if (!string_list_has_string(&existing_refs, ref)) { 159 printf("%s\n", buf); 160 } 161 } 162 163 string_list_clear(&existing_refs, 0); 164 return 0; 165 } 166 167 static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, 168 const char **refs) 169 { 170 if (!refs || !*refs) 171 die("--verify requires a reference"); 172 173 while (*refs) { 174 struct object_id oid; 175 176 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) && 177 !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) { 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) { 185 die("'%s' - not a valid ref", *refs); 186 } else { 187 return 1; 188 } 189 190 refs++; 191 } 192 193 return 0; 194 } 195 196 struct patterns_options { 197 int show_head; 198 int branches_only; 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, 204 const char **patterns) 205 { 206 struct show_ref_data show_ref_data = { 207 .show_one_opts = show_one_opts, 208 .show_head = opts->show_head, 209 }; 210 211 if (patterns && *patterns) 212 show_ref_data.patterns = patterns; 213 214 if (opts->show_head) 215 refs_head_ref(get_main_ref_store(the_repository), show_ref, 216 &show_ref_data); 217 if (opts->branches_only || opts->tags_only) { 218 struct refs_for_each_ref_options for_each_ref_opts = { 0 }; 219 220 if (opts->branches_only) { 221 for_each_ref_opts.prefix = "refs/heads/"; 222 refs_for_each_ref_ext(get_main_ref_store(the_repository), 223 show_ref, &show_ref_data, &for_each_ref_opts); 224 } 225 226 if (opts->tags_only) { 227 for_each_ref_opts.prefix = "refs/tags/"; 228 refs_for_each_ref_ext(get_main_ref_store(the_repository), 229 show_ref, &show_ref_data, &for_each_ref_opts); 230 } 231 } else { 232 refs_for_each_ref(get_main_ref_store(the_repository), 233 show_ref, &show_ref_data); 234 } 235 if (!show_ref_data.found_match) 236 return 1; 237 238 return 0; 239 } 240 241 static int cmd_show_ref__exists(const char **refs) 242 { 243 struct strbuf unused_referent = STRBUF_INIT; 244 struct object_id unused_oid; 245 unsigned int unused_type; 246 int failure_errno = 0; 247 const char *ref; 248 int ret = 0; 249 250 if (!refs || !*refs) 251 die("--exists requires a reference"); 252 ref = *refs++; 253 if (*refs) 254 die("--exists requires exactly one reference"); 255 256 if (refs_read_raw_ref(get_main_ref_store(the_repository), ref, 257 &unused_oid, &unused_referent, &unused_type, 258 &failure_errno)) { 259 if (failure_errno == ENOENT || failure_errno == EISDIR) { 260 error(_("reference does not exist")); 261 ret = 2; 262 } else { 263 errno = failure_errno; 264 error_errno(_("failed to look up reference")); 265 ret = 1; 266 } 267 268 goto out; 269 } 270 271 out: 272 strbuf_release(&unused_referent); 273 return ret; 274 } 275 276 static int hash_callback(const struct option *opt, const char *arg, int unset) 277 { 278 struct show_one_options *opts = opt->value; 279 struct option abbrev_opt = *opt; 280 281 opts->hash_only = 1; 282 /* Use full length SHA1 if no argument */ 283 if (!arg) 284 return 0; 285 286 abbrev_opt.value = &opts->abbrev; 287 return parse_opt_abbrev_cb(&abbrev_opt, arg, unset); 288 } 289 290 static int exclude_existing_callback(const struct option *opt, const char *arg, 291 int unset) 292 { 293 struct exclude_existing_options *opts = opt->value; 294 BUG_ON_OPT_NEG(unset); 295 opts->enabled = 1; 296 opts->pattern = arg; 297 return 0; 298 } 299 300 int cmd_show_ref(int argc, 301 const char **argv, 302 const char *prefix, 303 struct repository *repo UNUSED) 304 { 305 struct exclude_existing_options exclude_existing_opts = {0}; 306 struct patterns_options patterns_opts = {0}; 307 struct show_one_options show_one_opts = {0}; 308 int verify = 0, exists = 0; 309 const struct option show_ref_options[] = { 310 OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with --branches)")), 311 OPT_BOOL(0, "branches", &patterns_opts.branches_only, N_("only show branches (can be combined with --tags)")), 312 OPT_HIDDEN_BOOL(0, "heads", &patterns_opts.branches_only, 313 N_("deprecated synonym for --branches")), 314 OPT_BOOL(0, "exists", &exists, N_("check for reference existence without resolving")), 315 OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, " 316 "requires exact ref path")), 317 OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head, 318 N_("show the HEAD reference, even if it would be filtered out")), 319 OPT_BOOL(0, "head", &patterns_opts.show_head, 320 N_("show the HEAD reference, even if it would be filtered out")), 321 OPT_BOOL('d', "dereference", &show_one_opts.deref_tags, 322 N_("dereference tags into object IDs")), 323 OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"), 324 N_("only show SHA1 hash using digits"), 325 PARSE_OPT_OPTARG, &hash_callback), 326 OPT__ABBREV(&show_one_opts.abbrev), 327 OPT__QUIET(&show_one_opts.quiet, 328 N_("do not print results to stdout (useful with --verify)")), 329 OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts, 330 N_("pattern"), N_("show refs from stdin that aren't in local repository"), 331 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback), 332 OPT_END() 333 }; 334 335 repo_config(the_repository, git_default_config, NULL); 336 337 argc = parse_options(argc, argv, prefix, show_ref_options, 338 show_ref_usage, 0); 339 340 die_for_incompatible_opt3(exclude_existing_opts.enabled, "--exclude-existing", 341 verify, "--verify", 342 exists, "--exists"); 343 344 if (exclude_existing_opts.enabled) 345 return cmd_show_ref__exclude_existing(&exclude_existing_opts); 346 else if (verify) 347 return cmd_show_ref__verify(&show_one_opts, argv); 348 else if (exists) 349 return cmd_show_ref__exists(argv); 350 else 351 return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv); 352 }