show-ref.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "refs.h"
  5. #include "object-store.h"
  6. #include "object.h"
  7. #include "tag.h"
  8. #include "string-list.h"
  9. #include "parse-options.h"
  10. static const char * const show_ref_usage[] = {
  11. N_("git show-ref [-q | --quiet] [--verify] [--head] [-d | --dereference] [-s | --hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [<pattern>...]"),
  12. N_("git show-ref --exclude-existing[=<pattern>]"),
  13. NULL
  14. };
  15. static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
  16. quiet, hash_only, abbrev, exclude_arg;
  17. static const char **pattern;
  18. static const char *exclude_existing_arg;
  19. static void show_one(const char *refname, const struct object_id *oid)
  20. {
  21. const char *hex;
  22. struct object_id peeled;
  23. if (!has_object_file(oid))
  24. die("git show-ref: bad ref %s (%s)", refname,
  25. oid_to_hex(oid));
  26. if (quiet)
  27. return;
  28. hex = find_unique_abbrev(oid, abbrev);
  29. if (hash_only)
  30. printf("%s\n", hex);
  31. else
  32. printf("%s %s\n", hex, refname);
  33. if (!deref_tags)
  34. return;
  35. if (!peel_ref(refname, &peeled)) {
  36. hex = find_unique_abbrev(&peeled, abbrev);
  37. printf("%s %s^{}\n", hex, refname);
  38. }
  39. }
  40. static int show_ref(const char *refname, const struct object_id *oid,
  41. int flag, void *cbdata)
  42. {
  43. if (show_head && !strcmp(refname, "HEAD"))
  44. goto match;
  45. if (tags_only || heads_only) {
  46. int match;
  47. match = heads_only && starts_with(refname, "refs/heads/");
  48. match |= tags_only && starts_with(refname, "refs/tags/");
  49. if (!match)
  50. return 0;
  51. }
  52. if (pattern) {
  53. int reflen = strlen(refname);
  54. const char **p = pattern, *m;
  55. while ((m = *p++) != NULL) {
  56. int len = strlen(m);
  57. if (len > reflen)
  58. continue;
  59. if (memcmp(m, refname + reflen - len, len))
  60. continue;
  61. if (len == reflen)
  62. goto match;
  63. if (refname[reflen - len - 1] == '/')
  64. goto match;
  65. }
  66. return 0;
  67. }
  68. match:
  69. found_match++;
  70. show_one(refname, oid);
  71. return 0;
  72. }
  73. static int add_existing(const char *refname, const struct object_id *oid,
  74. int flag, void *cbdata)
  75. {
  76. struct string_list *list = (struct string_list *)cbdata;
  77. string_list_insert(list, refname);
  78. return 0;
  79. }
  80. /*
  81. * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
  82. * and
  83. * (1) strip "^{}" at the end of line if any;
  84. * (2) ignore if match is provided and does not head-match refname;
  85. * (3) warn if refname is not a well-formed refname and skip;
  86. * (4) ignore if refname is a ref that exists in the local repository;
  87. * (5) otherwise output the line.
  88. */
  89. static int exclude_existing(const char *match)
  90. {
  91. static struct string_list existing_refs = STRING_LIST_INIT_DUP;
  92. char buf[1024];
  93. int matchlen = match ? strlen(match) : 0;
  94. for_each_ref(add_existing, &existing_refs);
  95. while (fgets(buf, sizeof(buf), stdin)) {
  96. char *ref;
  97. int len = strlen(buf);
  98. if (len > 0 && buf[len - 1] == '\n')
  99. buf[--len] = '\0';
  100. if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
  101. len -= 3;
  102. buf[len] = '\0';
  103. }
  104. for (ref = buf + len; buf < ref; ref--)
  105. if (isspace(ref[-1]))
  106. break;
  107. if (match) {
  108. int reflen = buf + len - ref;
  109. if (reflen < matchlen)
  110. continue;
  111. if (strncmp(ref, match, matchlen))
  112. continue;
  113. }
  114. if (check_refname_format(ref, 0)) {
  115. warning("ref '%s' ignored", ref);
  116. continue;
  117. }
  118. if (!string_list_has_string(&existing_refs, ref)) {
  119. printf("%s\n", buf);
  120. }
  121. }
  122. return 0;
  123. }
  124. static int hash_callback(const struct option *opt, const char *arg, int unset)
  125. {
  126. hash_only = 1;
  127. /* Use full length SHA1 if no argument */
  128. if (!arg)
  129. return 0;
  130. return parse_opt_abbrev_cb(opt, arg, unset);
  131. }
  132. static int exclude_existing_callback(const struct option *opt, const char *arg,
  133. int unset)
  134. {
  135. BUG_ON_OPT_NEG(unset);
  136. exclude_arg = 1;
  137. *(const char **)opt->value = arg;
  138. return 0;
  139. }
  140. static const struct option show_ref_options[] = {
  141. OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
  142. OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
  143. OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
  144. "requires exact ref path")),
  145. OPT_HIDDEN_BOOL('h', NULL, &show_head,
  146. N_("show the HEAD reference, even if it would be filtered out")),
  147. OPT_BOOL(0, "head", &show_head,
  148. N_("show the HEAD reference, even if it would be filtered out")),
  149. OPT_BOOL('d', "dereference", &deref_tags,
  150. N_("dereference tags into object IDs")),
  151. OPT_CALLBACK_F('s', "hash", &abbrev, N_("n"),
  152. N_("only show SHA1 hash using <n> digits"),
  153. PARSE_OPT_OPTARG, &hash_callback),
  154. OPT__ABBREV(&abbrev),
  155. OPT__QUIET(&quiet,
  156. N_("do not print results to stdout (useful with --verify)")),
  157. OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_arg,
  158. N_("pattern"), N_("show refs from stdin that aren't in local repository"),
  159. PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback),
  160. OPT_END()
  161. };
  162. int cmd_show_ref(int argc, const char **argv, const char *prefix)
  163. {
  164. git_config(git_default_config, NULL);
  165. argc = parse_options(argc, argv, prefix, show_ref_options,
  166. show_ref_usage, 0);
  167. if (exclude_arg)
  168. return exclude_existing(exclude_existing_arg);
  169. pattern = argv;
  170. if (!*pattern)
  171. pattern = NULL;
  172. if (verify) {
  173. if (!pattern)
  174. die("--verify requires a reference");
  175. while (*pattern) {
  176. struct object_id oid;
  177. if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
  178. !read_ref(*pattern, &oid)) {
  179. show_one(*pattern, &oid);
  180. }
  181. else if (!quiet)
  182. die("'%s' - not a valid ref", *pattern);
  183. else
  184. return 1;
  185. pattern++;
  186. }
  187. return 0;
  188. }
  189. if (show_head)
  190. head_ref(show_ref, NULL);
  191. for_each_ref(show_ref, NULL);
  192. if (!found_match) {
  193. if (verify && !quiet)
  194. die("No match");
  195. return 1;
  196. }
  197. return 0;
  198. }