git-am documentation: describe what is taken from where.
[git/jnareb-git/bp-gitweb.git] / builtin-show-ref.c
blob9463ff0e69b15fc0e544259e64960bc942a98368
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*] < 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 void show_one(const char *refname, const unsigned char *sha1)
15 const char *hex = find_unique_abbrev(sha1, abbrev);
16 if (hash_only)
17 printf("%s\n", hex);
18 else
19 printf("%s %s\n", hex, refname);
22 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
24 struct object *obj;
25 const char *hex;
26 unsigned char peeled[20];
28 if (tags_only || heads_only) {
29 int match;
31 match = heads_only && !prefixcmp(refname, "refs/heads/");
32 match |= tags_only && !prefixcmp(refname, "refs/tags/");
33 if (!match)
34 return 0;
36 if (pattern) {
37 int reflen = strlen(refname);
38 const char **p = pattern, *m;
39 while ((m = *p++) != NULL) {
40 int len = strlen(m);
41 if (len > reflen)
42 continue;
43 if (memcmp(m, refname + reflen - len, len))
44 continue;
45 if (len == reflen)
46 goto match;
47 /* "--verify" requires an exact match */
48 if (verify)
49 continue;
50 if (refname[reflen - len - 1] == '/')
51 goto match;
53 return 0;
56 match:
57 found_match++;
59 /* This changes the semantics slightly that even under quiet we
60 * detect and return error if the repository is corrupt and
61 * ref points at a nonexistent object.
63 if (!has_sha1_file(sha1))
64 die("git-show-ref: bad ref %s (%s)", refname,
65 sha1_to_hex(sha1));
67 if (quiet)
68 return 0;
70 show_one(refname, sha1);
72 if (!deref_tags)
73 return 0;
75 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
76 if (!is_null_sha1(peeled)) {
77 hex = find_unique_abbrev(peeled, abbrev);
78 printf("%s %s^{}\n", hex, refname);
81 else {
82 obj = parse_object(sha1);
83 if (!obj)
84 die("git-show-ref: bad ref %s (%s)", refname,
85 sha1_to_hex(sha1));
86 if (obj->type == OBJ_TAG) {
87 obj = deref_tag(obj, refname, 0);
88 hex = find_unique_abbrev(obj->sha1, abbrev);
89 printf("%s %s^{}\n", hex, refname);
92 return 0;
95 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
97 struct path_list *list = (struct path_list *)cbdata;
98 path_list_insert(refname, list);
99 return 0;
103 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
104 * and
105 * (1) strip "^{}" at the end of line if any;
106 * (2) ignore if match is provided and does not head-match refname;
107 * (3) warn if refname is not a well-formed refname and skip;
108 * (4) ignore if refname is a ref that exists in the local repository;
109 * (5) otherwise output the line.
111 static int exclude_existing(const char *match)
113 static struct path_list existing_refs = { NULL, 0, 0, 0 };
114 char buf[1024];
115 int matchlen = match ? strlen(match) : 0;
117 for_each_ref(add_existing, &existing_refs);
118 while (fgets(buf, sizeof(buf), stdin)) {
119 char *ref;
120 int len = strlen(buf);
122 if (len > 0 && buf[len - 1] == '\n')
123 buf[--len] = '\0';
124 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
125 len -= 3;
126 buf[len] = '\0';
128 for (ref = buf + len; buf < ref; ref--)
129 if (isspace(ref[-1]))
130 break;
131 if (match) {
132 int reflen = buf + len - ref;
133 if (reflen < matchlen)
134 continue;
135 if (strncmp(ref, match, matchlen))
136 continue;
138 if (check_ref_format(ref)) {
139 fprintf(stderr, "warning: ref '%s' ignored\n", ref);
140 continue;
142 if (!path_list_has_path(&existing_refs, ref)) {
143 printf("%s\n", buf);
146 return 0;
149 int cmd_show_ref(int argc, const char **argv, const char *prefix)
151 int i;
153 for (i = 1; i < argc; i++) {
154 const char *arg = argv[i];
155 if (*arg != '-') {
156 pattern = argv + i;
157 break;
159 if (!strcmp(arg, "--")) {
160 pattern = argv + i + 1;
161 if (!*pattern)
162 pattern = NULL;
163 break;
165 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
166 quiet = 1;
167 continue;
169 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
170 show_head = 1;
171 continue;
173 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
174 deref_tags = 1;
175 continue;
177 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
178 hash_only = 1;
179 continue;
181 if (!prefixcmp(arg, "--hash=") ||
182 (!prefixcmp(arg, "--abbrev") &&
183 (arg[8] == '=' || arg[8] == '\0'))) {
184 if (arg[2] != 'h' && !arg[8])
185 /* --abbrev only */
186 abbrev = DEFAULT_ABBREV;
187 else {
188 /* --hash= or --abbrev= */
189 char *end;
190 if (arg[2] == 'h') {
191 hash_only = 1;
192 arg += 7;
194 else
195 arg += 9;
196 abbrev = strtoul(arg, &end, 10);
197 if (*end || abbrev > 40)
198 usage(show_ref_usage);
199 if (abbrev < MINIMUM_ABBREV)
200 abbrev = MINIMUM_ABBREV;
202 continue;
204 if (!strcmp(arg, "--verify")) {
205 verify = 1;
206 continue;
208 if (!strcmp(arg, "--tags")) {
209 tags_only = 1;
210 continue;
212 if (!strcmp(arg, "--heads")) {
213 heads_only = 1;
214 continue;
216 if (!strcmp(arg, "--exclude-existing"))
217 return exclude_existing(NULL);
218 if (!prefixcmp(arg, "--exclude-existing="))
219 return exclude_existing(arg + 19);
220 usage(show_ref_usage);
223 if (verify) {
224 if (!pattern)
225 die("--verify requires a reference");
226 while (*pattern) {
227 unsigned char sha1[20];
229 if (!prefixcmp(*pattern, "refs/") &&
230 resolve_ref(*pattern, sha1, 1, NULL)) {
231 if (!quiet)
232 show_one(*pattern, sha1);
234 else if (!quiet)
235 die("'%s' - not a valid ref", *pattern);
236 else
237 return 1;
238 pattern++;
240 return 0;
243 if (show_head)
244 head_ref(show_ref, NULL);
245 for_each_ref(show_ref, NULL);
246 if (!found_match) {
247 if (verify && !quiet)
248 die("No match");
249 return 1;
251 return 0;