grep: remove redundant and verbose re-assignments to 0
[git.git] / sha1_name.c
blobd2d732c19b62dd13c27852fcbf2385b29dd58b1d
1 #include "cache.h"
2 #include "config.h"
3 #include "tag.h"
4 #include "commit.h"
5 #include "tree.h"
6 #include "blob.h"
7 #include "tree-walk.h"
8 #include "refs.h"
9 #include "remote.h"
10 #include "dir.h"
11 #include "sha1-array.h"
13 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
15 typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
17 struct disambiguate_state {
18 int len; /* length of prefix in hex chars */
19 char hex_pfx[GIT_MAX_HEXSZ + 1];
20 struct object_id bin_pfx;
22 disambiguate_hint_fn fn;
23 void *cb_data;
24 struct object_id candidate;
25 unsigned candidate_exists:1;
26 unsigned candidate_checked:1;
27 unsigned candidate_ok:1;
28 unsigned disambiguate_fn_used:1;
29 unsigned ambiguous:1;
30 unsigned always_call_fn:1;
33 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
35 if (ds->always_call_fn) {
36 ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0;
37 return;
39 if (!ds->candidate_exists) {
40 /* this is the first candidate */
41 oidcpy(&ds->candidate, current);
42 ds->candidate_exists = 1;
43 return;
44 } else if (!oidcmp(&ds->candidate, current)) {
45 /* the same as what we already have seen */
46 return;
49 if (!ds->fn) {
50 /* cannot disambiguate between ds->candidate and current */
51 ds->ambiguous = 1;
52 return;
55 if (!ds->candidate_checked) {
56 ds->candidate_ok = ds->fn(&ds->candidate, ds->cb_data);
57 ds->disambiguate_fn_used = 1;
58 ds->candidate_checked = 1;
61 if (!ds->candidate_ok) {
62 /* discard the candidate; we know it does not satisfy fn */
63 oidcpy(&ds->candidate, current);
64 ds->candidate_checked = 0;
65 return;
68 /* if we reach this point, we know ds->candidate satisfies fn */
69 if (ds->fn(current, ds->cb_data)) {
71 * if both current and candidate satisfy fn, we cannot
72 * disambiguate.
74 ds->candidate_ok = 0;
75 ds->ambiguous = 1;
78 /* otherwise, current can be discarded and candidate is still good */
81 static void find_short_object_filename(struct disambiguate_state *ds)
83 struct alternate_object_database *alt;
84 char hex[GIT_MAX_HEXSZ];
85 static struct alternate_object_database *fakeent;
87 if (!fakeent) {
89 * Create a "fake" alternate object database that
90 * points to our own object database, to make it
91 * easier to get a temporary working space in
92 * alt->name/alt->base while iterating over the
93 * object databases including our own.
95 fakeent = alloc_alt_odb(get_object_directory());
97 fakeent->next = alt_odb_list;
99 xsnprintf(hex, sizeof(hex), "%.2s", ds->hex_pfx);
100 for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
101 struct strbuf *buf = alt_scratch_buf(alt);
102 struct dirent *de;
103 DIR *dir;
105 strbuf_addf(buf, "%.2s/", ds->hex_pfx);
106 dir = opendir(buf->buf);
107 if (!dir)
108 continue;
110 while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
111 struct object_id oid;
113 if (strlen(de->d_name) != GIT_SHA1_HEXSZ - 2)
114 continue;
115 if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
116 continue;
117 memcpy(hex + 2, de->d_name, GIT_SHA1_HEXSZ - 2);
118 if (!get_oid_hex(hex, &oid))
119 update_candidates(ds, &oid);
121 closedir(dir);
125 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
127 do {
128 if (*a != *b)
129 return 0;
130 a++;
131 b++;
132 len -= 2;
133 } while (len > 1);
134 if (len)
135 if ((*a ^ *b) & 0xf0)
136 return 0;
137 return 1;
140 static void unique_in_pack(struct packed_git *p,
141 struct disambiguate_state *ds)
143 uint32_t num, last, i, first = 0;
144 const struct object_id *current = NULL;
146 open_pack_index(p);
147 num = p->num_objects;
148 last = num;
149 while (first < last) {
150 uint32_t mid = (first + last) / 2;
151 const unsigned char *current;
152 int cmp;
154 current = nth_packed_object_sha1(p, mid);
155 cmp = hashcmp(ds->bin_pfx.hash, current);
156 if (!cmp) {
157 first = mid;
158 break;
160 if (cmp > 0) {
161 first = mid+1;
162 continue;
164 last = mid;
168 * At this point, "first" is the location of the lowest object
169 * with an object name that could match "bin_pfx". See if we have
170 * 0, 1 or more objects that actually match(es).
172 for (i = first; i < num && !ds->ambiguous; i++) {
173 struct object_id oid;
174 current = nth_packed_object_oid(&oid, p, i);
175 if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
176 break;
177 update_candidates(ds, current);
181 static void find_short_packed_object(struct disambiguate_state *ds)
183 struct packed_git *p;
185 prepare_packed_git();
186 for (p = packed_git; p && !ds->ambiguous; p = p->next)
187 unique_in_pack(p, ds);
190 #define SHORT_NAME_NOT_FOUND (-1)
191 #define SHORT_NAME_AMBIGUOUS (-2)
193 static int finish_object_disambiguation(struct disambiguate_state *ds,
194 unsigned char *sha1)
196 if (ds->ambiguous)
197 return SHORT_NAME_AMBIGUOUS;
199 if (!ds->candidate_exists)
200 return SHORT_NAME_NOT_FOUND;
202 if (!ds->candidate_checked)
204 * If this is the only candidate, there is no point
205 * calling the disambiguation hint callback.
207 * On the other hand, if the current candidate
208 * replaced an earlier candidate that did _not_ pass
209 * the disambiguation hint callback, then we do have
210 * more than one objects that match the short name
211 * given, so we should make sure this one matches;
212 * otherwise, if we discovered this one and the one
213 * that we previously discarded in the reverse order,
214 * we would end up showing different results in the
215 * same repository!
217 ds->candidate_ok = (!ds->disambiguate_fn_used ||
218 ds->fn(&ds->candidate, ds->cb_data));
220 if (!ds->candidate_ok)
221 return SHORT_NAME_AMBIGUOUS;
223 hashcpy(sha1, ds->candidate.hash);
224 return 0;
227 static int disambiguate_commit_only(const struct object_id *oid, void *cb_data_unused)
229 int kind = sha1_object_info(oid->hash, NULL);
230 return kind == OBJ_COMMIT;
233 static int disambiguate_committish_only(const struct object_id *oid, void *cb_data_unused)
235 struct object *obj;
236 int kind;
238 kind = sha1_object_info(oid->hash, NULL);
239 if (kind == OBJ_COMMIT)
240 return 1;
241 if (kind != OBJ_TAG)
242 return 0;
244 /* We need to do this the hard way... */
245 obj = deref_tag(parse_object(oid), NULL, 0);
246 if (obj && obj->type == OBJ_COMMIT)
247 return 1;
248 return 0;
251 static int disambiguate_tree_only(const struct object_id *oid, void *cb_data_unused)
253 int kind = sha1_object_info(oid->hash, NULL);
254 return kind == OBJ_TREE;
257 static int disambiguate_treeish_only(const struct object_id *oid, void *cb_data_unused)
259 struct object *obj;
260 int kind;
262 kind = sha1_object_info(oid->hash, NULL);
263 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
264 return 1;
265 if (kind != OBJ_TAG)
266 return 0;
268 /* We need to do this the hard way... */
269 obj = deref_tag(parse_object(oid), NULL, 0);
270 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
271 return 1;
272 return 0;
275 static int disambiguate_blob_only(const struct object_id *oid, void *cb_data_unused)
277 int kind = sha1_object_info(oid->hash, NULL);
278 return kind == OBJ_BLOB;
281 static disambiguate_hint_fn default_disambiguate_hint;
283 int set_disambiguate_hint_config(const char *var, const char *value)
285 static const struct {
286 const char *name;
287 disambiguate_hint_fn fn;
288 } hints[] = {
289 { "none", NULL },
290 { "commit", disambiguate_commit_only },
291 { "committish", disambiguate_committish_only },
292 { "tree", disambiguate_tree_only },
293 { "treeish", disambiguate_treeish_only },
294 { "blob", disambiguate_blob_only }
296 int i;
298 if (!value)
299 return config_error_nonbool(var);
301 for (i = 0; i < ARRAY_SIZE(hints); i++) {
302 if (!strcasecmp(value, hints[i].name)) {
303 default_disambiguate_hint = hints[i].fn;
304 return 0;
308 return error("unknown hint type for '%s': %s", var, value);
311 static int init_object_disambiguation(const char *name, int len,
312 struct disambiguate_state *ds)
314 int i;
316 if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ)
317 return -1;
319 memset(ds, 0, sizeof(*ds));
321 for (i = 0; i < len ;i++) {
322 unsigned char c = name[i];
323 unsigned char val;
324 if (c >= '0' && c <= '9')
325 val = c - '0';
326 else if (c >= 'a' && c <= 'f')
327 val = c - 'a' + 10;
328 else if (c >= 'A' && c <='F') {
329 val = c - 'A' + 10;
330 c -= 'A' - 'a';
332 else
333 return -1;
334 ds->hex_pfx[i] = c;
335 if (!(i & 1))
336 val <<= 4;
337 ds->bin_pfx.hash[i >> 1] |= val;
340 ds->len = len;
341 ds->hex_pfx[len] = '\0';
342 prepare_alt_odb();
343 return 0;
346 static int show_ambiguous_object(const struct object_id *oid, void *data)
348 const struct disambiguate_state *ds = data;
349 struct strbuf desc = STRBUF_INIT;
350 int type;
353 if (ds->fn && !ds->fn(oid, ds->cb_data))
354 return 0;
356 type = sha1_object_info(oid->hash, NULL);
357 if (type == OBJ_COMMIT) {
358 struct commit *commit = lookup_commit(oid);
359 if (commit) {
360 struct pretty_print_context pp = {0};
361 pp.date_mode.type = DATE_SHORT;
362 format_commit_message(commit, " %ad - %s", &desc, &pp);
364 } else if (type == OBJ_TAG) {
365 struct tag *tag = lookup_tag(oid);
366 if (!parse_tag(tag) && tag->tag)
367 strbuf_addf(&desc, " %s", tag->tag);
370 advise(" %s %s%s",
371 find_unique_abbrev(oid->hash, DEFAULT_ABBREV),
372 typename(type) ? typename(type) : "unknown type",
373 desc.buf);
375 strbuf_release(&desc);
376 return 0;
379 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
380 unsigned flags)
382 int status;
383 struct disambiguate_state ds;
384 int quietly = !!(flags & GET_SHA1_QUIETLY);
386 if (init_object_disambiguation(name, len, &ds) < 0)
387 return -1;
389 if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
390 die("BUG: multiple get_short_sha1 disambiguator flags");
392 if (flags & GET_SHA1_COMMIT)
393 ds.fn = disambiguate_commit_only;
394 else if (flags & GET_SHA1_COMMITTISH)
395 ds.fn = disambiguate_committish_only;
396 else if (flags & GET_SHA1_TREE)
397 ds.fn = disambiguate_tree_only;
398 else if (flags & GET_SHA1_TREEISH)
399 ds.fn = disambiguate_treeish_only;
400 else if (flags & GET_SHA1_BLOB)
401 ds.fn = disambiguate_blob_only;
402 else
403 ds.fn = default_disambiguate_hint;
405 find_short_object_filename(&ds);
406 find_short_packed_object(&ds);
407 status = finish_object_disambiguation(&ds, sha1);
409 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
410 error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
413 * We may still have ambiguity if we simply saw a series of
414 * candidates that did not satisfy our hint function. In
415 * that case, we still want to show them, so disable the hint
416 * function entirely.
418 if (!ds.ambiguous)
419 ds.fn = NULL;
421 advise(_("The candidates are:"));
422 for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds);
425 return status;
428 static int collect_ambiguous(const struct object_id *oid, void *data)
430 oid_array_append(data, oid);
431 return 0;
434 int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
436 struct oid_array collect = OID_ARRAY_INIT;
437 struct disambiguate_state ds;
438 int ret;
440 if (init_object_disambiguation(prefix, strlen(prefix), &ds) < 0)
441 return -1;
443 ds.always_call_fn = 1;
444 ds.fn = collect_ambiguous;
445 ds.cb_data = &collect;
446 find_short_object_filename(&ds);
447 find_short_packed_object(&ds);
449 ret = oid_array_for_each_unique(&collect, fn, cb_data);
450 oid_array_clear(&collect);
451 return ret;
455 * Return the slot of the most-significant bit set in "val". There are various
456 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
457 * probably not a big deal here.
459 static unsigned msb(unsigned long val)
461 unsigned r = 0;
462 while (val >>= 1)
463 r++;
464 return r;
467 int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
469 int status, exists;
471 if (len < 0) {
472 unsigned long count = approximate_object_count();
474 * Add one because the MSB only tells us the highest bit set,
475 * not including the value of all the _other_ bits (so "15"
476 * is only one off of 2^4, but the MSB is the 3rd bit.
478 len = msb(count) + 1;
480 * We now know we have on the order of 2^len objects, which
481 * expects a collision at 2^(len/2). But we also care about hex
482 * chars, not bits, and there are 4 bits per hex. So all
483 * together we need to divide by 2; but we also want to round
484 * odd numbers up, hence adding one before dividing.
486 len = (len + 1) / 2;
488 * For very small repos, we stick with our regular fallback.
490 if (len < FALLBACK_DEFAULT_ABBREV)
491 len = FALLBACK_DEFAULT_ABBREV;
494 sha1_to_hex_r(hex, sha1);
495 if (len == 40 || !len)
496 return 40;
497 exists = has_sha1_file(sha1);
498 while (len < 40) {
499 unsigned char sha1_ret[20];
500 status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
501 if (exists
502 ? !status
503 : status == SHORT_NAME_NOT_FOUND) {
504 hex[len] = 0;
505 return len;
507 len++;
509 return len;
512 const char *find_unique_abbrev(const unsigned char *sha1, int len)
514 static int bufno;
515 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
516 char *hex = hexbuffer[bufno];
517 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
518 find_unique_abbrev_r(hex, sha1, len);
519 return hex;
522 static int ambiguous_path(const char *path, int len)
524 int slash = 1;
525 int cnt;
527 for (cnt = 0; cnt < len; cnt++) {
528 switch (*path++) {
529 case '\0':
530 break;
531 case '/':
532 if (slash)
533 break;
534 slash = 1;
535 continue;
536 case '.':
537 continue;
538 default:
539 slash = 0;
540 continue;
542 break;
544 return slash;
547 static inline int at_mark(const char *string, int len,
548 const char **suffix, int nr)
550 int i;
552 for (i = 0; i < nr; i++) {
553 int suffix_len = strlen(suffix[i]);
554 if (suffix_len <= len
555 && !strncasecmp(string, suffix[i], suffix_len))
556 return suffix_len;
558 return 0;
561 static inline int upstream_mark(const char *string, int len)
563 const char *suffix[] = { "@{upstream}", "@{u}" };
564 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
567 static inline int push_mark(const char *string, int len)
569 const char *suffix[] = { "@{push}" };
570 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
573 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
574 static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
576 static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
577 unsigned int flags)
579 static const char *warn_msg = "refname '%.*s' is ambiguous.";
580 static const char *object_name_msg = N_(
581 "Git normally never creates a ref that ends with 40 hex characters\n"
582 "because it will be ignored when you just specify 40-hex. These refs\n"
583 "may be created by mistake. For example,\n"
584 "\n"
585 " git checkout -b $br $(git rev-parse ...)\n"
586 "\n"
587 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
588 "examine these refs and maybe delete them. Turn this message off by\n"
589 "running \"git config advice.objectNameWarning false\"");
590 unsigned char tmp_sha1[20];
591 char *real_ref = NULL;
592 int refs_found = 0;
593 int at, reflog_len, nth_prior = 0;
595 if (len == 40 && !get_sha1_hex(str, sha1)) {
596 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
597 refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
598 if (refs_found > 0) {
599 warning(warn_msg, len, str);
600 if (advice_object_name_warning)
601 fprintf(stderr, "%s\n", _(object_name_msg));
603 free(real_ref);
605 return 0;
608 /* basic@{time or number or -number} format to query ref-log */
609 reflog_len = at = 0;
610 if (len && str[len-1] == '}') {
611 for (at = len-4; at >= 0; at--) {
612 if (str[at] == '@' && str[at+1] == '{') {
613 if (str[at+2] == '-') {
614 if (at != 0)
615 /* @{-N} not at start */
616 return -1;
617 nth_prior = 1;
618 continue;
620 if (!upstream_mark(str + at, len - at) &&
621 !push_mark(str + at, len - at)) {
622 reflog_len = (len-1) - (at+2);
623 len = at;
625 break;
630 /* Accept only unambiguous ref paths. */
631 if (len && ambiguous_path(str, len))
632 return -1;
634 if (nth_prior) {
635 struct strbuf buf = STRBUF_INIT;
636 int detached;
638 if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
639 detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
640 strbuf_release(&buf);
641 if (detached)
642 return 0;
646 if (!len && reflog_len)
647 /* allow "@{...}" to mean the current branch reflog */
648 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
649 else if (reflog_len)
650 refs_found = dwim_log(str, len, sha1, &real_ref);
651 else
652 refs_found = dwim_ref(str, len, sha1, &real_ref);
654 if (!refs_found)
655 return -1;
657 if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
658 (refs_found > 1 ||
659 !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
660 warning(warn_msg, len, str);
662 if (reflog_len) {
663 int nth, i;
664 timestamp_t at_time;
665 timestamp_t co_time;
666 int co_tz, co_cnt;
668 /* Is it asking for N-th entry, or approxidate? */
669 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
670 char ch = str[at+2+i];
671 if ('0' <= ch && ch <= '9')
672 nth = nth * 10 + ch - '0';
673 else
674 nth = -1;
676 if (100000000 <= nth) {
677 at_time = nth;
678 nth = -1;
679 } else if (0 <= nth)
680 at_time = 0;
681 else {
682 int errors = 0;
683 char *tmp = xstrndup(str + at + 2, reflog_len);
684 at_time = approxidate_careful(tmp, &errors);
685 free(tmp);
686 if (errors) {
687 free(real_ref);
688 return -1;
691 if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
692 &co_time, &co_tz, &co_cnt)) {
693 if (!len) {
694 if (starts_with(real_ref, "refs/heads/")) {
695 str = real_ref + 11;
696 len = strlen(real_ref + 11);
697 } else {
698 /* detached HEAD */
699 str = "HEAD";
700 len = 4;
703 if (at_time) {
704 if (!(flags & GET_SHA1_QUIETLY)) {
705 warning("Log for '%.*s' only goes "
706 "back to %s.", len, str,
707 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
709 } else {
710 if (flags & GET_SHA1_QUIETLY) {
711 exit(128);
713 die("Log for '%.*s' only has %d entries.",
714 len, str, co_cnt);
719 free(real_ref);
720 return 0;
723 static int get_parent(const char *name, int len,
724 unsigned char *result, int idx)
726 struct object_id oid;
727 int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
728 struct commit *commit;
729 struct commit_list *p;
731 if (ret)
732 return ret;
733 commit = lookup_commit_reference(&oid);
734 if (parse_commit(commit))
735 return -1;
736 if (!idx) {
737 hashcpy(result, commit->object.oid.hash);
738 return 0;
740 p = commit->parents;
741 while (p) {
742 if (!--idx) {
743 hashcpy(result, p->item->object.oid.hash);
744 return 0;
746 p = p->next;
748 return -1;
751 static int get_nth_ancestor(const char *name, int len,
752 unsigned char *result, int generation)
754 struct object_id oid;
755 struct commit *commit;
756 int ret;
758 ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH);
759 if (ret)
760 return ret;
761 commit = lookup_commit_reference(&oid);
762 if (!commit)
763 return -1;
765 while (generation--) {
766 if (parse_commit(commit) || !commit->parents)
767 return -1;
768 commit = commit->parents->item;
770 hashcpy(result, commit->object.oid.hash);
771 return 0;
774 struct object *peel_to_type(const char *name, int namelen,
775 struct object *o, enum object_type expected_type)
777 if (name && !namelen)
778 namelen = strlen(name);
779 while (1) {
780 if (!o || (!o->parsed && !parse_object(&o->oid)))
781 return NULL;
782 if (expected_type == OBJ_ANY || o->type == expected_type)
783 return o;
784 if (o->type == OBJ_TAG)
785 o = ((struct tag*) o)->tagged;
786 else if (o->type == OBJ_COMMIT)
787 o = &(((struct commit *) o)->tree->object);
788 else {
789 if (name)
790 error("%.*s: expected %s type, but the object "
791 "dereferences to %s type",
792 namelen, name, typename(expected_type),
793 typename(o->type));
794 return NULL;
799 static int peel_onion(const char *name, int len, unsigned char *sha1,
800 unsigned lookup_flags)
802 struct object_id outer;
803 const char *sp;
804 unsigned int expected_type = 0;
805 struct object *o;
808 * "ref^{type}" dereferences ref repeatedly until you cannot
809 * dereference anymore, or you get an object of given type,
810 * whichever comes first. "ref^{}" means just dereference
811 * tags until you get a non-tag. "ref^0" is a shorthand for
812 * "ref^{commit}". "commit^{tree}" could be used to find the
813 * top-level tree of the given commit.
815 if (len < 4 || name[len-1] != '}')
816 return -1;
818 for (sp = name + len - 1; name <= sp; sp--) {
819 int ch = *sp;
820 if (ch == '{' && name < sp && sp[-1] == '^')
821 break;
823 if (sp <= name)
824 return -1;
826 sp++; /* beginning of type name, or closing brace for empty */
827 if (starts_with(sp, "commit}"))
828 expected_type = OBJ_COMMIT;
829 else if (starts_with(sp, "tag}"))
830 expected_type = OBJ_TAG;
831 else if (starts_with(sp, "tree}"))
832 expected_type = OBJ_TREE;
833 else if (starts_with(sp, "blob}"))
834 expected_type = OBJ_BLOB;
835 else if (starts_with(sp, "object}"))
836 expected_type = OBJ_ANY;
837 else if (sp[0] == '}')
838 expected_type = OBJ_NONE;
839 else if (sp[0] == '/')
840 expected_type = OBJ_COMMIT;
841 else
842 return -1;
844 lookup_flags &= ~GET_SHA1_DISAMBIGUATORS;
845 if (expected_type == OBJ_COMMIT)
846 lookup_flags |= GET_SHA1_COMMITTISH;
847 else if (expected_type == OBJ_TREE)
848 lookup_flags |= GET_SHA1_TREEISH;
850 if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags))
851 return -1;
853 o = parse_object(&outer);
854 if (!o)
855 return -1;
856 if (!expected_type) {
857 o = deref_tag(o, name, sp - name - 2);
858 if (!o || (!o->parsed && !parse_object(&o->oid)))
859 return -1;
860 hashcpy(sha1, o->oid.hash);
861 return 0;
865 * At this point, the syntax look correct, so
866 * if we do not get the needed object, we should
867 * barf.
869 o = peel_to_type(name, len, o, expected_type);
870 if (!o)
871 return -1;
873 hashcpy(sha1, o->oid.hash);
874 if (sp[0] == '/') {
875 /* "$commit^{/foo}" */
876 char *prefix;
877 int ret;
878 struct commit_list *list = NULL;
881 * $commit^{/}. Some regex implementation may reject.
882 * We don't need regex anyway. '' pattern always matches.
884 if (sp[1] == '}')
885 return 0;
887 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
888 commit_list_insert((struct commit *)o, &list);
889 ret = get_sha1_oneline(prefix, sha1, list);
890 free(prefix);
891 return ret;
893 return 0;
896 static int get_describe_name(const char *name, int len, unsigned char *sha1)
898 const char *cp;
899 unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
901 for (cp = name + len - 1; name + 2 <= cp; cp--) {
902 char ch = *cp;
903 if (!isxdigit(ch)) {
904 /* We must be looking at g in "SOMETHING-g"
905 * for it to be describe output.
907 if (ch == 'g' && cp[-1] == '-') {
908 cp++;
909 len -= cp - name;
910 return get_short_sha1(cp, len, sha1, flags);
914 return -1;
917 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
919 int ret, has_suffix;
920 const char *cp;
923 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
925 has_suffix = 0;
926 for (cp = name + len - 1; name <= cp; cp--) {
927 int ch = *cp;
928 if ('0' <= ch && ch <= '9')
929 continue;
930 if (ch == '~' || ch == '^')
931 has_suffix = ch;
932 break;
935 if (has_suffix) {
936 int num = 0;
937 int len1 = cp - name;
938 cp++;
939 while (cp < name + len)
940 num = num * 10 + *cp++ - '0';
941 if (!num && len1 == len - 1)
942 num = 1;
943 if (has_suffix == '^')
944 return get_parent(name, len1, sha1, num);
945 /* else if (has_suffix == '~') -- goes without saying */
946 return get_nth_ancestor(name, len1, sha1, num);
949 ret = peel_onion(name, len, sha1, lookup_flags);
950 if (!ret)
951 return 0;
953 ret = get_sha1_basic(name, len, sha1, lookup_flags);
954 if (!ret)
955 return 0;
957 /* It could be describe output that is "SOMETHING-gXXXX" */
958 ret = get_describe_name(name, len, sha1);
959 if (!ret)
960 return 0;
962 return get_short_sha1(name, len, sha1, lookup_flags);
966 * This interprets names like ':/Initial revision of "git"' by searching
967 * through history and returning the first commit whose message starts
968 * the given regular expression.
970 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
972 * For a literal '!' character at the beginning of a pattern, you have to repeat
973 * that, like: ':/!!foo'
975 * For future extension, all other sequences beginning with ':/!' are reserved.
978 /* Remember to update object flag allocation in object.h */
979 #define ONELINE_SEEN (1u<<20)
981 static int handle_one_ref(const char *path, const struct object_id *oid,
982 int flag, void *cb_data)
984 struct commit_list **list = cb_data;
985 struct object *object = parse_object(oid);
986 if (!object)
987 return 0;
988 if (object->type == OBJ_TAG) {
989 object = deref_tag(object, path, strlen(path));
990 if (!object)
991 return 0;
993 if (object->type != OBJ_COMMIT)
994 return 0;
995 commit_list_insert((struct commit *)object, list);
996 return 0;
999 static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
1000 struct commit_list *list)
1002 struct commit_list *backup = NULL, *l;
1003 int found = 0;
1004 int negative = 0;
1005 regex_t regex;
1007 if (prefix[0] == '!') {
1008 prefix++;
1010 if (prefix[0] == '-') {
1011 prefix++;
1012 negative = 1;
1013 } else if (prefix[0] != '!') {
1014 return -1;
1018 if (regcomp(&regex, prefix, REG_EXTENDED))
1019 return -1;
1021 for (l = list; l; l = l->next) {
1022 l->item->object.flags |= ONELINE_SEEN;
1023 commit_list_insert(l->item, &backup);
1025 while (list) {
1026 const char *p, *buf;
1027 struct commit *commit;
1028 int matches;
1030 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1031 if (!parse_object(&commit->object.oid))
1032 continue;
1033 buf = get_commit_buffer(commit, NULL);
1034 p = strstr(buf, "\n\n");
1035 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1036 unuse_commit_buffer(commit, buf);
1038 if (matches) {
1039 hashcpy(sha1, commit->object.oid.hash);
1040 found = 1;
1041 break;
1044 regfree(&regex);
1045 free_commit_list(list);
1046 for (l = backup; l; l = l->next)
1047 clear_commit_marks(l->item, ONELINE_SEEN);
1048 free_commit_list(backup);
1049 return found ? 0 : -1;
1052 struct grab_nth_branch_switch_cbdata {
1053 int remaining;
1054 struct strbuf buf;
1057 static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
1058 const char *email, timestamp_t timestamp, int tz,
1059 const char *message, void *cb_data)
1061 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1062 const char *match = NULL, *target = NULL;
1063 size_t len;
1065 if (skip_prefix(message, "checkout: moving from ", &match))
1066 target = strstr(match, " to ");
1068 if (!match || !target)
1069 return 0;
1070 if (--(cb->remaining) == 0) {
1071 len = target - match;
1072 strbuf_reset(&cb->buf);
1073 strbuf_add(&cb->buf, match, len);
1074 return 1; /* we are done */
1076 return 0;
1080 * Parse @{-N} syntax, return the number of characters parsed
1081 * if successful; otherwise signal an error with negative value.
1083 static int interpret_nth_prior_checkout(const char *name, int namelen,
1084 struct strbuf *buf)
1086 long nth;
1087 int retval;
1088 struct grab_nth_branch_switch_cbdata cb;
1089 const char *brace;
1090 char *num_end;
1092 if (namelen < 4)
1093 return -1;
1094 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1095 return -1;
1096 brace = memchr(name, '}', namelen);
1097 if (!brace)
1098 return -1;
1099 nth = strtol(name + 3, &num_end, 10);
1100 if (num_end != brace)
1101 return -1;
1102 if (nth <= 0)
1103 return -1;
1104 cb.remaining = nth;
1105 strbuf_init(&cb.buf, 20);
1107 retval = 0;
1108 if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) {
1109 strbuf_reset(buf);
1110 strbuf_addbuf(buf, &cb.buf);
1111 retval = brace - name + 1;
1114 strbuf_release(&cb.buf);
1115 return retval;
1118 int get_oid_mb(const char *name, struct object_id *oid)
1120 struct commit *one, *two;
1121 struct commit_list *mbs;
1122 struct object_id oid_tmp;
1123 const char *dots;
1124 int st;
1126 dots = strstr(name, "...");
1127 if (!dots)
1128 return get_oid(name, oid);
1129 if (dots == name)
1130 st = get_oid("HEAD", &oid_tmp);
1131 else {
1132 struct strbuf sb;
1133 strbuf_init(&sb, dots - name);
1134 strbuf_add(&sb, name, dots - name);
1135 st = get_sha1_committish(sb.buf, oid_tmp.hash);
1136 strbuf_release(&sb);
1138 if (st)
1139 return st;
1140 one = lookup_commit_reference_gently(&oid_tmp, 0);
1141 if (!one)
1142 return -1;
1144 if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
1145 return -1;
1146 two = lookup_commit_reference_gently(&oid_tmp, 0);
1147 if (!two)
1148 return -1;
1149 mbs = get_merge_bases(one, two);
1150 if (!mbs || mbs->next)
1151 st = -1;
1152 else {
1153 st = 0;
1154 oidcpy(oid, &mbs->item->object.oid);
1156 free_commit_list(mbs);
1157 return st;
1160 /* parse @something syntax, when 'something' is not {.*} */
1161 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1163 const char *next;
1165 if (len || name[1] == '{')
1166 return -1;
1168 /* make sure it's a single @, or @@{.*}, not @foo */
1169 next = memchr(name + len + 1, '@', namelen - len - 1);
1170 if (next && next[1] != '{')
1171 return -1;
1172 if (!next)
1173 next = name + namelen;
1174 if (next != name + 1)
1175 return -1;
1177 strbuf_reset(buf);
1178 strbuf_add(buf, "HEAD", 4);
1179 return 1;
1182 static int reinterpret(const char *name, int namelen, int len,
1183 struct strbuf *buf, unsigned allowed)
1185 /* we have extra data, which might need further processing */
1186 struct strbuf tmp = STRBUF_INIT;
1187 int used = buf->len;
1188 int ret;
1190 strbuf_add(buf, name + len, namelen - len);
1191 ret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed);
1192 /* that data was not interpreted, remove our cruft */
1193 if (ret < 0) {
1194 strbuf_setlen(buf, used);
1195 return len;
1197 strbuf_reset(buf);
1198 strbuf_addbuf(buf, &tmp);
1199 strbuf_release(&tmp);
1200 /* tweak for size of {-N} versus expanded ref name */
1201 return ret - used + len;
1204 static void set_shortened_ref(struct strbuf *buf, const char *ref)
1206 char *s = shorten_unambiguous_ref(ref, 0);
1207 strbuf_reset(buf);
1208 strbuf_addstr(buf, s);
1209 free(s);
1212 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1214 if (!allowed)
1215 return 1;
1217 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1218 starts_with(refname, "refs/heads/"))
1219 return 1;
1220 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1221 starts_with(refname, "refs/remotes/"))
1222 return 1;
1224 return 0;
1227 static int interpret_branch_mark(const char *name, int namelen,
1228 int at, struct strbuf *buf,
1229 int (*get_mark)(const char *, int),
1230 const char *(*get_data)(struct branch *,
1231 struct strbuf *),
1232 unsigned allowed)
1234 int len;
1235 struct branch *branch;
1236 struct strbuf err = STRBUF_INIT;
1237 const char *value;
1239 len = get_mark(name + at, namelen - at);
1240 if (!len)
1241 return -1;
1243 if (memchr(name, ':', at))
1244 return -1;
1246 if (at) {
1247 char *name_str = xmemdupz(name, at);
1248 branch = branch_get(name_str);
1249 free(name_str);
1250 } else
1251 branch = branch_get(NULL);
1253 value = get_data(branch, &err);
1254 if (!value)
1255 die("%s", err.buf);
1257 if (!branch_interpret_allowed(value, allowed))
1258 return -1;
1260 set_shortened_ref(buf, value);
1261 return len + at;
1264 int interpret_branch_name(const char *name, int namelen, struct strbuf *buf,
1265 unsigned allowed)
1267 char *at;
1268 const char *start;
1269 int len;
1271 if (!namelen)
1272 namelen = strlen(name);
1274 if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
1275 len = interpret_nth_prior_checkout(name, namelen, buf);
1276 if (!len) {
1277 return len; /* syntax Ok, not enough switches */
1278 } else if (len > 0) {
1279 if (len == namelen)
1280 return len; /* consumed all */
1281 else
1282 return reinterpret(name, namelen, len, buf, allowed);
1286 for (start = name;
1287 (at = memchr(start, '@', namelen - (start - name)));
1288 start = at + 1) {
1290 if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
1291 len = interpret_empty_at(name, namelen, at - name, buf);
1292 if (len > 0)
1293 return reinterpret(name, namelen, len, buf,
1294 allowed);
1297 len = interpret_branch_mark(name, namelen, at - name, buf,
1298 upstream_mark, branch_get_upstream,
1299 allowed);
1300 if (len > 0)
1301 return len;
1303 len = interpret_branch_mark(name, namelen, at - name, buf,
1304 push_mark, branch_get_push,
1305 allowed);
1306 if (len > 0)
1307 return len;
1310 return -1;
1313 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1315 int len = strlen(name);
1316 int used = interpret_branch_name(name, len, sb, allowed);
1318 if (used < 0)
1319 used = 0;
1320 strbuf_add(sb, name + used, len - used);
1323 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1325 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1326 if (name[0] == '-')
1327 return -1;
1328 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1329 return check_refname_format(sb->buf, 0);
1333 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
1334 * notably "xyz^" for "parent of xyz"
1336 int get_sha1(const char *name, unsigned char *sha1)
1338 struct object_context unused;
1339 return get_sha1_with_context(name, 0, sha1, &unused);
1343 * This is like "get_sha1()", but for struct object_id.
1345 int get_oid(const char *name, struct object_id *oid)
1347 return get_sha1(name, oid->hash);
1352 * Many callers know that the user meant to name a commit-ish by
1353 * syntactical positions where the object name appears. Calling this
1354 * function allows the machinery to disambiguate shorter-than-unique
1355 * abbreviated object names between commit-ish and others.
1357 * Note that this does NOT error out when the named object is not a
1358 * commit-ish. It is merely to give a hint to the disambiguation
1359 * machinery.
1361 int get_sha1_committish(const char *name, unsigned char *sha1)
1363 struct object_context unused;
1364 return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1365 sha1, &unused);
1368 int get_sha1_treeish(const char *name, unsigned char *sha1)
1370 struct object_context unused;
1371 return get_sha1_with_context(name, GET_SHA1_TREEISH,
1372 sha1, &unused);
1375 int get_sha1_commit(const char *name, unsigned char *sha1)
1377 struct object_context unused;
1378 return get_sha1_with_context(name, GET_SHA1_COMMIT,
1379 sha1, &unused);
1382 int get_sha1_tree(const char *name, unsigned char *sha1)
1384 struct object_context unused;
1385 return get_sha1_with_context(name, GET_SHA1_TREE,
1386 sha1, &unused);
1389 int get_sha1_blob(const char *name, unsigned char *sha1)
1391 struct object_context unused;
1392 return get_sha1_with_context(name, GET_SHA1_BLOB,
1393 sha1, &unused);
1396 /* Must be called only when object_name:filename doesn't exist. */
1397 static void diagnose_invalid_sha1_path(const char *prefix,
1398 const char *filename,
1399 const unsigned char *tree_sha1,
1400 const char *object_name,
1401 int object_name_len)
1403 unsigned char sha1[20];
1404 unsigned mode;
1406 if (!prefix)
1407 prefix = "";
1409 if (file_exists(filename))
1410 die("Path '%s' exists on disk, but not in '%.*s'.",
1411 filename, object_name_len, object_name);
1412 if (is_missing_file_error(errno)) {
1413 char *fullname = xstrfmt("%s%s", prefix, filename);
1415 if (!get_tree_entry(tree_sha1, fullname,
1416 sha1, &mode)) {
1417 die("Path '%s' exists, but not '%s'.\n"
1418 "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1419 fullname,
1420 filename,
1421 object_name_len, object_name,
1422 fullname,
1423 object_name_len, object_name,
1424 filename);
1426 die("Path '%s' does not exist in '%.*s'",
1427 filename, object_name_len, object_name);
1431 /* Must be called only when :stage:filename doesn't exist. */
1432 static void diagnose_invalid_index_path(int stage,
1433 const char *prefix,
1434 const char *filename)
1436 const struct cache_entry *ce;
1437 int pos;
1438 unsigned namelen = strlen(filename);
1439 struct strbuf fullname = STRBUF_INIT;
1441 if (!prefix)
1442 prefix = "";
1444 /* Wrong stage number? */
1445 pos = cache_name_pos(filename, namelen);
1446 if (pos < 0)
1447 pos = -pos - 1;
1448 if (pos < active_nr) {
1449 ce = active_cache[pos];
1450 if (ce_namelen(ce) == namelen &&
1451 !memcmp(ce->name, filename, namelen))
1452 die("Path '%s' is in the index, but not at stage %d.\n"
1453 "Did you mean ':%d:%s'?",
1454 filename, stage,
1455 ce_stage(ce), filename);
1458 /* Confusion between relative and absolute filenames? */
1459 strbuf_addstr(&fullname, prefix);
1460 strbuf_addstr(&fullname, filename);
1461 pos = cache_name_pos(fullname.buf, fullname.len);
1462 if (pos < 0)
1463 pos = -pos - 1;
1464 if (pos < active_nr) {
1465 ce = active_cache[pos];
1466 if (ce_namelen(ce) == fullname.len &&
1467 !memcmp(ce->name, fullname.buf, fullname.len))
1468 die("Path '%s' is in the index, but not '%s'.\n"
1469 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1470 fullname.buf, filename,
1471 ce_stage(ce), fullname.buf,
1472 ce_stage(ce), filename);
1475 if (file_exists(filename))
1476 die("Path '%s' exists on disk, but not in the index.", filename);
1477 if (is_missing_file_error(errno))
1478 die("Path '%s' does not exist (neither on disk nor in the index).",
1479 filename);
1481 strbuf_release(&fullname);
1485 static char *resolve_relative_path(const char *rel)
1487 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1488 return NULL;
1490 if (!is_inside_work_tree())
1491 die("relative path syntax can't be used outside working tree.");
1493 /* die() inside prefix_path() if resolved path is outside worktree */
1494 return prefix_path(startup_info->prefix,
1495 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1496 rel);
1499 static int get_sha1_with_context_1(const char *name,
1500 unsigned flags,
1501 const char *prefix,
1502 unsigned char *sha1,
1503 struct object_context *oc)
1505 int ret, bracket_depth;
1506 int namelen = strlen(name);
1507 const char *cp;
1508 int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
1510 if (only_to_die)
1511 flags |= GET_SHA1_QUIETLY;
1513 memset(oc, 0, sizeof(*oc));
1514 oc->mode = S_IFINVALID;
1515 strbuf_init(&oc->symlink_path, 0);
1516 ret = get_sha1_1(name, namelen, sha1, flags);
1517 if (!ret)
1518 return ret;
1520 * sha1:path --> object name of path in ent sha1
1521 * :path -> object name of absolute path in index
1522 * :./path -> object name of path relative to cwd in index
1523 * :[0-3]:path -> object name of path in index at stage
1524 * :/foo -> recent commit matching foo
1526 if (name[0] == ':') {
1527 int stage = 0;
1528 const struct cache_entry *ce;
1529 char *new_path = NULL;
1530 int pos;
1531 if (!only_to_die && namelen > 2 && name[1] == '/') {
1532 struct commit_list *list = NULL;
1534 for_each_ref(handle_one_ref, &list);
1535 commit_list_sort_by_date(&list);
1536 return get_sha1_oneline(name + 2, sha1, list);
1538 if (namelen < 3 ||
1539 name[2] != ':' ||
1540 name[1] < '0' || '3' < name[1])
1541 cp = name + 1;
1542 else {
1543 stage = name[1] - '0';
1544 cp = name + 3;
1546 new_path = resolve_relative_path(cp);
1547 if (!new_path) {
1548 namelen = namelen - (cp - name);
1549 } else {
1550 cp = new_path;
1551 namelen = strlen(cp);
1554 if (flags & GET_SHA1_RECORD_PATH)
1555 oc->path = xstrdup(cp);
1557 if (!active_cache)
1558 read_cache();
1559 pos = cache_name_pos(cp, namelen);
1560 if (pos < 0)
1561 pos = -pos - 1;
1562 while (pos < active_nr) {
1563 ce = active_cache[pos];
1564 if (ce_namelen(ce) != namelen ||
1565 memcmp(ce->name, cp, namelen))
1566 break;
1567 if (ce_stage(ce) == stage) {
1568 hashcpy(sha1, ce->oid.hash);
1569 oc->mode = ce->ce_mode;
1570 free(new_path);
1571 return 0;
1573 pos++;
1575 if (only_to_die && name[1] && name[1] != '/')
1576 diagnose_invalid_index_path(stage, prefix, cp);
1577 free(new_path);
1578 return -1;
1580 for (cp = name, bracket_depth = 0; *cp; cp++) {
1581 if (*cp == '{')
1582 bracket_depth++;
1583 else if (bracket_depth && *cp == '}')
1584 bracket_depth--;
1585 else if (!bracket_depth && *cp == ':')
1586 break;
1588 if (*cp == ':') {
1589 unsigned char tree_sha1[20];
1590 int len = cp - name;
1591 unsigned sub_flags = flags;
1593 sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
1594 sub_flags |= GET_SHA1_TREEISH;
1596 if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
1597 const char *filename = cp+1;
1598 char *new_filename = NULL;
1600 new_filename = resolve_relative_path(filename);
1601 if (new_filename)
1602 filename = new_filename;
1603 if (flags & GET_SHA1_FOLLOW_SYMLINKS) {
1604 ret = get_tree_entry_follow_symlinks(tree_sha1,
1605 filename, sha1, &oc->symlink_path,
1606 &oc->mode);
1607 } else {
1608 ret = get_tree_entry(tree_sha1, filename,
1609 sha1, &oc->mode);
1610 if (ret && only_to_die) {
1611 diagnose_invalid_sha1_path(prefix,
1612 filename,
1613 tree_sha1,
1614 name, len);
1617 hashcpy(oc->tree, tree_sha1);
1618 if (flags & GET_SHA1_RECORD_PATH)
1619 oc->path = xstrdup(filename);
1621 free(new_filename);
1622 return ret;
1623 } else {
1624 if (only_to_die)
1625 die("Invalid object name '%.*s'.", len, name);
1628 return ret;
1632 * Call this function when you know "name" given by the end user must
1633 * name an object but it doesn't; the function _may_ die with a better
1634 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1635 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1636 * you have a chance to diagnose the error further.
1638 void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1640 struct object_context oc;
1641 unsigned char sha1[20];
1642 get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
1645 int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc)
1647 if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE)
1648 die("BUG: incompatible flags for get_sha1_with_context");
1649 return get_sha1_with_context_1(str, flags, NULL, sha1, oc);