rebase -i: squash should retain the authorship of the _first_ commit
[git/jrn.git] / builtin-show-ref.c
blob65051d14fde44c14d12099df656ac423bc1c347e
1 #include "builtin.h"
2 #include "cache.h"
3 #include "refs.h"
4 #include "object.h"
5 #include "tag.h"
6 #include "path-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)
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);
23 static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
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;
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;
54 return 0;
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.
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);
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 hex = find_unique_abbrev(obj->sha1, abbrev);
90 printf("%s %s^{}\n", hex, refname);
93 return 0;
96 static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
98 struct path_list *list = (struct path_list *)cbdata;
99 path_list_insert(refname, list);
100 return 0;
104 * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
105 * and
106 * (1) strip "^{}" at the end of line if any;
107 * (2) ignore if match is provided and does not head-match refname;
108 * (3) warn if refname is not a well-formed refname and skip;
109 * (4) ignore if refname is a ref that exists in the local repository;
110 * (5) otherwise output the line.
112 static int exclude_existing(const char *match)
114 static struct path_list existing_refs = { NULL, 0, 0, 0 };
115 char buf[1024];
116 int matchlen = match ? strlen(match) : 0;
118 for_each_ref(add_existing, &existing_refs);
119 while (fgets(buf, sizeof(buf), stdin)) {
120 char *ref;
121 int len = strlen(buf);
123 if (len > 0 && buf[len - 1] == '\n')
124 buf[--len] = '\0';
125 if (3 <= len && !strcmp(buf + len - 3, "^{}")) {
126 len -= 3;
127 buf[len] = '\0';
129 for (ref = buf + len; buf < ref; ref--)
130 if (isspace(ref[-1]))
131 break;
132 if (match) {
133 int reflen = buf + len - ref;
134 if (reflen < matchlen)
135 continue;
136 if (strncmp(ref, match, matchlen))
137 continue;
139 if (check_ref_format(ref)) {
140 fprintf(stderr, "warning: ref '%s' ignored\n", ref);
141 continue;
143 if (!path_list_has_path(&existing_refs, ref)) {
144 printf("%s\n", buf);
147 return 0;
150 int cmd_show_ref(int argc, const char **argv, const char *prefix)
152 int i;
154 for (i = 1; i < argc; i++) {
155 const char *arg = argv[i];
156 if (*arg != '-') {
157 pattern = argv + i;
158 break;
160 if (!strcmp(arg, "--")) {
161 pattern = argv + i + 1;
162 if (!*pattern)
163 pattern = NULL;
164 break;
166 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
167 quiet = 1;
168 continue;
170 if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
171 show_head = 1;
172 continue;
174 if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
175 deref_tags = 1;
176 continue;
178 if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
179 hash_only = 1;
180 continue;
182 if (!prefixcmp(arg, "--hash=") ||
183 (!prefixcmp(arg, "--abbrev") &&
184 (arg[8] == '=' || arg[8] == '\0'))) {
185 if (arg[2] != 'h' && !arg[8])
186 /* --abbrev only */
187 abbrev = DEFAULT_ABBREV;
188 else {
189 /* --hash= or --abbrev= */
190 char *end;
191 if (arg[2] == 'h') {
192 hash_only = 1;
193 arg += 7;
195 else
196 arg += 9;
197 abbrev = strtoul(arg, &end, 10);
198 if (*end || abbrev > 40)
199 usage(show_ref_usage);
200 if (abbrev < MINIMUM_ABBREV)
201 abbrev = MINIMUM_ABBREV;
203 continue;
205 if (!strcmp(arg, "--verify")) {
206 verify = 1;
207 continue;
209 if (!strcmp(arg, "--tags")) {
210 tags_only = 1;
211 continue;
213 if (!strcmp(arg, "--heads")) {
214 heads_only = 1;
215 continue;
217 if (!strcmp(arg, "--exclude-existing"))
218 return exclude_existing(NULL);
219 if (!prefixcmp(arg, "--exclude-existing="))
220 return exclude_existing(arg + 19);
221 usage(show_ref_usage);
224 if (verify) {
225 if (!pattern)
226 die("--verify requires a reference");
227 while (*pattern) {
228 unsigned char sha1[20];
230 if (!prefixcmp(*pattern, "refs/") &&
231 resolve_ref(*pattern, sha1, 1, NULL)) {
232 if (!quiet)
233 show_one(*pattern, sha1);
235 else if (!quiet)
236 die("'%s' - not a valid ref", *pattern);
237 else
238 return 1;
239 pattern++;
241 return 0;
244 if (show_head)
245 head_ref(show_ref, NULL);
246 for_each_ref(show_ref, NULL);
247 if (!found_match) {
248 if (verify && !quiet)
249 die("No match");
250 return 1;
252 return 0;