Code

Rename path_list to string_list
[git.git] / builtin-show-ref.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "object.h"
5 #include "tag.h"
6 #include "string-list.h"
8 static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list";
10 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
11         found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
12 static const char **pattern;
14 static void show_one(const char *refname, const unsigned char *sha1)
15 {
16         const char *hex = find_unique_abbrev(sha1, abbrev);
17         if (hash_only)
18                 printf("%s\n", hex);
19         else
20                 printf("%s %s\n", hex, refname);
21 }
23 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
24 {
25         struct object *obj;
26         const char *hex;
27         unsigned char peeled[20];
29         if (tags_only || heads_only) {
30                 int match;
32                 match = heads_only && !prefixcmp(refname, "refs/heads/");
33                 match |= tags_only && !prefixcmp(refname, "refs/tags/");
34                 if (!match)
35                         return 0;
36         }
37         if (pattern) {
38                 int reflen = strlen(refname);
39                 const char **p = pattern, *m;
40                 while ((m = *p++) != NULL) {
41                         int len = strlen(m);
42                         if (len > reflen)
43                                 continue;
44                         if (memcmp(m, refname + reflen - len, len))
45                                 continue;
46                         if (len == reflen)
47                                 goto match;
48                         /* "--verify" requires an exact match */
49                         if (verify)
50                                 continue;
51                         if (refname[reflen - len - 1] == '/')
52                                 goto match;
53                 }
54                 return 0;
55         }
57 match:
58         found_match++;
60         /* This changes the semantics slightly that even under quiet we
61          * detect and return error if the repository is corrupt and
62          * ref points at a nonexistent object.
63          */
64         if (!has_sha1_file(sha1))
65                 die("git-show-ref: bad ref %s (%s)", refname,
66                     sha1_to_hex(sha1));
68         if (quiet)
69                 return 0;
71         show_one(refname, sha1);
73         if (!deref_tags)
74                 return 0;
76         if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
77                 if (!is_null_sha1(peeled)) {
78                         hex = find_unique_abbrev(peeled, abbrev);
79                         printf("%s %s^{}\n", hex, refname);
80                 }
81         }
82         else {
83                 obj = parse_object(sha1);
84                 if (!obj)
85                         die("git-show-ref: bad ref %s (%s)", refname,
86                             sha1_to_hex(sha1));
87                 if (obj->type == OBJ_TAG) {
88                         obj = deref_tag(obj, refname, 0);
89                         if (!obj)
90                                 die("git-show-ref: bad tag at ref %s (%s)", refname,
91                                     sha1_to_hex(sha1));
92                         hex = find_unique_abbrev(obj->sha1, abbrev);
93                         printf("%s %s^{}\n", hex, refname);
94                 }
95         }
96         return 0;
97 }
99 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
101         struct string_list *list = (struct string_list *)cbdata;
102         string_list_insert(refname, list);
103         return 0;
106 /*
107  * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
108  * and
109  * (1) strip "^{}" at the end of line if any;
110  * (2) ignore if match is provided and does not head-match refname;
111  * (3) warn if refname is not a well-formed refname and skip;
112  * (4) ignore if refname is a ref that exists in the local repository;
113  * (5) otherwise output the line.
114  */
115 static int exclude_existing(const char *match)
117         static struct string_list existing_refs = { NULL, 0, 0, 0 };
118         char buf[1024];
119         int matchlen = match ? strlen(match) : 0;
121         for_each_ref(add_existing, &existing_refs);
122         while (fgets(buf, sizeof(buf), stdin)) {
123                 char *ref;
124                 int len = strlen(buf);
126                 if (len > 0 && buf[len - 1] == '\n')
127                         buf[--len] = '\0';
128                 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
129                         len -= 3;
130                         buf[len] = '\0';
131                 }
132                 for (ref = buf + len; buf < ref; ref--)
133                         if (isspace(ref[-1]))
134                                 break;
135                 if (match) {
136                         int reflen = buf + len - ref;
137                         if (reflen < matchlen)
138                                 continue;
139                         if (strncmp(ref, match, matchlen))
140                                 continue;
141                 }
142                 if (check_ref_format(ref)) {
143                         fprintf(stderr, "warning: ref '%s' ignored\n", ref);
144                         continue;
145                 }
146                 if (!string_list_has_string(&existing_refs, ref)) {
147                         printf("%s\n", buf);
148                 }
149         }
150         return 0;
153 int cmd_show_ref(int argc, const char **argv, const char *prefix)
155         int i;
157         for (i = 1; i < argc; i++) {
158                 const char *arg = argv[i];
159                 if (*arg != '-') {
160                         pattern = argv + i;
161                         break;
162                 }
163                 if (!strcmp(arg, "--")) {
164                         pattern = argv + i + 1;
165                         if (!*pattern)
166                                 pattern = NULL;
167                         break;
168                 }
169                 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
170                         quiet = 1;
171                         continue;
172                 }
173                 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
174                         show_head = 1;
175                         continue;
176                 }
177                 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
178                         deref_tags = 1;
179                         continue;
180                 }
181                 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
182                         hash_only = 1;
183                         continue;
184                 }
185                 if (!prefixcmp(arg, "--hash=") ||
186                     (!prefixcmp(arg, "--abbrev") &&
187                      (arg[8] == '=' || arg[8] == '\0'))) {
188                         if (arg[2] != 'h' && !arg[8])
189                                 /* --abbrev only */
190                                 abbrev = DEFAULT_ABBREV;
191                         else {
192                                 /* --hash= or --abbrev= */
193                                 char *end;
194                                 if (arg[2] == 'h') {
195                                         hash_only = 1;
196                                         arg += 7;
197                                 }
198                                 else
199                                         arg += 9;
200                                 abbrev = strtoul(arg, &end, 10);
201                                 if (*end || abbrev > 40)
202                                         usage(show_ref_usage);
203                                 if (abbrev < MINIMUM_ABBREV)
204                                         abbrev = MINIMUM_ABBREV;
205                         }
206                         continue;
207                 }
208                 if (!strcmp(arg, "--verify")) {
209                         verify = 1;
210                         continue;
211                 }
212                 if (!strcmp(arg, "--tags")) {
213                         tags_only = 1;
214                         continue;
215                 }
216                 if (!strcmp(arg, "--heads")) {
217                         heads_only = 1;
218                         continue;
219                 }
220                 if (!strcmp(arg, "--exclude-existing"))
221                         return exclude_existing(NULL);
222                 if (!prefixcmp(arg, "--exclude-existing="))
223                         return exclude_existing(arg + 19);
224                 usage(show_ref_usage);
225         }
227         if (verify) {
228                 if (!pattern)
229                         die("--verify requires a reference");
230                 while (*pattern) {
231                         unsigned char sha1[20];
233                         if (!prefixcmp(*pattern, "refs/") &&
234                             resolve_ref(*pattern, sha1, 1, NULL)) {
235                                 if (!quiet)
236                                         show_one(*pattern, sha1);
237                         }
238                         else if (!quiet)
239                                 die("'%s' - not a valid ref", *pattern);
240                         else
241                                 return 1;
242                         pattern++;
243                 }
244                 return 0;
245         }
247         if (show_head)
248                 head_ref(show_ref, NULL);
249         for_each_ref(show_ref, NULL);
250         if (!found_match) {
251                 if (verify && !quiet)
252                         die("No match");
253                 return 1;
254         }
255         return 0;