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*] | --filter-invalid < 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 int show_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
17 unsigned char peeled
[20];
19 if (tags_only
|| heads_only
) {
22 match
= heads_only
&& !strncmp(refname
, "refs/heads/", 11);
23 match
|= tags_only
&& !strncmp(refname
, "refs/tags/", 10);
28 int reflen
= strlen(refname
);
29 const char **p
= pattern
, *m
;
30 while ((m
= *p
++) != NULL
) {
34 if (memcmp(m
, refname
+ reflen
- len
, len
))
38 /* "--verify" requires an exact match */
41 if (refname
[reflen
- len
- 1] == '/')
50 /* This changes the semantics slightly that even under quiet we
51 * detect and return error if the repository is corrupt and
52 * ref points at a nonexistent object.
54 if (!has_sha1_file(sha1
))
55 die("git-show-ref: bad ref %s (%s)", refname
,
61 hex
= find_unique_abbrev(sha1
, abbrev
);
65 printf("%s %s\n", hex
, refname
);
70 if ((flag
& REF_ISPACKED
) && !peel_ref(refname
, peeled
)) {
71 if (!is_null_sha1(peeled
)) {
72 hex
= find_unique_abbrev(peeled
, abbrev
);
73 printf("%s %s^{}\n", hex
, refname
);
77 obj
= parse_object(sha1
);
79 die("git-show-ref: bad ref %s (%s)", refname
,
81 if (obj
->type
== OBJ_TAG
) {
82 obj
= deref_tag(obj
, refname
, 0);
83 hex
= find_unique_abbrev(obj
->sha1
, abbrev
);
84 printf("%s %s^{}\n", hex
, refname
);
90 static int add_existing(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
92 struct path_list
*list
= (struct path_list
*)cbdata
;
93 path_list_insert(refname
, list
);
98 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
100 * (1) strip "^{}" at the end of line if any;
101 * (2) ignore if match is provided and does not head-match refname;
102 * (3) warn if refname is not a well-formed refname and skip;
103 * (4) ignore if refname is a ref that exists in the local repository;
104 * (5) otherwise output the line.
106 static int exclude_existing(const char *match
)
108 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
110 int matchlen
= match
? strlen(match
) : 0;
112 for_each_ref(add_existing
, &existing_refs
);
113 while (fgets(buf
, sizeof(buf
), stdin
)) {
114 int len
= strlen(buf
);
116 if (len
> 0 && buf
[len
- 1] == '\n')
118 if (!strcmp(buf
+ len
- 3, "^{}")) {
122 for (ref
= buf
+ len
; buf
< ref
; ref
--)
123 if (isspace(ref
[-1]))
126 int reflen
= buf
+ len
- ref
;
127 if (reflen
< matchlen
)
129 if (strncmp(ref
, match
, matchlen
))
132 if (check_ref_format(ref
)) {
133 fprintf(stderr
, "warning: ref '%s' ignored\n", ref
);
136 if (!path_list_has_path(&existing_refs
, ref
)) {
143 int cmd_show_ref(int argc
, const char **argv
, const char *prefix
)
147 for (i
= 1; i
< argc
; i
++) {
148 const char *arg
= argv
[i
];
153 if (!strcmp(arg
, "--")) {
154 pattern
= argv
+ i
+ 1;
159 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
163 if (!strcmp(arg
, "-h") || !strcmp(arg
, "--head")) {
167 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--dereference")) {
171 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--hash")) {
175 if (!strncmp(arg
, "--hash=", 7) ||
176 (!strncmp(arg
, "--abbrev", 8) &&
177 (arg
[8] == '=' || arg
[8] == '\0'))) {
178 if (arg
[3] != 'h' && !arg
[8])
180 abbrev
= DEFAULT_ABBREV
;
182 /* --hash= or --abbrev= */
190 abbrev
= strtoul(arg
, &end
, 10);
191 if (*end
|| abbrev
> 40)
192 usage(show_ref_usage
);
193 if (abbrev
< MINIMUM_ABBREV
)
194 abbrev
= MINIMUM_ABBREV
;
198 if (!strcmp(arg
, "--verify")) {
202 if (!strcmp(arg
, "--tags")) {
206 if (!strcmp(arg
, "--heads")) {
210 if (!strcmp(arg
, "--exclude-existing"))
211 return exclude_existing(NULL
);
212 if (!strncmp(arg
, "--exclude-existing=", 19))
213 return exclude_existing(arg
+ 19);
214 usage(show_ref_usage
);
218 unsigned char sha1
[20];
221 if (resolve_ref(*pattern
, sha1
, 1, NULL
))
222 printf("%s %s\n", sha1_to_hex(sha1
),
225 die("'%s' - not a valid ref", *pattern
);
234 head_ref(show_ref
, NULL
);
235 for_each_ref(show_ref
, NULL
);
237 if (verify
&& !quiet
)