rev-parse: A and B in "rev-parse A..B" refer to committish
[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 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
aa1dec9e
JH
221static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unused)
222{
223 int kind = sha1_object_info(sha1, NULL);
224 return kind == OBJ_COMMIT;
225}
226
e2643617
JH
227static int disambiguate_committish_only(const unsigned char *sha1, void *cb_data_unused)
228{
229 struct object *obj;
230 int kind;
231
232 kind = sha1_object_info(sha1, NULL);
233 if (kind == OBJ_COMMIT)
234 return 1;
235 if (kind != OBJ_TAG)
236 return 0;
237
238 /* We need to do this the hard way... */
239 obj = deref_tag(lookup_object(sha1), NULL, 0);
240 if (obj && obj->type == OBJ_COMMIT)
241 return 1;
242 return 0;
243}
244
daba53ae
JH
245static int disambiguate_tree_only(const unsigned char *sha1, void *cb_data_unused)
246{
247 int kind = sha1_object_info(sha1, NULL);
248 return kind == OBJ_TREE;
249}
250
251static int disambiguate_treeish_only(const unsigned char *sha1, void *cb_data_unused)
252{
253 struct object *obj;
254 int kind;
255
256 kind = sha1_object_info(sha1, NULL);
257 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
258 return 1;
259 if (kind != OBJ_TAG)
260 return 0;
261
262 /* We need to do this the hard way... */
263 obj = deref_tag(lookup_object(sha1), NULL, 0);
264 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
265 return 1;
266 return 0;
267}
268
269static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unused)
270{
271 int kind = sha1_object_info(sha1, NULL);
272 return kind == OBJ_BLOB;
273}
274
013f276e 275static int get_short_sha1(const char *name, int len, unsigned char *sha1,
37c00e55 276 unsigned flags)
9938af6a 277{
013f276e 278 int i, status;
1703f9aa
JH
279 char hex_pfx[40];
280 unsigned char bin_pfx[20];
a78fafe7 281 struct disambiguate_state ds;
37c00e55 282 int quietly = !!(flags & GET_SHA1_QUIETLY);
9938af6a 283
8a83157e 284 if (len < MINIMUM_ABBREV || len > 40)
af61c6e0 285 return -1;
1703f9aa
JH
286 hashclr(bin_pfx);
287 memset(hex_pfx, 'x', 40);
af61c6e0 288 for (i = 0; i < len ;i++) {
9938af6a
JH
289 unsigned char c = name[i];
290 unsigned char val;
9938af6a
JH
291 if (c >= '0' && c <= '9')
292 val = c - '0';
293 else if (c >= 'a' && c <= 'f')
294 val = c - 'a' + 10;
295 else if (c >= 'A' && c <='F') {
296 val = c - 'A' + 10;
297 c -= 'A' - 'a';
298 }
299 else
300 return -1;
1703f9aa 301 hex_pfx[i] = c;
9938af6a
JH
302 if (!(i & 1))
303 val <<= 4;
1703f9aa 304 bin_pfx[i >> 1] |= val;
9938af6a 305 }
99a19b43 306
a78fafe7
JH
307 prepare_alt_odb();
308
309 memset(&ds, 0, sizeof(ds));
aa1dec9e
JH
310 if (flags & GET_SHA1_COMMIT)
311 ds.fn = disambiguate_commit_only;
e2643617
JH
312 else if (flags & GET_SHA1_COMMITTISH)
313 ds.fn = disambiguate_committish_only;
daba53ae
JH
314 else if (flags & GET_SHA1_TREE)
315 ds.fn = disambiguate_tree_only;
316 else if (flags & GET_SHA1_TREEISH)
317 ds.fn = disambiguate_treeish_only;
318 else if (flags & GET_SHA1_BLOB)
319 ds.fn = disambiguate_blob_only;
aa1dec9e 320
a78fafe7
JH
321 find_short_object_filename(len, hex_pfx, &ds);
322 find_short_packed_object(len, bin_pfx, &ds);
323 status = finish_object_disambiguation(&ds, sha1);
324
013f276e 325 if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
1703f9aa 326 return error("short SHA1 %.*s is ambiguous.", len, hex_pfx);
013f276e
JH
327 return status;
328}
329
330const char *find_unique_abbrev(const unsigned char *sha1, int len)
331{
b66fde9a 332 int status, exists;
013f276e 333 static char hex[41];
47dd0d59 334
b66fde9a 335 exists = has_sha1_file(sha1);
013f276e 336 memcpy(hex, sha1_to_hex(sha1), 40);
02c5cba2 337 if (len == 40 || !len)
47dd0d59 338 return hex;
013f276e
JH
339 while (len < 40) {
340 unsigned char sha1_ret[20];
37c00e55 341 status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
b66fde9a
JH
342 if (exists
343 ? !status
344 : status == SHORT_NAME_NOT_FOUND) {
ea2c69ed 345 hex[len] = 0;
013f276e
JH
346 return hex;
347 }
013f276e
JH
348 len++;
349 }
b66fde9a 350 return hex;
9938af6a
JH
351}
352
6677c466 353static int ambiguous_path(const char *path, int len)
af13cdf2
LT
354{
355 int slash = 1;
6677c466 356 int cnt;
af13cdf2 357
6677c466 358 for (cnt = 0; cnt < len; cnt++) {
af13cdf2
LT
359 switch (*path++) {
360 case '\0':
361 break;
362 case '/':
363 if (slash)
364 break;
365 slash = 1;
366 continue;
367 case '.':
368 continue;
369 default:
370 slash = 0;
371 continue;
372 }
c054d64e 373 break;
af13cdf2 374 }
6677c466 375 return slash;
af13cdf2
LT
376}
377
ae0ba8e2
JH
378static inline int upstream_mark(const char *string, int len)
379{
380 const char *suffix[] = { "@{upstream}", "@{u}" };
381 int i;
382
383 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
384 int suffix_len = strlen(suffix[i]);
385 if (suffix_len <= len
386 && !memcmp(string, suffix[i], suffix_len))
387 return suffix_len;
388 }
389 return 0;
390}
391
e48ba200 392static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
d18ba221 393
e86eb666
JH
394static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
395{
eedce784 396 static const char *warn_msg = "refname '%.*s' is ambiguous.";
ed378ec7 397 char *real_ref = NULL;
ab2a1a32
JH
398 int refs_found = 0;
399 int at, reflog_len;
9938af6a 400
3c3852e3 401 if (len == 40 && !get_sha1_hex(str, sha1))
9938af6a
JH
402 return 0;
403
d18ba221 404 /* basic@{time or number or -number} format to query ref-log */
694500ed 405 reflog_len = at = 0;
f265458f 406 if (len && str[len-1] == '}') {
aa9c55b6 407 for (at = len-2; at >= 0; at--) {
ab2a1a32 408 if (str[at] == '@' && str[at+1] == '{') {
ae0ba8e2 409 if (!upstream_mark(str + at, len - at)) {
28fb8438
JS
410 reflog_len = (len-1) - (at+2);
411 len = at;
412 }
ab2a1a32
JH
413 break;
414 }
d556fae2
SP
415 }
416 }
417
af13cdf2 418 /* Accept only unambiguous ref paths. */
11cf8801 419 if (len && ambiguous_path(str, len))
af13cdf2
LT
420 return -1;
421
11cf8801 422 if (!len && reflog_len) {
d18ba221
TR
423 struct strbuf buf = STRBUF_INIT;
424 int ret;
425 /* try the @{-N} syntax for n-th checkout */
431b1969 426 ret = interpret_branch_name(str+at, &buf);
d18ba221
TR
427 if (ret > 0) {
428 /* substitute this branch name and restart */
e48ba200 429 return get_sha1_1(buf.buf, buf.len, sha1, 0);
d18ba221
TR
430 } else if (ret == 0) {
431 return -1;
432 }
11cf8801
NP
433 /* allow "@{...}" to mean the current branch reflog */
434 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
f2eba66d
NP
435 } else if (reflog_len)
436 refs_found = dwim_log(str, len, sha1, &real_ref);
437 else
11cf8801 438 refs_found = dwim_ref(str, len, sha1, &real_ref);
d556fae2
SP
439
440 if (!refs_found)
441 return -1;
442
443 if (warn_ambiguous_refs && refs_found > 1)
eedce784 444 warning(warn_msg, len, str);
d556fae2 445
ab2a1a32 446 if (reflog_len) {
ab2a1a32
JH
447 int nth, i;
448 unsigned long at_time;
16d7cc90
JH
449 unsigned long co_time;
450 int co_tz, co_cnt;
451
12a258c0
JK
452 /* a @{-N} placed anywhere except the start is an error */
453 if (str[at+2] == '-')
454 return -1;
455
fe558516 456 /* Is it asking for N-th entry, or approxidate? */
ab2a1a32
JH
457 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
458 char ch = str[at+2+i];
459 if ('0' <= ch && ch <= '9')
460 nth = nth * 10 + ch - '0';
461 else
462 nth = -1;
463 }
ea360dd0
SP
464 if (100000000 <= nth) {
465 at_time = nth;
466 nth = -1;
467 } else if (0 <= nth)
ab2a1a32 468 at_time = 0;
861f00e3 469 else {
93cfa7c7 470 int errors = 0;
861f00e3 471 char *tmp = xstrndup(str + at + 2, reflog_len);
93cfa7c7 472 at_time = approxidate_careful(tmp, &errors);
861f00e3 473 free(tmp);
a5e10acb
JH
474 if (errors)
475 return -1;
861f00e3 476 }
16d7cc90
JH
477 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
478 &co_time, &co_tz, &co_cnt)) {
479 if (at_time)
eedce784
JS
480 warning("Log for '%.*s' only goes "
481 "back to %s.", len, str,
73013afd 482 show_date(co_time, co_tz, DATE_RFC2822));
e6eedc31
JS
483 else {
484 free(real_ref);
485 die("Log for '%.*s' only has %d entries.",
486 len, str, co_cnt);
487 }
16d7cc90 488 }
d556fae2
SP
489 }
490
ed378ec7 491 free(real_ref);
d556fae2 492 return 0;
9938af6a
JH
493}
494
9938af6a
JH
495static int get_parent(const char *name, int len,
496 unsigned char *result, int idx)
497{
498 unsigned char sha1[20];
e2643617 499 int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
9938af6a
JH
500 struct commit *commit;
501 struct commit_list *p;
502
503 if (ret)
504 return ret;
505 commit = lookup_commit_reference(sha1);
506 if (!commit)
507 return -1;
508 if (parse_commit(commit))
509 return -1;
510 if (!idx) {
e702496e 511 hashcpy(result, commit->object.sha1);
9938af6a
JH
512 return 0;
513 }
514 p = commit->parents;
515 while (p) {
516 if (!--idx) {
e702496e 517 hashcpy(result, p->item->object.sha1);
9938af6a
JH
518 return 0;
519 }
520 p = p->next;
521 }
522 return -1;
523}
524
4f7599ac
JH
525static int get_nth_ancestor(const char *name, int len,
526 unsigned char *result, int generation)
527{
528 unsigned char sha1[20];
621ff675
LT
529 struct commit *commit;
530 int ret;
531
e2643617 532 ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
4f7599ac
JH
533 if (ret)
534 return ret;
621ff675
LT
535 commit = lookup_commit_reference(sha1);
536 if (!commit)
537 return -1;
4f7599ac
JH
538
539 while (generation--) {
621ff675 540 if (parse_commit(commit) || !commit->parents)
4f7599ac 541 return -1;
621ff675 542 commit = commit->parents->item;
4f7599ac 543 }
621ff675 544 hashcpy(result, commit->object.sha1);
4f7599ac
JH
545 return 0;
546}
547
81776315
JH
548struct object *peel_to_type(const char *name, int namelen,
549 struct object *o, enum object_type expected_type)
550{
551 if (name && !namelen)
552 namelen = strlen(name);
81776315
JH
553 while (1) {
554 if (!o || (!o->parsed && !parse_object(o->sha1)))
555 return NULL;
556 if (o->type == expected_type)
557 return o;
558 if (o->type == OBJ_TAG)
559 o = ((struct tag*) o)->tagged;
560 else if (o->type == OBJ_COMMIT)
561 o = &(((struct commit *) o)->tree->object);
562 else {
563 if (name)
564 error("%.*s: expected %s type, but the object "
565 "dereferences to %s type",
566 namelen, name, typename(expected_type),
567 typename(o->type));
568 return NULL;
569 }
570 }
571}
572
5385f52d
JH
573static int peel_onion(const char *name, int len, unsigned char *sha1)
574{
575 unsigned char outer[20];
576 const char *sp;
885a86ab 577 unsigned int expected_type = 0;
e2643617 578 unsigned lookup_flags = 0;
5385f52d
JH
579 struct object *o;
580
581 /*
582 * "ref^{type}" dereferences ref repeatedly until you cannot
583 * dereference anymore, or you get an object of given type,
584 * whichever comes first. "ref^{}" means just dereference
585 * tags until you get a non-tag. "ref^0" is a shorthand for
586 * "ref^{commit}". "commit^{tree}" could be used to find the
587 * top-level tree of the given commit.
588 */
589 if (len < 4 || name[len-1] != '}')
590 return -1;
591
592 for (sp = name + len - 1; name <= sp; sp--) {
593 int ch = *sp;
594 if (ch == '{' && name < sp && sp[-1] == '^')
595 break;
596 }
597 if (sp <= name)
598 return -1;
599
600 sp++; /* beginning of type name, or closing brace for empty */
601 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
1974632c 602 expected_type = OBJ_COMMIT;
5385f52d 603 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
1974632c 604 expected_type = OBJ_TREE;
5385f52d 605 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
1974632c 606 expected_type = OBJ_BLOB;
5385f52d 607 else if (sp[0] == '}')
1974632c 608 expected_type = OBJ_NONE;
32574b68
NTND
609 else if (sp[0] == '/')
610 expected_type = OBJ_COMMIT;
5385f52d
JH
611 else
612 return -1;
613
e2643617
JH
614 if (expected_type == OBJ_COMMIT)
615 lookup_flags = GET_SHA1_COMMITTISH;
616
617 if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
5385f52d
JH
618 return -1;
619
620 o = parse_object(outer);
621 if (!o)
622 return -1;
885a86ab 623 if (!expected_type) {
9534f40b 624 o = deref_tag(o, name, sp - name - 2);
6e1c6c10
JH
625 if (!o || (!o->parsed && !parse_object(o->sha1)))
626 return -1;
e702496e 627 hashcpy(sha1, o->sha1);
32574b68 628 return 0;
5385f52d 629 }
32574b68
NTND
630
631 /*
632 * At this point, the syntax look correct, so
633 * if we do not get the needed object, we should
634 * barf.
635 */
636 o = peel_to_type(name, len, o, expected_type);
637 if (!o)
81776315 638 return -1;
32574b68
NTND
639
640 hashcpy(sha1, o->sha1);
641 if (sp[0] == '/') {
642 /* "$commit^{/foo}" */
643 char *prefix;
644 int ret;
645 struct commit_list *list = NULL;
646
81776315 647 /*
4322842a
NTND
648 * $commit^{/}. Some regex implementation may reject.
649 * We don't need regex anyway. '' pattern always matches.
5385f52d 650 */
4322842a 651 if (sp[1] == '}')
81776315 652 return 0;
4322842a 653
32574b68
NTND
654 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
655 commit_list_insert((struct commit *)o, &list);
656 ret = get_sha1_oneline(prefix, sha1, list);
657 free(prefix);
658 return ret;
5385f52d
JH
659 }
660 return 0;
661}
662
7dd45e15
JH
663static int get_describe_name(const char *name, int len, unsigned char *sha1)
664{
665 const char *cp;
6269b6b6 666 unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
7dd45e15
JH
667
668 for (cp = name + len - 1; name + 2 <= cp; cp--) {
669 char ch = *cp;
670 if (hexval(ch) & ~0377) {
671 /* We must be looking at g in "SOMETHING-g"
672 * for it to be describe output.
673 */
674 if (ch == 'g' && cp[-1] == '-') {
675 cp++;
676 len -= cp - name;
6269b6b6 677 return get_short_sha1(cp, len, sha1, flags);
7dd45e15
JH
678 }
679 }
680 }
681 return -1;
682}
683
e48ba200 684static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
9938af6a 685{
0601dbe1 686 int ret, has_suffix;
4f7599ac 687 const char *cp;
9938af6a 688
621ff675
LT
689 /*
690 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
4f7599ac 691 */
0601dbe1 692 has_suffix = 0;
4f7599ac
JH
693 for (cp = name + len - 1; name <= cp; cp--) {
694 int ch = *cp;
695 if ('0' <= ch && ch <= '9')
696 continue;
0601dbe1
JH
697 if (ch == '~' || ch == '^')
698 has_suffix = ch;
4f7599ac
JH
699 break;
700 }
0601dbe1
JH
701
702 if (has_suffix) {
703 int num = 0;
4f7599ac
JH
704 int len1 = cp - name;
705 cp++;
706 while (cp < name + len)
0601dbe1 707 num = num * 10 + *cp++ - '0';
621ff675
LT
708 if (!num && len1 == len - 1)
709 num = 1;
710 if (has_suffix == '^')
0601dbe1 711 return get_parent(name, len1, sha1, num);
0601dbe1
JH
712 /* else if (has_suffix == '~') -- goes without saying */
713 return get_nth_ancestor(name, len1, sha1, num);
4f7599ac
JH
714 }
715
5385f52d
JH
716 ret = peel_onion(name, len, sha1);
717 if (!ret)
718 return 0;
719
9938af6a
JH
720 ret = get_sha1_basic(name, len, sha1);
721 if (!ret)
722 return 0;
7dd45e15
JH
723
724 /* It could be describe output that is "SOMETHING-gXXXX" */
725 ret = get_describe_name(name, len, sha1);
726 if (!ret)
727 return 0;
728
e2643617 729 return get_short_sha1(name, len, sha1, lookup_flags);
9938af6a
JH
730}
731
f7bff003
JH
732/*
733 * This interprets names like ':/Initial revision of "git"' by searching
734 * through history and returning the first commit whose message starts
3d045897 735 * the given regular expression.
f7bff003
JH
736 *
737 * For future extension, ':/!' is reserved. If you want to match a message
738 * beginning with a '!', you have to repeat the exclamation mark.
739 */
740#define ONELINE_SEEN (1u<<20)
741
28a4d940
JS
742static int handle_one_ref(const char *path,
743 const unsigned char *sha1, int flag, void *cb_data)
744{
745 struct commit_list **list = cb_data;
746 struct object *object = parse_object(sha1);
747 if (!object)
748 return 0;
affeef12 749 if (object->type == OBJ_TAG) {
28a4d940 750 object = deref_tag(object, path, strlen(path));
affeef12
MK
751 if (!object)
752 return 0;
753 }
28a4d940
JS
754 if (object->type != OBJ_COMMIT)
755 return 0;
47e44ed1 756 commit_list_insert_by_date((struct commit *)object, list);
28a4d940
JS
757 return 0;
758}
759
84baa31b
NTND
760static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
761 struct commit_list *list)
28a4d940 762{
84baa31b 763 struct commit_list *backup = NULL, *l;
28042dbc 764 int found = 0;
57895105 765 regex_t regex;
28a4d940
JS
766
767 if (prefix[0] == '!') {
768 if (prefix[1] != '!')
769 die ("Invalid search pattern: %s", prefix);
770 prefix++;
771 }
57895105
LT
772
773 if (regcomp(&regex, prefix, REG_EXTENDED))
774 die("Invalid search pattern: %s", prefix);
775
84baa31b
NTND
776 for (l = list; l; l = l->next) {
777 l->item->object.flags |= ONELINE_SEEN;
28a4d940 778 commit_list_insert(l->item, &backup);
84baa31b 779 }
ed8ad7e2 780 while (list) {
28042dbc 781 char *p, *to_free = NULL;
1358e7d6 782 struct commit *commit;
364d3e65
JS
783 enum object_type type;
784 unsigned long size;
28042dbc 785 int matches;
ed8ad7e2
JM
786
787 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
283cdbcf
MK
788 if (!parse_object(commit->object.sha1))
789 continue;
364d3e65
JS
790 if (commit->buffer)
791 p = commit->buffer;
792 else {
793 p = read_sha1_file(commit->object.sha1, &type, &size);
794 if (!p)
795 continue;
28042dbc 796 to_free = p;
364d3e65 797 }
28042dbc
JH
798
799 p = strstr(p, "\n\n");
800 matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
801 free(to_free);
802
803 if (matches) {
28a4d940 804 hashcpy(sha1, commit->object.sha1);
28042dbc 805 found = 1;
28a4d940
JS
806 break;
807 }
808 }
57895105 809 regfree(&regex);
28a4d940
JS
810 free_commit_list(list);
811 for (l = backup; l; l = l->next)
812 clear_commit_marks(l->item, ONELINE_SEEN);
28042dbc
JH
813 free_commit_list(backup);
814 return found ? 0 : -1;
28a4d940
JS
815}
816
ae5a6c36 817struct grab_nth_branch_switch_cbdata {
c2883e62 818 long cnt, alloc;
ae5a6c36
JH
819 struct strbuf *buf;
820};
821
822static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
823 const char *email, unsigned long timestamp, int tz,
824 const char *message, void *cb_data)
825{
826 struct grab_nth_branch_switch_cbdata *cb = cb_data;
a884d0cb
TR
827 const char *match = NULL, *target = NULL;
828 size_t len;
c2883e62 829 int nth;
a884d0cb
TR
830
831 if (!prefixcmp(message, "checkout: moving from ")) {
832 match = message + strlen("checkout: moving from ");
d7c03c1f 833 target = strstr(match, " to ");
ae5a6c36
JH
834 }
835
c829774c 836 if (!match || !target)
ae5a6c36
JH
837 return 0;
838
d7c03c1f 839 len = target - match;
c2883e62
JH
840 nth = cb->cnt++ % cb->alloc;
841 strbuf_reset(&cb->buf[nth]);
842 strbuf_add(&cb->buf[nth], match, len);
ae5a6c36
JH
843 return 0;
844}
845
846/*
ae0ba8e2
JH
847 * Parse @{-N} syntax, return the number of characters parsed
848 * if successful; otherwise signal an error with negative value.
ae5a6c36 849 */
ae0ba8e2 850static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf)
ae5a6c36 851{
c2883e62 852 long nth;
39765e59 853 int i, retval;
ae5a6c36 854 struct grab_nth_branch_switch_cbdata cb;
a884d0cb
TR
855 const char *brace;
856 char *num_end;
ae5a6c36
JH
857
858 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
859 return -1;
a884d0cb
TR
860 brace = strchr(name, '}');
861 if (!brace)
862 return -1;
863 nth = strtol(name+3, &num_end, 10);
864 if (num_end != brace)
ae5a6c36 865 return -1;
c2883e62
JH
866 if (nth <= 0)
867 return -1;
868 cb.alloc = nth;
869 cb.buf = xmalloc(nth * sizeof(struct strbuf));
870 for (i = 0; i < nth; i++)
871 strbuf_init(&cb.buf[i], 20);
872 cb.cnt = 0;
39765e59 873 retval = 0;
101d15e0
JH
874 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
875 if (cb.cnt < nth) {
876 cb.cnt = 0;
101d15e0
JH
877 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
878 }
c2883e62 879 if (cb.cnt < nth)
39765e59 880 goto release_return;
c2883e62
JH
881 i = cb.cnt % nth;
882 strbuf_reset(buf);
883 strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
39765e59
JH
884 retval = brace-name+1;
885
886release_return:
c2883e62
JH
887 for (i = 0; i < nth; i++)
888 strbuf_release(&cb.buf[i]);
889 free(cb.buf);
a884d0cb 890
39765e59 891 return retval;
ae5a6c36
JH
892}
893
619a644d
JH
894int get_sha1_mb(const char *name, unsigned char *sha1)
895{
896 struct commit *one, *two;
897 struct commit_list *mbs;
898 unsigned char sha1_tmp[20];
899 const char *dots;
900 int st;
901
902 dots = strstr(name, "...");
903 if (!dots)
904 return get_sha1(name, sha1);
905 if (dots == name)
906 st = get_sha1("HEAD", sha1_tmp);
907 else {
908 struct strbuf sb;
909 strbuf_init(&sb, dots - name);
910 strbuf_add(&sb, name, dots - name);
cd74e473 911 st = get_sha1_committish(sb.buf, sha1_tmp);
619a644d
JH
912 strbuf_release(&sb);
913 }
914 if (st)
915 return st;
916 one = lookup_commit_reference_gently(sha1_tmp, 0);
917 if (!one)
918 return -1;
919
cd74e473 920 if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
619a644d
JH
921 return -1;
922 two = lookup_commit_reference_gently(sha1_tmp, 0);
923 if (!two)
924 return -1;
925 mbs = get_merge_bases(one, two, 1);
926 if (!mbs || mbs->next)
927 st = -1;
928 else {
929 st = 0;
930 hashcpy(sha1, mbs->item->object.sha1);
931 }
932 free_commit_list(mbs);
933 return st;
934}
935
ae0ba8e2
JH
936/*
937 * This reads short-hand syntax that not only evaluates to a commit
938 * object name, but also can act as if the end user spelled the name
939 * of the branch from the command line.
940 *
941 * - "@{-N}" finds the name of the Nth previous branch we were on, and
942 * places the name of the branch in the given buf and returns the
943 * number of characters parsed if successful.
944 *
945 * - "<branch>@{upstream}" finds the name of the other ref that
946 * <branch> is configured to merge with (missing <branch> defaults
947 * to the current branch), and places the name of the branch in the
948 * given buf and returns the number of characters parsed if
949 * successful.
950 *
951 * If the input is not of the accepted format, it returns a negative
952 * number to signal an error.
953 *
954 * If the input was ok but there are not N branch switches in the
955 * reflog, it returns 0.
956 */
957int interpret_branch_name(const char *name, struct strbuf *buf)
958{
959 char *cp;
960 struct branch *upstream;
961 int namelen = strlen(name);
962 int len = interpret_nth_prior_checkout(name, buf);
963 int tmp_len;
964
965 if (!len)
966 return len; /* syntax Ok, not enough switches */
d46a8301
JK
967 if (0 < len && len == namelen)
968 return len; /* consumed all */
969 else if (0 < len) {
970 /* we have extra data, which might need further processing */
971 struct strbuf tmp = STRBUF_INIT;
972 int used = buf->len;
973 int ret;
974
975 strbuf_add(buf, name + len, namelen - len);
976 ret = interpret_branch_name(buf->buf, &tmp);
977 /* that data was not interpreted, remove our cruft */
978 if (ret < 0) {
979 strbuf_setlen(buf, used);
980 return len;
981 }
982 strbuf_reset(buf);
983 strbuf_addbuf(buf, &tmp);
984 strbuf_release(&tmp);
985 /* tweak for size of {-N} versus expanded ref name */
986 return ret - used + len;
987 }
988
ae0ba8e2
JH
989 cp = strchr(name, '@');
990 if (!cp)
991 return -1;
992 tmp_len = upstream_mark(cp, namelen - (cp - name));
993 if (!tmp_len)
994 return -1;
995 len = cp + tmp_len - name;
996 cp = xstrndup(name, cp - name);
997 upstream = branch_get(*cp ? cp : NULL);
998 if (!upstream
999 || !upstream->merge
1000 || !upstream->merge[0]->dst)
1001 return error("No upstream branch found for '%s'", cp);
1002 free(cp);
1003 cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
1004 strbuf_reset(buf);
1005 strbuf_addstr(buf, cp);
1006 free(cp);
1007 return len;
1008}
1009
6bab74e7
JN
1010int strbuf_branchname(struct strbuf *sb, const char *name)
1011{
1012 int len = strlen(name);
1013 if (interpret_branch_name(name, sb) == len)
1014 return 0;
1015 strbuf_add(sb, name, len);
1016 return len;
1017}
1018
1019int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1020{
1021 strbuf_branchname(sb, name);
1022 if (name[0] == '-')
8d9c5010 1023 return -1;
6bab74e7 1024 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
8d9c5010 1025 return check_refname_format(sb->buf, 0);
6bab74e7
JN
1026}
1027
9938af6a
JH
1028/*
1029 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
1030 * notably "xyz^" for "parent of xyz"
1031 */
1032int get_sha1(const char *name, unsigned char *sha1)
1033{
573285e5 1034 struct object_context unused;
33bd598c 1035 return get_sha1_with_context(name, 0, sha1, &unused);
a0cd87a5
MK
1036}
1037
cd74e473
JH
1038/*
1039 * Many callers know that the user meant to name a committish by
1040 * syntactical positions where the object name appears. Calling this
1041 * function allows the machinery to disambiguate shorter-than-unique
1042 * abbreviated object names between committish and others.
1043 *
1044 * Note that this does NOT error out when the named object is not a
1045 * committish. It is merely to give a hint to the disambiguation
1046 * machinery.
1047 */
1048int get_sha1_committish(const char *name, unsigned char *sha1)
1049{
1050 struct object_context unused;
1051 return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1052 sha1, &unused);
1053}
1054
daba53ae
JH
1055int get_sha1_treeish(const char *name, unsigned char *sha1)
1056{
1057 struct object_context unused;
1058 return get_sha1_with_context(name, GET_SHA1_TREEISH,
1059 sha1, &unused);
1060}
1061
1062int get_sha1_commit(const char *name, unsigned char *sha1)
1063{
1064 struct object_context unused;
1065 return get_sha1_with_context(name, GET_SHA1_COMMIT,
1066 sha1, &unused);
1067}
1068
1069int get_sha1_tree(const char *name, unsigned char *sha1)
1070{
1071 struct object_context unused;
1072 return get_sha1_with_context(name, GET_SHA1_TREE,
1073 sha1, &unused);
1074}
1075
1076int get_sha1_blob(const char *name, unsigned char *sha1)
1077{
1078 struct object_context unused;
1079 return get_sha1_with_context(name, GET_SHA1_BLOB,
1080 sha1, &unused);
1081}
1082
009fee47
MM
1083/* Must be called only when object_name:filename doesn't exist. */
1084static void diagnose_invalid_sha1_path(const char *prefix,
1085 const char *filename,
1086 const unsigned char *tree_sha1,
1087 const char *object_name)
1088{
1089 struct stat st;
1090 unsigned char sha1[20];
1091 unsigned mode;
1092
1093 if (!prefix)
1094 prefix = "";
1095
1096 if (!lstat(filename, &st))
1097 die("Path '%s' exists on disk, but not in '%s'.",
1098 filename, object_name);
1099 if (errno == ENOENT || errno == ENOTDIR) {
1100 char *fullname = xmalloc(strlen(filename)
1101 + strlen(prefix) + 1);
1102 strcpy(fullname, prefix);
1103 strcat(fullname, filename);
1104
1105 if (!get_tree_entry(tree_sha1, fullname,
1106 sha1, &mode)) {
1107 die("Path '%s' exists, but not '%s'.\n"
e41d718c 1108 "Did you mean '%s:%s' aka '%s:./%s'?",
009fee47
MM
1109 fullname,
1110 filename,
1111 object_name,
e41d718c
MG
1112 fullname,
1113 object_name,
1114 filename);
009fee47
MM
1115 }
1116 die("Path '%s' does not exist in '%s'",
1117 filename, object_name);
1118 }
1119}
1120
1121/* Must be called only when :stage:filename doesn't exist. */
1122static void diagnose_invalid_index_path(int stage,
1123 const char *prefix,
1124 const char *filename)
1125{
1126 struct stat st;
1127 struct cache_entry *ce;
1128 int pos;
1129 unsigned namelen = strlen(filename);
1130 unsigned fullnamelen;
1131 char *fullname;
1132
1133 if (!prefix)
1134 prefix = "";
1135
1136 /* Wrong stage number? */
1137 pos = cache_name_pos(filename, namelen);
1138 if (pos < 0)
1139 pos = -pos - 1;
77e8466f
MH
1140 if (pos < active_nr) {
1141 ce = active_cache[pos];
1142 if (ce_namelen(ce) == namelen &&
1143 !memcmp(ce->name, filename, namelen))
1144 die("Path '%s' is in the index, but not at stage %d.\n"
1145 "Did you mean ':%d:%s'?",
1146 filename, stage,
1147 ce_stage(ce), filename);
1148 }
009fee47
MM
1149
1150 /* Confusion between relative and absolute filenames? */
1151 fullnamelen = namelen + strlen(prefix);
1152 fullname = xmalloc(fullnamelen + 1);
1153 strcpy(fullname, prefix);
1154 strcat(fullname, filename);
1155 pos = cache_name_pos(fullname, fullnamelen);
1156 if (pos < 0)
1157 pos = -pos - 1;
77e8466f
MH
1158 if (pos < active_nr) {
1159 ce = active_cache[pos];
1160 if (ce_namelen(ce) == fullnamelen &&
1161 !memcmp(ce->name, fullname, fullnamelen))
1162 die("Path '%s' is in the index, but not '%s'.\n"
e41d718c 1163 "Did you mean ':%d:%s' aka ':%d:./%s'?",
77e8466f 1164 fullname, filename,
e41d718c
MG
1165 ce_stage(ce), fullname,
1166 ce_stage(ce), filename);
77e8466f 1167 }
009fee47
MM
1168
1169 if (!lstat(filename, &st))
1170 die("Path '%s' exists on disk, but not in the index.", filename);
1171 if (errno == ENOENT || errno == ENOTDIR)
1172 die("Path '%s' does not exist (neither on disk nor in the index).",
1173 filename);
1174
1175 free(fullname);
1176}
1177
1178
979f7929
NTND
1179static char *resolve_relative_path(const char *rel)
1180{
1181 if (prefixcmp(rel, "./") && prefixcmp(rel, "../"))
1182 return NULL;
1183
1184 if (!startup_info)
1185 die("BUG: startup_info struct is not initialized.");
1186
1187 if (!is_inside_work_tree())
1188 die("relative path syntax can't be used outside working tree.");
1189
1190 /* die() inside prefix_path() if resolved path is outside worktree */
1191 return prefix_path(startup_info->prefix,
1192 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1193 rel);
1194}
1195
33bd598c
JH
1196static int get_sha1_with_context_1(const char *name,
1197 unsigned flags,
1198 const char *prefix,
1199 unsigned char *sha1,
1200 struct object_context *oc)
a0cd87a5
MK
1201{
1202 int ret, bracket_depth;
73b0e5af
JH
1203 int namelen = strlen(name);
1204 const char *cp;
33bd598c 1205 int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
5119602a 1206
573285e5
CP
1207 memset(oc, 0, sizeof(*oc));
1208 oc->mode = S_IFINVALID;
33bd598c 1209 ret = get_sha1_1(name, namelen, sha1, flags);
73b0e5af
JH
1210 if (!ret)
1211 return ret;
33bd598c
JH
1212 /*
1213 * sha1:path --> object name of path in ent sha1
979f7929
NTND
1214 * :path -> object name of absolute path in index
1215 * :./path -> object name of path relative to cwd in index
73b0e5af 1216 * :[0-3]:path -> object name of path in index at stage
95ad6d2d 1217 * :/foo -> recent commit matching foo
73b0e5af
JH
1218 */
1219 if (name[0] == ':') {
1220 int stage = 0;
1221 struct cache_entry *ce;
979f7929 1222 char *new_path = NULL;
73b0e5af 1223 int pos;
2e83b66c 1224 if (!only_to_die && namelen > 2 && name[1] == '/') {
84baa31b
NTND
1225 struct commit_list *list = NULL;
1226 for_each_ref(handle_one_ref, &list);
1227 return get_sha1_oneline(name + 2, sha1, list);
1228 }
73b0e5af
JH
1229 if (namelen < 3 ||
1230 name[2] != ':' ||
1231 name[1] < '0' || '3' < name[1])
1232 cp = name + 1;
1233 else {
1234 stage = name[1] - '0';
1235 cp = name + 3;
5119602a 1236 }
3d6e0f74
JH
1237 new_path = resolve_relative_path(cp);
1238 if (!new_path) {
1239 namelen = namelen - (cp - name);
1240 } else {
1241 cp = new_path;
1242 namelen = strlen(cp);
1243 }
573285e5
CP
1244
1245 strncpy(oc->path, cp,
1246 sizeof(oc->path));
1247 oc->path[sizeof(oc->path)-1] = '\0';
1248
73b0e5af
JH
1249 if (!active_cache)
1250 read_cache();
73b0e5af
JH
1251 pos = cache_name_pos(cp, namelen);
1252 if (pos < 0)
1253 pos = -pos - 1;
1254 while (pos < active_nr) {
1255 ce = active_cache[pos];
1256 if (ce_namelen(ce) != namelen ||
1257 memcmp(ce->name, cp, namelen))
1258 break;
1259 if (ce_stage(ce) == stage) {
e702496e 1260 hashcpy(sha1, ce->sha1);
90064710 1261 oc->mode = ce->ce_mode;
979f7929 1262 free(new_path);
73b0e5af
JH
1263 return 0;
1264 }
e7cef45f 1265 pos++;
73b0e5af 1266 }
2e83b66c 1267 if (only_to_die && name[1] && name[1] != '/')
009fee47 1268 diagnose_invalid_index_path(stage, prefix, cp);
979f7929 1269 free(new_path);
73b0e5af
JH
1270 return -1;
1271 }
cce91a2c
SP
1272 for (cp = name, bracket_depth = 0; *cp; cp++) {
1273 if (*cp == '{')
1274 bracket_depth++;
1275 else if (bracket_depth && *cp == '}')
1276 bracket_depth--;
1277 else if (!bracket_depth && *cp == ':')
1278 break;
1279 }
1280 if (*cp == ':') {
73b0e5af 1281 unsigned char tree_sha1[20];
009fee47 1282 char *object_name = NULL;
2e83b66c 1283 if (only_to_die) {
009fee47
MM
1284 object_name = xmalloc(cp-name+1);
1285 strncpy(object_name, name, cp-name);
1286 object_name[cp-name] = '\0';
1287 }
daba53ae 1288 if (!get_sha1_1(name, cp-name, tree_sha1, GET_SHA1_TREEISH)) {
009fee47 1289 const char *filename = cp+1;
979f7929
NTND
1290 char *new_filename = NULL;
1291
1292 new_filename = resolve_relative_path(filename);
1293 if (new_filename)
1294 filename = new_filename;
573285e5 1295 ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
2e83b66c 1296 if (only_to_die) {
009fee47
MM
1297 diagnose_invalid_sha1_path(prefix, filename,
1298 tree_sha1, object_name);
1299 free(object_name);
1300 }
573285e5
CP
1301 hashcpy(oc->tree, tree_sha1);
1302 strncpy(oc->path, filename,
1303 sizeof(oc->path));
1304 oc->path[sizeof(oc->path)-1] = '\0';
1305
979f7929 1306 free(new_filename);
009fee47
MM
1307 return ret;
1308 } else {
2e83b66c 1309 if (only_to_die)
009fee47
MM
1310 die("Invalid object name '%s'.", object_name);
1311 }
5119602a
LT
1312 }
1313 return ret;
9938af6a 1314}
f01cc14c 1315
8c135ea2
JH
1316/*
1317 * Call this function when you know "name" given by the end user must
1318 * name an object but it doesn't; the function _may_ die with a better
1319 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1320 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1321 * you have a chance to diagnose the error further.
1322 */
1323void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
f01cc14c
JH
1324{
1325 struct object_context oc;
8c135ea2 1326 unsigned char sha1[20];
33bd598c 1327 get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
8c135ea2
JH
1328}
1329
33bd598c 1330int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc)
f01cc14c 1331{
33bd598c 1332 return get_sha1_with_context_1(str, flags, NULL, sha1, orc);
f01cc14c 1333}