]> git.ipfire.org Git - thirdparty/git.git/blame - sha1_name.c
get_sha1(): fix error status regression
[thirdparty/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 9
32574b68
NTND
10static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
11
a78fafe7
JH
12typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
13
14struct disambiguate_state {
15 disambiguate_hint_fn fn;
16 void *cb_data;
17 unsigned char candidate[20];
18 unsigned candidate_exists:1;
19 unsigned candidate_checked:1;
20 unsigned candidate_ok:1;
21 unsigned disambiguate_fn_used:1;
22 unsigned ambiguous:1;
23};
24
25static void update_candidates(struct disambiguate_state *ds, const unsigned char *current)
26{
27 if (!ds->candidate_exists) {
28 /* this is the first candidate */
29 hashcpy(ds->candidate, current);
30 ds->candidate_exists = 1;
31 return;
32 } else if (!hashcmp(ds->candidate, current)) {
33 /* the same as what we already have seen */
34 return;
35 }
36
37 if (!ds->fn) {
38 /* cannot disambiguate between ds->candidate and current */
39 ds->ambiguous = 1;
40 return;
41 }
42
43 if (!ds->candidate_checked) {
44 ds->candidate_ok = ds->fn(ds->candidate, ds->cb_data);
45 ds->disambiguate_fn_used = 1;
46 ds->candidate_checked = 1;
47 }
48
49 if (!ds->candidate_ok) {
50 /* discard the candidate; we know it does not satisify fn */
51 hashcpy(ds->candidate, current);
52 ds->candidate_checked = 0;
53 return;
54 }
55
56 /* if we reach this point, we know ds->candidate satisfies fn */
57 if (ds->fn(current, ds->cb_data)) {
58 /*
59 * if both current and candidate satisfy fn, we cannot
60 * disambiguate.
61 */
62 ds->candidate_ok = 0;
63 ds->ambiguous = 1;
64 }
65
66 /* otherwise, current can be discarded and candidate is still good */
67}
68
69static void find_short_object_filename(int len, const char *hex_pfx, struct disambiguate_state *ds)
9938af6a 70{
99a19b43 71 struct alternate_object_database *alt;
9938af6a 72 char hex[40];
99a19b43
JH
73 static struct alternate_object_database *fakeent;
74
75 if (!fakeent) {
274ac009
JH
76 /*
77 * Create a "fake" alternate object database that
78 * points to our own object database, to make it
79 * easier to get a temporary working space in
80 * alt->name/alt->base while iterating over the
81 * object databases including our own.
82 */
99a19b43
JH
83 const char *objdir = get_object_directory();
84 int objdir_len = strlen(objdir);
85 int entlen = objdir_len + 43;
86 fakeent = xmalloc(sizeof(*fakeent) + entlen);
87 memcpy(fakeent->base, objdir, objdir_len);
88 fakeent->name = fakeent->base + objdir_len + 1;
89 fakeent->name[-1] = '/';
90 }
91 fakeent->next = alt_odb_list;
9938af6a 92
1703f9aa 93 sprintf(hex, "%.2s", hex_pfx);
a78fafe7 94 for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
9938af6a 95 struct dirent *de;
99a19b43 96 DIR *dir;
1703f9aa 97 sprintf(alt->name, "%.2s/", hex_pfx);
99a19b43
JH
98 dir = opendir(alt->base);
99 if (!dir)
100 continue;
a78fafe7
JH
101
102 while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
103 unsigned char sha1[20];
104
9938af6a
JH
105 if (strlen(de->d_name) != 38)
106 continue;
1703f9aa 107 if (memcmp(de->d_name, hex_pfx + 2, len - 2))
9938af6a 108 continue;
a78fafe7
JH
109 memcpy(hex + 2, de->d_name, 38);
110 if (!get_sha1_hex(hex, sha1))
111 update_candidates(ds, sha1);
9938af6a
JH
112 }
113 closedir(dir);
114 }
9938af6a
JH
115}
116
117static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
118{
119 do {
120 if (*a != *b)
121 return 0;
122 a++;
123 b++;
124 len -= 2;
125 } while (len > 1);
126 if (len)
127 if ((*a ^ *b) & 0xf0)
128 return 0;
129 return 1;
130}
131
a78fafe7 132static void unique_in_pack(int len,
1703f9aa 133 const unsigned char *bin_pfx,
a78fafe7
JH
134 struct packed_git *p,
135 struct disambiguate_state *ds)
f703e6ea
JH
136{
137 uint32_t num, last, i, first = 0;
138 const unsigned char *current = NULL;
139
140 open_pack_index(p);
141 num = p->num_objects;
142 last = num;
143 while (first < last) {
144 uint32_t mid = (first + last) / 2;
145 const unsigned char *current;
146 int cmp;
147
148 current = nth_packed_object_sha1(p, mid);
1703f9aa 149 cmp = hashcmp(bin_pfx, current);
f703e6ea
JH
150 if (!cmp) {
151 first = mid;
152 break;
153 }
154 if (cmp > 0) {
155 first = mid+1;
156 continue;
157 }
158 last = mid;
159 }
160
161 /*
162 * At this point, "first" is the location of the lowest object
1703f9aa 163 * with an object name that could match "bin_pfx". See if we have
f703e6ea
JH
164 * 0, 1 or more objects that actually match(es).
165 */
a78fafe7
JH
166 for (i = first; i < num && !ds->ambiguous; i++) {
167 current = nth_packed_object_sha1(p, i);
1703f9aa 168 if (!match_sha(len, bin_pfx, current))
f703e6ea 169 break;
a78fafe7 170 update_candidates(ds, current);
f703e6ea 171 }
f703e6ea
JH
172}
173
a78fafe7
JH
174static void find_short_packed_object(int len, const unsigned char *bin_pfx,
175 struct disambiguate_state *ds)
9938af6a
JH
176{
177 struct packed_git *p;
178
179 prepare_packed_git();
a78fafe7
JH
180 for (p = packed_git; p && !ds->ambiguous; p = p->next)
181 unique_in_pack(len, bin_pfx, p, ds);
99a19b43
JH
182}
183
013f276e
JH
184#define SHORT_NAME_NOT_FOUND (-1)
185#define SHORT_NAME_AMBIGUOUS (-2)
186
a78fafe7
JH
187static int finish_object_disambiguation(struct disambiguate_state *ds,
188 unsigned char *sha1)
99a19b43 189{
a78fafe7
JH
190 if (ds->ambiguous)
191 return SHORT_NAME_AMBIGUOUS;
99a19b43 192
a78fafe7 193 if (!ds->candidate_exists)
013f276e 194 return SHORT_NAME_NOT_FOUND;
a78fafe7
JH
195
196 if (!ds->candidate_checked)
197 /*
198 * If this is the only candidate, there is no point
199 * calling the disambiguation hint callback.
200 *
201 * On the other hand, if the current candidate
202 * replaced an earlier candidate that did _not_ pass
203 * the disambiguation hint callback, then we do have
204 * more than one objects that match the short name
205 * given, so we should make sure this one matches;
206 * otherwise, if we discovered this one and the one
207 * that we previously discarded in the reverse order,
208 * we would end up showing different results in the
209 * same repository!
210 */
211 ds->candidate_ok = (!ds->disambiguate_fn_used ||
212 ds->fn(ds->candidate, ds->cb_data));
213
214 if (!ds->candidate_ok)
c005e986 215 return SHORT_NAME_AMBIGUOUS;
a78fafe7
JH
216
217 hashcpy(sha1, ds->candidate);
9938af6a
JH
218 return 0;
219}
220
013f276e
JH
221static int get_short_sha1(const char *name, int len, unsigned char *sha1,
222 int quietly)
9938af6a 223{
013f276e 224 int i, status;
1703f9aa
JH
225 char hex_pfx[40];
226 unsigned char bin_pfx[20];
a78fafe7 227 struct disambiguate_state ds;
9938af6a 228
8a83157e 229 if (len < MINIMUM_ABBREV || len > 40)
af61c6e0 230 return -1;
1703f9aa
JH
231 hashclr(bin_pfx);
232 memset(hex_pfx, 'x', 40);
af61c6e0 233 for (i = 0; i < len ;i++) {
9938af6a
JH
234 unsigned char c = name[i];
235 unsigned char val;
9938af6a
JH
236 if (c >= '0' && c <= '9')
237 val = c - '0';
238 else if (c >= 'a' && c <= 'f')
239 val = c - 'a' + 10;
240 else if (c >= 'A' && c <='F') {
241 val = c - 'A' + 10;
242 c -= 'A' - 'a';
243 }
244 else
245 return -1;
1703f9aa 246 hex_pfx[i] = c;
9938af6a
JH
247 if (!(i & 1))
248 val <<= 4;
1703f9aa 249 bin_pfx[i >> 1] |= val;
9938af6a 250 }
99a19b43 251
a78fafe7
JH
252 prepare_alt_odb();
253
254 memset(&ds, 0, sizeof(ds));
255 find_short_object_filename(len, hex_pfx, &ds);
256 find_short_packed_object(len, bin_pfx, &ds);
257 status = finish_object_disambiguation(&ds, sha1);
258
013f276e 259 if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
1703f9aa 260 return error("short SHA1 %.*s is ambiguous.", len, hex_pfx);
013f276e
JH
261 return status;
262}
263
264const char *find_unique_abbrev(const unsigned char *sha1, int len)
265{
b66fde9a 266 int status, exists;
013f276e 267 static char hex[41];
47dd0d59 268
b66fde9a 269 exists = has_sha1_file(sha1);
013f276e 270 memcpy(hex, sha1_to_hex(sha1), 40);
02c5cba2 271 if (len == 40 || !len)
47dd0d59 272 return hex;
013f276e
JH
273 while (len < 40) {
274 unsigned char sha1_ret[20];
275 status = get_short_sha1(hex, len, sha1_ret, 1);
b66fde9a
JH
276 if (exists
277 ? !status
278 : status == SHORT_NAME_NOT_FOUND) {
ea2c69ed 279 hex[len] = 0;
013f276e
JH
280 return hex;
281 }
013f276e
JH
282 len++;
283 }
b66fde9a 284 return hex;
9938af6a
JH
285}
286
6677c466 287static int ambiguous_path(const char *path, int len)
af13cdf2
LT
288{
289 int slash = 1;
6677c466 290 int cnt;
af13cdf2 291
6677c466 292 for (cnt = 0; cnt < len; cnt++) {
af13cdf2
LT
293 switch (*path++) {
294 case '\0':
295 break;
296 case '/':
297 if (slash)
298 break;
299 slash = 1;
300 continue;
301 case '.':
302 continue;
303 default:
304 slash = 0;
305 continue;
306 }
c054d64e 307 break;
af13cdf2 308 }
6677c466 309 return slash;
af13cdf2
LT
310}
311
ae0ba8e2
JH
312static inline int upstream_mark(const char *string, int len)
313{
314 const char *suffix[] = { "@{upstream}", "@{u}" };
315 int i;
316
317 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
318 int suffix_len = strlen(suffix[i]);
319 if (suffix_len <= len
320 && !memcmp(string, suffix[i], suffix_len))
321 return suffix_len;
322 }
323 return 0;
324}
325
d18ba221
TR
326static int get_sha1_1(const char *name, int len, unsigned char *sha1);
327
e86eb666
JH
328static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
329{
eedce784 330 static const char *warn_msg = "refname '%.*s' is ambiguous.";
ed378ec7 331 char *real_ref = NULL;
ab2a1a32
JH
332 int refs_found = 0;
333 int at, reflog_len;
9938af6a 334
3c3852e3 335 if (len == 40 && !get_sha1_hex(str, sha1))
9938af6a
JH
336 return 0;
337
d18ba221 338 /* basic@{time or number or -number} format to query ref-log */
694500ed 339 reflog_len = at = 0;
f265458f 340 if (len && str[len-1] == '}') {
aa9c55b6 341 for (at = len-2; at >= 0; at--) {
ab2a1a32 342 if (str[at] == '@' && str[at+1] == '{') {
ae0ba8e2 343 if (!upstream_mark(str + at, len - at)) {
28fb8438
JS
344 reflog_len = (len-1) - (at+2);
345 len = at;
346 }
ab2a1a32
JH
347 break;
348 }
d556fae2
SP
349 }
350 }
351
af13cdf2 352 /* Accept only unambiguous ref paths. */
11cf8801 353 if (len && ambiguous_path(str, len))
af13cdf2
LT
354 return -1;
355
11cf8801 356 if (!len && reflog_len) {
d18ba221
TR
357 struct strbuf buf = STRBUF_INIT;
358 int ret;
359 /* try the @{-N} syntax for n-th checkout */
431b1969 360 ret = interpret_branch_name(str+at, &buf);
d18ba221
TR
361 if (ret > 0) {
362 /* substitute this branch name and restart */
363 return get_sha1_1(buf.buf, buf.len, sha1);
364 } else if (ret == 0) {
365 return -1;
366 }
11cf8801
NP
367 /* allow "@{...}" to mean the current branch reflog */
368 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
f2eba66d
NP
369 } else if (reflog_len)
370 refs_found = dwim_log(str, len, sha1, &real_ref);
371 else
11cf8801 372 refs_found = dwim_ref(str, len, sha1, &real_ref);
d556fae2
SP
373
374 if (!refs_found)
375 return -1;
376
377 if (warn_ambiguous_refs && refs_found > 1)
eedce784 378 warning(warn_msg, len, str);
d556fae2 379
ab2a1a32 380 if (reflog_len) {
ab2a1a32
JH
381 int nth, i;
382 unsigned long at_time;
16d7cc90
JH
383 unsigned long co_time;
384 int co_tz, co_cnt;
385
12a258c0
JK
386 /* a @{-N} placed anywhere except the start is an error */
387 if (str[at+2] == '-')
388 return -1;
389
fe558516 390 /* Is it asking for N-th entry, or approxidate? */
ab2a1a32
JH
391 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
392 char ch = str[at+2+i];
393 if ('0' <= ch && ch <= '9')
394 nth = nth * 10 + ch - '0';
395 else
396 nth = -1;
397 }
ea360dd0
SP
398 if (100000000 <= nth) {
399 at_time = nth;
400 nth = -1;
401 } else if (0 <= nth)
ab2a1a32 402 at_time = 0;
861f00e3 403 else {
93cfa7c7 404 int errors = 0;
861f00e3 405 char *tmp = xstrndup(str + at + 2, reflog_len);
93cfa7c7 406 at_time = approxidate_careful(tmp, &errors);
861f00e3 407 free(tmp);
a5e10acb
JH
408 if (errors)
409 return -1;
861f00e3 410 }
16d7cc90
JH
411 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
412 &co_time, &co_tz, &co_cnt)) {
413 if (at_time)
eedce784
JS
414 warning("Log for '%.*s' only goes "
415 "back to %s.", len, str,
73013afd 416 show_date(co_time, co_tz, DATE_RFC2822));
e6eedc31
JS
417 else {
418 free(real_ref);
419 die("Log for '%.*s' only has %d entries.",
420 len, str, co_cnt);
421 }
16d7cc90 422 }
d556fae2
SP
423 }
424
ed378ec7 425 free(real_ref);
d556fae2 426 return 0;
9938af6a
JH
427}
428
9938af6a
JH
429static int get_parent(const char *name, int len,
430 unsigned char *result, int idx)
431{
432 unsigned char sha1[20];
433 int ret = get_sha1_1(name, len, sha1);
434 struct commit *commit;
435 struct commit_list *p;
436
437 if (ret)
438 return ret;
439 commit = lookup_commit_reference(sha1);
440 if (!commit)
441 return -1;
442 if (parse_commit(commit))
443 return -1;
444 if (!idx) {
e702496e 445 hashcpy(result, commit->object.sha1);
9938af6a
JH
446 return 0;
447 }
448 p = commit->parents;
449 while (p) {
450 if (!--idx) {
e702496e 451 hashcpy(result, p->item->object.sha1);
9938af6a
JH
452 return 0;
453 }
454 p = p->next;
455 }
456 return -1;
457}
458
4f7599ac
JH
459static int get_nth_ancestor(const char *name, int len,
460 unsigned char *result, int generation)
461{
462 unsigned char sha1[20];
621ff675
LT
463 struct commit *commit;
464 int ret;
465
466 ret = get_sha1_1(name, len, sha1);
4f7599ac
JH
467 if (ret)
468 return ret;
621ff675
LT
469 commit = lookup_commit_reference(sha1);
470 if (!commit)
471 return -1;
4f7599ac
JH
472
473 while (generation--) {
621ff675 474 if (parse_commit(commit) || !commit->parents)
4f7599ac 475 return -1;
621ff675 476 commit = commit->parents->item;
4f7599ac 477 }
621ff675 478 hashcpy(result, commit->object.sha1);
4f7599ac
JH
479 return 0;
480}
481
81776315
JH
482struct object *peel_to_type(const char *name, int namelen,
483 struct object *o, enum object_type expected_type)
484{
485 if (name && !namelen)
486 namelen = strlen(name);
81776315
JH
487 while (1) {
488 if (!o || (!o->parsed && !parse_object(o->sha1)))
489 return NULL;
490 if (o->type == expected_type)
491 return o;
492 if (o->type == OBJ_TAG)
493 o = ((struct tag*) o)->tagged;
494 else if (o->type == OBJ_COMMIT)
495 o = &(((struct commit *) o)->tree->object);
496 else {
497 if (name)
498 error("%.*s: expected %s type, but the object "
499 "dereferences to %s type",
500 namelen, name, typename(expected_type),
501 typename(o->type));
502 return NULL;
503 }
504 }
505}
506
5385f52d
JH
507static int peel_onion(const char *name, int len, unsigned char *sha1)
508{
509 unsigned char outer[20];
510 const char *sp;
885a86ab 511 unsigned int expected_type = 0;
5385f52d
JH
512 struct object *o;
513
514 /*
515 * "ref^{type}" dereferences ref repeatedly until you cannot
516 * dereference anymore, or you get an object of given type,
517 * whichever comes first. "ref^{}" means just dereference
518 * tags until you get a non-tag. "ref^0" is a shorthand for
519 * "ref^{commit}". "commit^{tree}" could be used to find the
520 * top-level tree of the given commit.
521 */
522 if (len < 4 || name[len-1] != '}')
523 return -1;
524
525 for (sp = name + len - 1; name <= sp; sp--) {
526 int ch = *sp;
527 if (ch == '{' && name < sp && sp[-1] == '^')
528 break;
529 }
530 if (sp <= name)
531 return -1;
532
533 sp++; /* beginning of type name, or closing brace for empty */
534 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
1974632c 535 expected_type = OBJ_COMMIT;
5385f52d 536 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
1974632c 537 expected_type = OBJ_TREE;
5385f52d 538 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
1974632c 539 expected_type = OBJ_BLOB;
5385f52d 540 else if (sp[0] == '}')
1974632c 541 expected_type = OBJ_NONE;
32574b68
NTND
542 else if (sp[0] == '/')
543 expected_type = OBJ_COMMIT;
5385f52d
JH
544 else
545 return -1;
546
547 if (get_sha1_1(name, sp - name - 2, outer))
548 return -1;
549
550 o = parse_object(outer);
551 if (!o)
552 return -1;
885a86ab 553 if (!expected_type) {
9534f40b 554 o = deref_tag(o, name, sp - name - 2);
6e1c6c10
JH
555 if (!o || (!o->parsed && !parse_object(o->sha1)))
556 return -1;
e702496e 557 hashcpy(sha1, o->sha1);
32574b68 558 return 0;
5385f52d 559 }
32574b68
NTND
560
561 /*
562 * At this point, the syntax look correct, so
563 * if we do not get the needed object, we should
564 * barf.
565 */
566 o = peel_to_type(name, len, o, expected_type);
567 if (!o)
81776315 568 return -1;
32574b68
NTND
569
570 hashcpy(sha1, o->sha1);
571 if (sp[0] == '/') {
572 /* "$commit^{/foo}" */
573 char *prefix;
574 int ret;
575 struct commit_list *list = NULL;
576
81776315 577 /*
4322842a
NTND
578 * $commit^{/}. Some regex implementation may reject.
579 * We don't need regex anyway. '' pattern always matches.
5385f52d 580 */
4322842a 581 if (sp[1] == '}')
81776315 582 return 0;
4322842a 583
32574b68
NTND
584 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
585 commit_list_insert((struct commit *)o, &list);
586 ret = get_sha1_oneline(prefix, sha1, list);
587 free(prefix);
588 return ret;
5385f52d
JH
589 }
590 return 0;
591}
592
7dd45e15
JH
593static int get_describe_name(const char *name, int len, unsigned char *sha1)
594{
595 const char *cp;
596
597 for (cp = name + len - 1; name + 2 <= cp; cp--) {
598 char ch = *cp;
599 if (hexval(ch) & ~0377) {
600 /* We must be looking at g in "SOMETHING-g"
601 * for it to be describe output.
602 */
603 if (ch == 'g' && cp[-1] == '-') {
604 cp++;
605 len -= cp - name;
606 return get_short_sha1(cp, len, sha1, 1);
607 }
608 }
609 }
610 return -1;
611}
612
9938af6a
JH
613static int get_sha1_1(const char *name, int len, unsigned char *sha1)
614{
0601dbe1 615 int ret, has_suffix;
4f7599ac 616 const char *cp;
9938af6a 617
621ff675
LT
618 /*
619 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
4f7599ac 620 */
0601dbe1 621 has_suffix = 0;
4f7599ac
JH
622 for (cp = name + len - 1; name <= cp; cp--) {
623 int ch = *cp;
624 if ('0' <= ch && ch <= '9')
625 continue;
0601dbe1
JH
626 if (ch == '~' || ch == '^')
627 has_suffix = ch;
4f7599ac
JH
628 break;
629 }
0601dbe1
JH
630
631 if (has_suffix) {
632 int num = 0;
4f7599ac
JH
633 int len1 = cp - name;
634 cp++;
635 while (cp < name + len)
0601dbe1 636 num = num * 10 + *cp++ - '0';
621ff675
LT
637 if (!num && len1 == len - 1)
638 num = 1;
639 if (has_suffix == '^')
0601dbe1 640 return get_parent(name, len1, sha1, num);
0601dbe1
JH
641 /* else if (has_suffix == '~') -- goes without saying */
642 return get_nth_ancestor(name, len1, sha1, num);
4f7599ac
JH
643 }
644
5385f52d
JH
645 ret = peel_onion(name, len, sha1);
646 if (!ret)
647 return 0;
648
9938af6a
JH
649 ret = get_sha1_basic(name, len, sha1);
650 if (!ret)
651 return 0;
7dd45e15
JH
652
653 /* It could be describe output that is "SOMETHING-gXXXX" */
654 ret = get_describe_name(name, len, sha1);
655 if (!ret)
656 return 0;
657
013f276e 658 return get_short_sha1(name, len, sha1, 0);
9938af6a
JH
659}
660
f7bff003
JH
661/*
662 * This interprets names like ':/Initial revision of "git"' by searching
663 * through history and returning the first commit whose message starts
3d045897 664 * the given regular expression.
f7bff003
JH
665 *
666 * For future extension, ':/!' is reserved. If you want to match a message
667 * beginning with a '!', you have to repeat the exclamation mark.
668 */
669#define ONELINE_SEEN (1u<<20)
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;
47e44ed1 685 commit_list_insert_by_date((struct commit *)object, list);
28a4d940
JS
686 return 0;
687}
688
84baa31b
NTND
689static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
690 struct commit_list *list)
28a4d940 691{
84baa31b 692 struct commit_list *backup = NULL, *l;
28042dbc 693 int found = 0;
57895105 694 regex_t regex;
28a4d940
JS
695
696 if (prefix[0] == '!') {
697 if (prefix[1] != '!')
698 die ("Invalid search pattern: %s", prefix);
699 prefix++;
700 }
57895105
LT
701
702 if (regcomp(&regex, prefix, REG_EXTENDED))
703 die("Invalid search pattern: %s", prefix);
704
84baa31b
NTND
705 for (l = list; l; l = l->next) {
706 l->item->object.flags |= ONELINE_SEEN;
28a4d940 707 commit_list_insert(l->item, &backup);
84baa31b 708 }
ed8ad7e2 709 while (list) {
28042dbc 710 char *p, *to_free = NULL;
1358e7d6 711 struct commit *commit;
364d3e65
JS
712 enum object_type type;
713 unsigned long size;
28042dbc 714 int matches;
ed8ad7e2
JM
715
716 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
283cdbcf
MK
717 if (!parse_object(commit->object.sha1))
718 continue;
364d3e65
JS
719 if (commit->buffer)
720 p = commit->buffer;
721 else {
722 p = read_sha1_file(commit->object.sha1, &type, &size);
723 if (!p)
724 continue;
28042dbc 725 to_free = p;
364d3e65 726 }
28042dbc
JH
727
728 p = strstr(p, "\n\n");
729 matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
730 free(to_free);
731
732 if (matches) {
28a4d940 733 hashcpy(sha1, commit->object.sha1);
28042dbc 734 found = 1;
28a4d940
JS
735 break;
736 }
737 }
57895105 738 regfree(&regex);
28a4d940
JS
739 free_commit_list(list);
740 for (l = backup; l; l = l->next)
741 clear_commit_marks(l->item, ONELINE_SEEN);
28042dbc
JH
742 free_commit_list(backup);
743 return found ? 0 : -1;
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/*
ae0ba8e2
JH
776 * Parse @{-N} syntax, return the number of characters parsed
777 * if successful; otherwise signal an error with negative value.
ae5a6c36 778 */
ae0ba8e2 779static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf)
ae5a6c36 780{
c2883e62 781 long nth;
39765e59 782 int i, retval;
ae5a6c36 783 struct grab_nth_branch_switch_cbdata cb;
a884d0cb
TR
784 const char *brace;
785 char *num_end;
ae5a6c36
JH
786
787 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
788 return -1;
a884d0cb
TR
789 brace = strchr(name, '}');
790 if (!brace)
791 return -1;
792 nth = strtol(name+3, &num_end, 10);
793 if (num_end != brace)
ae5a6c36 794 return -1;
c2883e62
JH
795 if (nth <= 0)
796 return -1;
797 cb.alloc = nth;
798 cb.buf = xmalloc(nth * sizeof(struct strbuf));
799 for (i = 0; i < nth; i++)
800 strbuf_init(&cb.buf[i], 20);
801 cb.cnt = 0;
39765e59 802 retval = 0;
101d15e0
JH
803 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
804 if (cb.cnt < nth) {
805 cb.cnt = 0;
101d15e0
JH
806 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
807 }
c2883e62 808 if (cb.cnt < nth)
39765e59 809 goto release_return;
c2883e62
JH
810 i = cb.cnt % nth;
811 strbuf_reset(buf);
812 strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
39765e59
JH
813 retval = brace-name+1;
814
815release_return:
c2883e62
JH
816 for (i = 0; i < nth; i++)
817 strbuf_release(&cb.buf[i]);
818 free(cb.buf);
a884d0cb 819
39765e59 820 return retval;
ae5a6c36
JH
821}
822
619a644d
JH
823int get_sha1_mb(const char *name, unsigned char *sha1)
824{
825 struct commit *one, *two;
826 struct commit_list *mbs;
827 unsigned char sha1_tmp[20];
828 const char *dots;
829 int st;
830
831 dots = strstr(name, "...");
832 if (!dots)
833 return get_sha1(name, sha1);
834 if (dots == name)
835 st = get_sha1("HEAD", sha1_tmp);
836 else {
837 struct strbuf sb;
838 strbuf_init(&sb, dots - name);
839 strbuf_add(&sb, name, dots - name);
840 st = get_sha1(sb.buf, sha1_tmp);
841 strbuf_release(&sb);
842 }
843 if (st)
844 return st;
845 one = lookup_commit_reference_gently(sha1_tmp, 0);
846 if (!one)
847 return -1;
848
849 if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
850 return -1;
851 two = lookup_commit_reference_gently(sha1_tmp, 0);
852 if (!two)
853 return -1;
854 mbs = get_merge_bases(one, two, 1);
855 if (!mbs || mbs->next)
856 st = -1;
857 else {
858 st = 0;
859 hashcpy(sha1, mbs->item->object.sha1);
860 }
861 free_commit_list(mbs);
862 return st;
863}
864
ae0ba8e2
JH
865/*
866 * This reads short-hand syntax that not only evaluates to a commit
867 * object name, but also can act as if the end user spelled the name
868 * of the branch from the command line.
869 *
870 * - "@{-N}" finds the name of the Nth previous branch we were on, and
871 * places the name of the branch in the given buf and returns the
872 * number of characters parsed if successful.
873 *
874 * - "<branch>@{upstream}" finds the name of the other ref that
875 * <branch> is configured to merge with (missing <branch> defaults
876 * to the current branch), and places the name of the branch in the
877 * given buf and returns the number of characters parsed if
878 * successful.
879 *
880 * If the input is not of the accepted format, it returns a negative
881 * number to signal an error.
882 *
883 * If the input was ok but there are not N branch switches in the
884 * reflog, it returns 0.
885 */
886int interpret_branch_name(const char *name, struct strbuf *buf)
887{
888 char *cp;
889 struct branch *upstream;
890 int namelen = strlen(name);
891 int len = interpret_nth_prior_checkout(name, buf);
892 int tmp_len;
893
894 if (!len)
895 return len; /* syntax Ok, not enough switches */
d46a8301
JK
896 if (0 < len && len == namelen)
897 return len; /* consumed all */
898 else if (0 < len) {
899 /* we have extra data, which might need further processing */
900 struct strbuf tmp = STRBUF_INIT;
901 int used = buf->len;
902 int ret;
903
904 strbuf_add(buf, name + len, namelen - len);
905 ret = interpret_branch_name(buf->buf, &tmp);
906 /* that data was not interpreted, remove our cruft */
907 if (ret < 0) {
908 strbuf_setlen(buf, used);
909 return len;
910 }
911 strbuf_reset(buf);
912 strbuf_addbuf(buf, &tmp);
913 strbuf_release(&tmp);
914 /* tweak for size of {-N} versus expanded ref name */
915 return ret - used + len;
916 }
917
ae0ba8e2
JH
918 cp = strchr(name, '@');
919 if (!cp)
920 return -1;
921 tmp_len = upstream_mark(cp, namelen - (cp - name));
922 if (!tmp_len)
923 return -1;
924 len = cp + tmp_len - name;
925 cp = xstrndup(name, cp - name);
926 upstream = branch_get(*cp ? cp : NULL);
927 if (!upstream
928 || !upstream->merge
929 || !upstream->merge[0]->dst)
930 return error("No upstream branch found for '%s'", cp);
931 free(cp);
932 cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
933 strbuf_reset(buf);
934 strbuf_addstr(buf, cp);
935 free(cp);
936 return len;
937}
938
6bab74e7
JN
939int strbuf_branchname(struct strbuf *sb, const char *name)
940{
941 int len = strlen(name);
942 if (interpret_branch_name(name, sb) == len)
943 return 0;
944 strbuf_add(sb, name, len);
945 return len;
946}
947
948int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
949{
950 strbuf_branchname(sb, name);
951 if (name[0] == '-')
8d9c5010 952 return -1;
6bab74e7 953 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
8d9c5010 954 return check_refname_format(sb->buf, 0);
6bab74e7
JN
955}
956
9938af6a
JH
957/*
958 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
959 * notably "xyz^" for "parent of xyz"
960 */
961int get_sha1(const char *name, unsigned char *sha1)
962{
573285e5
CP
963 struct object_context unused;
964 return get_sha1_with_context(name, sha1, &unused);
a0cd87a5
MK
965}
966
009fee47
MM
967/* Must be called only when object_name:filename doesn't exist. */
968static void diagnose_invalid_sha1_path(const char *prefix,
969 const char *filename,
970 const unsigned char *tree_sha1,
971 const char *object_name)
972{
973 struct stat st;
974 unsigned char sha1[20];
975 unsigned mode;
976
977 if (!prefix)
978 prefix = "";
979
980 if (!lstat(filename, &st))
981 die("Path '%s' exists on disk, but not in '%s'.",
982 filename, object_name);
983 if (errno == ENOENT || errno == ENOTDIR) {
984 char *fullname = xmalloc(strlen(filename)
985 + strlen(prefix) + 1);
986 strcpy(fullname, prefix);
987 strcat(fullname, filename);
988
989 if (!get_tree_entry(tree_sha1, fullname,
990 sha1, &mode)) {
991 die("Path '%s' exists, but not '%s'.\n"
e41d718c 992 "Did you mean '%s:%s' aka '%s:./%s'?",
009fee47
MM
993 fullname,
994 filename,
995 object_name,
e41d718c
MG
996 fullname,
997 object_name,
998 filename);
009fee47
MM
999 }
1000 die("Path '%s' does not exist in '%s'",
1001 filename, object_name);
1002 }
1003}
1004
1005/* Must be called only when :stage:filename doesn't exist. */
1006static void diagnose_invalid_index_path(int stage,
1007 const char *prefix,
1008 const char *filename)
1009{
1010 struct stat st;
1011 struct cache_entry *ce;
1012 int pos;
1013 unsigned namelen = strlen(filename);
1014 unsigned fullnamelen;
1015 char *fullname;
1016
1017 if (!prefix)
1018 prefix = "";
1019
1020 /* Wrong stage number? */
1021 pos = cache_name_pos(filename, namelen);
1022 if (pos < 0)
1023 pos = -pos - 1;
77e8466f
MH
1024 if (pos < active_nr) {
1025 ce = active_cache[pos];
1026 if (ce_namelen(ce) == namelen &&
1027 !memcmp(ce->name, filename, namelen))
1028 die("Path '%s' is in the index, but not at stage %d.\n"
1029 "Did you mean ':%d:%s'?",
1030 filename, stage,
1031 ce_stage(ce), filename);
1032 }
009fee47
MM
1033
1034 /* Confusion between relative and absolute filenames? */
1035 fullnamelen = namelen + strlen(prefix);
1036 fullname = xmalloc(fullnamelen + 1);
1037 strcpy(fullname, prefix);
1038 strcat(fullname, filename);
1039 pos = cache_name_pos(fullname, fullnamelen);
1040 if (pos < 0)
1041 pos = -pos - 1;
77e8466f
MH
1042 if (pos < active_nr) {
1043 ce = active_cache[pos];
1044 if (ce_namelen(ce) == fullnamelen &&
1045 !memcmp(ce->name, fullname, fullnamelen))
1046 die("Path '%s' is in the index, but not '%s'.\n"
e41d718c 1047 "Did you mean ':%d:%s' aka ':%d:./%s'?",
77e8466f 1048 fullname, filename,
e41d718c
MG
1049 ce_stage(ce), fullname,
1050 ce_stage(ce), filename);
77e8466f 1051 }
009fee47
MM
1052
1053 if (!lstat(filename, &st))
1054 die("Path '%s' exists on disk, but not in the index.", filename);
1055 if (errno == ENOENT || errno == ENOTDIR)
1056 die("Path '%s' does not exist (neither on disk nor in the index).",
1057 filename);
1058
1059 free(fullname);
1060}
1061
1062
979f7929
NTND
1063static char *resolve_relative_path(const char *rel)
1064{
1065 if (prefixcmp(rel, "./") && prefixcmp(rel, "../"))
1066 return NULL;
1067
1068 if (!startup_info)
1069 die("BUG: startup_info struct is not initialized.");
1070
1071 if (!is_inside_work_tree())
1072 die("relative path syntax can't be used outside working tree.");
1073
1074 /* die() inside prefix_path() if resolved path is outside worktree */
1075 return prefix_path(startup_info->prefix,
1076 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1077 rel);
1078}
1079
f01cc14c
JH
1080static int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1081 struct object_context *oc,
1082 int only_to_die, const char *prefix)
a0cd87a5
MK
1083{
1084 int ret, bracket_depth;
73b0e5af
JH
1085 int namelen = strlen(name);
1086 const char *cp;
5119602a 1087
573285e5
CP
1088 memset(oc, 0, sizeof(*oc));
1089 oc->mode = S_IFINVALID;
73b0e5af
JH
1090 ret = get_sha1_1(name, namelen, sha1);
1091 if (!ret)
1092 return ret;
1093 /* sha1:path --> object name of path in ent sha1
979f7929
NTND
1094 * :path -> object name of absolute path in index
1095 * :./path -> object name of path relative to cwd in index
73b0e5af 1096 * :[0-3]:path -> object name of path in index at stage
95ad6d2d 1097 * :/foo -> recent commit matching foo
73b0e5af
JH
1098 */
1099 if (name[0] == ':') {
1100 int stage = 0;
1101 struct cache_entry *ce;
979f7929 1102 char *new_path = NULL;
73b0e5af 1103 int pos;
2e83b66c 1104 if (!only_to_die && namelen > 2 && name[1] == '/') {
84baa31b
NTND
1105 struct commit_list *list = NULL;
1106 for_each_ref(handle_one_ref, &list);
1107 return get_sha1_oneline(name + 2, sha1, list);
1108 }
73b0e5af
JH
1109 if (namelen < 3 ||
1110 name[2] != ':' ||
1111 name[1] < '0' || '3' < name[1])
1112 cp = name + 1;
1113 else {
1114 stage = name[1] - '0';
1115 cp = name + 3;
5119602a 1116 }
3d6e0f74
JH
1117 new_path = resolve_relative_path(cp);
1118 if (!new_path) {
1119 namelen = namelen - (cp - name);
1120 } else {
1121 cp = new_path;
1122 namelen = strlen(cp);
1123 }
573285e5
CP
1124
1125 strncpy(oc->path, cp,
1126 sizeof(oc->path));
1127 oc->path[sizeof(oc->path)-1] = '\0';
1128
73b0e5af
JH
1129 if (!active_cache)
1130 read_cache();
73b0e5af
JH
1131 pos = cache_name_pos(cp, namelen);
1132 if (pos < 0)
1133 pos = -pos - 1;
1134 while (pos < active_nr) {
1135 ce = active_cache[pos];
1136 if (ce_namelen(ce) != namelen ||
1137 memcmp(ce->name, cp, namelen))
1138 break;
1139 if (ce_stage(ce) == stage) {
e702496e 1140 hashcpy(sha1, ce->sha1);
90064710 1141 oc->mode = ce->ce_mode;
979f7929 1142 free(new_path);
73b0e5af
JH
1143 return 0;
1144 }
e7cef45f 1145 pos++;
73b0e5af 1146 }
2e83b66c 1147 if (only_to_die && name[1] && name[1] != '/')
009fee47 1148 diagnose_invalid_index_path(stage, prefix, cp);
979f7929 1149 free(new_path);
73b0e5af
JH
1150 return -1;
1151 }
cce91a2c
SP
1152 for (cp = name, bracket_depth = 0; *cp; cp++) {
1153 if (*cp == '{')
1154 bracket_depth++;
1155 else if (bracket_depth && *cp == '}')
1156 bracket_depth--;
1157 else if (!bracket_depth && *cp == ':')
1158 break;
1159 }
1160 if (*cp == ':') {
73b0e5af 1161 unsigned char tree_sha1[20];
009fee47 1162 char *object_name = NULL;
2e83b66c 1163 if (only_to_die) {
009fee47
MM
1164 object_name = xmalloc(cp-name+1);
1165 strncpy(object_name, name, cp-name);
1166 object_name[cp-name] = '\0';
1167 }
1168 if (!get_sha1_1(name, cp-name, tree_sha1)) {
1169 const char *filename = cp+1;
979f7929
NTND
1170 char *new_filename = NULL;
1171
1172 new_filename = resolve_relative_path(filename);
1173 if (new_filename)
1174 filename = new_filename;
573285e5 1175 ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
2e83b66c 1176 if (only_to_die) {
009fee47
MM
1177 diagnose_invalid_sha1_path(prefix, filename,
1178 tree_sha1, object_name);
1179 free(object_name);
1180 }
573285e5
CP
1181 hashcpy(oc->tree, tree_sha1);
1182 strncpy(oc->path, filename,
1183 sizeof(oc->path));
1184 oc->path[sizeof(oc->path)-1] = '\0';
1185
979f7929 1186 free(new_filename);
009fee47
MM
1187 return ret;
1188 } else {
2e83b66c 1189 if (only_to_die)
009fee47
MM
1190 die("Invalid object name '%s'.", object_name);
1191 }
5119602a
LT
1192 }
1193 return ret;
9938af6a 1194}
f01cc14c 1195
8c135ea2
JH
1196/*
1197 * Call this function when you know "name" given by the end user must
1198 * name an object but it doesn't; the function _may_ die with a better
1199 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1200 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1201 * you have a chance to diagnose the error further.
1202 */
1203void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
f01cc14c
JH
1204{
1205 struct object_context oc;
8c135ea2
JH
1206 unsigned char sha1[20];
1207 get_sha1_with_context_1(name, sha1, &oc, 1, prefix);
1208}
1209
f01cc14c
JH
1210int get_sha1_with_context(const char *str, unsigned char *sha1, struct object_context *orc)
1211{
1212 return get_sha1_with_context_1(str, sha1, orc, 0, NULL);
1213}