Merge branch 'maint'
[git] / builtin-show-ref.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
5
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*]";
7
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;
11
12 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
13 {
14         struct object *obj;
15         const char *hex;
16         unsigned char peeled[20];
17
18         if (tags_only || heads_only) {
19                 int match;
20
21                 match = heads_only && !strncmp(refname, "refs/heads/", 11);
22                 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
23                 if (!match)
24                         return 0;
25         }
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;
42                 }
43                 return 0;
44         }
45
46 match:
47         found_match++;
48
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.
52          */
53         if (!has_sha1_file(sha1))
54                 die("git-show-ref: bad ref %s (%s)", refname,
55                     sha1_to_hex(sha1));
56
57         if (quiet)
58                 return 0;
59
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);
65
66         if (!deref_tags)
67                 return 0;
68
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);
73                 }
74         }
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);
84                 }
85         }
86         return 0;
87 }
88
89 int cmd_show_ref(int argc, const char **argv, const char *prefix)
90 {
91         int i;
92
93         for (i = 1; i < argc; i++) {
94                 const char *arg = argv[i];
95                 if (*arg != '-') {
96                         pattern = argv + i;
97                         break;
98                 }
99                 if (!strcmp(arg, "--")) {
100                         pattern = argv + i + 1;
101                         if (!*pattern)
102                                 pattern = NULL;
103                         break;
104                 }
105                 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
106                         quiet = 1;
107                         continue;
108                 }
109                 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
110                         show_head = 1;
111                         continue;
112                 }
113                 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
114                         deref_tags = 1;
115                         continue;
116                 }
117                 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
118                         hash_only = 1;
119                         continue;
120                 }
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;
133                                 }
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;
141                         }
142                         continue;
143                 }
144                 if (!strcmp(arg, "--verify")) {
145                         verify = 1;
146                         continue;
147                 }
148                 if (!strcmp(arg, "--tags")) {
149                         tags_only = 1;
150                         continue;
151                 }
152                 if (!strcmp(arg, "--heads")) {
153                         heads_only = 1;
154                         continue;
155                 }
156                 usage(show_ref_usage);
157         }
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;
165         }
166         return 0;
167 }