t1506: more test for @{upstream} syntax
[git/git.git] / sha1_name.c
CommitLineData
9938af6a 1#include "cache.h"
5385f52d 2#include "tag.h"
9938af6a 3#include "commit.h"
5385f52d
JH
4#include "tree.h"
5#include "blob.h"
f3ab49db 6#include "tree-walk.h"
d556fae2 7#include "refs.h"
28fb8438 8#include "remote.h"
9938af6a
JH
9
10static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
11{
99a19b43 12 struct alternate_object_database *alt;
9938af6a 13 char hex[40];
99a19b43
JH
14 int found = 0;
15 static struct alternate_object_database *fakeent;
16
17 if (!fakeent) {
18 const char *objdir = get_object_directory();
19 int objdir_len = strlen(objdir);
20 int entlen = objdir_len + 43;
21 fakeent = xmalloc(sizeof(*fakeent) + entlen);
22 memcpy(fakeent->base, objdir, objdir_len);
23 fakeent->name = fakeent->base + objdir_len + 1;
24 fakeent->name[-1] = '/';
25 }
26 fakeent->next = alt_odb_list;
9938af6a 27
9938af6a 28 sprintf(hex, "%.2s", name);
99a19b43 29 for (alt = fakeent; alt && found < 2; alt = alt->next) {
9938af6a 30 struct dirent *de;
99a19b43
JH
31 DIR *dir;
32 sprintf(alt->name, "%.2s/", name);
33 dir = opendir(alt->base);
34 if (!dir)
35 continue;
9938af6a
JH
36 while ((de = readdir(dir)) != NULL) {
37 if (strlen(de->d_name) != 38)
38 continue;
99a19b43 39 if (memcmp(de->d_name, name + 2, len - 2))
9938af6a 40 continue;
99a19b43
JH
41 if (!found) {
42 memcpy(hex + 2, de->d_name, 38);
43 found++;
44 }
45 else if (memcmp(hex + 2, de->d_name, 38)) {
46 found = 2;
9938af6a 47 break;
99a19b43 48 }
9938af6a
JH
49 }
50 closedir(dir);
51 }
52 if (found == 1)
53 return get_sha1_hex(hex, sha1) == 0;
99a19b43 54 return found;
9938af6a
JH
55}
56
57static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
58{
59 do {
60 if (*a != *b)
61 return 0;
62 a++;
63 b++;
64 len -= 2;
65 } while (len > 1);
66 if (len)
67 if ((*a ^ *b) & 0xf0)
68 return 0;
69 return 1;
70}
71
72static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
73{
74 struct packed_git *p;
d72308e0 75 const unsigned char *found_sha1 = NULL;
99a19b43 76 int found = 0;
9938af6a
JH
77
78 prepare_packed_git();
99a19b43 79 for (p = packed_git; p && found < 2; p = p->next) {
1055880e
JB
80 uint32_t num, last;
81 uint32_t first = 0;
82 open_pack_index(p);
83 num = p->num_objects;
84 last = num;
9938af6a 85 while (first < last) {
326bf396 86 uint32_t mid = (first + last) / 2;
d72308e0 87 const unsigned char *now;
9938af6a
JH
88 int cmp;
89
d72308e0 90 now = nth_packed_object_sha1(p, mid);
a89fccd2 91 cmp = hashcmp(match, now);
9938af6a
JH
92 if (!cmp) {
93 first = mid;
94 break;
95 }
96 if (cmp > 0) {
97 first = mid+1;
98 continue;
99 }
100 last = mid;
101 }
102 if (first < num) {
d72308e0
NP
103 const unsigned char *now, *next;
104 now = nth_packed_object_sha1(p, first);
9938af6a 105 if (match_sha(len, match, now)) {
d72308e0
NP
106 next = nth_packed_object_sha1(p, first+1);
107 if (!next|| !match_sha(len, match, next)) {
0bc45890
JH
108 /* unique within this pack */
109 if (!found) {
d72308e0 110 found_sha1 = now;
0bc45890
JH
111 found++;
112 }
a89fccd2 113 else if (hashcmp(found_sha1, now)) {
0bc45890
JH
114 found = 2;
115 break;
116 }
99a19b43 117 }
0bc45890
JH
118 else {
119 /* not even unique within this pack */
99a19b43
JH
120 found = 2;
121 break;
9938af6a
JH
122 }
123 }
124 }
125 }
99a19b43 126 if (found == 1)
e702496e 127 hashcpy(sha1, found_sha1);
99a19b43
JH
128 return found;
129}
130
013f276e
JH
131#define SHORT_NAME_NOT_FOUND (-1)
132#define SHORT_NAME_AMBIGUOUS (-2)
133
99a19b43
JH
134static int find_unique_short_object(int len, char *canonical,
135 unsigned char *res, unsigned char *sha1)
136{
137 int has_unpacked, has_packed;
138 unsigned char unpacked_sha1[20], packed_sha1[20];
139
693d2bc6 140 prepare_alt_odb();
99a19b43
JH
141 has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
142 has_packed = find_short_packed_object(len, res, packed_sha1);
143 if (!has_unpacked && !has_packed)
013f276e 144 return SHORT_NAME_NOT_FOUND;
99a19b43 145 if (1 < has_unpacked || 1 < has_packed)
013f276e 146 return SHORT_NAME_AMBIGUOUS;
99a19b43 147 if (has_unpacked != has_packed) {
e702496e 148 hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1));
99a19b43
JH
149 return 0;
150 }
151 /* Both have unique ones -- do they match? */
a89fccd2 152 if (hashcmp(packed_sha1, unpacked_sha1))
e974c9ab 153 return SHORT_NAME_AMBIGUOUS;
e702496e 154 hashcpy(sha1, packed_sha1);
9938af6a
JH
155 return 0;
156}
157
013f276e
JH
158static int get_short_sha1(const char *name, int len, unsigned char *sha1,
159 int quietly)
9938af6a 160{
013f276e 161 int i, status;
9938af6a
JH
162 char canonical[40];
163 unsigned char res[20];
164
8a83157e 165 if (len < MINIMUM_ABBREV || len > 40)
af61c6e0 166 return -1;
a8e0d16d 167 hashclr(res);
9938af6a 168 memset(canonical, 'x', 40);
af61c6e0 169 for (i = 0; i < len ;i++) {
9938af6a
JH
170 unsigned char c = name[i];
171 unsigned char val;
9938af6a
JH
172 if (c >= '0' && c <= '9')
173 val = c - '0';
174 else if (c >= 'a' && c <= 'f')
175 val = c - 'a' + 10;
176 else if (c >= 'A' && c <='F') {
177 val = c - 'A' + 10;
178 c -= 'A' - 'a';
179 }
180 else
181 return -1;
182 canonical[i] = c;
183 if (!(i & 1))
184 val <<= 4;
185 res[i >> 1] |= val;
186 }
99a19b43 187
013f276e
JH
188 status = find_unique_short_object(i, canonical, res, sha1);
189 if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
190 return error("short SHA1 %.*s is ambiguous.", len, canonical);
191 return status;
192}
193
194const char *find_unique_abbrev(const unsigned char *sha1, int len)
195{
b66fde9a 196 int status, exists;
013f276e 197 static char hex[41];
47dd0d59 198
b66fde9a 199 exists = has_sha1_file(sha1);
013f276e 200 memcpy(hex, sha1_to_hex(sha1), 40);
02c5cba2 201 if (len == 40 || !len)
47dd0d59 202 return hex;
013f276e
JH
203 while (len < 40) {
204 unsigned char sha1_ret[20];
205 status = get_short_sha1(hex, len, sha1_ret, 1);
b66fde9a
JH
206 if (exists
207 ? !status
208 : status == SHORT_NAME_NOT_FOUND) {
013f276e
JH
209 hex[len] = 0;
210 return hex;
211 }
013f276e
JH
212 len++;
213 }
b66fde9a 214 return hex;
9938af6a
JH
215}
216
6677c466 217static int ambiguous_path(const char *path, int len)
af13cdf2
LT
218{
219 int slash = 1;
6677c466 220 int cnt;
af13cdf2 221
6677c466 222 for (cnt = 0; cnt < len; cnt++) {
af13cdf2
LT
223 switch (*path++) {
224 case '\0':
225 break;
226 case '/':
227 if (slash)
228 break;
229 slash = 1;
230 continue;
231 case '.':
232 continue;
233 default:
234 slash = 0;
235 continue;
236 }
c054d64e 237 break;
af13cdf2 238 }
6677c466 239 return slash;
af13cdf2
LT
240}
241
28fb8438
JS
242static inline int tracked_suffix(const char *string, int len)
243{
244 const char *suffix[] = { "@{upstream}", "@{u}" };
245 int i;
246
247 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
248 int suffix_len = strlen(suffix[i]);
249 if (len >= suffix_len && !memcmp(string + len - suffix_len,
250 suffix[i], suffix_len))
251 return suffix_len;
252 }
253 return 0;
254}
255
aa9c55b6
JS
256/*
257 * *string and *len will only be substituted, and *string returned (for
28fb8438
JS
258 * later free()ing) if the string passed in is of the form @{-<n>} or
259 * of the form <branch>@{upstream}.
aa9c55b6 260 */
431b1969 261static char *substitute_branch_name(const char **string, int *len)
aa9c55b6
JS
262{
263 struct strbuf buf = STRBUF_INIT;
431b1969 264 int ret = interpret_branch_name(*string, &buf);
aa9c55b6
JS
265
266 if (ret == *len) {
267 size_t size;
268 *string = strbuf_detach(&buf, &size);
269 *len = size;
270 return (char *)*string;
271 }
272
28fb8438
JS
273 ret = tracked_suffix(*string, *len);
274 if (ret) {
275 char *ref = xstrndup(*string, *len - ret);
276 struct branch *tracking = branch_get(*ref ? ref : NULL);
277
278 if (!tracking)
279 die ("No tracking branch found for '%s'", ref);
280 free(ref);
281 if (tracking->merge && tracking->merge[0]->dst) {
282 *string = xstrdup(tracking->merge[0]->dst);
283 *len = strlen(*string);
284 return (char *)*string;
285 }
286 }
287
aa9c55b6
JS
288 return NULL;
289}
290
e86eb666 291int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
9938af6a 292{
431b1969 293 char *last_branch = substitute_branch_name(&str, &len);
e86eb666
JH
294 const char **p, *r;
295 int refs_found = 0;
296
297 *ref = NULL;
79803322 298 for (p = ref_rev_parse_rules; *p; p++) {
94cc3552 299 char fullref[PATH_MAX];
e86eb666
JH
300 unsigned char sha1_from_ref[20];
301 unsigned char *this_result;
057e7138 302 int flag;
e86eb666
JH
303
304 this_result = refs_found ? sha1_from_ref : sha1;
94cc3552 305 mksnpath(fullref, sizeof(fullref), *p, len, str);
057e7138 306 r = resolve_ref(fullref, this_result, 1, &flag);
e86eb666
JH
307 if (r) {
308 if (!refs_found++)
309 *ref = xstrdup(r);
310 if (!warn_ambiguous_refs)
311 break;
9e5b80cd
JH
312 } else if ((flag & REF_ISSYMREF) &&
313 (len != 4 || strcmp(str, "HEAD")))
057e7138 314 warning("ignoring dangling symref %s.", fullref);
e86eb666 315 }
aa9c55b6 316 free(last_branch);
e86eb666
JH
317 return refs_found;
318}
319
eb3a4822 320int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
f2eba66d 321{
431b1969 322 char *last_branch = substitute_branch_name(&str, &len);
f2eba66d
NP
323 const char **p;
324 int logs_found = 0;
325
326 *log = NULL;
79803322 327 for (p = ref_rev_parse_rules; *p; p++) {
f2eba66d 328 struct stat st;
40facde0
JH
329 unsigned char hash[20];
330 char path[PATH_MAX];
331 const char *ref, *it;
332
94cc3552 333 mksnpath(path, sizeof(path), *p, len, str);
0c4cd7f4 334 ref = resolve_ref(path, hash, 1, NULL);
40facde0
JH
335 if (!ref)
336 continue;
f2eba66d 337 if (!stat(git_path("logs/%s", path), &st) &&
40facde0
JH
338 S_ISREG(st.st_mode))
339 it = path;
340 else if (strcmp(ref, path) &&
341 !stat(git_path("logs/%s", ref), &st) &&
342 S_ISREG(st.st_mode))
343 it = ref;
344 else
345 continue;
346 if (!logs_found++) {
347 *log = xstrdup(it);
348 hashcpy(sha1, hash);
f2eba66d 349 }
40facde0
JH
350 if (!warn_ambiguous_refs)
351 break;
f2eba66d 352 }
aa9c55b6 353 free(last_branch);
f2eba66d
NP
354 return logs_found;
355}
356
d18ba221
TR
357static int get_sha1_1(const char *name, int len, unsigned char *sha1);
358
e86eb666
JH
359static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
360{
d556fae2 361 static const char *warning = "warning: refname '%.*s' is ambiguous.\n";
ed378ec7 362 char *real_ref = NULL;
ab2a1a32
JH
363 int refs_found = 0;
364 int at, reflog_len;
9938af6a 365
3c3852e3 366 if (len == 40 && !get_sha1_hex(str, sha1))
9938af6a
JH
367 return 0;
368
d18ba221 369 /* basic@{time or number or -number} format to query ref-log */
694500ed 370 reflog_len = at = 0;
f265458f 371 if (len && str[len-1] == '}') {
aa9c55b6 372 for (at = len-2; at >= 0; at--) {
ab2a1a32 373 if (str[at] == '@' && str[at+1] == '{') {
28fb8438
JS
374 if (!tracked_suffix(str + at, len - at)) {
375 reflog_len = (len-1) - (at+2);
376 len = at;
377 }
ab2a1a32
JH
378 break;
379 }
d556fae2
SP
380 }
381 }
382
af13cdf2 383 /* Accept only unambiguous ref paths. */
11cf8801 384 if (len && ambiguous_path(str, len))
af13cdf2
LT
385 return -1;
386
11cf8801 387 if (!len && reflog_len) {
d18ba221
TR
388 struct strbuf buf = STRBUF_INIT;
389 int ret;
390 /* try the @{-N} syntax for n-th checkout */
431b1969 391 ret = interpret_branch_name(str+at, &buf);
d18ba221
TR
392 if (ret > 0) {
393 /* substitute this branch name and restart */
394 return get_sha1_1(buf.buf, buf.len, sha1);
395 } else if (ret == 0) {
396 return -1;
397 }
11cf8801
NP
398 /* allow "@{...}" to mean the current branch reflog */
399 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
f2eba66d
NP
400 } else if (reflog_len)
401 refs_found = dwim_log(str, len, sha1, &real_ref);
402 else
11cf8801 403 refs_found = dwim_ref(str, len, sha1, &real_ref);
d556fae2
SP
404
405 if (!refs_found)
406 return -1;
407
408 if (warn_ambiguous_refs && refs_found > 1)
409 fprintf(stderr, warning, len, str);
410
ab2a1a32 411 if (reflog_len) {
ab2a1a32
JH
412 int nth, i;
413 unsigned long at_time;
16d7cc90
JH
414 unsigned long co_time;
415 int co_tz, co_cnt;
416
fe558516 417 /* Is it asking for N-th entry, or approxidate? */
ab2a1a32
JH
418 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
419 char ch = str[at+2+i];
420 if ('0' <= ch && ch <= '9')
421 nth = nth * 10 + ch - '0';
422 else
423 nth = -1;
424 }
ea360dd0
SP
425 if (100000000 <= nth) {
426 at_time = nth;
427 nth = -1;
428 } else if (0 <= nth)
ab2a1a32 429 at_time = 0;
861f00e3
JK
430 else {
431 char *tmp = xstrndup(str + at + 2, reflog_len);
432 at_time = approxidate(tmp);
433 free(tmp);
434 }
16d7cc90
JH
435 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
436 &co_time, &co_tz, &co_cnt)) {
437 if (at_time)
438 fprintf(stderr,
439 "warning: Log for '%.*s' only goes "
440 "back to %s.\n", len, str,
73013afd 441 show_date(co_time, co_tz, DATE_RFC2822));
16d7cc90
JH
442 else
443 fprintf(stderr,
444 "warning: Log for '%.*s' only has "
445 "%d entries.\n", len, str, co_cnt);
446 }
d556fae2
SP
447 }
448
ed378ec7 449 free(real_ref);
d556fae2 450 return 0;
9938af6a
JH
451}
452
9938af6a
JH
453static int get_parent(const char *name, int len,
454 unsigned char *result, int idx)
455{
456 unsigned char sha1[20];
457 int ret = get_sha1_1(name, len, sha1);
458 struct commit *commit;
459 struct commit_list *p;
460
461 if (ret)
462 return ret;
463 commit = lookup_commit_reference(sha1);
464 if (!commit)
465 return -1;
466 if (parse_commit(commit))
467 return -1;
468 if (!idx) {
e702496e 469 hashcpy(result, commit->object.sha1);
9938af6a
JH
470 return 0;
471 }
472 p = commit->parents;
473 while (p) {
474 if (!--idx) {
e702496e 475 hashcpy(result, p->item->object.sha1);
9938af6a
JH
476 return 0;
477 }
478 p = p->next;
479 }
480 return -1;
481}
482
4f7599ac
JH
483static int get_nth_ancestor(const char *name, int len,
484 unsigned char *result, int generation)
485{
486 unsigned char sha1[20];
621ff675
LT
487 struct commit *commit;
488 int ret;
489
490 ret = get_sha1_1(name, len, sha1);
4f7599ac
JH
491 if (ret)
492 return ret;
621ff675
LT
493 commit = lookup_commit_reference(sha1);
494 if (!commit)
495 return -1;
4f7599ac
JH
496
497 while (generation--) {
621ff675 498 if (parse_commit(commit) || !commit->parents)
4f7599ac 499 return -1;
621ff675 500 commit = commit->parents->item;
4f7599ac 501 }
621ff675 502 hashcpy(result, commit->object.sha1);
4f7599ac
JH
503 return 0;
504}
505
81776315
JH
506struct object *peel_to_type(const char *name, int namelen,
507 struct object *o, enum object_type expected_type)
508{
509 if (name && !namelen)
510 namelen = strlen(name);
511 if (!o) {
512 unsigned char sha1[20];
513 if (get_sha1_1(name, namelen, sha1))
514 return NULL;
515 o = parse_object(sha1);
516 }
517 while (1) {
518 if (!o || (!o->parsed && !parse_object(o->sha1)))
519 return NULL;
520 if (o->type == expected_type)
521 return o;
522 if (o->type == OBJ_TAG)
523 o = ((struct tag*) o)->tagged;
524 else if (o->type == OBJ_COMMIT)
525 o = &(((struct commit *) o)->tree->object);
526 else {
527 if (name)
528 error("%.*s: expected %s type, but the object "
529 "dereferences to %s type",
530 namelen, name, typename(expected_type),
531 typename(o->type));
532 return NULL;
533 }
534 }
535}
536
5385f52d
JH
537static int peel_onion(const char *name, int len, unsigned char *sha1)
538{
539 unsigned char outer[20];
540 const char *sp;
885a86ab 541 unsigned int expected_type = 0;
5385f52d
JH
542 struct object *o;
543
544 /*
545 * "ref^{type}" dereferences ref repeatedly until you cannot
546 * dereference anymore, or you get an object of given type,
547 * whichever comes first. "ref^{}" means just dereference
548 * tags until you get a non-tag. "ref^0" is a shorthand for
549 * "ref^{commit}". "commit^{tree}" could be used to find the
550 * top-level tree of the given commit.
551 */
552 if (len < 4 || name[len-1] != '}')
553 return -1;
554
555 for (sp = name + len - 1; name <= sp; sp--) {
556 int ch = *sp;
557 if (ch == '{' && name < sp && sp[-1] == '^')
558 break;
559 }
560 if (sp <= name)
561 return -1;
562
563 sp++; /* beginning of type name, or closing brace for empty */
564 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
1974632c 565 expected_type = OBJ_COMMIT;
5385f52d 566 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
1974632c 567 expected_type = OBJ_TREE;
5385f52d 568 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
1974632c 569 expected_type = OBJ_BLOB;
5385f52d 570 else if (sp[0] == '}')
1974632c 571 expected_type = OBJ_NONE;
5385f52d
JH
572 else
573 return -1;
574
575 if (get_sha1_1(name, sp - name - 2, outer))
576 return -1;
577
578 o = parse_object(outer);
579 if (!o)
580 return -1;
885a86ab 581 if (!expected_type) {
9534f40b 582 o = deref_tag(o, name, sp - name - 2);
6e1c6c10
JH
583 if (!o || (!o->parsed && !parse_object(o->sha1)))
584 return -1;
e702496e 585 hashcpy(sha1, o->sha1);
5385f52d
JH
586 }
587 else {
81776315
JH
588 /*
589 * At this point, the syntax look correct, so
5385f52d
JH
590 * if we do not get the needed object, we should
591 * barf.
592 */
81776315
JH
593 o = peel_to_type(name, len, o, expected_type);
594 if (o) {
595 hashcpy(sha1, o->sha1);
596 return 0;
5385f52d 597 }
81776315 598 return -1;
5385f52d
JH
599 }
600 return 0;
601}
602
7dd45e15
JH
603static int get_describe_name(const char *name, int len, unsigned char *sha1)
604{
605 const char *cp;
606
607 for (cp = name + len - 1; name + 2 <= cp; cp--) {
608 char ch = *cp;
609 if (hexval(ch) & ~0377) {
610 /* We must be looking at g in "SOMETHING-g"
611 * for it to be describe output.
612 */
613 if (ch == 'g' && cp[-1] == '-') {
614 cp++;
615 len -= cp - name;
616 return get_short_sha1(cp, len, sha1, 1);
617 }
618 }
619 }
620 return -1;
621}
622
9938af6a
JH
623static int get_sha1_1(const char *name, int len, unsigned char *sha1)
624{
0601dbe1 625 int ret, has_suffix;
4f7599ac 626 const char *cp;
9938af6a 627
621ff675
LT
628 /*
629 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
4f7599ac 630 */
0601dbe1 631 has_suffix = 0;
4f7599ac
JH
632 for (cp = name + len - 1; name <= cp; cp--) {
633 int ch = *cp;
634 if ('0' <= ch && ch <= '9')
635 continue;
0601dbe1
JH
636 if (ch == '~' || ch == '^')
637 has_suffix = ch;
4f7599ac
JH
638 break;
639 }
0601dbe1
JH
640
641 if (has_suffix) {
642 int num = 0;
4f7599ac
JH
643 int len1 = cp - name;
644 cp++;
645 while (cp < name + len)
0601dbe1 646 num = num * 10 + *cp++ - '0';
621ff675
LT
647 if (!num && len1 == len - 1)
648 num = 1;
649 if (has_suffix == '^')
0601dbe1 650 return get_parent(name, len1, sha1, num);
0601dbe1
JH
651 /* else if (has_suffix == '~') -- goes without saying */
652 return get_nth_ancestor(name, len1, sha1, num);
4f7599ac
JH
653 }
654
5385f52d
JH
655 ret = peel_onion(name, len, sha1);
656 if (!ret)
657 return 0;
658
9938af6a
JH
659 ret = get_sha1_basic(name, len, sha1);
660 if (!ret)
661 return 0;
7dd45e15
JH
662
663 /* It could be describe output that is "SOMETHING-gXXXX" */
664 ret = get_describe_name(name, len, sha1);
665 if (!ret)
666 return 0;
667
013f276e 668 return get_short_sha1(name, len, sha1, 0);
9938af6a
JH
669}
670
28a4d940
JS
671static int handle_one_ref(const char *path,
672 const unsigned char *sha1, int flag, void *cb_data)
673{
674 struct commit_list **list = cb_data;
675 struct object *object = parse_object(sha1);
676 if (!object)
677 return 0;
affeef12 678 if (object->type == OBJ_TAG) {
28a4d940 679 object = deref_tag(object, path, strlen(path));
affeef12
MK
680 if (!object)
681 return 0;
682 }
28a4d940
JS
683 if (object->type != OBJ_COMMIT)
684 return 0;
685 insert_by_date((struct commit *)object, list);
686 return 0;
687}
688
689/*
690 * This interprets names like ':/Initial revision of "git"' by searching
691 * through history and returning the first commit whose message starts
692 * with the given string.
693 *
694 * For future extension, ':/!' is reserved. If you want to match a message
695 * beginning with a '!', you have to repeat the exclamation mark.
696 */
697
698#define ONELINE_SEEN (1u<<20)
1358e7d6 699static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
28a4d940
JS
700{
701 struct commit_list *list = NULL, *backup = NULL, *l;
1358e7d6 702 int retval = -1;
364d3e65 703 char *temp_commit_buffer = NULL;
28a4d940
JS
704
705 if (prefix[0] == '!') {
706 if (prefix[1] != '!')
707 die ("Invalid search pattern: %s", prefix);
708 prefix++;
709 }
28a4d940
JS
710 for_each_ref(handle_one_ref, &list);
711 for (l = list; l; l = l->next)
712 commit_list_insert(l->item, &backup);
ed8ad7e2 713 while (list) {
28a4d940 714 char *p;
1358e7d6 715 struct commit *commit;
364d3e65
JS
716 enum object_type type;
717 unsigned long size;
ed8ad7e2
JM
718
719 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
283cdbcf
MK
720 if (!parse_object(commit->object.sha1))
721 continue;
8e0f7003 722 free(temp_commit_buffer);
364d3e65
JS
723 if (commit->buffer)
724 p = commit->buffer;
725 else {
726 p = read_sha1_file(commit->object.sha1, &type, &size);
727 if (!p)
728 continue;
729 temp_commit_buffer = p;
730 }
731 if (!(p = strstr(p, "\n\n")))
28a4d940
JS
732 continue;
733 if (!prefixcmp(p + 2, prefix)) {
734 hashcpy(sha1, commit->object.sha1);
1358e7d6 735 retval = 0;
28a4d940
JS
736 break;
737 }
738 }
8e0f7003 739 free(temp_commit_buffer);
28a4d940
JS
740 free_commit_list(list);
741 for (l = backup; l; l = l->next)
742 clear_commit_marks(l->item, ONELINE_SEEN);
1358e7d6 743 return retval;
28a4d940
JS
744}
745
ae5a6c36 746struct grab_nth_branch_switch_cbdata {
c2883e62 747 long cnt, alloc;
ae5a6c36
JH
748 struct strbuf *buf;
749};
750
751static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
752 const char *email, unsigned long timestamp, int tz,
753 const char *message, void *cb_data)
754{
755 struct grab_nth_branch_switch_cbdata *cb = cb_data;
a884d0cb
TR
756 const char *match = NULL, *target = NULL;
757 size_t len;
c2883e62 758 int nth;
a884d0cb
TR
759
760 if (!prefixcmp(message, "checkout: moving from ")) {
761 match = message + strlen("checkout: moving from ");
d7c03c1f 762 target = strstr(match, " to ");
ae5a6c36
JH
763 }
764
c829774c 765 if (!match || !target)
ae5a6c36
JH
766 return 0;
767
d7c03c1f 768 len = target - match;
c2883e62
JH
769 nth = cb->cnt++ % cb->alloc;
770 strbuf_reset(&cb->buf[nth]);
771 strbuf_add(&cb->buf[nth], match, len);
ae5a6c36
JH
772 return 0;
773}
774
775/*
776 * This reads "@{-N}" syntax, finds the name of the Nth previous
777 * branch we were on, and places the name of the branch in the given
a884d0cb 778 * buf and returns the number of characters parsed if successful.
ae5a6c36
JH
779 *
780 * If the input is not of the accepted format, it returns a negative
781 * number to signal an error.
a884d0cb
TR
782 *
783 * If the input was ok but there are not N branch switches in the
784 * reflog, it returns 0.
ae5a6c36 785 */
431b1969 786int interpret_branch_name(const char *name, struct strbuf *buf)
ae5a6c36 787{
c2883e62 788 long nth;
39765e59 789 int i, retval;
ae5a6c36 790 struct grab_nth_branch_switch_cbdata cb;
a884d0cb
TR
791 const char *brace;
792 char *num_end;
ae5a6c36
JH
793
794 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
795 return -1;
a884d0cb
TR
796 brace = strchr(name, '}');
797 if (!brace)
798 return -1;
799 nth = strtol(name+3, &num_end, 10);
800 if (num_end != brace)
ae5a6c36 801 return -1;
c2883e62
JH
802 if (nth <= 0)
803 return -1;
804 cb.alloc = nth;
805 cb.buf = xmalloc(nth * sizeof(struct strbuf));
806 for (i = 0; i < nth; i++)
807 strbuf_init(&cb.buf[i], 20);
808 cb.cnt = 0;
39765e59 809 retval = 0;
101d15e0
JH
810 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
811 if (cb.cnt < nth) {
812 cb.cnt = 0;
101d15e0
JH
813 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
814 }
c2883e62 815 if (cb.cnt < nth)
39765e59 816 goto release_return;
c2883e62
JH
817 i = cb.cnt % nth;
818 strbuf_reset(buf);
819 strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
39765e59
JH
820 retval = brace-name+1;
821
822release_return:
c2883e62
JH
823 for (i = 0; i < nth; i++)
824 strbuf_release(&cb.buf[i]);
825 free(cb.buf);
a884d0cb 826
39765e59 827 return retval;
ae5a6c36
JH
828}
829
9938af6a
JH
830/*
831 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
832 * notably "xyz^" for "parent of xyz"
833 */
834int get_sha1(const char *name, unsigned char *sha1)
835{
041a7308 836 unsigned unused;
a0cd87a5
MK
837 return get_sha1_with_mode(name, sha1, &unused);
838}
839
840int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
841{
842 int ret, bracket_depth;
73b0e5af
JH
843 int namelen = strlen(name);
844 const char *cp;
5119602a 845
a0cd87a5 846 *mode = S_IFINVALID;
73b0e5af
JH
847 ret = get_sha1_1(name, namelen, sha1);
848 if (!ret)
849 return ret;
850 /* sha1:path --> object name of path in ent sha1
851 * :path -> object name of path in index
852 * :[0-3]:path -> object name of path in index at stage
853 */
854 if (name[0] == ':') {
855 int stage = 0;
856 struct cache_entry *ce;
857 int pos;
28a4d940
JS
858 if (namelen > 2 && name[1] == '/')
859 return get_sha1_oneline(name + 2, sha1);
73b0e5af
JH
860 if (namelen < 3 ||
861 name[2] != ':' ||
862 name[1] < '0' || '3' < name[1])
863 cp = name + 1;
864 else {
865 stage = name[1] - '0';
866 cp = name + 3;
5119602a 867 }
73b0e5af
JH
868 namelen = namelen - (cp - name);
869 if (!active_cache)
870 read_cache();
73b0e5af
JH
871 pos = cache_name_pos(cp, namelen);
872 if (pos < 0)
873 pos = -pos - 1;
874 while (pos < active_nr) {
875 ce = active_cache[pos];
876 if (ce_namelen(ce) != namelen ||
877 memcmp(ce->name, cp, namelen))
878 break;
879 if (ce_stage(ce) == stage) {
e702496e 880 hashcpy(sha1, ce->sha1);
7a51ed66 881 *mode = ce->ce_mode;
73b0e5af
JH
882 return 0;
883 }
e7cef45f 884 pos++;
73b0e5af
JH
885 }
886 return -1;
887 }
cce91a2c
SP
888 for (cp = name, bracket_depth = 0; *cp; cp++) {
889 if (*cp == '{')
890 bracket_depth++;
891 else if (bracket_depth && *cp == '}')
892 bracket_depth--;
893 else if (!bracket_depth && *cp == ':')
894 break;
895 }
896 if (*cp == ':') {
73b0e5af
JH
897 unsigned char tree_sha1[20];
898 if (!get_sha1_1(name, cp-name, tree_sha1))
899 return get_tree_entry(tree_sha1, cp+1, sha1,
a0cd87a5 900 mode);
5119602a
LT
901 }
902 return ret;
9938af6a 903}