hash: provide per-algorithm null OIDs
[alt-git.git] / object-name.c
blob64202de60b1f5af3f09b15ba9c6240d7bb136930
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 "oid-array.h"
12 #include "packfile.h"
13 #include "object-store.h"
14 #include "repository.h"
15 #include "submodule.h"
16 #include "midx.h"
17 #include "commit-reach.h"
19 static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);
21 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
23 struct disambiguate_state {
24 int len; /* length of prefix in hex chars */
25 char hex_pfx[GIT_MAX_HEXSZ + 1];
26 struct object_id bin_pfx;
28 struct repository *repo;
29 disambiguate_hint_fn fn;
30 void *cb_data;
31 struct object_id candidate;
32 unsigned candidate_exists:1;
33 unsigned candidate_checked:1;
34 unsigned candidate_ok:1;
35 unsigned disambiguate_fn_used:1;
36 unsigned ambiguous:1;
37 unsigned always_call_fn:1;
40 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
42 if (ds->always_call_fn) {
43 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
44 return;
46 if (!ds->candidate_exists) {
47 /* this is the first candidate */
48 oidcpy(&ds->candidate, current);
49 ds->candidate_exists = 1;
50 return;
51 } else if (oideq(&ds->candidate, current)) {
52 /* the same as what we already have seen */
53 return;
56 if (!ds->fn) {
57 /* cannot disambiguate between ds->candidate and current */
58 ds->ambiguous = 1;
59 return;
62 if (!ds->candidate_checked) {
63 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
64 ds->disambiguate_fn_used = 1;
65 ds->candidate_checked = 1;
68 if (!ds->candidate_ok) {
69 /* discard the candidate; we know it does not satisfy fn */
70 oidcpy(&ds->candidate, current);
71 ds->candidate_checked = 0;
72 return;
75 /* if we reach this point, we know ds->candidate satisfies fn */
76 if (ds->fn(ds->repo, current, ds->cb_data)) {
78 * if both current and candidate satisfy fn, we cannot
79 * disambiguate.
81 ds->candidate_ok = 0;
82 ds->ambiguous = 1;
85 /* otherwise, current can be discarded and candidate is still good */
88 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
90 static void find_short_object_filename(struct disambiguate_state *ds)
92 struct object_directory *odb;
94 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) {
95 int pos;
96 struct oid_array *loose_objects;
98 loose_objects = odb_loose_cache(odb, &ds->bin_pfx);
99 pos = oid_array_lookup(loose_objects, &ds->bin_pfx);
100 if (pos < 0)
101 pos = -1 - pos;
102 while (!ds->ambiguous && pos < loose_objects->nr) {
103 const struct object_id *oid;
104 oid = loose_objects->oid + pos;
105 if (!match_hash(ds->len, ds->bin_pfx.hash, oid->hash))
106 break;
107 update_candidates(ds, oid);
108 pos++;
113 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
115 do {
116 if (*a != *b)
117 return 0;
118 a++;
119 b++;
120 len -= 2;
121 } while (len > 1);
122 if (len)
123 if ((*a ^ *b) & 0xf0)
124 return 0;
125 return 1;
128 static void unique_in_midx(struct multi_pack_index *m,
129 struct disambiguate_state *ds)
131 uint32_t num, i, first = 0;
132 const struct object_id *current = NULL;
133 num = m->num_objects;
135 if (!num)
136 return;
138 bsearch_midx(&ds->bin_pfx, m, &first);
141 * At this point, "first" is the location of the lowest object
142 * with an object name that could match "bin_pfx". See if we have
143 * 0, 1 or more objects that actually match(es).
145 for (i = first; i < num && !ds->ambiguous; i++) {
146 struct object_id oid;
147 current = nth_midxed_object_oid(&oid, m, i);
148 if (!match_hash(ds->len, ds->bin_pfx.hash, current->hash))
149 break;
150 update_candidates(ds, current);
154 static void unique_in_pack(struct packed_git *p,
155 struct disambiguate_state *ds)
157 uint32_t num, i, first = 0;
159 if (p->multi_pack_index)
160 return;
162 if (open_pack_index(p) || !p->num_objects)
163 return;
165 num = p->num_objects;
166 bsearch_pack(&ds->bin_pfx, p, &first);
169 * At this point, "first" is the location of the lowest object
170 * with an object name that could match "bin_pfx". See if we have
171 * 0, 1 or more objects that actually match(es).
173 for (i = first; i < num && !ds->ambiguous; i++) {
174 struct object_id oid;
175 nth_packed_object_id(&oid, p, i);
176 if (!match_hash(ds->len, ds->bin_pfx.hash, oid.hash))
177 break;
178 update_candidates(ds, &oid);
182 static void find_short_packed_object(struct disambiguate_state *ds)
184 struct multi_pack_index *m;
185 struct packed_git *p;
187 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
188 m = m->next)
189 unique_in_midx(m, ds);
190 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
191 p = p->next)
192 unique_in_pack(p, ds);
195 static int finish_object_disambiguation(struct disambiguate_state *ds,
196 struct object_id *oid)
198 if (ds->ambiguous)
199 return SHORT_NAME_AMBIGUOUS;
201 if (!ds->candidate_exists)
202 return MISSING_OBJECT;
204 if (!ds->candidate_checked)
206 * If this is the only candidate, there is no point
207 * calling the disambiguation hint callback.
209 * On the other hand, if the current candidate
210 * replaced an earlier candidate that did _not_ pass
211 * the disambiguation hint callback, then we do have
212 * more than one objects that match the short name
213 * given, so we should make sure this one matches;
214 * otherwise, if we discovered this one and the one
215 * that we previously discarded in the reverse order,
216 * we would end up showing different results in the
217 * same repository!
219 ds->candidate_ok = (!ds->disambiguate_fn_used ||
220 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
222 if (!ds->candidate_ok)
223 return SHORT_NAME_AMBIGUOUS;
225 oidcpy(oid, &ds->candidate);
226 return 0;
229 static int disambiguate_commit_only(struct repository *r,
230 const struct object_id *oid,
231 void *cb_data_unused)
233 int kind = oid_object_info(r, oid, NULL);
234 return kind == OBJ_COMMIT;
237 static int disambiguate_committish_only(struct repository *r,
238 const struct object_id *oid,
239 void *cb_data_unused)
241 struct object *obj;
242 int kind;
244 kind = oid_object_info(r, oid, NULL);
245 if (kind == OBJ_COMMIT)
246 return 1;
247 if (kind != OBJ_TAG)
248 return 0;
250 /* We need to do this the hard way... */
251 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
252 if (obj && obj->type == OBJ_COMMIT)
253 return 1;
254 return 0;
257 static int disambiguate_tree_only(struct repository *r,
258 const struct object_id *oid,
259 void *cb_data_unused)
261 int kind = oid_object_info(r, oid, NULL);
262 return kind == OBJ_TREE;
265 static int disambiguate_treeish_only(struct repository *r,
266 const struct object_id *oid,
267 void *cb_data_unused)
269 struct object *obj;
270 int kind;
272 kind = oid_object_info(r, oid, NULL);
273 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
274 return 1;
275 if (kind != OBJ_TAG)
276 return 0;
278 /* We need to do this the hard way... */
279 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
280 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
281 return 1;
282 return 0;
285 static int disambiguate_blob_only(struct repository *r,
286 const struct object_id *oid,
287 void *cb_data_unused)
289 int kind = oid_object_info(r, oid, NULL);
290 return kind == OBJ_BLOB;
293 static disambiguate_hint_fn default_disambiguate_hint;
295 int set_disambiguate_hint_config(const char *var, const char *value)
297 static const struct {
298 const char *name;
299 disambiguate_hint_fn fn;
300 } hints[] = {
301 { "none", NULL },
302 { "commit", disambiguate_commit_only },
303 { "committish", disambiguate_committish_only },
304 { "tree", disambiguate_tree_only },
305 { "treeish", disambiguate_treeish_only },
306 { "blob", disambiguate_blob_only }
308 int i;
310 if (!value)
311 return config_error_nonbool(var);
313 for (i = 0; i < ARRAY_SIZE(hints); i++) {
314 if (!strcasecmp(value, hints[i].name)) {
315 default_disambiguate_hint = hints[i].fn;
316 return 0;
320 return error("unknown hint type for '%s': %s", var, value);
323 static int init_object_disambiguation(struct repository *r,
324 const char *name, int len,
325 struct disambiguate_state *ds)
327 int i;
329 if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz)
330 return -1;
332 memset(ds, 0, sizeof(*ds));
334 for (i = 0; i < len ;i++) {
335 unsigned char c = name[i];
336 unsigned char val;
337 if (c >= '0' && c <= '9')
338 val = c - '0';
339 else if (c >= 'a' && c <= 'f')
340 val = c - 'a' + 10;
341 else if (c >= 'A' && c <='F') {
342 val = c - 'A' + 10;
343 c -= 'A' - 'a';
345 else
346 return -1;
347 ds->hex_pfx[i] = c;
348 if (!(i & 1))
349 val <<= 4;
350 ds->bin_pfx.hash[i >> 1] |= val;
353 ds->len = len;
354 ds->hex_pfx[len] = '\0';
355 ds->repo = r;
356 prepare_alt_odb(r);
357 return 0;
360 static int show_ambiguous_object(const struct object_id *oid, void *data)
362 const struct disambiguate_state *ds = data;
363 struct strbuf desc = STRBUF_INIT;
364 int type;
366 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
367 return 0;
369 type = oid_object_info(ds->repo, oid, NULL);
370 if (type == OBJ_COMMIT) {
371 struct commit *commit = lookup_commit(ds->repo, oid);
372 if (commit) {
373 struct pretty_print_context pp = {0};
374 pp.date_mode.type = DATE_SHORT;
375 format_commit_message(commit, " %ad - %s", &desc, &pp);
377 } else if (type == OBJ_TAG) {
378 struct tag *tag = lookup_tag(ds->repo, oid);
379 if (!parse_tag(tag) && tag->tag)
380 strbuf_addf(&desc, " %s", tag->tag);
383 advise(" %s %s%s",
384 repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV),
385 type_name(type) ? type_name(type) : "unknown type",
386 desc.buf);
388 strbuf_release(&desc);
389 return 0;
392 static int collect_ambiguous(const struct object_id *oid, void *data)
394 oid_array_append(data, oid);
395 return 0;
398 static int repo_collect_ambiguous(struct repository *r,
399 const struct object_id *oid,
400 void *data)
402 return collect_ambiguous(oid, data);
405 static int sort_ambiguous(const void *a, const void *b, void *ctx)
407 struct repository *sort_ambiguous_repo = ctx;
408 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
409 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
410 int a_type_sort;
411 int b_type_sort;
414 * Sorts by hash within the same object type, just as
415 * oid_array_for_each_unique() would do.
417 if (a_type == b_type)
418 return oidcmp(a, b);
421 * Between object types show tags, then commits, and finally
422 * trees and blobs.
424 * The object_type enum is commit, tree, blob, tag, but we
425 * want tag, commit, tree blob. Cleverly (perhaps too
426 * cleverly) do that with modulus, since the enum assigns 1 to
427 * commit, so tag becomes 0.
429 a_type_sort = a_type % 4;
430 b_type_sort = b_type % 4;
431 return a_type_sort > b_type_sort ? 1 : -1;
434 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
436 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
439 static enum get_oid_result get_short_oid(struct repository *r,
440 const char *name, int len,
441 struct object_id *oid,
442 unsigned flags)
444 int status;
445 struct disambiguate_state ds;
446 int quietly = !!(flags & GET_OID_QUIETLY);
448 if (init_object_disambiguation(r, name, len, &ds) < 0)
449 return -1;
451 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
452 BUG("multiple get_short_oid disambiguator flags");
454 if (flags & GET_OID_COMMIT)
455 ds.fn = disambiguate_commit_only;
456 else if (flags & GET_OID_COMMITTISH)
457 ds.fn = disambiguate_committish_only;
458 else if (flags & GET_OID_TREE)
459 ds.fn = disambiguate_tree_only;
460 else if (flags & GET_OID_TREEISH)
461 ds.fn = disambiguate_treeish_only;
462 else if (flags & GET_OID_BLOB)
463 ds.fn = disambiguate_blob_only;
464 else
465 ds.fn = default_disambiguate_hint;
467 find_short_object_filename(&ds);
468 find_short_packed_object(&ds);
469 status = finish_object_disambiguation(&ds, oid);
472 * If we didn't find it, do the usual reprepare() slow-path,
473 * since the object may have recently been added to the repository
474 * or migrated from loose to packed.
476 if (status == MISSING_OBJECT) {
477 reprepare_packed_git(r);
478 find_short_object_filename(&ds);
479 find_short_packed_object(&ds);
480 status = finish_object_disambiguation(&ds, oid);
483 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
484 struct oid_array collect = OID_ARRAY_INIT;
486 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
489 * We may still have ambiguity if we simply saw a series of
490 * candidates that did not satisfy our hint function. In
491 * that case, we still want to show them, so disable the hint
492 * function entirely.
494 if (!ds.ambiguous)
495 ds.fn = NULL;
497 advise(_("The candidates are:"));
498 repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect);
499 sort_ambiguous_oid_array(r, &collect);
501 if (oid_array_for_each(&collect, show_ambiguous_object, &ds))
502 BUG("show_ambiguous_object shouldn't return non-zero");
503 oid_array_clear(&collect);
506 return status;
509 int repo_for_each_abbrev(struct repository *r, const char *prefix,
510 each_abbrev_fn fn, void *cb_data)
512 struct oid_array collect = OID_ARRAY_INIT;
513 struct disambiguate_state ds;
514 int ret;
516 if (init_object_disambiguation(r, prefix, strlen(prefix), &ds) < 0)
517 return -1;
519 ds.always_call_fn = 1;
520 ds.fn = repo_collect_ambiguous;
521 ds.cb_data = &collect;
522 find_short_object_filename(&ds);
523 find_short_packed_object(&ds);
525 ret = oid_array_for_each_unique(&collect, fn, cb_data);
526 oid_array_clear(&collect);
527 return ret;
531 * Return the slot of the most-significant bit set in "val". There are various
532 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
533 * probably not a big deal here.
535 static unsigned msb(unsigned long val)
537 unsigned r = 0;
538 while (val >>= 1)
539 r++;
540 return r;
543 struct min_abbrev_data {
544 unsigned int init_len;
545 unsigned int cur_len;
546 char *hex;
547 struct repository *repo;
548 const struct object_id *oid;
551 static inline char get_hex_char_from_oid(const struct object_id *oid,
552 unsigned int pos)
554 static const char hex[] = "0123456789abcdef";
556 if ((pos & 1) == 0)
557 return hex[oid->hash[pos >> 1] >> 4];
558 else
559 return hex[oid->hash[pos >> 1] & 0xf];
562 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
564 struct min_abbrev_data *mad = cb_data;
566 unsigned int i = mad->init_len;
567 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
568 i++;
570 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
571 mad->cur_len = i + 1;
573 return 0;
576 static int repo_extend_abbrev_len(struct repository *r,
577 const struct object_id *oid,
578 void *cb_data)
580 return extend_abbrev_len(oid, cb_data);
583 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
584 struct min_abbrev_data *mad)
586 int match = 0;
587 uint32_t num, first = 0;
588 struct object_id oid;
589 const struct object_id *mad_oid;
591 if (!m->num_objects)
592 return;
594 num = m->num_objects;
595 mad_oid = mad->oid;
596 match = bsearch_midx(mad_oid, m, &first);
599 * first is now the position in the packfile where we would insert
600 * mad->hash if it does not exist (or the position of mad->hash if
601 * it does exist). Hence, we consider a maximum of two objects
602 * nearby for the abbreviation length.
604 mad->init_len = 0;
605 if (!match) {
606 if (nth_midxed_object_oid(&oid, m, first))
607 extend_abbrev_len(&oid, mad);
608 } else if (first < num - 1) {
609 if (nth_midxed_object_oid(&oid, m, first + 1))
610 extend_abbrev_len(&oid, mad);
612 if (first > 0) {
613 if (nth_midxed_object_oid(&oid, m, first - 1))
614 extend_abbrev_len(&oid, mad);
616 mad->init_len = mad->cur_len;
619 static void find_abbrev_len_for_pack(struct packed_git *p,
620 struct min_abbrev_data *mad)
622 int match = 0;
623 uint32_t num, first = 0;
624 struct object_id oid;
625 const struct object_id *mad_oid;
627 if (p->multi_pack_index)
628 return;
630 if (open_pack_index(p) || !p->num_objects)
631 return;
633 num = p->num_objects;
634 mad_oid = mad->oid;
635 match = bsearch_pack(mad_oid, p, &first);
638 * first is now the position in the packfile where we would insert
639 * mad->hash if it does not exist (or the position of mad->hash if
640 * it does exist). Hence, we consider a maximum of two objects
641 * nearby for the abbreviation length.
643 mad->init_len = 0;
644 if (!match) {
645 if (!nth_packed_object_id(&oid, p, first))
646 extend_abbrev_len(&oid, mad);
647 } else if (first < num - 1) {
648 if (!nth_packed_object_id(&oid, p, first + 1))
649 extend_abbrev_len(&oid, mad);
651 if (first > 0) {
652 if (!nth_packed_object_id(&oid, p, first - 1))
653 extend_abbrev_len(&oid, mad);
655 mad->init_len = mad->cur_len;
658 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
660 struct multi_pack_index *m;
661 struct packed_git *p;
663 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
664 find_abbrev_len_for_midx(m, mad);
665 for (p = get_packed_git(mad->repo); p; p = p->next)
666 find_abbrev_len_for_pack(p, mad);
669 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
670 const struct object_id *oid, int len)
672 struct disambiguate_state ds;
673 struct min_abbrev_data mad;
674 struct object_id oid_ret;
675 const unsigned hexsz = r->hash_algo->hexsz;
677 if (len < 0) {
678 unsigned long count = repo_approximate_object_count(r);
680 * Add one because the MSB only tells us the highest bit set,
681 * not including the value of all the _other_ bits (so "15"
682 * is only one off of 2^4, but the MSB is the 3rd bit.
684 len = msb(count) + 1;
686 * We now know we have on the order of 2^len objects, which
687 * expects a collision at 2^(len/2). But we also care about hex
688 * chars, not bits, and there are 4 bits per hex. So all
689 * together we need to divide by 2 and round up.
691 len = DIV_ROUND_UP(len, 2);
693 * For very small repos, we stick with our regular fallback.
695 if (len < FALLBACK_DEFAULT_ABBREV)
696 len = FALLBACK_DEFAULT_ABBREV;
699 oid_to_hex_r(hex, oid);
700 if (len == hexsz || !len)
701 return hexsz;
703 mad.repo = r;
704 mad.init_len = len;
705 mad.cur_len = len;
706 mad.hex = hex;
707 mad.oid = oid;
709 find_abbrev_len_packed(&mad);
711 if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0)
712 return -1;
714 ds.fn = repo_extend_abbrev_len;
715 ds.always_call_fn = 1;
716 ds.cb_data = (void *)&mad;
718 find_short_object_filename(&ds);
719 (void)finish_object_disambiguation(&ds, &oid_ret);
721 hex[mad.cur_len] = 0;
722 return mad.cur_len;
725 const char *repo_find_unique_abbrev(struct repository *r,
726 const struct object_id *oid,
727 int len)
729 static int bufno;
730 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
731 char *hex = hexbuffer[bufno];
732 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
733 repo_find_unique_abbrev_r(r, hex, oid, len);
734 return hex;
737 static int ambiguous_path(const char *path, int len)
739 int slash = 1;
740 int cnt;
742 for (cnt = 0; cnt < len; cnt++) {
743 switch (*path++) {
744 case '\0':
745 break;
746 case '/':
747 if (slash)
748 break;
749 slash = 1;
750 continue;
751 case '.':
752 continue;
753 default:
754 slash = 0;
755 continue;
757 break;
759 return slash;
762 static inline int at_mark(const char *string, int len,
763 const char **suffix, int nr)
765 int i;
767 for (i = 0; i < nr; i++) {
768 int suffix_len = strlen(suffix[i]);
769 if (suffix_len <= len
770 && !strncasecmp(string, suffix[i], suffix_len))
771 return suffix_len;
773 return 0;
776 static inline int upstream_mark(const char *string, int len)
778 const char *suffix[] = { "@{upstream}", "@{u}" };
779 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
782 static inline int push_mark(const char *string, int len)
784 const char *suffix[] = { "@{push}" };
785 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
788 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
789 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
791 static int get_oid_basic(struct repository *r, const char *str, int len,
792 struct object_id *oid, unsigned int flags)
794 static const char *warn_msg = "refname '%.*s' is ambiguous.";
795 static const char *object_name_msg = N_(
796 "Git normally never creates a ref that ends with 40 hex characters\n"
797 "because it will be ignored when you just specify 40-hex. These refs\n"
798 "may be created by mistake. For example,\n"
799 "\n"
800 " git switch -c $br $(git rev-parse ...)\n"
801 "\n"
802 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
803 "examine these refs and maybe delete them. Turn this message off by\n"
804 "running \"git config advice.objectNameWarning false\"");
805 struct object_id tmp_oid;
806 char *real_ref = NULL;
807 int refs_found = 0;
808 int at, reflog_len, nth_prior = 0;
810 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
811 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
812 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
813 if (refs_found > 0) {
814 warning(warn_msg, len, str);
815 if (advice_object_name_warning)
816 fprintf(stderr, "%s\n", _(object_name_msg));
818 free(real_ref);
820 return 0;
823 /* basic@{time or number or -number} format to query ref-log */
824 reflog_len = at = 0;
825 if (len && str[len-1] == '}') {
826 for (at = len-4; at >= 0; at--) {
827 if (str[at] == '@' && str[at+1] == '{') {
828 if (str[at+2] == '-') {
829 if (at != 0)
830 /* @{-N} not at start */
831 return -1;
832 nth_prior = 1;
833 continue;
835 if (!upstream_mark(str + at, len - at) &&
836 !push_mark(str + at, len - at)) {
837 reflog_len = (len-1) - (at+2);
838 len = at;
840 break;
845 /* Accept only unambiguous ref paths. */
846 if (len && ambiguous_path(str, len))
847 return -1;
849 if (nth_prior) {
850 struct strbuf buf = STRBUF_INIT;
851 int detached;
853 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
854 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
855 strbuf_release(&buf);
856 if (detached)
857 return 0;
861 if (!len && reflog_len)
862 /* allow "@{...}" to mean the current branch reflog */
863 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, 0);
864 else if (reflog_len)
865 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
866 else
867 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, 0);
869 if (!refs_found)
870 return -1;
872 if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
873 (refs_found > 1 ||
874 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
875 warning(warn_msg, len, str);
877 if (reflog_len) {
878 int nth, i;
879 timestamp_t at_time;
880 timestamp_t co_time;
881 int co_tz, co_cnt;
883 /* Is it asking for N-th entry, or approxidate? */
884 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
885 char ch = str[at+2+i];
886 if ('0' <= ch && ch <= '9')
887 nth = nth * 10 + ch - '0';
888 else
889 nth = -1;
891 if (100000000 <= nth) {
892 at_time = nth;
893 nth = -1;
894 } else if (0 <= nth)
895 at_time = 0;
896 else {
897 int errors = 0;
898 char *tmp = xstrndup(str + at + 2, reflog_len);
899 at_time = approxidate_careful(tmp, &errors);
900 free(tmp);
901 if (errors) {
902 free(real_ref);
903 return -1;
906 if (read_ref_at(get_main_ref_store(r),
907 real_ref, flags, at_time, nth, oid, NULL,
908 &co_time, &co_tz, &co_cnt)) {
909 if (!len) {
910 if (!skip_prefix(real_ref, "refs/heads/", &str))
911 str = "HEAD";
912 len = strlen(str);
914 if (at_time) {
915 if (!(flags & GET_OID_QUIETLY)) {
916 warning(_("log for '%.*s' only goes back to %s"),
917 len, str,
918 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
920 } else {
921 if (flags & GET_OID_QUIETLY) {
922 exit(128);
924 die(_("log for '%.*s' only has %d entries"),
925 len, str, co_cnt);
930 free(real_ref);
931 return 0;
934 static enum get_oid_result get_parent(struct repository *r,
935 const char *name, int len,
936 struct object_id *result, int idx)
938 struct object_id oid;
939 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
940 GET_OID_COMMITTISH);
941 struct commit *commit;
942 struct commit_list *p;
944 if (ret)
945 return ret;
946 commit = lookup_commit_reference(r, &oid);
947 if (parse_commit(commit))
948 return MISSING_OBJECT;
949 if (!idx) {
950 oidcpy(result, &commit->object.oid);
951 return FOUND;
953 p = commit->parents;
954 while (p) {
955 if (!--idx) {
956 oidcpy(result, &p->item->object.oid);
957 return FOUND;
959 p = p->next;
961 return MISSING_OBJECT;
964 static enum get_oid_result get_nth_ancestor(struct repository *r,
965 const char *name, int len,
966 struct object_id *result,
967 int generation)
969 struct object_id oid;
970 struct commit *commit;
971 int ret;
973 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
974 if (ret)
975 return ret;
976 commit = lookup_commit_reference(r, &oid);
977 if (!commit)
978 return MISSING_OBJECT;
980 while (generation--) {
981 if (parse_commit(commit) || !commit->parents)
982 return MISSING_OBJECT;
983 commit = commit->parents->item;
985 oidcpy(result, &commit->object.oid);
986 return FOUND;
989 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
990 struct object *o, enum object_type expected_type)
992 if (name && !namelen)
993 namelen = strlen(name);
994 while (1) {
995 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
996 return NULL;
997 if (expected_type == OBJ_ANY || o->type == expected_type)
998 return o;
999 if (o->type == OBJ_TAG)
1000 o = ((struct tag*) o)->tagged;
1001 else if (o->type == OBJ_COMMIT)
1002 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1003 else {
1004 if (name)
1005 error("%.*s: expected %s type, but the object "
1006 "dereferences to %s type",
1007 namelen, name, type_name(expected_type),
1008 type_name(o->type));
1009 return NULL;
1014 static int peel_onion(struct repository *r, const char *name, int len,
1015 struct object_id *oid, unsigned lookup_flags)
1017 struct object_id outer;
1018 const char *sp;
1019 unsigned int expected_type = 0;
1020 struct object *o;
1023 * "ref^{type}" dereferences ref repeatedly until you cannot
1024 * dereference anymore, or you get an object of given type,
1025 * whichever comes first. "ref^{}" means just dereference
1026 * tags until you get a non-tag. "ref^0" is a shorthand for
1027 * "ref^{commit}". "commit^{tree}" could be used to find the
1028 * top-level tree of the given commit.
1030 if (len < 4 || name[len-1] != '}')
1031 return -1;
1033 for (sp = name + len - 1; name <= sp; sp--) {
1034 int ch = *sp;
1035 if (ch == '{' && name < sp && sp[-1] == '^')
1036 break;
1038 if (sp <= name)
1039 return -1;
1041 sp++; /* beginning of type name, or closing brace for empty */
1042 if (starts_with(sp, "commit}"))
1043 expected_type = OBJ_COMMIT;
1044 else if (starts_with(sp, "tag}"))
1045 expected_type = OBJ_TAG;
1046 else if (starts_with(sp, "tree}"))
1047 expected_type = OBJ_TREE;
1048 else if (starts_with(sp, "blob}"))
1049 expected_type = OBJ_BLOB;
1050 else if (starts_with(sp, "object}"))
1051 expected_type = OBJ_ANY;
1052 else if (sp[0] == '}')
1053 expected_type = OBJ_NONE;
1054 else if (sp[0] == '/')
1055 expected_type = OBJ_COMMIT;
1056 else
1057 return -1;
1059 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1060 if (expected_type == OBJ_COMMIT)
1061 lookup_flags |= GET_OID_COMMITTISH;
1062 else if (expected_type == OBJ_TREE)
1063 lookup_flags |= GET_OID_TREEISH;
1065 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1066 return -1;
1068 o = parse_object(r, &outer);
1069 if (!o)
1070 return -1;
1071 if (!expected_type) {
1072 o = deref_tag(r, o, name, sp - name - 2);
1073 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1074 return -1;
1075 oidcpy(oid, &o->oid);
1076 return 0;
1080 * At this point, the syntax look correct, so
1081 * if we do not get the needed object, we should
1082 * barf.
1084 o = repo_peel_to_type(r, name, len, o, expected_type);
1085 if (!o)
1086 return -1;
1088 oidcpy(oid, &o->oid);
1089 if (sp[0] == '/') {
1090 /* "$commit^{/foo}" */
1091 char *prefix;
1092 int ret;
1093 struct commit_list *list = NULL;
1096 * $commit^{/}. Some regex implementation may reject.
1097 * We don't need regex anyway. '' pattern always matches.
1099 if (sp[1] == '}')
1100 return 0;
1102 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1103 commit_list_insert((struct commit *)o, &list);
1104 ret = get_oid_oneline(r, prefix, oid, list);
1105 free(prefix);
1106 return ret;
1108 return 0;
1111 static int get_describe_name(struct repository *r,
1112 const char *name, int len,
1113 struct object_id *oid)
1115 const char *cp;
1116 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1118 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1119 char ch = *cp;
1120 if (!isxdigit(ch)) {
1121 /* We must be looking at g in "SOMETHING-g"
1122 * for it to be describe output.
1124 if (ch == 'g' && cp[-1] == '-') {
1125 cp++;
1126 len -= cp - name;
1127 return get_short_oid(r,
1128 cp, len, oid, flags);
1132 return -1;
1135 static enum get_oid_result get_oid_1(struct repository *r,
1136 const char *name, int len,
1137 struct object_id *oid,
1138 unsigned lookup_flags)
1140 int ret, has_suffix;
1141 const char *cp;
1144 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1146 has_suffix = 0;
1147 for (cp = name + len - 1; name <= cp; cp--) {
1148 int ch = *cp;
1149 if ('0' <= ch && ch <= '9')
1150 continue;
1151 if (ch == '~' || ch == '^')
1152 has_suffix = ch;
1153 break;
1156 if (has_suffix) {
1157 unsigned int num = 0;
1158 int len1 = cp - name;
1159 cp++;
1160 while (cp < name + len) {
1161 unsigned int digit = *cp++ - '0';
1162 if (unsigned_mult_overflows(num, 10))
1163 return MISSING_OBJECT;
1164 num *= 10;
1165 if (unsigned_add_overflows(num, digit))
1166 return MISSING_OBJECT;
1167 num += digit;
1169 if (!num && len1 == len - 1)
1170 num = 1;
1171 else if (num > INT_MAX)
1172 return MISSING_OBJECT;
1173 if (has_suffix == '^')
1174 return get_parent(r, name, len1, oid, num);
1175 /* else if (has_suffix == '~') -- goes without saying */
1176 return get_nth_ancestor(r, name, len1, oid, num);
1179 ret = peel_onion(r, name, len, oid, lookup_flags);
1180 if (!ret)
1181 return FOUND;
1183 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1184 if (!ret)
1185 return FOUND;
1187 /* It could be describe output that is "SOMETHING-gXXXX" */
1188 ret = get_describe_name(r, name, len, oid);
1189 if (!ret)
1190 return FOUND;
1192 return get_short_oid(r, name, len, oid, lookup_flags);
1196 * This interprets names like ':/Initial revision of "git"' by searching
1197 * through history and returning the first commit whose message starts
1198 * the given regular expression.
1200 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1202 * For a literal '!' character at the beginning of a pattern, you have to repeat
1203 * that, like: ':/!!foo'
1205 * For future extension, all other sequences beginning with ':/!' are reserved.
1208 /* Remember to update object flag allocation in object.h */
1209 #define ONELINE_SEEN (1u<<20)
1211 struct handle_one_ref_cb {
1212 struct repository *repo;
1213 struct commit_list **list;
1216 static int handle_one_ref(const char *path, const struct object_id *oid,
1217 int flag, void *cb_data)
1219 struct handle_one_ref_cb *cb = cb_data;
1220 struct commit_list **list = cb->list;
1221 struct object *object = parse_object(cb->repo, oid);
1222 if (!object)
1223 return 0;
1224 if (object->type == OBJ_TAG) {
1225 object = deref_tag(cb->repo, object, path,
1226 strlen(path));
1227 if (!object)
1228 return 0;
1230 if (object->type != OBJ_COMMIT)
1231 return 0;
1232 commit_list_insert((struct commit *)object, list);
1233 return 0;
1236 static int get_oid_oneline(struct repository *r,
1237 const char *prefix, struct object_id *oid,
1238 struct commit_list *list)
1240 struct commit_list *backup = NULL, *l;
1241 int found = 0;
1242 int negative = 0;
1243 regex_t regex;
1245 if (prefix[0] == '!') {
1246 prefix++;
1248 if (prefix[0] == '-') {
1249 prefix++;
1250 negative = 1;
1251 } else if (prefix[0] != '!') {
1252 return -1;
1256 if (regcomp(&regex, prefix, REG_EXTENDED))
1257 return -1;
1259 for (l = list; l; l = l->next) {
1260 l->item->object.flags |= ONELINE_SEEN;
1261 commit_list_insert(l->item, &backup);
1263 while (list) {
1264 const char *p, *buf;
1265 struct commit *commit;
1266 int matches;
1268 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1269 if (!parse_object(r, &commit->object.oid))
1270 continue;
1271 buf = get_commit_buffer(commit, NULL);
1272 p = strstr(buf, "\n\n");
1273 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1274 unuse_commit_buffer(commit, buf);
1276 if (matches) {
1277 oidcpy(oid, &commit->object.oid);
1278 found = 1;
1279 break;
1282 regfree(&regex);
1283 free_commit_list(list);
1284 for (l = backup; l; l = l->next)
1285 clear_commit_marks(l->item, ONELINE_SEEN);
1286 free_commit_list(backup);
1287 return found ? 0 : -1;
1290 struct grab_nth_branch_switch_cbdata {
1291 int remaining;
1292 struct strbuf *sb;
1295 static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
1296 const char *email, timestamp_t timestamp, int tz,
1297 const char *message, void *cb_data)
1299 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1300 const char *match = NULL, *target = NULL;
1301 size_t len;
1303 if (skip_prefix(message, "checkout: moving from ", &match))
1304 target = strstr(match, " to ");
1306 if (!match || !target)
1307 return 0;
1308 if (--(cb->remaining) == 0) {
1309 len = target - match;
1310 strbuf_reset(cb->sb);
1311 strbuf_add(cb->sb, match, len);
1312 return 1; /* we are done */
1314 return 0;
1318 * Parse @{-N} syntax, return the number of characters parsed
1319 * if successful; otherwise signal an error with negative value.
1321 static int interpret_nth_prior_checkout(struct repository *r,
1322 const char *name, int namelen,
1323 struct strbuf *buf)
1325 long nth;
1326 int retval;
1327 struct grab_nth_branch_switch_cbdata cb;
1328 const char *brace;
1329 char *num_end;
1331 if (namelen < 4)
1332 return -1;
1333 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1334 return -1;
1335 brace = memchr(name, '}', namelen);
1336 if (!brace)
1337 return -1;
1338 nth = strtol(name + 3, &num_end, 10);
1339 if (num_end != brace)
1340 return -1;
1341 if (nth <= 0)
1342 return -1;
1343 cb.remaining = nth;
1344 cb.sb = buf;
1346 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1347 "HEAD", grab_nth_branch_switch, &cb);
1348 if (0 < retval) {
1349 retval = brace - name + 1;
1350 } else
1351 retval = 0;
1353 return retval;
1356 int repo_get_oid_mb(struct repository *r,
1357 const char *name,
1358 struct object_id *oid)
1360 struct commit *one, *two;
1361 struct commit_list *mbs;
1362 struct object_id oid_tmp;
1363 const char *dots;
1364 int st;
1366 dots = strstr(name, "...");
1367 if (!dots)
1368 return repo_get_oid(r, name, oid);
1369 if (dots == name)
1370 st = repo_get_oid(r, "HEAD", &oid_tmp);
1371 else {
1372 struct strbuf sb;
1373 strbuf_init(&sb, dots - name);
1374 strbuf_add(&sb, name, dots - name);
1375 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1376 strbuf_release(&sb);
1378 if (st)
1379 return st;
1380 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1381 if (!one)
1382 return -1;
1384 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1385 return -1;
1386 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1387 if (!two)
1388 return -1;
1389 mbs = repo_get_merge_bases(r, one, two);
1390 if (!mbs || mbs->next)
1391 st = -1;
1392 else {
1393 st = 0;
1394 oidcpy(oid, &mbs->item->object.oid);
1396 free_commit_list(mbs);
1397 return st;
1400 /* parse @something syntax, when 'something' is not {.*} */
1401 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1403 const char *next;
1405 if (len || name[1] == '{')
1406 return -1;
1408 /* make sure it's a single @, or @@{.*}, not @foo */
1409 next = memchr(name + len + 1, '@', namelen - len - 1);
1410 if (next && next[1] != '{')
1411 return -1;
1412 if (!next)
1413 next = name + namelen;
1414 if (next != name + 1)
1415 return -1;
1417 strbuf_reset(buf);
1418 strbuf_add(buf, "HEAD", 4);
1419 return 1;
1422 static int reinterpret(struct repository *r,
1423 const char *name, int namelen, int len,
1424 struct strbuf *buf, unsigned allowed)
1426 /* we have extra data, which might need further processing */
1427 struct strbuf tmp = STRBUF_INIT;
1428 int used = buf->len;
1429 int ret;
1430 struct interpret_branch_name_options options = {
1431 .allowed = allowed
1434 strbuf_add(buf, name + len, namelen - len);
1435 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1436 /* that data was not interpreted, remove our cruft */
1437 if (ret < 0) {
1438 strbuf_setlen(buf, used);
1439 return len;
1441 strbuf_reset(buf);
1442 strbuf_addbuf(buf, &tmp);
1443 strbuf_release(&tmp);
1444 /* tweak for size of {-N} versus expanded ref name */
1445 return ret - used + len;
1448 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1450 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1451 strbuf_reset(buf);
1452 strbuf_addstr(buf, s);
1453 free(s);
1456 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1458 if (!allowed)
1459 return 1;
1461 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1462 starts_with(refname, "refs/heads/"))
1463 return 1;
1464 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1465 starts_with(refname, "refs/remotes/"))
1466 return 1;
1468 return 0;
1471 static int interpret_branch_mark(struct repository *r,
1472 const char *name, int namelen,
1473 int at, struct strbuf *buf,
1474 int (*get_mark)(const char *, int),
1475 const char *(*get_data)(struct branch *,
1476 struct strbuf *),
1477 const struct interpret_branch_name_options *options)
1479 int len;
1480 struct branch *branch;
1481 struct strbuf err = STRBUF_INIT;
1482 const char *value;
1484 len = get_mark(name + at, namelen - at);
1485 if (!len)
1486 return -1;
1488 if (memchr(name, ':', at))
1489 return -1;
1491 if (at) {
1492 char *name_str = xmemdupz(name, at);
1493 branch = branch_get(name_str);
1494 free(name_str);
1495 } else
1496 branch = branch_get(NULL);
1498 value = get_data(branch, &err);
1499 if (!value) {
1500 if (options->nonfatal_dangling_mark) {
1501 strbuf_release(&err);
1502 return -1;
1503 } else {
1504 die("%s", err.buf);
1508 if (!branch_interpret_allowed(value, options->allowed))
1509 return -1;
1511 set_shortened_ref(r, buf, value);
1512 return len + at;
1515 int repo_interpret_branch_name(struct repository *r,
1516 const char *name, int namelen,
1517 struct strbuf *buf,
1518 const struct interpret_branch_name_options *options)
1520 char *at;
1521 const char *start;
1522 int len;
1524 if (!namelen)
1525 namelen = strlen(name);
1527 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1528 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1529 if (!len) {
1530 return len; /* syntax Ok, not enough switches */
1531 } else if (len > 0) {
1532 if (len == namelen)
1533 return len; /* consumed all */
1534 else
1535 return reinterpret(r, name, namelen, len, buf,
1536 options->allowed);
1540 for (start = name;
1541 (at = memchr(start, '@', namelen - (start - name)));
1542 start = at + 1) {
1544 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1545 len = interpret_empty_at(name, namelen, at - name, buf);
1546 if (len > 0)
1547 return reinterpret(r, name, namelen, len, buf,
1548 options->allowed);
1551 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1552 upstream_mark, branch_get_upstream,
1553 options);
1554 if (len > 0)
1555 return len;
1557 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1558 push_mark, branch_get_push,
1559 options);
1560 if (len > 0)
1561 return len;
1564 return -1;
1567 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1569 int len = strlen(name);
1570 struct interpret_branch_name_options options = {
1571 .allowed = allowed
1573 int used = interpret_branch_name(name, len, sb, &options);
1575 if (used < 0)
1576 used = 0;
1577 strbuf_add(sb, name + used, len - used);
1580 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1582 if (startup_info->have_repository)
1583 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1584 else
1585 strbuf_addstr(sb, name);
1588 * This splice must be done even if we end up rejecting the
1589 * name; builtin/branch.c::copy_or_rename_branch() still wants
1590 * to see what the name expanded to so that "branch -m" can be
1591 * used as a tool to correct earlier mistakes.
1593 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1595 if (*name == '-' ||
1596 !strcmp(sb->buf, "refs/heads/HEAD"))
1597 return -1;
1599 return check_refname_format(sb->buf, 0);
1603 * This is like "get_oid_basic()", except it allows "object ID expressions",
1604 * notably "xyz^" for "parent of xyz"
1606 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1608 struct object_context unused;
1609 return get_oid_with_context(r, name, 0, oid, &unused);
1613 * This returns a non-zero value if the string (built using printf
1614 * format and the given arguments) is not a valid object.
1616 int get_oidf(struct object_id *oid, const char *fmt, ...)
1618 va_list ap;
1619 int ret;
1620 struct strbuf sb = STRBUF_INIT;
1622 va_start(ap, fmt);
1623 strbuf_vaddf(&sb, fmt, ap);
1624 va_end(ap);
1626 ret = get_oid(sb.buf, oid);
1627 strbuf_release(&sb);
1629 return ret;
1633 * Many callers know that the user meant to name a commit-ish by
1634 * syntactical positions where the object name appears. Calling this
1635 * function allows the machinery to disambiguate shorter-than-unique
1636 * abbreviated object names between commit-ish and others.
1638 * Note that this does NOT error out when the named object is not a
1639 * commit-ish. It is merely to give a hint to the disambiguation
1640 * machinery.
1642 int repo_get_oid_committish(struct repository *r,
1643 const char *name,
1644 struct object_id *oid)
1646 struct object_context unused;
1647 return get_oid_with_context(r, name, GET_OID_COMMITTISH,
1648 oid, &unused);
1651 int repo_get_oid_treeish(struct repository *r,
1652 const char *name,
1653 struct object_id *oid)
1655 struct object_context unused;
1656 return get_oid_with_context(r, name, GET_OID_TREEISH,
1657 oid, &unused);
1660 int repo_get_oid_commit(struct repository *r,
1661 const char *name,
1662 struct object_id *oid)
1664 struct object_context unused;
1665 return get_oid_with_context(r, name, GET_OID_COMMIT,
1666 oid, &unused);
1669 int repo_get_oid_tree(struct repository *r,
1670 const char *name,
1671 struct object_id *oid)
1673 struct object_context unused;
1674 return get_oid_with_context(r, name, GET_OID_TREE,
1675 oid, &unused);
1678 int repo_get_oid_blob(struct repository *r,
1679 const char *name,
1680 struct object_id *oid)
1682 struct object_context unused;
1683 return get_oid_with_context(r, name, GET_OID_BLOB,
1684 oid, &unused);
1687 /* Must be called only when object_name:filename doesn't exist. */
1688 static void diagnose_invalid_oid_path(struct repository *r,
1689 const char *prefix,
1690 const char *filename,
1691 const struct object_id *tree_oid,
1692 const char *object_name,
1693 int object_name_len)
1695 struct object_id oid;
1696 unsigned short mode;
1698 if (!prefix)
1699 prefix = "";
1701 if (file_exists(filename))
1702 die(_("path '%s' exists on disk, but not in '%.*s'"),
1703 filename, object_name_len, object_name);
1704 if (is_missing_file_error(errno)) {
1705 char *fullname = xstrfmt("%s%s", prefix, filename);
1707 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1708 die(_("path '%s' exists, but not '%s'\n"
1709 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1710 fullname,
1711 filename,
1712 object_name_len, object_name,
1713 fullname,
1714 object_name_len, object_name,
1715 filename);
1717 die(_("path '%s' does not exist in '%.*s'"),
1718 filename, object_name_len, object_name);
1722 /* Must be called only when :stage:filename doesn't exist. */
1723 static void diagnose_invalid_index_path(struct repository *r,
1724 int stage,
1725 const char *prefix,
1726 const char *filename)
1728 struct index_state *istate = r->index;
1729 const struct cache_entry *ce;
1730 int pos;
1731 unsigned namelen = strlen(filename);
1732 struct strbuf fullname = STRBUF_INIT;
1734 if (!prefix)
1735 prefix = "";
1737 /* Wrong stage number? */
1738 pos = index_name_pos(istate, filename, namelen);
1739 if (pos < 0)
1740 pos = -pos - 1;
1741 if (pos < istate->cache_nr) {
1742 ce = istate->cache[pos];
1743 if (ce_namelen(ce) == namelen &&
1744 !memcmp(ce->name, filename, namelen))
1745 die(_("path '%s' is in the index, but not at stage %d\n"
1746 "hint: Did you mean ':%d:%s'?"),
1747 filename, stage,
1748 ce_stage(ce), filename);
1751 /* Confusion between relative and absolute filenames? */
1752 strbuf_addstr(&fullname, prefix);
1753 strbuf_addstr(&fullname, filename);
1754 pos = index_name_pos(istate, fullname.buf, fullname.len);
1755 if (pos < 0)
1756 pos = -pos - 1;
1757 if (pos < istate->cache_nr) {
1758 ce = istate->cache[pos];
1759 if (ce_namelen(ce) == fullname.len &&
1760 !memcmp(ce->name, fullname.buf, fullname.len))
1761 die(_("path '%s' is in the index, but not '%s'\n"
1762 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1763 fullname.buf, filename,
1764 ce_stage(ce), fullname.buf,
1765 ce_stage(ce), filename);
1768 if (repo_file_exists(r, filename))
1769 die(_("path '%s' exists on disk, but not in the index"), filename);
1770 if (is_missing_file_error(errno))
1771 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1772 filename);
1774 strbuf_release(&fullname);
1778 static char *resolve_relative_path(struct repository *r, const char *rel)
1780 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1781 return NULL;
1783 if (r != the_repository || !is_inside_work_tree())
1784 die(_("relative path syntax can't be used outside working tree"));
1786 /* die() inside prefix_path() if resolved path is outside worktree */
1787 return prefix_path(startup_info->prefix,
1788 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1789 rel);
1792 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1793 const char *name,
1794 unsigned flags,
1795 const char *prefix,
1796 struct object_id *oid,
1797 struct object_context *oc)
1799 int ret, bracket_depth;
1800 int namelen = strlen(name);
1801 const char *cp;
1802 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
1804 if (only_to_die)
1805 flags |= GET_OID_QUIETLY;
1807 memset(oc, 0, sizeof(*oc));
1808 oc->mode = S_IFINVALID;
1809 strbuf_init(&oc->symlink_path, 0);
1810 ret = get_oid_1(repo, name, namelen, oid, flags);
1811 if (!ret)
1812 return ret;
1814 * tree:path --> object name of path in tree
1815 * :path -> object name of absolute path in index
1816 * :./path -> object name of path relative to cwd in index
1817 * :[0-3]:path -> object name of path in index at stage
1818 * :/foo -> recent commit matching foo
1820 if (name[0] == ':') {
1821 int stage = 0;
1822 const struct cache_entry *ce;
1823 char *new_path = NULL;
1824 int pos;
1825 if (!only_to_die && namelen > 2 && name[1] == '/') {
1826 struct handle_one_ref_cb cb;
1827 struct commit_list *list = NULL;
1829 cb.repo = repo;
1830 cb.list = &list;
1831 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
1832 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
1833 commit_list_sort_by_date(&list);
1834 return get_oid_oneline(repo, name + 2, oid, list);
1836 if (namelen < 3 ||
1837 name[2] != ':' ||
1838 name[1] < '0' || '3' < name[1])
1839 cp = name + 1;
1840 else {
1841 stage = name[1] - '0';
1842 cp = name + 3;
1844 new_path = resolve_relative_path(repo, cp);
1845 if (!new_path) {
1846 namelen = namelen - (cp - name);
1847 } else {
1848 cp = new_path;
1849 namelen = strlen(cp);
1852 if (flags & GET_OID_RECORD_PATH)
1853 oc->path = xstrdup(cp);
1855 if (!repo->index || !repo->index->cache)
1856 repo_read_index(repo);
1857 pos = index_name_pos(repo->index, cp, namelen);
1858 if (pos < 0)
1859 pos = -pos - 1;
1860 while (pos < repo->index->cache_nr) {
1861 ce = repo->index->cache[pos];
1862 if (ce_namelen(ce) != namelen ||
1863 memcmp(ce->name, cp, namelen))
1864 break;
1865 if (ce_stage(ce) == stage) {
1866 oidcpy(oid, &ce->oid);
1867 oc->mode = ce->ce_mode;
1868 free(new_path);
1869 return 0;
1871 pos++;
1873 if (only_to_die && name[1] && name[1] != '/')
1874 diagnose_invalid_index_path(repo, stage, prefix, cp);
1875 free(new_path);
1876 return -1;
1878 for (cp = name, bracket_depth = 0; *cp; cp++) {
1879 if (*cp == '{')
1880 bracket_depth++;
1881 else if (bracket_depth && *cp == '}')
1882 bracket_depth--;
1883 else if (!bracket_depth && *cp == ':')
1884 break;
1886 if (*cp == ':') {
1887 struct object_id tree_oid;
1888 int len = cp - name;
1889 unsigned sub_flags = flags;
1891 sub_flags &= ~GET_OID_DISAMBIGUATORS;
1892 sub_flags |= GET_OID_TREEISH;
1894 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
1895 const char *filename = cp+1;
1896 char *new_filename = NULL;
1898 new_filename = resolve_relative_path(repo, filename);
1899 if (new_filename)
1900 filename = new_filename;
1901 if (flags & GET_OID_FOLLOW_SYMLINKS) {
1902 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
1903 filename, oid, &oc->symlink_path,
1904 &oc->mode);
1905 } else {
1906 ret = get_tree_entry(repo, &tree_oid, filename, oid,
1907 &oc->mode);
1908 if (ret && only_to_die) {
1909 diagnose_invalid_oid_path(repo, prefix,
1910 filename,
1911 &tree_oid,
1912 name, len);
1915 if (flags & GET_OID_RECORD_PATH)
1916 oc->path = xstrdup(filename);
1918 free(new_filename);
1919 return ret;
1920 } else {
1921 if (only_to_die)
1922 die(_("invalid object name '%.*s'."), len, name);
1925 return ret;
1929 * Call this function when you know "name" given by the end user must
1930 * name an object but it doesn't; the function _may_ die with a better
1931 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1932 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1933 * you have a chance to diagnose the error further.
1935 void maybe_die_on_misspelt_object_name(struct repository *r,
1936 const char *name,
1937 const char *prefix)
1939 struct object_context oc;
1940 struct object_id oid;
1941 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE,
1942 prefix, &oid, &oc);
1945 enum get_oid_result get_oid_with_context(struct repository *repo,
1946 const char *str,
1947 unsigned flags,
1948 struct object_id *oid,
1949 struct object_context *oc)
1951 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
1952 BUG("incompatible flags for get_oid_with_context");
1953 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);