7 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";
9 static int deref_tags
= 0, show_head
= 0, tags_only
= 0, heads_only
= 0,
10 found_match
= 0, verify
= 0, quiet
= 0, hash_only
= 0, abbrev
= 0;
11 static const char **pattern
;
13 static void show_one(const char *refname
, const unsigned char *sha1
)
15 const char *hex
= find_unique_abbrev(sha1
, abbrev
);
19 printf("%s %s\n", hex
, refname
);
22 static int show_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
26 unsigned char peeled
[20];
28 if (tags_only
|| heads_only
) {
31 match
= heads_only
&& !prefixcmp(refname
, "refs/heads/");
32 match
|= tags_only
&& !prefixcmp(refname
, "refs/tags/");
37 int reflen
= strlen(refname
);
38 const char **p
= pattern
, *m
;
39 while ((m
= *p
++) != NULL
) {
43 if (memcmp(m
, refname
+ reflen
- len
, len
))
47 /* "--verify" requires an exact match */
50 if (refname
[reflen
- len
- 1] == '/')
59 /* This changes the semantics slightly that even under quiet we
60 * detect and return error if the repository is corrupt and
61 * ref points at a nonexistent object.
63 if (!has_sha1_file(sha1
))
64 die("git-show-ref: bad ref %s (%s)", refname
,
70 show_one(refname
, sha1
);
75 if ((flag
& REF_ISPACKED
) && !peel_ref(refname
, peeled
)) {
76 if (!is_null_sha1(peeled
)) {
77 hex
= find_unique_abbrev(peeled
, abbrev
);
78 printf("%s %s^{}\n", hex
, refname
);
82 obj
= parse_object(sha1
);
84 die("git-show-ref: bad ref %s (%s)", refname
,
86 if (obj
->type
== OBJ_TAG
) {
87 obj
= deref_tag(obj
, refname
, 0);
88 hex
= find_unique_abbrev(obj
->sha1
, abbrev
);
89 printf("%s %s^{}\n", hex
, refname
);
95 static int add_existing(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
97 struct path_list
*list
= (struct path_list
*)cbdata
;
98 path_list_insert(refname
, list
);
103 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
105 * (1) strip "^{}" at the end of line if any;
106 * (2) ignore if match is provided and does not head-match refname;
107 * (3) warn if refname is not a well-formed refname and skip;
108 * (4) ignore if refname is a ref that exists in the local repository;
109 * (5) otherwise output the line.
111 static int exclude_existing(const char *match
)
113 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
115 int matchlen
= match
? strlen(match
) : 0;
117 for_each_ref(add_existing
, &existing_refs
);
118 while (fgets(buf
, sizeof(buf
), stdin
)) {
120 int len
= strlen(buf
);
122 if (len
> 0 && buf
[len
- 1] == '\n')
124 if (3 <= len
&& !strcmp(buf
+ len
- 3, "^{}")) {
128 for (ref
= buf
+ len
; buf
< ref
; ref
--)
129 if (isspace(ref
[-1]))
132 int reflen
= buf
+ len
- ref
;
133 if (reflen
< matchlen
)
135 if (strncmp(ref
, match
, matchlen
))
138 if (check_ref_format(ref
)) {
139 fprintf(stderr
, "warning: ref '%s' ignored\n", ref
);
142 if (!path_list_has_path(&existing_refs
, ref
)) {
149 int cmd_show_ref(int argc
, const char **argv
, const char *prefix
)
153 for (i
= 1; i
< argc
; i
++) {
154 const char *arg
= argv
[i
];
159 if (!strcmp(arg
, "--")) {
160 pattern
= argv
+ i
+ 1;
165 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
169 if (!strcmp(arg
, "-h") || !strcmp(arg
, "--head")) {
173 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--dereference")) {
177 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--hash")) {
181 if (!prefixcmp(arg
, "--hash=") ||
182 (!prefixcmp(arg
, "--abbrev") &&
183 (arg
[8] == '=' || arg
[8] == '\0'))) {
184 if (arg
[2] != 'h' && !arg
[8])
186 abbrev
= DEFAULT_ABBREV
;
188 /* --hash= or --abbrev= */
196 abbrev
= strtoul(arg
, &end
, 10);
197 if (*end
|| abbrev
> 40)
198 usage(show_ref_usage
);
199 if (abbrev
< MINIMUM_ABBREV
)
200 abbrev
= MINIMUM_ABBREV
;
204 if (!strcmp(arg
, "--verify")) {
208 if (!strcmp(arg
, "--tags")) {
212 if (!strcmp(arg
, "--heads")) {
216 if (!strcmp(arg
, "--exclude-existing"))
217 return exclude_existing(NULL
);
218 if (!prefixcmp(arg
, "--exclude-existing="))
219 return exclude_existing(arg
+ 19);
220 usage(show_ref_usage
);
225 die("--verify requires a reference");
227 unsigned char sha1
[20];
229 if (!prefixcmp(*pattern
, "refs/") &&
230 resolve_ref(*pattern
, sha1
, 1, NULL
)) {
232 show_one(*pattern
, sha1
);
235 die("'%s' - not a valid ref", *pattern
);
244 head_ref(show_ref
, NULL
);
245 for_each_ref(show_ref
, NULL
);
247 if (verify
&& !quiet
)