Store peeled refs in packed-refs file.
[git.git] / builtin-show-ref.c
blob9ae3d08546c74a579e9bb13285424290b4449d4d
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
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)
14 struct object *obj;
15 const char *hex;
16 unsigned char peeled[20];
18 if (tags_only || heads_only) {
19 int match;
21 match = heads_only && !strncmp(refname, "refs/heads/", 11);
22 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
23 if (!match)
24 return 0;
26 if (pattern) {
27 int reflen = strlen(refname);
28 const char **p = pattern, *m;
29 while ((m = *p++) != NULL) {
30 int len = strlen(m);
31 if (len > reflen)
32 continue;
33 if (memcmp(m, refname + reflen - len, len))
34 continue;
35 if (len == reflen)
36 goto match;
37 /* "--verify" requires an exact match */
38 if (verify)
39 continue;
40 if (refname[reflen - len - 1] == '/')
41 goto match;
43 return 0;
46 match:
47 found_match++;
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,
55 sha1_to_hex(sha1));
57 if (quiet)
58 return 0;
60 hex = find_unique_abbrev(sha1, abbrev);
61 if (hash_only)
62 printf("%s\n", hex);
63 else
64 printf("%s %s\n", hex, refname);
66 if (!deref_tags)
67 return 0;
69 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
70 hex = find_unique_abbrev(peeled, abbrev);
71 printf("%s %s^{}\n", hex, refname);
73 else {
74 obj = parse_object(sha1);
75 if (!obj)
76 die("git-show-ref: bad ref %s (%s)", refname,
77 sha1_to_hex(sha1));
78 if (obj->type == OBJ_TAG) {
79 obj = deref_tag(obj, refname, 0);
80 hex = find_unique_abbrev(obj->sha1, abbrev);
81 printf("%s %s^{}\n", hex, refname);
84 return 0;
87 int cmd_show_ref(int argc, const char **argv, const char *prefix)
89 int i;
91 for (i = 1; i < argc; i++) {
92 const char *arg = argv[i];
93 if (*arg != '-') {
94 pattern = argv + i;
95 break;
97 if (!strcmp(arg, "--")) {
98 pattern = argv + i + 1;
99 if (!*pattern)
100 pattern = NULL;
101 break;
103 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
104 quiet = 1;
105 continue;
107 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
108 show_head = 1;
109 continue;
111 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
112 deref_tags = 1;
113 continue;
115 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
116 hash_only = 1;
117 continue;
119 if (!strncmp(arg, "--hash=", 7) ||
120 (!strncmp(arg, "--abbrev", 8) &&
121 (arg[8] == '=' || arg[8] == '\0'))) {
122 if (arg[3] != 'h' && !arg[8])
123 /* --abbrev only */
124 abbrev = DEFAULT_ABBREV;
125 else {
126 /* --hash= or --abbrev= */
127 char *end;
128 if (arg[3] == 'h') {
129 hash_only = 1;
130 arg += 7;
132 else
133 arg += 9;
134 abbrev = strtoul(arg, &end, 10);
135 if (*end || abbrev > 40)
136 usage(show_ref_usage);
137 if (abbrev < MINIMUM_ABBREV)
138 abbrev = MINIMUM_ABBREV;
140 continue;
142 if (!strcmp(arg, "--verify")) {
143 verify = 1;
144 continue;
146 if (!strcmp(arg, "--tags")) {
147 tags_only = 1;
148 continue;
150 if (!strcmp(arg, "--heads")) {
151 heads_only = 1;
152 continue;
154 usage(show_ref_usage);
156 if (show_head)
157 head_ref(show_ref, NULL);
158 for_each_ref(show_ref, NULL);
159 if (!found_match) {
160 if (verify && !quiet)
161 die("No match");
162 return 1;
164 return 0;