use DIV_ROUND_UP
[git/raj.git] / sha1_name.c
blobf7b6242914be6306f74e038c3638ef98ac24f7e9
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "tree-walk.h"
7 #include "refs.h"
8 #include "remote.h"
9 #include "dir.h"
10 #include "sha1-array.h"
12 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
14 typedef int (*disambiguate_hint_fn)(const struct object_id *, void *);
16 struct disambiguate_state {
17 int len; /* length of prefix in hex chars */
18 char hex_pfx[GIT_MAX_HEXSZ + 1];
19 struct object_id bin_pfx;
21 disambiguate_hint_fn fn;
22 void *cb_data;
23 struct object_id candidate;
24 unsigned candidate_exists:1;
25 unsigned candidate_checked:1;
26 unsigned candidate_ok:1;
27 unsigned disambiguate_fn_used:1;
28 unsigned ambiguous:1;
29 unsigned always_call_fn:1;
32 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
34 if (ds->always_call_fn) {
35 ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0;
36 return;
38 if (!ds->candidate_exists) {
39 /* this is the first candidate */
40 oidcpy(&ds->candidate, current);
41 ds->candidate_exists = 1;
42 return;
43 } else if (!oidcmp(&ds->candidate, current)) {
44 /* the same as what we already have seen */
45 return;
48 if (!ds->fn) {
49 /* cannot disambiguate between ds->candidate and current */
50 ds->ambiguous = 1;
51 return;
54 if (!ds->candidate_checked) {
55 ds->candidate_ok = ds->fn(&ds->candidate, ds->cb_data);
56 ds->disambiguate_fn_used = 1;
57 ds->candidate_checked = 1;
60 if (!ds->candidate_ok) {
61 /* discard the candidate; we know it does not satisfy fn */
62 oidcpy(&ds->candidate, current);
63 ds->candidate_checked = 0;
64 return;
67 /* if we reach this point, we know ds->candidate satisfies fn */
68 if (ds->fn(current, ds->cb_data)) {
70 * if both current and candidate satisfy fn, we cannot
71 * disambiguate.
73 ds->candidate_ok = 0;
74 ds->ambiguous = 1;
77 /* otherwise, current can be discarded and candidate is still good */
80 static void find_short_object_filename(struct disambiguate_state *ds)
82 struct alternate_object_database *alt;
83 char hex[GIT_MAX_HEXSZ];
84 static struct alternate_object_database *fakeent;
86 if (!fakeent) {
88 * Create a "fake" alternate object database that
89 * points to our own object database, to make it
90 * easier to get a temporary working space in
91 * alt->name/alt->base while iterating over the
92 * object databases including our own.
94 fakeent = alloc_alt_odb(get_object_directory());
96 fakeent->next = alt_odb_list;
98 xsnprintf(hex, sizeof(hex), "%.2s", ds->hex_pfx);
99 for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
100 struct strbuf *buf = alt_scratch_buf(alt);
101 struct dirent *de;
102 DIR *dir;
104 strbuf_addf(buf, "%.2s/", ds->hex_pfx);
105 dir = opendir(buf->buf);
106 if (!dir)
107 continue;
109 while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
110 struct object_id oid;
112 if (strlen(de->d_name) != GIT_SHA1_HEXSZ - 2)
113 continue;
114 if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
115 continue;
116 memcpy(hex + 2, de->d_name, GIT_SHA1_HEXSZ - 2);
117 if (!get_oid_hex(hex, &oid))
118 update_candidates(ds, &oid);
120 closedir(dir);
124 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
126 do {
127 if (*a != *b)
128 return 0;
129 a++;
130 b++;
131 len -= 2;
132 } while (len > 1);
133 if (len)
134 if ((*a ^ *b) & 0xf0)
135 return 0;
136 return 1;
139 static void unique_in_pack(struct packed_git *p,
140 struct disambiguate_state *ds)
142 uint32_t num, last, i, first = 0;
143 const struct object_id *current = NULL;
145 open_pack_index(p);
146 num = p->num_objects;
147 last = num;
148 while (first < last) {
149 uint32_t mid = (first + last) / 2;
150 const unsigned char *current;
151 int cmp;
153 current = nth_packed_object_sha1(p, mid);
154 cmp = hashcmp(ds->bin_pfx.hash, current);
155 if (!cmp) {
156 first = mid;
157 break;
159 if (cmp > 0) {
160 first = mid+1;
161 continue;
163 last = mid;
167 * At this point, "first" is the location of the lowest object
168 * with an object name that could match "bin_pfx". See if we have
169 * 0, 1 or more objects that actually match(es).
171 for (i = first; i < num && !ds->ambiguous; i++) {
172 struct object_id oid;
173 current = nth_packed_object_oid(&oid, p, i);
174 if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
175 break;
176 update_candidates(ds, current);
180 static void find_short_packed_object(struct disambiguate_state *ds)
182 struct packed_git *p;
184 prepare_packed_git();
185 for (p = packed_git; p && !ds->ambiguous; p = p->next)
186 unique_in_pack(p, ds);
189 #define SHORT_NAME_NOT_FOUND (-1)
190 #define SHORT_NAME_AMBIGUOUS (-2)
192 static int finish_object_disambiguation(struct disambiguate_state *ds,
193 unsigned char *sha1)
195 if (ds->ambiguous)
196 return SHORT_NAME_AMBIGUOUS;
198 if (!ds->candidate_exists)
199 return SHORT_NAME_NOT_FOUND;
201 if (!ds->candidate_checked)
203 * If this is the only candidate, there is no point
204 * calling the disambiguation hint callback.
206 * On the other hand, if the current candidate
207 * replaced an earlier candidate that did _not_ pass
208 * the disambiguation hint callback, then we do have
209 * more than one objects that match the short name
210 * given, so we should make sure this one matches;
211 * otherwise, if we discovered this one and the one
212 * that we previously discarded in the reverse order,
213 * we would end up showing different results in the
214 * same repository!
216 ds->candidate_ok = (!ds->disambiguate_fn_used ||
217 ds->fn(&ds->candidate, ds->cb_data));
219 if (!ds->candidate_ok)
220 return SHORT_NAME_AMBIGUOUS;
222 hashcpy(sha1, ds->candidate.hash);
223 return 0;
226 static int disambiguate_commit_only(const struct object_id *oid, void *cb_data_unused)
228 int kind = sha1_object_info(oid->hash, NULL);
229 return kind == OBJ_COMMIT;
232 static int disambiguate_committish_only(const struct object_id *oid, void *cb_data_unused)
234 struct object *obj;
235 int kind;
237 kind = sha1_object_info(oid->hash, NULL);
238 if (kind == OBJ_COMMIT)
239 return 1;
240 if (kind != OBJ_TAG)
241 return 0;
243 /* We need to do this the hard way... */
244 obj = deref_tag(parse_object(oid->hash), NULL, 0);
245 if (obj && obj->type == OBJ_COMMIT)
246 return 1;
247 return 0;
250 static int disambiguate_tree_only(const struct object_id *oid, void *cb_data_unused)
252 int kind = sha1_object_info(oid->hash, NULL);
253 return kind == OBJ_TREE;
256 static int disambiguate_treeish_only(const struct object_id *oid, void *cb_data_unused)
258 struct object *obj;
259 int kind;
261 kind = sha1_object_info(oid->hash, NULL);
262 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
263 return 1;
264 if (kind != OBJ_TAG)
265 return 0;
267 /* We need to do this the hard way... */
268 obj = deref_tag(parse_object(oid->hash), NULL, 0);
269 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
270 return 1;
271 return 0;
274 static int disambiguate_blob_only(const struct object_id *oid, void *cb_data_unused)
276 int kind = sha1_object_info(oid->hash, NULL);
277 return kind == OBJ_BLOB;
280 static disambiguate_hint_fn default_disambiguate_hint;
282 int set_disambiguate_hint_config(const char *var, const char *value)
284 static const struct {
285 const char *name;
286 disambiguate_hint_fn fn;
287 } hints[] = {
288 { "none", NULL },
289 { "commit", disambiguate_commit_only },
290 { "committish", disambiguate_committish_only },
291 { "tree", disambiguate_tree_only },
292 { "treeish", disambiguate_treeish_only },
293 { "blob", disambiguate_blob_only }
295 int i;
297 if (!value)
298 return config_error_nonbool(var);
300 for (i = 0; i < ARRAY_SIZE(hints); i++) {
301 if (!strcasecmp(value, hints[i].name)) {
302 default_disambiguate_hint = hints[i].fn;
303 return 0;
307 return error("unknown hint type for '%s': %s", var, value);
310 static int init_object_disambiguation(const char *name, int len,
311 struct disambiguate_state *ds)
313 int i;
315 if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ)
316 return -1;
318 memset(ds, 0, sizeof(*ds));
320 for (i = 0; i < len ;i++) {
321 unsigned char c = name[i];
322 unsigned char val;
323 if (c >= '0' && c <= '9')
324 val = c - '0';
325 else if (c >= 'a' && c <= 'f')
326 val = c - 'a' + 10;
327 else if (c >= 'A' && c <='F') {
328 val = c - 'A' + 10;
329 c -= 'A' - 'a';
331 else
332 return -1;
333 ds->hex_pfx[i] = c;
334 if (!(i & 1))
335 val <<= 4;
336 ds->bin_pfx.hash[i >> 1] |= val;
339 ds->len = len;
340 ds->hex_pfx[len] = '\0';
341 prepare_alt_odb();
342 return 0;
345 static int show_ambiguous_object(const struct object_id *oid, void *data)
347 const struct disambiguate_state *ds = data;
348 struct strbuf desc = STRBUF_INIT;
349 int type;
352 if (ds->fn && !ds->fn(oid, ds->cb_data))
353 return 0;
355 type = sha1_object_info(oid->hash, NULL);
356 if (type == OBJ_COMMIT) {
357 struct commit *commit = lookup_commit(oid->hash);
358 if (commit) {
359 struct pretty_print_context pp = {0};
360 pp.date_mode.type = DATE_SHORT;
361 format_commit_message(commit, " %ad - %s", &desc, &pp);
363 } else if (type == OBJ_TAG) {
364 struct tag *tag = lookup_tag(oid->hash);
365 if (!parse_tag(tag) && tag->tag)
366 strbuf_addf(&desc, " %s", tag->tag);
369 advise(" %s %s%s",
370 find_unique_abbrev(oid->hash, DEFAULT_ABBREV),
371 typename(type) ? typename(type) : "unknown type",
372 desc.buf);
374 strbuf_release(&desc);
375 return 0;
378 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
379 unsigned flags)
381 int status;
382 struct disambiguate_state ds;
383 int quietly = !!(flags & GET_SHA1_QUIETLY);
385 if (init_object_disambiguation(name, len, &ds) < 0)
386 return -1;
388 if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS))
389 die("BUG: multiple get_short_sha1 disambiguator flags");
391 if (flags & GET_SHA1_COMMIT)
392 ds.fn = disambiguate_commit_only;
393 else if (flags & GET_SHA1_COMMITTISH)
394 ds.fn = disambiguate_committish_only;
395 else if (flags & GET_SHA1_TREE)
396 ds.fn = disambiguate_tree_only;
397 else if (flags & GET_SHA1_TREEISH)
398 ds.fn = disambiguate_treeish_only;
399 else if (flags & GET_SHA1_BLOB)
400 ds.fn = disambiguate_blob_only;
401 else
402 ds.fn = default_disambiguate_hint;
404 find_short_object_filename(&ds);
405 find_short_packed_object(&ds);
406 status = finish_object_disambiguation(&ds, sha1);
408 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
409 error(_("short SHA1 %s is ambiguous"), ds.hex_pfx);
412 * We may still have ambiguity if we simply saw a series of
413 * candidates that did not satisfy our hint function. In
414 * that case, we still want to show them, so disable the hint
415 * function entirely.
417 if (!ds.ambiguous)
418 ds.fn = NULL;
420 advise(_("The candidates are:"));
421 for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds);
424 return status;
427 static int collect_ambiguous(const struct object_id *oid, void *data)
429 oid_array_append(data, oid);
430 return 0;
433 int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
435 struct oid_array collect = OID_ARRAY_INIT;
436 struct disambiguate_state ds;
437 int ret;
439 if (init_object_disambiguation(prefix, strlen(prefix), &ds) < 0)
440 return -1;
442 ds.always_call_fn = 1;
443 ds.fn = collect_ambiguous;
444 ds.cb_data = &collect;
445 find_short_object_filename(&ds);
446 find_short_packed_object(&ds);
448 ret = oid_array_for_each_unique(&collect, fn, cb_data);
449 oid_array_clear(&collect);
450 return ret;
454 * Return the slot of the most-significant bit set in "val". There are various
455 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
456 * probably not a big deal here.
458 static unsigned msb(unsigned long val)
460 unsigned r = 0;
461 while (val >>= 1)
462 r++;
463 return r;
466 int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
468 int status, exists;
470 if (len < 0) {
471 unsigned long count = approximate_object_count();
473 * Add one because the MSB only tells us the highest bit set,
474 * not including the value of all the _other_ bits (so "15"
475 * is only one off of 2^4, but the MSB is the 3rd bit.
477 len = msb(count) + 1;
479 * We now know we have on the order of 2^len objects, which
480 * expects a collision at 2^(len/2). But we also care about hex
481 * chars, not bits, and there are 4 bits per hex. So all
482 * together we need to divide by 2 and round up.
484 len = DIV_ROUND_UP(len, 2);
486 * For very small repos, we stick with our regular fallback.
488 if (len < FALLBACK_DEFAULT_ABBREV)
489 len = FALLBACK_DEFAULT_ABBREV;
492 sha1_to_hex_r(hex, sha1);
493 if (len == 40 || !len)
494 return 40;
495 exists = has_sha1_file(sha1);
496 while (len < 40) {
497 unsigned char sha1_ret[20];
498 status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
499 if (exists
500 ? !status
501 : status == SHORT_NAME_NOT_FOUND) {
502 hex[len] = 0;
503 return len;
505 len++;
507 return len;
510 const char *find_unique_abbrev(const unsigned char *sha1, int len)
512 static int bufno;
513 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
514 char *hex = hexbuffer[bufno];
515 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
516 find_unique_abbrev_r(hex, sha1, len);
517 return hex;
520 static int ambiguous_path(const char *path, int len)
522 int slash = 1;
523 int cnt;
525 for (cnt = 0; cnt < len; cnt++) {
526 switch (*path++) {
527 case '\0':
528 break;
529 case '/':
530 if (slash)
531 break;
532 slash = 1;
533 continue;
534 case '.':
535 continue;
536 default:
537 slash = 0;
538 continue;
540 break;
542 return slash;
545 static inline int at_mark(const char *string, int len,
546 const char **suffix, int nr)
548 int i;
550 for (i = 0; i < nr; i++) {
551 int suffix_len = strlen(suffix[i]);
552 if (suffix_len <= len
553 && !strncasecmp(string, suffix[i], suffix_len))
554 return suffix_len;
556 return 0;
559 static inline int upstream_mark(const char *string, int len)
561 const char *suffix[] = { "@{upstream}", "@{u}" };
562 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
565 static inline int push_mark(const char *string, int len)
567 const char *suffix[] = { "@{push}" };
568 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
571 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
572 static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf);
574 static int get_sha1_basic(const char *str, int len, unsigned char *sha1,
575 unsigned int flags)
577 static const char *warn_msg = "refname '%.*s' is ambiguous.";
578 static const char *object_name_msg = N_(
579 "Git normally never creates a ref that ends with 40 hex characters\n"
580 "because it will be ignored when you just specify 40-hex. These refs\n"
581 "may be created by mistake. For example,\n"
582 "\n"
583 " git checkout -b $br $(git rev-parse ...)\n"
584 "\n"
585 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
586 "examine these refs and maybe delete them. Turn this message off by\n"
587 "running \"git config advice.objectNameWarning false\"");
588 unsigned char tmp_sha1[20];
589 char *real_ref = NULL;
590 int refs_found = 0;
591 int at, reflog_len, nth_prior = 0;
593 if (len == 40 && !get_sha1_hex(str, sha1)) {
594 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
595 refs_found = dwim_ref(str, len, tmp_sha1, &real_ref);
596 if (refs_found > 0) {
597 warning(warn_msg, len, str);
598 if (advice_object_name_warning)
599 fprintf(stderr, "%s\n", _(object_name_msg));
601 free(real_ref);
603 return 0;
606 /* basic@{time or number or -number} format to query ref-log */
607 reflog_len = at = 0;
608 if (len && str[len-1] == '}') {
609 for (at = len-4; at >= 0; at--) {
610 if (str[at] == '@' && str[at+1] == '{') {
611 if (str[at+2] == '-') {
612 if (at != 0)
613 /* @{-N} not at start */
614 return -1;
615 nth_prior = 1;
616 continue;
618 if (!upstream_mark(str + at, len - at) &&
619 !push_mark(str + at, len - at)) {
620 reflog_len = (len-1) - (at+2);
621 len = at;
623 break;
628 /* Accept only unambiguous ref paths. */
629 if (len && ambiguous_path(str, len))
630 return -1;
632 if (nth_prior) {
633 struct strbuf buf = STRBUF_INIT;
634 int detached;
636 if (interpret_nth_prior_checkout(str, len, &buf) > 0) {
637 detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1));
638 strbuf_release(&buf);
639 if (detached)
640 return 0;
644 if (!len && reflog_len)
645 /* allow "@{...}" to mean the current branch reflog */
646 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
647 else if (reflog_len)
648 refs_found = dwim_log(str, len, sha1, &real_ref);
649 else
650 refs_found = dwim_ref(str, len, sha1, &real_ref);
652 if (!refs_found)
653 return -1;
655 if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) &&
656 (refs_found > 1 ||
657 !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY)))
658 warning(warn_msg, len, str);
660 if (reflog_len) {
661 int nth, i;
662 unsigned long at_time;
663 unsigned long co_time;
664 int co_tz, co_cnt;
666 /* Is it asking for N-th entry, or approxidate? */
667 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
668 char ch = str[at+2+i];
669 if ('0' <= ch && ch <= '9')
670 nth = nth * 10 + ch - '0';
671 else
672 nth = -1;
674 if (100000000 <= nth) {
675 at_time = nth;
676 nth = -1;
677 } else if (0 <= nth)
678 at_time = 0;
679 else {
680 int errors = 0;
681 char *tmp = xstrndup(str + at + 2, reflog_len);
682 at_time = approxidate_careful(tmp, &errors);
683 free(tmp);
684 if (errors) {
685 free(real_ref);
686 return -1;
689 if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL,
690 &co_time, &co_tz, &co_cnt)) {
691 if (!len) {
692 if (starts_with(real_ref, "refs/heads/")) {
693 str = real_ref + 11;
694 len = strlen(real_ref + 11);
695 } else {
696 /* detached HEAD */
697 str = "HEAD";
698 len = 4;
701 if (at_time) {
702 if (!(flags & GET_SHA1_QUIETLY)) {
703 warning("Log for '%.*s' only goes "
704 "back to %s.", len, str,
705 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
707 } else {
708 if (flags & GET_SHA1_QUIETLY) {
709 exit(128);
711 die("Log for '%.*s' only has %d entries.",
712 len, str, co_cnt);
717 free(real_ref);
718 return 0;
721 static int get_parent(const char *name, int len,
722 unsigned char *result, int idx)
724 unsigned char sha1[20];
725 int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
726 struct commit *commit;
727 struct commit_list *p;
729 if (ret)
730 return ret;
731 commit = lookup_commit_reference(sha1);
732 if (parse_commit(commit))
733 return -1;
734 if (!idx) {
735 hashcpy(result, commit->object.oid.hash);
736 return 0;
738 p = commit->parents;
739 while (p) {
740 if (!--idx) {
741 hashcpy(result, p->item->object.oid.hash);
742 return 0;
744 p = p->next;
746 return -1;
749 static int get_nth_ancestor(const char *name, int len,
750 unsigned char *result, int generation)
752 unsigned char sha1[20];
753 struct commit *commit;
754 int ret;
756 ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
757 if (ret)
758 return ret;
759 commit = lookup_commit_reference(sha1);
760 if (!commit)
761 return -1;
763 while (generation--) {
764 if (parse_commit(commit) || !commit->parents)
765 return -1;
766 commit = commit->parents->item;
768 hashcpy(result, commit->object.oid.hash);
769 return 0;
772 struct object *peel_to_type(const char *name, int namelen,
773 struct object *o, enum object_type expected_type)
775 if (name && !namelen)
776 namelen = strlen(name);
777 while (1) {
778 if (!o || (!o->parsed && !parse_object(o->oid.hash)))
779 return NULL;
780 if (expected_type == OBJ_ANY || o->type == expected_type)
781 return o;
782 if (o->type == OBJ_TAG)
783 o = ((struct tag*) o)->tagged;
784 else if (o->type == OBJ_COMMIT)
785 o = &(((struct commit *) o)->tree->object);
786 else {
787 if (name)
788 error("%.*s: expected %s type, but the object "
789 "dereferences to %s type",
790 namelen, name, typename(expected_type),
791 typename(o->type));
792 return NULL;
797 static int peel_onion(const char *name, int len, unsigned char *sha1,
798 unsigned lookup_flags)
800 unsigned char outer[20];
801 const char *sp;
802 unsigned int expected_type = 0;
803 struct object *o;
806 * "ref^{type}" dereferences ref repeatedly until you cannot
807 * dereference anymore, or you get an object of given type,
808 * whichever comes first. "ref^{}" means just dereference
809 * tags until you get a non-tag. "ref^0" is a shorthand for
810 * "ref^{commit}". "commit^{tree}" could be used to find the
811 * top-level tree of the given commit.
813 if (len < 4 || name[len-1] != '}')
814 return -1;
816 for (sp = name + len - 1; name <= sp; sp--) {
817 int ch = *sp;
818 if (ch == '{' && name < sp && sp[-1] == '^')
819 break;
821 if (sp <= name)
822 return -1;
824 sp++; /* beginning of type name, or closing brace for empty */
825 if (starts_with(sp, "commit}"))
826 expected_type = OBJ_COMMIT;
827 else if (starts_with(sp, "tag}"))
828 expected_type = OBJ_TAG;
829 else if (starts_with(sp, "tree}"))
830 expected_type = OBJ_TREE;
831 else if (starts_with(sp, "blob}"))
832 expected_type = OBJ_BLOB;
833 else if (starts_with(sp, "object}"))
834 expected_type = OBJ_ANY;
835 else if (sp[0] == '}')
836 expected_type = OBJ_NONE;
837 else if (sp[0] == '/')
838 expected_type = OBJ_COMMIT;
839 else
840 return -1;
842 lookup_flags &= ~GET_SHA1_DISAMBIGUATORS;
843 if (expected_type == OBJ_COMMIT)
844 lookup_flags |= GET_SHA1_COMMITTISH;
845 else if (expected_type == OBJ_TREE)
846 lookup_flags |= GET_SHA1_TREEISH;
848 if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
849 return -1;
851 o = parse_object(outer);
852 if (!o)
853 return -1;
854 if (!expected_type) {
855 o = deref_tag(o, name, sp - name - 2);
856 if (!o || (!o->parsed && !parse_object(o->oid.hash)))
857 return -1;
858 hashcpy(sha1, o->oid.hash);
859 return 0;
863 * At this point, the syntax look correct, so
864 * if we do not get the needed object, we should
865 * barf.
867 o = peel_to_type(name, len, o, expected_type);
868 if (!o)
869 return -1;
871 hashcpy(sha1, o->oid.hash);
872 if (sp[0] == '/') {
873 /* "$commit^{/foo}" */
874 char *prefix;
875 int ret;
876 struct commit_list *list = NULL;
879 * $commit^{/}. Some regex implementation may reject.
880 * We don't need regex anyway. '' pattern always matches.
882 if (sp[1] == '}')
883 return 0;
885 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
886 commit_list_insert((struct commit *)o, &list);
887 ret = get_sha1_oneline(prefix, sha1, list);
888 free(prefix);
889 return ret;
891 return 0;
894 static int get_describe_name(const char *name, int len, unsigned char *sha1)
896 const char *cp;
897 unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
899 for (cp = name + len - 1; name + 2 <= cp; cp--) {
900 char ch = *cp;
901 if (!isxdigit(ch)) {
902 /* We must be looking at g in "SOMETHING-g"
903 * for it to be describe output.
905 if (ch == 'g' && cp[-1] == '-') {
906 cp++;
907 len -= cp - name;
908 return get_short_sha1(cp, len, sha1, flags);
912 return -1;
915 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
917 int ret, has_suffix;
918 const char *cp;
921 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
923 has_suffix = 0;
924 for (cp = name + len - 1; name <= cp; cp--) {
925 int ch = *cp;
926 if ('0' <= ch && ch <= '9')
927 continue;
928 if (ch == '~' || ch == '^')
929 has_suffix = ch;
930 break;
933 if (has_suffix) {
934 int num = 0;
935 int len1 = cp - name;
936 cp++;
937 while (cp < name + len)
938 num = num * 10 + *cp++ - '0';
939 if (!num && len1 == len - 1)
940 num = 1;
941 if (has_suffix == '^')
942 return get_parent(name, len1, sha1, num);
943 /* else if (has_suffix == '~') -- goes without saying */
944 return get_nth_ancestor(name, len1, sha1, num);
947 ret = peel_onion(name, len, sha1, lookup_flags);
948 if (!ret)
949 return 0;
951 ret = get_sha1_basic(name, len, sha1, lookup_flags);
952 if (!ret)
953 return 0;
955 /* It could be describe output that is "SOMETHING-gXXXX" */
956 ret = get_describe_name(name, len, sha1);
957 if (!ret)
958 return 0;
960 return get_short_sha1(name, len, sha1, lookup_flags);
964 * This interprets names like ':/Initial revision of "git"' by searching
965 * through history and returning the first commit whose message starts
966 * the given regular expression.
968 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
970 * For a literal '!' character at the beginning of a pattern, you have to repeat
971 * that, like: ':/!!foo'
973 * For future extension, all other sequences beginning with ':/!' are reserved.
976 /* Remember to update object flag allocation in object.h */
977 #define ONELINE_SEEN (1u<<20)
979 static int handle_one_ref(const char *path, const struct object_id *oid,
980 int flag, void *cb_data)
982 struct commit_list **list = cb_data;
983 struct object *object = parse_object(oid->hash);
984 if (!object)
985 return 0;
986 if (object->type == OBJ_TAG) {
987 object = deref_tag(object, path, strlen(path));
988 if (!object)
989 return 0;
991 if (object->type != OBJ_COMMIT)
992 return 0;
993 commit_list_insert((struct commit *)object, list);
994 return 0;
997 static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
998 struct commit_list *list)
1000 struct commit_list *backup = NULL, *l;
1001 int found = 0;
1002 int negative = 0;
1003 regex_t regex;
1005 if (prefix[0] == '!') {
1006 prefix++;
1008 if (prefix[0] == '-') {
1009 prefix++;
1010 negative = 1;
1011 } else if (prefix[0] != '!') {
1012 return -1;
1016 if (regcomp(&regex, prefix, REG_EXTENDED))
1017 return -1;
1019 for (l = list; l; l = l->next) {
1020 l->item->object.flags |= ONELINE_SEEN;
1021 commit_list_insert(l->item, &backup);
1023 while (list) {
1024 const char *p, *buf;
1025 struct commit *commit;
1026 int matches;
1028 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1029 if (!parse_object(commit->object.oid.hash))
1030 continue;
1031 buf = get_commit_buffer(commit, NULL);
1032 p = strstr(buf, "\n\n");
1033 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1034 unuse_commit_buffer(commit, buf);
1036 if (matches) {
1037 hashcpy(sha1, commit->object.oid.hash);
1038 found = 1;
1039 break;
1042 regfree(&regex);
1043 free_commit_list(list);
1044 for (l = backup; l; l = l->next)
1045 clear_commit_marks(l->item, ONELINE_SEEN);
1046 free_commit_list(backup);
1047 return found ? 0 : -1;
1050 struct grab_nth_branch_switch_cbdata {
1051 int remaining;
1052 struct strbuf buf;
1055 static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
1056 const char *email, unsigned long timestamp, int tz,
1057 const char *message, void *cb_data)
1059 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1060 const char *match = NULL, *target = NULL;
1061 size_t len;
1063 if (skip_prefix(message, "checkout: moving from ", &match))
1064 target = strstr(match, " to ");
1066 if (!match || !target)
1067 return 0;
1068 if (--(cb->remaining) == 0) {
1069 len = target - match;
1070 strbuf_reset(&cb->buf);
1071 strbuf_add(&cb->buf, match, len);
1072 return 1; /* we are done */
1074 return 0;
1078 * Parse @{-N} syntax, return the number of characters parsed
1079 * if successful; otherwise signal an error with negative value.
1081 static int interpret_nth_prior_checkout(const char *name, int namelen,
1082 struct strbuf *buf)
1084 long nth;
1085 int retval;
1086 struct grab_nth_branch_switch_cbdata cb;
1087 const char *brace;
1088 char *num_end;
1090 if (namelen < 4)
1091 return -1;
1092 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1093 return -1;
1094 brace = memchr(name, '}', namelen);
1095 if (!brace)
1096 return -1;
1097 nth = strtol(name + 3, &num_end, 10);
1098 if (num_end != brace)
1099 return -1;
1100 if (nth <= 0)
1101 return -1;
1102 cb.remaining = nth;
1103 strbuf_init(&cb.buf, 20);
1105 retval = 0;
1106 if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) {
1107 strbuf_reset(buf);
1108 strbuf_addbuf(buf, &cb.buf);
1109 retval = brace - name + 1;
1112 strbuf_release(&cb.buf);
1113 return retval;
1116 int get_oid_mb(const char *name, struct object_id *oid)
1118 struct commit *one, *two;
1119 struct commit_list *mbs;
1120 struct object_id oid_tmp;
1121 const char *dots;
1122 int st;
1124 dots = strstr(name, "...");
1125 if (!dots)
1126 return get_oid(name, oid);
1127 if (dots == name)
1128 st = get_oid("HEAD", &oid_tmp);
1129 else {
1130 struct strbuf sb;
1131 strbuf_init(&sb, dots - name);
1132 strbuf_add(&sb, name, dots - name);
1133 st = get_sha1_committish(sb.buf, oid_tmp.hash);
1134 strbuf_release(&sb);
1136 if (st)
1137 return st;
1138 one = lookup_commit_reference_gently(oid_tmp.hash, 0);
1139 if (!one)
1140 return -1;
1142 if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash))
1143 return -1;
1144 two = lookup_commit_reference_gently(oid_tmp.hash, 0);
1145 if (!two)
1146 return -1;
1147 mbs = get_merge_bases(one, two);
1148 if (!mbs || mbs->next)
1149 st = -1;
1150 else {
1151 st = 0;
1152 oidcpy(oid, &mbs->item->object.oid);
1154 free_commit_list(mbs);
1155 return st;
1158 /* parse @something syntax, when 'something' is not {.*} */
1159 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1161 const char *next;
1163 if (len || name[1] == '{')
1164 return -1;
1166 /* make sure it's a single @, or @@{.*}, not @foo */
1167 next = memchr(name + len + 1, '@', namelen - len - 1);
1168 if (next && next[1] != '{')
1169 return -1;
1170 if (!next)
1171 next = name + namelen;
1172 if (next != name + 1)
1173 return -1;
1175 strbuf_reset(buf);
1176 strbuf_add(buf, "HEAD", 4);
1177 return 1;
1180 static int reinterpret(const char *name, int namelen, int len,
1181 struct strbuf *buf, unsigned allowed)
1183 /* we have extra data, which might need further processing */
1184 struct strbuf tmp = STRBUF_INIT;
1185 int used = buf->len;
1186 int ret;
1188 strbuf_add(buf, name + len, namelen - len);
1189 ret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed);
1190 /* that data was not interpreted, remove our cruft */
1191 if (ret < 0) {
1192 strbuf_setlen(buf, used);
1193 return len;
1195 strbuf_reset(buf);
1196 strbuf_addbuf(buf, &tmp);
1197 strbuf_release(&tmp);
1198 /* tweak for size of {-N} versus expanded ref name */
1199 return ret - used + len;
1202 static void set_shortened_ref(struct strbuf *buf, const char *ref)
1204 char *s = shorten_unambiguous_ref(ref, 0);
1205 strbuf_reset(buf);
1206 strbuf_addstr(buf, s);
1207 free(s);
1210 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1212 if (!allowed)
1213 return 1;
1215 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1216 starts_with(refname, "refs/heads/"))
1217 return 1;
1218 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1219 starts_with(refname, "refs/remotes/"))
1220 return 1;
1222 return 0;
1225 static int interpret_branch_mark(const char *name, int namelen,
1226 int at, struct strbuf *buf,
1227 int (*get_mark)(const char *, int),
1228 const char *(*get_data)(struct branch *,
1229 struct strbuf *),
1230 unsigned allowed)
1232 int len;
1233 struct branch *branch;
1234 struct strbuf err = STRBUF_INIT;
1235 const char *value;
1237 len = get_mark(name + at, namelen - at);
1238 if (!len)
1239 return -1;
1241 if (memchr(name, ':', at))
1242 return -1;
1244 if (at) {
1245 char *name_str = xmemdupz(name, at);
1246 branch = branch_get(name_str);
1247 free(name_str);
1248 } else
1249 branch = branch_get(NULL);
1251 value = get_data(branch, &err);
1252 if (!value)
1253 die("%s", err.buf);
1255 if (!branch_interpret_allowed(value, allowed))
1256 return -1;
1258 set_shortened_ref(buf, value);
1259 return len + at;
1262 int interpret_branch_name(const char *name, int namelen, struct strbuf *buf,
1263 unsigned allowed)
1265 char *at;
1266 const char *start;
1267 int len;
1269 if (!namelen)
1270 namelen = strlen(name);
1272 if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) {
1273 len = interpret_nth_prior_checkout(name, namelen, buf);
1274 if (!len) {
1275 return len; /* syntax Ok, not enough switches */
1276 } else if (len > 0) {
1277 if (len == namelen)
1278 return len; /* consumed all */
1279 else
1280 return reinterpret(name, namelen, len, buf, allowed);
1284 for (start = name;
1285 (at = memchr(start, '@', namelen - (start - name)));
1286 start = at + 1) {
1288 if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) {
1289 len = interpret_empty_at(name, namelen, at - name, buf);
1290 if (len > 0)
1291 return reinterpret(name, namelen, len, buf,
1292 allowed);
1295 len = interpret_branch_mark(name, namelen, at - name, buf,
1296 upstream_mark, branch_get_upstream,
1297 allowed);
1298 if (len > 0)
1299 return len;
1301 len = interpret_branch_mark(name, namelen, at - name, buf,
1302 push_mark, branch_get_push,
1303 allowed);
1304 if (len > 0)
1305 return len;
1308 return -1;
1311 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1313 int len = strlen(name);
1314 int used = interpret_branch_name(name, len, sb, allowed);
1316 if (used < 0)
1317 used = 0;
1318 strbuf_add(sb, name + used, len - used);
1321 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1323 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1324 if (name[0] == '-')
1325 return -1;
1326 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1327 return check_refname_format(sb->buf, 0);
1331 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
1332 * notably "xyz^" for "parent of xyz"
1334 int get_sha1(const char *name, unsigned char *sha1)
1336 struct object_context unused;
1337 return get_sha1_with_context(name, 0, sha1, &unused);
1341 * This is like "get_sha1()", but for struct object_id.
1343 int get_oid(const char *name, struct object_id *oid)
1345 return get_sha1(name, oid->hash);
1350 * Many callers know that the user meant to name a commit-ish by
1351 * syntactical positions where the object name appears. Calling this
1352 * function allows the machinery to disambiguate shorter-than-unique
1353 * abbreviated object names between commit-ish and others.
1355 * Note that this does NOT error out when the named object is not a
1356 * commit-ish. It is merely to give a hint to the disambiguation
1357 * machinery.
1359 int get_sha1_committish(const char *name, unsigned char *sha1)
1361 struct object_context unused;
1362 return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1363 sha1, &unused);
1366 int get_sha1_treeish(const char *name, unsigned char *sha1)
1368 struct object_context unused;
1369 return get_sha1_with_context(name, GET_SHA1_TREEISH,
1370 sha1, &unused);
1373 int get_sha1_commit(const char *name, unsigned char *sha1)
1375 struct object_context unused;
1376 return get_sha1_with_context(name, GET_SHA1_COMMIT,
1377 sha1, &unused);
1380 int get_sha1_tree(const char *name, unsigned char *sha1)
1382 struct object_context unused;
1383 return get_sha1_with_context(name, GET_SHA1_TREE,
1384 sha1, &unused);
1387 int get_sha1_blob(const char *name, unsigned char *sha1)
1389 struct object_context unused;
1390 return get_sha1_with_context(name, GET_SHA1_BLOB,
1391 sha1, &unused);
1394 /* Must be called only when object_name:filename doesn't exist. */
1395 static void diagnose_invalid_sha1_path(const char *prefix,
1396 const char *filename,
1397 const unsigned char *tree_sha1,
1398 const char *object_name,
1399 int object_name_len)
1401 unsigned char sha1[20];
1402 unsigned mode;
1404 if (!prefix)
1405 prefix = "";
1407 if (file_exists(filename))
1408 die("Path '%s' exists on disk, but not in '%.*s'.",
1409 filename, object_name_len, object_name);
1410 if (errno == ENOENT || errno == ENOTDIR) {
1411 char *fullname = xstrfmt("%s%s", prefix, filename);
1413 if (!get_tree_entry(tree_sha1, fullname,
1414 sha1, &mode)) {
1415 die("Path '%s' exists, but not '%s'.\n"
1416 "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1417 fullname,
1418 filename,
1419 object_name_len, object_name,
1420 fullname,
1421 object_name_len, object_name,
1422 filename);
1424 die("Path '%s' does not exist in '%.*s'",
1425 filename, object_name_len, object_name);
1429 /* Must be called only when :stage:filename doesn't exist. */
1430 static void diagnose_invalid_index_path(int stage,
1431 const char *prefix,
1432 const char *filename)
1434 const struct cache_entry *ce;
1435 int pos;
1436 unsigned namelen = strlen(filename);
1437 struct strbuf fullname = STRBUF_INIT;
1439 if (!prefix)
1440 prefix = "";
1442 /* Wrong stage number? */
1443 pos = cache_name_pos(filename, namelen);
1444 if (pos < 0)
1445 pos = -pos - 1;
1446 if (pos < active_nr) {
1447 ce = active_cache[pos];
1448 if (ce_namelen(ce) == namelen &&
1449 !memcmp(ce->name, filename, namelen))
1450 die("Path '%s' is in the index, but not at stage %d.\n"
1451 "Did you mean ':%d:%s'?",
1452 filename, stage,
1453 ce_stage(ce), filename);
1456 /* Confusion between relative and absolute filenames? */
1457 strbuf_addstr(&fullname, prefix);
1458 strbuf_addstr(&fullname, filename);
1459 pos = cache_name_pos(fullname.buf, fullname.len);
1460 if (pos < 0)
1461 pos = -pos - 1;
1462 if (pos < active_nr) {
1463 ce = active_cache[pos];
1464 if (ce_namelen(ce) == fullname.len &&
1465 !memcmp(ce->name, fullname.buf, fullname.len))
1466 die("Path '%s' is in the index, but not '%s'.\n"
1467 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1468 fullname.buf, filename,
1469 ce_stage(ce), fullname.buf,
1470 ce_stage(ce), filename);
1473 if (file_exists(filename))
1474 die("Path '%s' exists on disk, but not in the index.", filename);
1475 if (errno == ENOENT || errno == ENOTDIR)
1476 die("Path '%s' does not exist (neither on disk nor in the index).",
1477 filename);
1479 strbuf_release(&fullname);
1483 static char *resolve_relative_path(const char *rel)
1485 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1486 return NULL;
1488 if (!is_inside_work_tree())
1489 die("relative path syntax can't be used outside working tree.");
1491 /* die() inside prefix_path() if resolved path is outside worktree */
1492 return prefix_path(startup_info->prefix,
1493 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1494 rel);
1497 static int get_sha1_with_context_1(const char *name,
1498 unsigned flags,
1499 const char *prefix,
1500 unsigned char *sha1,
1501 struct object_context *oc)
1503 int ret, bracket_depth;
1504 int namelen = strlen(name);
1505 const char *cp;
1506 int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
1508 if (only_to_die)
1509 flags |= GET_SHA1_QUIETLY;
1511 memset(oc, 0, sizeof(*oc));
1512 oc->mode = S_IFINVALID;
1513 strbuf_init(&oc->symlink_path, 0);
1514 ret = get_sha1_1(name, namelen, sha1, flags);
1515 if (!ret)
1516 return ret;
1518 * sha1:path --> object name of path in ent sha1
1519 * :path -> object name of absolute path in index
1520 * :./path -> object name of path relative to cwd in index
1521 * :[0-3]:path -> object name of path in index at stage
1522 * :/foo -> recent commit matching foo
1524 if (name[0] == ':') {
1525 int stage = 0;
1526 const struct cache_entry *ce;
1527 char *new_path = NULL;
1528 int pos;
1529 if (!only_to_die && namelen > 2 && name[1] == '/') {
1530 struct commit_list *list = NULL;
1532 for_each_ref(handle_one_ref, &list);
1533 commit_list_sort_by_date(&list);
1534 return get_sha1_oneline(name + 2, sha1, list);
1536 if (namelen < 3 ||
1537 name[2] != ':' ||
1538 name[1] < '0' || '3' < name[1])
1539 cp = name + 1;
1540 else {
1541 stage = name[1] - '0';
1542 cp = name + 3;
1544 new_path = resolve_relative_path(cp);
1545 if (!new_path) {
1546 namelen = namelen - (cp - name);
1547 } else {
1548 cp = new_path;
1549 namelen = strlen(cp);
1552 if (flags & GET_SHA1_RECORD_PATH)
1553 oc->path = xstrdup(cp);
1555 if (!active_cache)
1556 read_cache();
1557 pos = cache_name_pos(cp, namelen);
1558 if (pos < 0)
1559 pos = -pos - 1;
1560 while (pos < active_nr) {
1561 ce = active_cache[pos];
1562 if (ce_namelen(ce) != namelen ||
1563 memcmp(ce->name, cp, namelen))
1564 break;
1565 if (ce_stage(ce) == stage) {
1566 hashcpy(sha1, ce->oid.hash);
1567 oc->mode = ce->ce_mode;
1568 free(new_path);
1569 return 0;
1571 pos++;
1573 if (only_to_die && name[1] && name[1] != '/')
1574 diagnose_invalid_index_path(stage, prefix, cp);
1575 free(new_path);
1576 return -1;
1578 for (cp = name, bracket_depth = 0; *cp; cp++) {
1579 if (*cp == '{')
1580 bracket_depth++;
1581 else if (bracket_depth && *cp == '}')
1582 bracket_depth--;
1583 else if (!bracket_depth && *cp == ':')
1584 break;
1586 if (*cp == ':') {
1587 unsigned char tree_sha1[20];
1588 int len = cp - name;
1589 unsigned sub_flags = flags;
1591 sub_flags &= ~GET_SHA1_DISAMBIGUATORS;
1592 sub_flags |= GET_SHA1_TREEISH;
1594 if (!get_sha1_1(name, len, tree_sha1, sub_flags)) {
1595 const char *filename = cp+1;
1596 char *new_filename = NULL;
1598 new_filename = resolve_relative_path(filename);
1599 if (new_filename)
1600 filename = new_filename;
1601 if (flags & GET_SHA1_FOLLOW_SYMLINKS) {
1602 ret = get_tree_entry_follow_symlinks(tree_sha1,
1603 filename, sha1, &oc->symlink_path,
1604 &oc->mode);
1605 } else {
1606 ret = get_tree_entry(tree_sha1, filename,
1607 sha1, &oc->mode);
1608 if (ret && only_to_die) {
1609 diagnose_invalid_sha1_path(prefix,
1610 filename,
1611 tree_sha1,
1612 name, len);
1615 hashcpy(oc->tree, tree_sha1);
1616 if (flags & GET_SHA1_RECORD_PATH)
1617 oc->path = xstrdup(filename);
1619 free(new_filename);
1620 return ret;
1621 } else {
1622 if (only_to_die)
1623 die("Invalid object name '%.*s'.", len, name);
1626 return ret;
1630 * Call this function when you know "name" given by the end user must
1631 * name an object but it doesn't; the function _may_ die with a better
1632 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1633 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1634 * you have a chance to diagnose the error further.
1636 void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1638 struct object_context oc;
1639 unsigned char sha1[20];
1640 get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
1643 int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *oc)
1645 if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE)
1646 die("BUG: incompatible flags for get_sha1_with_context");
1647 return get_sha1_with_context_1(str, flags, NULL, sha1, oc);