blob: 572b114119db15f5f42dd79e3bc15e6d219f71db [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -07002#include "cache.h"
3#include "refs.h"
4#include "object.h"
5#include "tag.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Linus Torvalds358ddb62006-09-15 11:19:32 -07007
Junio C Hamanod8285af2006-12-18 13:33:47 -08008static 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";
Linus Torvalds358ddb62006-09-15 11:19:32 -07009
Christian Couderc40abef2006-09-17 06:20:24 +020010static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
Junio C Hamano2eaf2222006-10-01 00:27:27 -070011 found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
Linus Torvalds358ddb62006-09-15 11:19:32 -070012static const char **pattern;
13
Junio C Hamano64fe0312006-12-17 19:27:49 -080014static 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}
22
Junio C Hamanoeaf12a82006-09-21 00:40:28 -070023static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
Linus Torvalds358ddb62006-09-15 11:19:32 -070024{
25 struct object *obj;
Junio C Hamano2eaf2222006-10-01 00:27:27 -070026 const char *hex;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080027 unsigned char peeled[20];
Linus Torvalds358ddb62006-09-15 11:19:32 -070028
29 if (tags_only || heads_only) {
30 int match;
31
Junio C Hamanocc44c762007-02-20 01:53:29 -080032 match = heads_only && !prefixcmp(refname, "refs/heads/");
33 match |= tags_only && !prefixcmp(refname, "refs/tags/");
Linus Torvalds358ddb62006-09-15 11:19:32 -070034 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 }
56
57match:
58 found_match++;
Junio C Hamanocf0adba2006-11-19 13:22:44 -080059
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))
Junio C Hamano7e44c932008-08-31 09:39:19 -070065 die("git show-ref: bad ref %s (%s)", refname,
Junio C Hamanocf0adba2006-11-19 13:22:44 -080066 sha1_to_hex(sha1));
67
Linus Torvalds358ddb62006-09-15 11:19:32 -070068 if (quiet)
69 return 0;
Junio C Hamano2eaf2222006-10-01 00:27:27 -070070
Junio C Hamano64fe0312006-12-17 19:27:49 -080071 show_one(refname, sha1);
Junio C Hamanocf0adba2006-11-19 13:22:44 -080072
73 if (!deref_tags)
74 return 0;
75
76 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
Junio C Hamanof4204ab2006-11-21 23:36:35 -080077 if (!is_null_sha1(peeled)) {
78 hex = find_unique_abbrev(peeled, abbrev);
79 printf("%s %s^{}\n", hex, refname);
80 }
Linus Torvalds358ddb62006-09-15 11:19:32 -070081 }
Junio C Hamanocf0adba2006-11-19 13:22:44 -080082 else {
83 obj = parse_object(sha1);
84 if (!obj)
Junio C Hamano7e44c932008-08-31 09:39:19 -070085 die("git show-ref: bad ref %s (%s)", refname,
Junio C Hamanocf0adba2006-11-19 13:22:44 -080086 sha1_to_hex(sha1));
87 if (obj->type == OBJ_TAG) {
88 obj = deref_tag(obj, refname, 0);
Martin Koegleraffeef12008-02-18 08:31:54 +010089 if (!obj)
Junio C Hamano7e44c932008-08-31 09:39:19 -070090 die("git show-ref: bad tag at ref %s (%s)", refname,
Martin Koegleraffeef12008-02-18 08:31:54 +010091 sha1_to_hex(sha1));
Junio C Hamanocf0adba2006-11-19 13:22:44 -080092 hex = find_unique_abbrev(obj->sha1, abbrev);
93 printf("%s %s^{}\n", hex, refname);
94 }
95 }
Linus Torvalds358ddb62006-09-15 11:19:32 -070096 return 0;
97}
98
Junio C Hamanoed9f7c92006-12-17 17:57:19 -080099static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
100{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100101 struct string_list *list = (struct string_list *)cbdata;
102 string_list_insert(refname, list);
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800103 return 0;
104}
105
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 */
115static int exclude_existing(const char *match)
116{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100117 static struct string_list existing_refs = { NULL, 0, 0, 0 };
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800118 char buf[1024];
119 int matchlen = match ? strlen(match) : 0;
120
121 for_each_ref(add_existing, &existing_refs);
122 while (fgets(buf, sizeof(buf), stdin)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800123 char *ref;
Junio C Hamanod8285af2006-12-18 13:33:47 -0800124 int len = strlen(buf);
125
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800126 if (len > 0 && buf[len - 1] == '\n')
127 buf[--len] = '\0';
Junio C Hamanod8285af2006-12-18 13:33:47 -0800128 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800129 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 }
Johannes Schindelinc455c872008-07-21 19:03:49 +0100146 if (!string_list_has_string(&existing_refs, ref)) {
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800147 printf("%s\n", buf);
148 }
149 }
150 return 0;
151}
152
Linus Torvalds358ddb62006-09-15 11:19:32 -0700153int cmd_show_ref(int argc, const char **argv, const char *prefix)
154{
155 int i;
156
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 }
Christian Couderc40abef2006-09-17 06:20:24 +0200181 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
182 hash_only = 1;
183 continue;
184 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800185 if (!prefixcmp(arg, "--hash=") ||
186 (!prefixcmp(arg, "--abbrev") &&
Junio C Hamano2eaf2222006-10-01 00:27:27 -0700187 (arg[8] == '=' || arg[8] == '\0'))) {
Junio C Hamano64fe0312006-12-17 19:27:49 -0800188 if (arg[2] != 'h' && !arg[8])
Junio C Hamano2eaf2222006-10-01 00:27:27 -0700189 /* --abbrev only */
190 abbrev = DEFAULT_ABBREV;
191 else {
192 /* --hash= or --abbrev= */
193 char *end;
Junio C Hamano64fe0312006-12-17 19:27:49 -0800194 if (arg[2] == 'h') {
Junio C Hamano2eaf2222006-10-01 00:27:27 -0700195 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 }
Linus Torvalds358ddb62006-09-15 11:19:32 -0700208 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 }
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800220 if (!strcmp(arg, "--exclude-existing"))
221 return exclude_existing(NULL);
Junio C Hamanocc44c762007-02-20 01:53:29 -0800222 if (!prefixcmp(arg, "--exclude-existing="))
Junio C Hamanoed9f7c92006-12-17 17:57:19 -0800223 return exclude_existing(arg + 19);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700224 usage(show_ref_usage);
225 }
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800226
227 if (verify) {
Dmitry V. Levin8ab40a22007-02-23 20:12:33 +0300228 if (!pattern)
229 die("--verify requires a reference");
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800230 while (*pattern) {
Dmitry V. Levin8ab40a22007-02-23 20:12:33 +0300231 unsigned char sha1[20];
232
Junio C Hamanocc44c762007-02-20 01:53:29 -0800233 if (!prefixcmp(*pattern, "refs/") &&
Junio C Hamano64fe0312006-12-17 19:27:49 -0800234 resolve_ref(*pattern, sha1, 1, NULL)) {
Junio C Hamanodd914292006-12-17 18:53:24 -0800235 if (!quiet)
Junio C Hamano64fe0312006-12-17 19:27:49 -0800236 show_one(*pattern, sha1);
Junio C Hamanodd914292006-12-17 18:53:24 -0800237 }
Junio C Hamano26cdd1e2006-12-17 18:08:52 -0800238 else if (!quiet)
239 die("'%s' - not a valid ref", *pattern);
240 else
241 return 1;
242 pattern++;
243 }
244 return 0;
245 }
246
Linus Torvalds358ddb62006-09-15 11:19:32 -0700247 if (show_head)
Junio C Hamanoeaf12a82006-09-21 00:40:28 -0700248 head_ref(show_ref, NULL);
249 for_each_ref(show_ref, NULL);
Linus Torvalds358ddb62006-09-15 11:19:32 -0700250 if (!found_match) {
251 if (verify && !quiet)
252 die("No match");
253 return 1;
254 }
255 return 0;
256}