6 #include "string-list.h"
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);
90 die("git show-ref: bad tag at ref %s (%s)", refname
,
92 hex
= find_unique_abbrev(obj
->sha1
, abbrev
);
93 printf("%s %s^{}\n", hex
, refname
);
99 static int add_existing(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
101 struct string_list
*list
= (struct string_list
*)cbdata
;
102 string_list_insert(refname
, list
);
107 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
109 * (1) strip "^{}" at the end of line if any;
110 * (2) ignore if match is provided and does not head-match refname;
111 * (3) warn if refname is not a well-formed refname and skip;
112 * (4) ignore if refname is a ref that exists in the local repository;
113 * (5) otherwise output the line.
115 static int exclude_existing(const char *match
)
117 static struct string_list existing_refs
= { NULL
, 0, 0, 0 };
119 int matchlen
= match
? strlen(match
) : 0;
121 for_each_ref(add_existing
, &existing_refs
);
122 while (fgets(buf
, sizeof(buf
), stdin
)) {
124 int len
= strlen(buf
);
126 if (len
> 0 && buf
[len
- 1] == '\n')
128 if (3 <= len
&& !strcmp(buf
+ len
- 3, "^{}")) {
132 for (ref
= buf
+ len
; buf
< ref
; ref
--)
133 if (isspace(ref
[-1]))
136 int reflen
= buf
+ len
- ref
;
137 if (reflen
< matchlen
)
139 if (strncmp(ref
, match
, matchlen
))
142 if (check_ref_format(ref
)) {
143 fprintf(stderr
, "warning: ref '%s' ignored\n", ref
);
146 if (!string_list_has_string(&existing_refs
, ref
)) {
153 int cmd_show_ref(int argc
, const char **argv
, const char *prefix
)
157 for (i
= 1; i
< argc
; i
++) {
158 const char *arg
= argv
[i
];
163 if (!strcmp(arg
, "--")) {
164 pattern
= argv
+ i
+ 1;
169 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
173 if (!strcmp(arg
, "-h") || !strcmp(arg
, "--head")) {
177 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--dereference")) {
181 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--hash")) {
185 if (!prefixcmp(arg
, "--hash=") ||
186 (!prefixcmp(arg
, "--abbrev") &&
187 (arg
[8] == '=' || arg
[8] == '\0'))) {
188 if (arg
[2] != 'h' && !arg
[8])
190 abbrev
= DEFAULT_ABBREV
;
192 /* --hash= or --abbrev= */
200 abbrev
= strtoul(arg
, &end
, 10);
201 if (*end
|| abbrev
> 40)
202 usage(show_ref_usage
);
203 if (abbrev
< MINIMUM_ABBREV
)
204 abbrev
= MINIMUM_ABBREV
;
208 if (!strcmp(arg
, "--verify")) {
212 if (!strcmp(arg
, "--tags")) {
216 if (!strcmp(arg
, "--heads")) {
220 if (!strcmp(arg
, "--exclude-existing"))
221 return exclude_existing(NULL
);
222 if (!prefixcmp(arg
, "--exclude-existing="))
223 return exclude_existing(arg
+ 19);
224 usage(show_ref_usage
);
229 die("--verify requires a reference");
231 unsigned char sha1
[20];
233 if (!prefixcmp(*pattern
, "refs/") &&
234 resolve_ref(*pattern
, sha1
, 1, NULL
)) {
236 show_one(*pattern
, sha1
);
239 die("'%s' - not a valid ref", *pattern
);
248 head_ref(show_ref
, NULL
);
249 for_each_ref(show_ref
, NULL
);
251 if (verify
&& !quiet
)