6 static const char show_ref_usage
[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*]";
8 static int deref_tags
= 0, show_head
= 0, tags_only
= 0, heads_only
= 0,
9 found_match
= 0, verify
= 0, quiet
= 0, hash_only
= 0, abbrev
= 0;
10 static const char **pattern
;
12 static int show_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cbdata
)
16 unsigned char peeled
[20];
18 if (tags_only
|| heads_only
) {
21 match
= heads_only
&& !strncmp(refname
, "refs/heads/", 11);
22 match
|= tags_only
&& !strncmp(refname
, "refs/tags/", 10);
27 int reflen
= strlen(refname
);
28 const char **p
= pattern
, *m
;
29 while ((m
= *p
++) != NULL
) {
33 if (memcmp(m
, refname
+ reflen
- len
, len
))
37 /* "--verify" requires an exact match */
40 if (refname
[reflen
- len
- 1] == '/')
49 /* This changes the semantics slightly that even under quiet we
50 * detect and return error if the repository is corrupt and
51 * ref points at a nonexistent object.
53 if (!has_sha1_file(sha1
))
54 die("git-show-ref: bad ref %s (%s)", refname
,
60 hex
= find_unique_abbrev(sha1
, abbrev
);
64 printf("%s %s\n", hex
, refname
);
69 if ((flag
& REF_ISPACKED
) && !peel_ref(refname
, peeled
)) {
70 if (!is_null_sha1(peeled
)) {
71 hex
= find_unique_abbrev(peeled
, abbrev
);
72 printf("%s %s^{}\n", hex
, refname
);
76 obj
= parse_object(sha1
);
78 die("git-show-ref: bad ref %s (%s)", refname
,
80 if (obj
->type
== OBJ_TAG
) {
81 obj
= deref_tag(obj
, refname
, 0);
82 hex
= find_unique_abbrev(obj
->sha1
, abbrev
);
83 printf("%s %s^{}\n", hex
, refname
);
89 int cmd_show_ref(int argc
, const char **argv
, const char *prefix
)
93 for (i
= 1; i
< argc
; i
++) {
94 const char *arg
= argv
[i
];
99 if (!strcmp(arg
, "--")) {
100 pattern
= argv
+ i
+ 1;
105 if (!strcmp(arg
, "-q") || !strcmp(arg
, "--quiet")) {
109 if (!strcmp(arg
, "-h") || !strcmp(arg
, "--head")) {
113 if (!strcmp(arg
, "-d") || !strcmp(arg
, "--dereference")) {
117 if (!strcmp(arg
, "-s") || !strcmp(arg
, "--hash")) {
121 if (!strncmp(arg
, "--hash=", 7) ||
122 (!strncmp(arg
, "--abbrev", 8) &&
123 (arg
[8] == '=' || arg
[8] == '\0'))) {
124 if (arg
[3] != 'h' && !arg
[8])
126 abbrev
= DEFAULT_ABBREV
;
128 /* --hash= or --abbrev= */
136 abbrev
= strtoul(arg
, &end
, 10);
137 if (*end
|| abbrev
> 40)
138 usage(show_ref_usage
);
139 if (abbrev
< MINIMUM_ABBREV
)
140 abbrev
= MINIMUM_ABBREV
;
144 if (!strcmp(arg
, "--verify")) {
148 if (!strcmp(arg
, "--tags")) {
152 if (!strcmp(arg
, "--heads")) {
156 usage(show_ref_usage
);
159 head_ref(show_ref
, NULL
);
160 for_each_ref(show_ref
, NULL
);
162 if (verify
&& !quiet
)