Code

23e0ff8fbfea1ca29e4beb18c95475dd8bdae05b
[git.git] / builtin-show-ref.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "tag.h"
5 #include "path-list.h"
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)
14 {
15         struct object *obj;
16         const char *hex;
17         unsigned char peeled[20];
19         if (tags_only || heads_only) {
20                 int match;
22                 match = heads_only && !strncmp(refname, "refs/heads/", 11);
23                 match |= tags_only && !strncmp(refname, "refs/tags/", 10);
24                 if (!match)
25                         return 0;
26         }
27         if (pattern) {
28                 int reflen = strlen(refname);
29                 const char **p = pattern, *m;
30                 while ((m = *p++) != NULL) {
31                         int len = strlen(m);
32                         if (len > reflen)
33                                 continue;
34                         if (memcmp(m, refname + reflen - len, len))
35                                 continue;
36                         if (len == reflen)
37                                 goto match;
38                         /* "--verify" requires an exact match */
39                         if (verify)
40                                 continue;
41                         if (refname[reflen - len - 1] == '/')
42                                 goto match;
43                 }
44                 return 0;
45         }
47 match:
48         found_match++;
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.
53          */
54         if (!has_sha1_file(sha1))
55                 die("git-show-ref: bad ref %s (%s)", refname,
56                     sha1_to_hex(sha1));
58         if (quiet)
59                 return 0;
61         hex = find_unique_abbrev(sha1, abbrev);
62         if (hash_only)
63                 printf("%s\n", hex);
64         else
65                 printf("%s %s\n", hex, refname);
67         if (!deref_tags)
68                 return 0;
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);
74                 }
75         }
76         else {
77                 obj = parse_object(sha1);
78                 if (!obj)
79                         die("git-show-ref: bad ref %s (%s)", refname,
80                             sha1_to_hex(sha1));
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);
85                 }
86         }
87         return 0;
88 }
90 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
91 {
92         struct path_list *list = (struct path_list *)cbdata;
93         path_list_insert(refname, list);
94         return 0;
95 }
97 /*
98  * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
99  * and
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.
105  */
106 static int exclude_existing(const char *match)
108         static struct path_list existing_refs = { NULL, 0, 0, 0 };
109         char buf[1024];
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);
115                 char *ref;
116                 if (len > 0 && buf[len - 1] == '\n')
117                         buf[--len] = '\0';
118                 if (!strcmp(buf + len - 3, "^{}")) {
119                         len -= 3;
120                         buf[len] = '\0';
121                 }
122                 for (ref = buf + len; buf < ref; ref--)
123                         if (isspace(ref[-1]))
124                                 break;
125                 if (match) {
126                         int reflen = buf + len - ref;
127                         if (reflen < matchlen)
128                                 continue;
129                         if (strncmp(ref, match, matchlen))
130                                 continue;
131                 }
132                 if (check_ref_format(ref)) {
133                         fprintf(stderr, "warning: ref '%s' ignored\n", ref);
134                         continue;
135                 }
136                 if (!path_list_has_path(&existing_refs, ref)) {
137                         printf("%s\n", buf);
138                 }
139         }
140         return 0;
143 int cmd_show_ref(int argc, const char **argv, const char *prefix)
145         int i;
147         for (i = 1; i < argc; i++) {
148                 const char *arg = argv[i];
149                 if (*arg != '-') {
150                         pattern = argv + i;
151                         break;
152                 }
153                 if (!strcmp(arg, "--")) {
154                         pattern = argv + i + 1;
155                         if (!*pattern)
156                                 pattern = NULL;
157                         break;
158                 }
159                 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
160                         quiet = 1;
161                         continue;
162                 }
163                 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
164                         show_head = 1;
165                         continue;
166                 }
167                 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
168                         deref_tags = 1;
169                         continue;
170                 }
171                 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
172                         hash_only = 1;
173                         continue;
174                 }
175                 if (!strncmp(arg, "--hash=", 7) ||
176                     (!strncmp(arg, "--abbrev", 8) &&
177                      (arg[8] == '=' || arg[8] == '\0'))) {
178                         if (arg[3] != 'h' && !arg[8])
179                                 /* --abbrev only */
180                                 abbrev = DEFAULT_ABBREV;
181                         else {
182                                 /* --hash= or --abbrev= */
183                                 char *end;
184                                 if (arg[3] == 'h') {
185                                         hash_only = 1;
186                                         arg += 7;
187                                 }
188                                 else
189                                         arg += 9;
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;
195                         }
196                         continue;
197                 }
198                 if (!strcmp(arg, "--verify")) {
199                         verify = 1;
200                         continue;
201                 }
202                 if (!strcmp(arg, "--tags")) {
203                         tags_only = 1;
204                         continue;
205                 }
206                 if (!strcmp(arg, "--heads")) {
207                         heads_only = 1;
208                         continue;
209                 }
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);
215         }
217         if (verify) {
218                 unsigned char sha1[20];
220                 while (*pattern) {
221                         if (resolve_ref(*pattern, sha1, 1, NULL))
222                                 printf("%s %s\n", sha1_to_hex(sha1),
223                                        *pattern);
224                         else if (!quiet)
225                                 die("'%s' - not a valid ref", *pattern);
226                         else
227                                 return 1;
228                         pattern++;
229                 }
230                 return 0;
231         }
233         if (show_head)
234                 head_ref(show_ref, NULL);
235         for_each_ref(show_ref, NULL);
236         if (!found_match) {
237                 if (verify && !quiet)
238                         die("No match");
239                 return 1;
240         }
241         return 0;