De-emphasise the symbolic link documentation.
[git/jrn.git] / builtin-show-ref.c
blob073979855b30b72363ed8116288b7bbbb2f0ff74
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 if (!is_null_sha1(peeled)) {
71 hex = find_unique_abbrev(peeled, abbrev);
72 printf("%s %s^{}\n", hex, refname);
75 else {
76 obj = parse_object(sha1);
77 if (!obj)
78 die("git-show-ref: bad ref %s (%s)", refname,
79 sha1_to_hex(sha1));
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);
86 return 0;
89 int cmd_show_ref(int argc, const char **argv, const char *prefix)
91 int i;
93 for (i = 1; i < argc; i++) {
94 const char *arg = argv[i];
95 if (*arg != '-') {
96 pattern = argv + i;
97 break;
99 if (!strcmp(arg, "--")) {
100 pattern = argv + i + 1;
101 if (!*pattern)
102 pattern = NULL;
103 break;
105 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
106 quiet = 1;
107 continue;
109 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
110 show_head = 1;
111 continue;
113 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
114 deref_tags = 1;
115 continue;
117 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
118 hash_only = 1;
119 continue;
121 if (!strncmp(arg, "--hash=", 7) ||
122 (!strncmp(arg, "--abbrev", 8) &&
123 (arg[8] == '=' || arg[8] == '\0'))) {
124 if (arg[3] != 'h' && !arg[8])
125 /* --abbrev only */
126 abbrev = DEFAULT_ABBREV;
127 else {
128 /* --hash= or --abbrev= */
129 char *end;
130 if (arg[3] == 'h') {
131 hash_only = 1;
132 arg += 7;
134 else
135 arg += 9;
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;
142 continue;
144 if (!strcmp(arg, "--verify")) {
145 verify = 1;
146 continue;
148 if (!strcmp(arg, "--tags")) {
149 tags_only = 1;
150 continue;
152 if (!strcmp(arg, "--heads")) {
153 heads_only = 1;
154 continue;
156 usage(show_ref_usage);
158 if (show_head)
159 head_ref(show_ref, NULL);
160 for_each_ref(show_ref, NULL);
161 if (!found_match) {
162 if (verify && !quiet)
163 die("No match");
164 return 1;
166 return 0;