8 static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list";
10 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
11 found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
12 static const char **pattern;
14 static void show_one(const char *refname, const unsigned char *sha1)
16 const char *hex = find_unique_abbrev(sha1, abbrev);
20 printf("%s %s\n", hex, refname);
23 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
27 unsigned char peeled[20];
29 if (tags_only || heads_only) {
32 match = heads_only && !prefixcmp(refname, "refs/heads/");
33 match |= tags_only && !prefixcmp(refname, "refs/tags/");
38 int reflen = strlen(refname);
39 const char **p = pattern, *m;
40 while ((m = *p++) != NULL) {
44 if (memcmp(m, refname + reflen - len, len))
48 /* "--verify" requires an exact match */
51 if (refname[reflen - len - 1] == '/')
60 /* This changes the semantics slightly that even under quiet we
61 * detect and return error if the repository is corrupt and
62 * ref points at a nonexistent object.
64 if (!has_sha1_file(sha1))
65 die("git-show-ref: bad ref %s (%s)", refname,
71 show_one(refname, sha1);
76 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
77 if (!is_null_sha1(peeled)) {
78 hex = find_unique_abbrev(peeled, abbrev);
79 printf("%s %s^{}\n", hex, refname);
83 obj = parse_object(sha1);
85 die("git-show-ref: bad ref %s (%s)", refname,
87 if (obj->type == OBJ_TAG) {
88 obj = deref_tag(obj, refname, 0);
89 hex = find_unique_abbrev(obj->sha1, abbrev);
90 printf("%s %s^{}\n", hex, refname);
96 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
98 struct path_list *list = (struct path_list *)cbdata;
99 path_list_insert(refname, list);
104 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
106 * (1) strip "^{}" at the end of line if any;
107 * (2) ignore if match is provided and does not head-match refname;
108 * (3) warn if refname is not a well-formed refname and skip;
109 * (4) ignore if refname is a ref that exists in the local repository;
110 * (5) otherwise output the line.
112 static int exclude_existing(const char *match)
114 static struct path_list existing_refs = { NULL, 0, 0, 0 };
116 int matchlen = match ? strlen(match) : 0;
118 for_each_ref(add_existing, &existing_refs);
119 while (fgets(buf, sizeof(buf), stdin)) {
121 int len = strlen(buf);
123 if (len > 0 && buf[len - 1] == '\n')
125 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
129 for (ref = buf + len; buf < ref; ref--)
130 if (isspace(ref[-1]))
133 int reflen = buf + len - ref;
134 if (reflen < matchlen)
136 if (strncmp(ref, match, matchlen))
139 if (check_ref_format(ref)) {
140 fprintf(stderr, "warning: ref '%s' ignored\n", ref);
143 if (!path_list_has_path(&existing_refs, ref)) {
150 int cmd_show_ref(int argc, const char **argv, const char *prefix)
154 for (i = 1; i < argc; i++) {
155 const char *arg = argv[i];
160 if (!strcmp(arg, "--")) {
161 pattern = argv + i + 1;
166 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
170 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
174 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
178 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
182 if (!prefixcmp(arg, "--hash=") ||
183 (!prefixcmp(arg, "--abbrev") &&
184 (arg[8] == '=' || arg[8] == '\0'))) {
185 if (arg[2] != 'h' && !arg[8])
187 abbrev = DEFAULT_ABBREV;
189 /* --hash= or --abbrev= */
197 abbrev = strtoul(arg, &end, 10);
198 if (*end || abbrev > 40)
199 usage(show_ref_usage);
200 if (abbrev < MINIMUM_ABBREV)
201 abbrev = MINIMUM_ABBREV;
205 if (!strcmp(arg, "--verify")) {
209 if (!strcmp(arg, "--tags")) {
213 if (!strcmp(arg, "--heads")) {
217 if (!strcmp(arg, "--exclude-existing"))
218 return exclude_existing(NULL);
219 if (!prefixcmp(arg, "--exclude-existing="))
220 return exclude_existing(arg + 19);
221 usage(show_ref_usage);
226 die("--verify requires a reference");
228 unsigned char sha1[20];
230 if (!prefixcmp(*pattern, "refs/") &&
231 resolve_ref(*pattern, sha1, 1, NULL)) {
233 show_one(*pattern, sha1);
236 die("'%s' - not a valid ref", *pattern);
245 head_ref(show_ref, NULL);
246 for_each_ref(show_ref, NULL);
248 if (verify && !quiet)