The fifth batch
[alt-git.git] / object-name.c
blob945d5bdef25217c43e9501eca5d0a25d820ddab1
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "object-name.h"
6 #include "advice.h"
7 #include "config.h"
8 #include "environment.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "tag.h"
12 #include "commit.h"
13 #include "tree.h"
14 #include "tree-walk.h"
15 #include "refs.h"
16 #include "remote.h"
17 #include "dir.h"
18 #include "oid-array.h"
19 #include "oidtree.h"
20 #include "packfile.h"
21 #include "pretty.h"
22 #include "object-store-ll.h"
23 #include "read-cache-ll.h"
24 #include "repo-settings.h"
25 #include "repository.h"
26 #include "setup.h"
27 #include "midx.h"
28 #include "commit-reach.h"
29 #include "date.h"
30 #include "object-file-convert.h"
32 static int get_oid_oneline(struct repository *r, const char *, struct object_id *,
33 const struct commit_list *);
35 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
37 struct disambiguate_state {
38 int len; /* length of prefix in hex chars */
39 char hex_pfx[GIT_MAX_HEXSZ + 1];
40 struct object_id bin_pfx;
42 struct repository *repo;
43 disambiguate_hint_fn fn;
44 void *cb_data;
45 struct object_id candidate;
46 unsigned candidate_exists:1;
47 unsigned candidate_checked:1;
48 unsigned candidate_ok:1;
49 unsigned disambiguate_fn_used:1;
50 unsigned ambiguous:1;
51 unsigned always_call_fn:1;
54 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
56 /* The hash algorithm of current has already been filtered */
57 if (ds->always_call_fn) {
58 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
59 return;
61 if (!ds->candidate_exists) {
62 /* this is the first candidate */
63 oidcpy(&ds->candidate, current);
64 ds->candidate_exists = 1;
65 return;
66 } else if (oideq(&ds->candidate, current)) {
67 /* the same as what we already have seen */
68 return;
71 if (!ds->fn) {
72 /* cannot disambiguate between ds->candidate and current */
73 ds->ambiguous = 1;
74 return;
77 if (!ds->candidate_checked) {
78 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
79 ds->disambiguate_fn_used = 1;
80 ds->candidate_checked = 1;
83 if (!ds->candidate_ok) {
84 /* discard the candidate; we know it does not satisfy fn */
85 oidcpy(&ds->candidate, current);
86 ds->candidate_checked = 0;
87 return;
90 /* if we reach this point, we know ds->candidate satisfies fn */
91 if (ds->fn(ds->repo, current, ds->cb_data)) {
93 * if both current and candidate satisfy fn, we cannot
94 * disambiguate.
96 ds->candidate_ok = 0;
97 ds->ambiguous = 1;
100 /* otherwise, current can be discarded and candidate is still good */
103 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
105 static enum cb_next match_prefix(const struct object_id *oid, void *arg)
107 struct disambiguate_state *ds = arg;
108 /* no need to call match_hash, oidtree_each did prefix match */
109 update_candidates(ds, oid);
110 return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
113 static void find_short_object_filename(struct disambiguate_state *ds)
115 struct object_directory *odb;
117 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next)
118 oidtree_each(odb_loose_cache(odb, &ds->bin_pfx),
119 &ds->bin_pfx, ds->len, match_prefix, ds);
122 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
124 do {
125 if (*a != *b)
126 return 0;
127 a++;
128 b++;
129 len -= 2;
130 } while (len > 1);
131 if (len)
132 if ((*a ^ *b) & 0xf0)
133 return 0;
134 return 1;
137 static void unique_in_midx(struct multi_pack_index *m,
138 struct disambiguate_state *ds)
140 for (; m; m = m->base_midx) {
141 uint32_t num, i, first = 0;
142 const struct object_id *current = NULL;
143 int len = ds->len > ds->repo->hash_algo->hexsz ?
144 ds->repo->hash_algo->hexsz : ds->len;
146 if (!m->num_objects)
147 continue;
149 num = m->num_objects + m->num_objects_in_base;
151 bsearch_one_midx(&ds->bin_pfx, m, &first);
154 * At this point, "first" is the location of the lowest
155 * object with an object name that could match
156 * "bin_pfx". See if we have 0, 1 or more objects that
157 * actually match(es).
159 for (i = first; i < num && !ds->ambiguous; i++) {
160 struct object_id oid;
161 current = nth_midxed_object_oid(&oid, m, i);
162 if (!match_hash(len, ds->bin_pfx.hash, current->hash))
163 break;
164 update_candidates(ds, current);
169 static void unique_in_pack(struct packed_git *p,
170 struct disambiguate_state *ds)
172 uint32_t num, i, first = 0;
173 int len = ds->len > ds->repo->hash_algo->hexsz ?
174 ds->repo->hash_algo->hexsz : ds->len;
176 if (p->multi_pack_index)
177 return;
179 if (open_pack_index(p) || !p->num_objects)
180 return;
182 num = p->num_objects;
183 bsearch_pack(&ds->bin_pfx, p, &first);
186 * At this point, "first" is the location of the lowest object
187 * with an object name that could match "bin_pfx". See if we have
188 * 0, 1 or more objects that actually match(es).
190 for (i = first; i < num && !ds->ambiguous; i++) {
191 struct object_id oid;
192 nth_packed_object_id(&oid, p, i);
193 if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
194 break;
195 update_candidates(ds, &oid);
199 static void find_short_packed_object(struct disambiguate_state *ds)
201 struct multi_pack_index *m;
202 struct packed_git *p;
204 /* Skip, unless oids from the storage hash algorithm are wanted */
205 if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
206 return;
208 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
209 m = m->next)
210 unique_in_midx(m, ds);
211 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
212 p = p->next)
213 unique_in_pack(p, ds);
216 static int finish_object_disambiguation(struct disambiguate_state *ds,
217 struct object_id *oid)
219 if (ds->ambiguous)
220 return SHORT_NAME_AMBIGUOUS;
222 if (!ds->candidate_exists)
223 return MISSING_OBJECT;
225 if (!ds->candidate_checked)
227 * If this is the only candidate, there is no point
228 * calling the disambiguation hint callback.
230 * On the other hand, if the current candidate
231 * replaced an earlier candidate that did _not_ pass
232 * the disambiguation hint callback, then we do have
233 * more than one objects that match the short name
234 * given, so we should make sure this one matches;
235 * otherwise, if we discovered this one and the one
236 * that we previously discarded in the reverse order,
237 * we would end up showing different results in the
238 * same repository!
240 ds->candidate_ok = (!ds->disambiguate_fn_used ||
241 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
243 if (!ds->candidate_ok)
244 return SHORT_NAME_AMBIGUOUS;
246 oidcpy(oid, &ds->candidate);
247 return 0;
250 static int disambiguate_commit_only(struct repository *r,
251 const struct object_id *oid,
252 void *cb_data UNUSED)
254 int kind = oid_object_info(r, oid, NULL);
255 return kind == OBJ_COMMIT;
258 static int disambiguate_committish_only(struct repository *r,
259 const struct object_id *oid,
260 void *cb_data UNUSED)
262 struct object *obj;
263 int kind;
265 kind = oid_object_info(r, oid, NULL);
266 if (kind == OBJ_COMMIT)
267 return 1;
268 if (kind != OBJ_TAG)
269 return 0;
271 /* We need to do this the hard way... */
272 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
273 if (obj && obj->type == OBJ_COMMIT)
274 return 1;
275 return 0;
278 static int disambiguate_tree_only(struct repository *r,
279 const struct object_id *oid,
280 void *cb_data UNUSED)
282 int kind = oid_object_info(r, oid, NULL);
283 return kind == OBJ_TREE;
286 static int disambiguate_treeish_only(struct repository *r,
287 const struct object_id *oid,
288 void *cb_data UNUSED)
290 struct object *obj;
291 int kind;
293 kind = oid_object_info(r, oid, NULL);
294 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
295 return 1;
296 if (kind != OBJ_TAG)
297 return 0;
299 /* We need to do this the hard way... */
300 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
301 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
302 return 1;
303 return 0;
306 static int disambiguate_blob_only(struct repository *r,
307 const struct object_id *oid,
308 void *cb_data UNUSED)
310 int kind = oid_object_info(r, oid, NULL);
311 return kind == OBJ_BLOB;
314 static disambiguate_hint_fn default_disambiguate_hint;
316 int set_disambiguate_hint_config(const char *var, const char *value)
318 static const struct {
319 const char *name;
320 disambiguate_hint_fn fn;
321 } hints[] = {
322 { "none", NULL },
323 { "commit", disambiguate_commit_only },
324 { "committish", disambiguate_committish_only },
325 { "tree", disambiguate_tree_only },
326 { "treeish", disambiguate_treeish_only },
327 { "blob", disambiguate_blob_only }
329 int i;
331 if (!value)
332 return config_error_nonbool(var);
334 for (i = 0; i < ARRAY_SIZE(hints); i++) {
335 if (!strcasecmp(value, hints[i].name)) {
336 default_disambiguate_hint = hints[i].fn;
337 return 0;
341 return error("unknown hint type for '%s': %s", var, value);
344 static int init_object_disambiguation(struct repository *r,
345 const char *name, int len,
346 const struct git_hash_algo *algo,
347 struct disambiguate_state *ds)
349 int i;
351 if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
352 return -1;
354 memset(ds, 0, sizeof(*ds));
356 for (i = 0; i < len ;i++) {
357 unsigned char c = name[i];
358 unsigned char val;
359 if (c >= '0' && c <= '9')
360 val = c - '0';
361 else if (c >= 'a' && c <= 'f')
362 val = c - 'a' + 10;
363 else if (c >= 'A' && c <='F') {
364 val = c - 'A' + 10;
365 c -= 'A' - 'a';
367 else
368 return -1;
369 ds->hex_pfx[i] = c;
370 if (!(i & 1))
371 val <<= 4;
372 ds->bin_pfx.hash[i >> 1] |= val;
375 ds->len = len;
376 ds->hex_pfx[len] = '\0';
377 ds->repo = r;
378 ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
379 prepare_alt_odb(r);
380 return 0;
383 struct ambiguous_output {
384 const struct disambiguate_state *ds;
385 struct strbuf advice;
386 struct strbuf sb;
389 static int show_ambiguous_object(const struct object_id *oid, void *data)
391 struct ambiguous_output *state = data;
392 const struct disambiguate_state *ds = state->ds;
393 struct strbuf *advice = &state->advice;
394 struct strbuf *sb = &state->sb;
395 int type;
396 const char *hash;
398 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
399 return 0;
401 hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
402 type = oid_object_info(ds->repo, oid, NULL);
404 if (type < 0) {
406 * TRANSLATORS: This is a line of ambiguous object
407 * output shown when we cannot look up or parse the
408 * object in question. E.g. "deadbeef [bad object]".
410 strbuf_addf(sb, _("%s [bad object]"), hash);
411 goto out;
414 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
415 type == OBJ_BLOB || type == OBJ_TAG);
417 if (type == OBJ_COMMIT) {
418 struct strbuf date = STRBUF_INIT;
419 struct strbuf msg = STRBUF_INIT;
420 struct commit *commit = lookup_commit(ds->repo, oid);
422 if (commit) {
423 struct pretty_print_context pp = {0};
424 pp.date_mode.type = DATE_SHORT;
425 repo_format_commit_message(the_repository, commit,
426 "%ad", &date, &pp);
427 repo_format_commit_message(the_repository, commit,
428 "%s", &msg, &pp);
432 * TRANSLATORS: This is a line of ambiguous commit
433 * object output. E.g.:
435 * "deadbeef commit 2021-01-01 - Some Commit Message"
437 strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
438 msg.buf);
440 strbuf_release(&date);
441 strbuf_release(&msg);
442 } else if (type == OBJ_TAG) {
443 struct tag *tag = lookup_tag(ds->repo, oid);
445 if (!parse_tag(tag) && tag->tag) {
447 * TRANSLATORS: This is a line of ambiguous
448 * tag object output. E.g.:
450 * "deadbeef tag 2022-01-01 - Some Tag Message"
452 * The second argument is the YYYY-MM-DD found
453 * in the tag.
455 * The third argument is the "tag" string
456 * from object.c.
458 strbuf_addf(sb, _("%s tag %s - %s"), hash,
459 show_date(tag->date, 0, DATE_MODE(SHORT)),
460 tag->tag);
461 } else {
463 * TRANSLATORS: This is a line of ambiguous
464 * tag object output where we couldn't parse
465 * the tag itself. E.g.:
467 * "deadbeef [bad tag, could not parse it]"
469 strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
470 hash);
472 } else if (type == OBJ_TREE) {
474 * TRANSLATORS: This is a line of ambiguous <type>
475 * object output. E.g. "deadbeef tree".
477 strbuf_addf(sb, _("%s tree"), hash);
478 } else if (type == OBJ_BLOB) {
480 * TRANSLATORS: This is a line of ambiguous <type>
481 * object output. E.g. "deadbeef blob".
483 strbuf_addf(sb, _("%s blob"), hash);
487 out:
489 * TRANSLATORS: This is line item of ambiguous object output
490 * from describe_ambiguous_object() above. For RTL languages
491 * you'll probably want to swap the "%s" and leading " " space
492 * around.
494 strbuf_addf(advice, _(" %s\n"), sb->buf);
496 strbuf_reset(sb);
497 return 0;
500 static int collect_ambiguous(const struct object_id *oid, void *data)
502 oid_array_append(data, oid);
503 return 0;
506 static int repo_collect_ambiguous(struct repository *r UNUSED,
507 const struct object_id *oid,
508 void *data)
510 return collect_ambiguous(oid, data);
513 static int sort_ambiguous(const void *va, const void *vb, void *ctx)
515 struct repository *sort_ambiguous_repo = ctx;
516 const struct object_id *a = va, *b = vb;
517 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
518 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
519 int a_type_sort;
520 int b_type_sort;
523 * Sorts by hash within the same object type, just as
524 * oid_array_for_each_unique() would do.
526 if (a_type == b_type) {
527 if (a->algo == b->algo)
528 return oidcmp(a, b);
529 else
530 return a->algo > b->algo ? 1 : -1;
534 * Between object types show tags, then commits, and finally
535 * trees and blobs.
537 * The object_type enum is commit, tree, blob, tag, but we
538 * want tag, commit, tree blob. Cleverly (perhaps too
539 * cleverly) do that with modulus, since the enum assigns 1 to
540 * commit, so tag becomes 0.
542 a_type_sort = a_type % 4;
543 b_type_sort = b_type % 4;
544 return a_type_sort > b_type_sort ? 1 : -1;
547 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
549 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
552 static enum get_oid_result get_short_oid(struct repository *r,
553 const char *name, int len,
554 struct object_id *oid,
555 unsigned flags)
557 int status;
558 struct disambiguate_state ds;
559 int quietly = !!(flags & GET_OID_QUIETLY);
560 const struct git_hash_algo *algo = r->hash_algo;
562 if (flags & GET_OID_HASH_ANY)
563 algo = NULL;
565 if (init_object_disambiguation(r, name, len, algo, &ds) < 0)
566 return -1;
568 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
569 BUG("multiple get_short_oid disambiguator flags");
571 if (flags & GET_OID_COMMIT)
572 ds.fn = disambiguate_commit_only;
573 else if (flags & GET_OID_COMMITTISH)
574 ds.fn = disambiguate_committish_only;
575 else if (flags & GET_OID_TREE)
576 ds.fn = disambiguate_tree_only;
577 else if (flags & GET_OID_TREEISH)
578 ds.fn = disambiguate_treeish_only;
579 else if (flags & GET_OID_BLOB)
580 ds.fn = disambiguate_blob_only;
581 else
582 ds.fn = default_disambiguate_hint;
584 find_short_object_filename(&ds);
585 find_short_packed_object(&ds);
586 status = finish_object_disambiguation(&ds, oid);
589 * If we didn't find it, do the usual reprepare() slow-path,
590 * since the object may have recently been added to the repository
591 * or migrated from loose to packed.
593 if (status == MISSING_OBJECT) {
594 reprepare_packed_git(r);
595 find_short_object_filename(&ds);
596 find_short_packed_object(&ds);
597 status = finish_object_disambiguation(&ds, oid);
600 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
601 struct oid_array collect = OID_ARRAY_INIT;
602 struct ambiguous_output out = {
603 .ds = &ds,
604 .sb = STRBUF_INIT,
605 .advice = STRBUF_INIT,
608 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
611 * We may still have ambiguity if we simply saw a series of
612 * candidates that did not satisfy our hint function. In
613 * that case, we still want to show them, so disable the hint
614 * function entirely.
616 if (!ds.ambiguous)
617 ds.fn = NULL;
619 repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect);
620 sort_ambiguous_oid_array(r, &collect);
622 if (oid_array_for_each(&collect, show_ambiguous_object, &out))
623 BUG("show_ambiguous_object shouldn't return non-zero");
626 * TRANSLATORS: The argument is the list of ambiguous
627 * objects composed in show_ambiguous_object(). See
628 * its "TRANSLATORS" comments for details.
630 advise(_("The candidates are:\n%s"), out.advice.buf);
632 oid_array_clear(&collect);
633 strbuf_release(&out.advice);
634 strbuf_release(&out.sb);
637 return status;
640 int repo_for_each_abbrev(struct repository *r, const char *prefix,
641 const struct git_hash_algo *algo,
642 each_abbrev_fn fn, void *cb_data)
644 struct oid_array collect = OID_ARRAY_INIT;
645 struct disambiguate_state ds;
646 int ret;
648 if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0)
649 return -1;
651 ds.always_call_fn = 1;
652 ds.fn = repo_collect_ambiguous;
653 ds.cb_data = &collect;
654 find_short_object_filename(&ds);
655 find_short_packed_object(&ds);
657 ret = oid_array_for_each_unique(&collect, fn, cb_data);
658 oid_array_clear(&collect);
659 return ret;
663 * Return the slot of the most-significant bit set in "val". There are various
664 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
665 * probably not a big deal here.
667 static unsigned msb(unsigned long val)
669 unsigned r = 0;
670 while (val >>= 1)
671 r++;
672 return r;
675 struct min_abbrev_data {
676 unsigned int init_len;
677 unsigned int cur_len;
678 char *hex;
679 struct repository *repo;
680 const struct object_id *oid;
683 static inline char get_hex_char_from_oid(const struct object_id *oid,
684 unsigned int pos)
686 static const char hex[] = "0123456789abcdef";
688 if ((pos & 1) == 0)
689 return hex[oid->hash[pos >> 1] >> 4];
690 else
691 return hex[oid->hash[pos >> 1] & 0xf];
694 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
696 struct min_abbrev_data *mad = cb_data;
698 unsigned int i = mad->init_len;
699 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
700 i++;
702 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
703 mad->cur_len = i + 1;
705 return 0;
708 static int repo_extend_abbrev_len(struct repository *r UNUSED,
709 const struct object_id *oid,
710 void *cb_data)
712 return extend_abbrev_len(oid, cb_data);
715 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
716 struct min_abbrev_data *mad)
718 for (; m; m = m->base_midx) {
719 int match = 0;
720 uint32_t num, first = 0;
721 struct object_id oid;
722 const struct object_id *mad_oid;
724 if (!m->num_objects)
725 continue;
727 num = m->num_objects + m->num_objects_in_base;
728 mad_oid = mad->oid;
729 match = bsearch_one_midx(mad_oid, m, &first);
732 * first is now the position in the packfile where we
733 * would insert mad->hash if it does not exist (or the
734 * position of mad->hash if it does exist). Hence, we
735 * consider a maximum of two objects nearby for the
736 * abbreviation length.
738 mad->init_len = 0;
739 if (!match) {
740 if (nth_midxed_object_oid(&oid, m, first))
741 extend_abbrev_len(&oid, mad);
742 } else if (first < num - 1) {
743 if (nth_midxed_object_oid(&oid, m, first + 1))
744 extend_abbrev_len(&oid, mad);
746 if (first > 0) {
747 if (nth_midxed_object_oid(&oid, m, first - 1))
748 extend_abbrev_len(&oid, mad);
750 mad->init_len = mad->cur_len;
754 static void find_abbrev_len_for_pack(struct packed_git *p,
755 struct min_abbrev_data *mad)
757 int match = 0;
758 uint32_t num, first = 0;
759 struct object_id oid;
760 const struct object_id *mad_oid;
762 if (p->multi_pack_index)
763 return;
765 if (open_pack_index(p) || !p->num_objects)
766 return;
768 num = p->num_objects;
769 mad_oid = mad->oid;
770 match = bsearch_pack(mad_oid, p, &first);
773 * first is now the position in the packfile where we would insert
774 * mad->hash if it does not exist (or the position of mad->hash if
775 * it does exist). Hence, we consider a maximum of two objects
776 * nearby for the abbreviation length.
778 mad->init_len = 0;
779 if (!match) {
780 if (!nth_packed_object_id(&oid, p, first))
781 extend_abbrev_len(&oid, mad);
782 } else if (first < num - 1) {
783 if (!nth_packed_object_id(&oid, p, first + 1))
784 extend_abbrev_len(&oid, mad);
786 if (first > 0) {
787 if (!nth_packed_object_id(&oid, p, first - 1))
788 extend_abbrev_len(&oid, mad);
790 mad->init_len = mad->cur_len;
793 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
795 struct multi_pack_index *m;
796 struct packed_git *p;
798 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
799 find_abbrev_len_for_midx(m, mad);
800 for (p = get_packed_git(mad->repo); p; p = p->next)
801 find_abbrev_len_for_pack(p, mad);
804 void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
805 const struct object_id *oid, int abbrev_len)
807 int r;
808 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
809 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
810 strbuf_setlen(sb, sb->len + r);
813 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
814 int abbrev_len)
816 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
819 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
820 const struct object_id *oid, int len)
822 const struct git_hash_algo *algo =
823 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
824 struct disambiguate_state ds;
825 struct min_abbrev_data mad;
826 struct object_id oid_ret;
827 const unsigned hexsz = algo->hexsz;
829 if (len < 0) {
830 unsigned long count = repo_approximate_object_count(r);
832 * Add one because the MSB only tells us the highest bit set,
833 * not including the value of all the _other_ bits (so "15"
834 * is only one off of 2^4, but the MSB is the 3rd bit.
836 len = msb(count) + 1;
838 * We now know we have on the order of 2^len objects, which
839 * expects a collision at 2^(len/2). But we also care about hex
840 * chars, not bits, and there are 4 bits per hex. So all
841 * together we need to divide by 2 and round up.
843 len = DIV_ROUND_UP(len, 2);
845 * For very small repos, we stick with our regular fallback.
847 if (len < FALLBACK_DEFAULT_ABBREV)
848 len = FALLBACK_DEFAULT_ABBREV;
851 oid_to_hex_r(hex, oid);
852 if (len >= hexsz || !len)
853 return hexsz;
855 mad.repo = r;
856 mad.init_len = len;
857 mad.cur_len = len;
858 mad.hex = hex;
859 mad.oid = oid;
861 find_abbrev_len_packed(&mad);
863 if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
864 return -1;
866 ds.fn = repo_extend_abbrev_len;
867 ds.always_call_fn = 1;
868 ds.cb_data = (void *)&mad;
870 find_short_object_filename(&ds);
871 (void)finish_object_disambiguation(&ds, &oid_ret);
873 hex[mad.cur_len] = 0;
874 return mad.cur_len;
877 const char *repo_find_unique_abbrev(struct repository *r,
878 const struct object_id *oid,
879 int len)
881 static int bufno;
882 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
883 char *hex = hexbuffer[bufno];
884 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
885 repo_find_unique_abbrev_r(r, hex, oid, len);
886 return hex;
889 static int ambiguous_path(const char *path, int len)
891 int slash = 1;
892 int cnt;
894 for (cnt = 0; cnt < len; cnt++) {
895 switch (*path++) {
896 case '\0':
897 break;
898 case '/':
899 if (slash)
900 break;
901 slash = 1;
902 continue;
903 case '.':
904 continue;
905 default:
906 slash = 0;
907 continue;
909 break;
911 return slash;
914 static inline int at_mark(const char *string, int len,
915 const char **suffix, int nr)
917 int i;
919 for (i = 0; i < nr; i++) {
920 int suffix_len = strlen(suffix[i]);
921 if (suffix_len <= len
922 && !strncasecmp(string, suffix[i], suffix_len))
923 return suffix_len;
925 return 0;
928 static inline int upstream_mark(const char *string, int len)
930 const char *suffix[] = { "@{upstream}", "@{u}" };
931 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
934 static inline int push_mark(const char *string, int len)
936 const char *suffix[] = { "@{push}" };
937 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
940 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
941 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
943 static int get_oid_basic(struct repository *r, const char *str, int len,
944 struct object_id *oid, unsigned int flags)
946 static const char *warn_msg = "refname '%.*s' is ambiguous.";
947 static const char *object_name_msg = N_(
948 "Git normally never creates a ref that ends with 40 hex characters\n"
949 "because it will be ignored when you just specify 40-hex. These refs\n"
950 "may be created by mistake. For example,\n"
951 "\n"
952 " git switch -c $br $(git rev-parse ...)\n"
953 "\n"
954 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
955 "examine these refs and maybe delete them. Turn this message off by\n"
956 "running \"git config set advice.objectNameWarning false\"");
957 struct object_id tmp_oid;
958 char *real_ref = NULL;
959 int refs_found = 0;
960 int at, reflog_len, nth_prior = 0;
961 int fatal = !(flags & GET_OID_QUIETLY);
963 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
964 if (repo_settings_get_warn_ambiguous_refs(r) && warn_on_object_refname_ambiguity) {
965 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
966 if (refs_found > 0) {
967 warning(warn_msg, len, str);
968 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
969 fprintf(stderr, "%s\n", _(object_name_msg));
971 free(real_ref);
973 return 0;
976 /* basic@{time or number or -number} format to query ref-log */
977 reflog_len = at = 0;
978 if (len && str[len-1] == '}') {
979 for (at = len-4; at >= 0; at--) {
980 if (str[at] == '@' && str[at+1] == '{') {
981 if (str[at+2] == '-') {
982 if (at != 0)
983 /* @{-N} not at start */
984 return -1;
985 nth_prior = 1;
986 continue;
988 if (!upstream_mark(str + at, len - at) &&
989 !push_mark(str + at, len - at)) {
990 reflog_len = (len-1) - (at+2);
991 len = at;
993 break;
998 /* Accept only unambiguous ref paths. */
999 if (len && ambiguous_path(str, len))
1000 return -1;
1002 if (nth_prior) {
1003 struct strbuf buf = STRBUF_INIT;
1004 int detached;
1006 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
1007 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
1008 strbuf_release(&buf);
1009 if (detached)
1010 return 0;
1014 if (!len && reflog_len)
1015 /* allow "@{...}" to mean the current branch reflog */
1016 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
1017 else if (reflog_len)
1018 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
1019 else
1020 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
1022 if (!refs_found)
1023 return -1;
1025 if (repo_settings_get_warn_ambiguous_refs(r) && !(flags & GET_OID_QUIETLY) &&
1026 (refs_found > 1 ||
1027 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
1028 warning(warn_msg, len, str);
1030 if (reflog_len) {
1031 int nth, i;
1032 timestamp_t at_time;
1033 timestamp_t co_time;
1034 int co_tz, co_cnt;
1036 /* Is it asking for N-th entry, or approxidate? */
1037 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
1038 char ch = str[at+2+i];
1039 if ('0' <= ch && ch <= '9')
1040 nth = nth * 10 + ch - '0';
1041 else
1042 nth = -1;
1044 if (100000000 <= nth) {
1045 at_time = nth;
1046 nth = -1;
1047 } else if (0 <= nth)
1048 at_time = 0;
1049 else {
1050 int errors = 0;
1051 char *tmp = xstrndup(str + at + 2, reflog_len);
1052 at_time = approxidate_careful(tmp, &errors);
1053 free(tmp);
1054 if (errors) {
1055 free(real_ref);
1056 return -1;
1059 if (read_ref_at(get_main_ref_store(r),
1060 real_ref, flags, at_time, nth, oid, NULL,
1061 &co_time, &co_tz, &co_cnt)) {
1062 if (!len) {
1063 if (!skip_prefix(real_ref, "refs/heads/", &str))
1064 str = "HEAD";
1065 len = strlen(str);
1067 if (at_time) {
1068 if (!(flags & GET_OID_QUIETLY)) {
1069 warning(_("log for '%.*s' only goes back to %s"),
1070 len, str,
1071 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
1073 } else if (nth == co_cnt && !is_null_oid(oid)) {
1075 * We were asked for the Nth reflog (counting
1076 * from 0), but there were only N entries.
1077 * read_ref_at() will have returned "1" to tell
1078 * us it did not find an entry, but it did
1079 * still fill in the oid with the "old" value,
1080 * which we can use.
1082 } else {
1083 if (flags & GET_OID_QUIETLY) {
1084 exit(128);
1086 die(_("log for '%.*s' only has %d entries"),
1087 len, str, co_cnt);
1092 free(real_ref);
1093 return 0;
1096 static enum get_oid_result get_parent(struct repository *r,
1097 const char *name, int len,
1098 struct object_id *result, int idx)
1100 struct object_id oid;
1101 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
1102 GET_OID_COMMITTISH);
1103 struct commit *commit;
1104 struct commit_list *p;
1106 if (ret)
1107 return ret;
1108 commit = lookup_commit_reference(r, &oid);
1109 if (repo_parse_commit(r, commit))
1110 return MISSING_OBJECT;
1111 if (!idx) {
1112 oidcpy(result, &commit->object.oid);
1113 return FOUND;
1115 p = commit->parents;
1116 while (p) {
1117 if (!--idx) {
1118 oidcpy(result, &p->item->object.oid);
1119 return FOUND;
1121 p = p->next;
1123 return MISSING_OBJECT;
1126 static enum get_oid_result get_nth_ancestor(struct repository *r,
1127 const char *name, int len,
1128 struct object_id *result,
1129 int generation)
1131 struct object_id oid;
1132 struct commit *commit;
1133 int ret;
1135 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
1136 if (ret)
1137 return ret;
1138 commit = lookup_commit_reference(r, &oid);
1139 if (!commit)
1140 return MISSING_OBJECT;
1142 while (generation--) {
1143 if (repo_parse_commit(r, commit) || !commit->parents)
1144 return MISSING_OBJECT;
1145 commit = commit->parents->item;
1147 oidcpy(result, &commit->object.oid);
1148 return FOUND;
1151 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
1152 struct object *o, enum object_type expected_type)
1154 if (name && !namelen)
1155 namelen = strlen(name);
1156 while (1) {
1157 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1158 return NULL;
1159 if (expected_type == OBJ_ANY || o->type == expected_type)
1160 return o;
1161 if (o->type == OBJ_TAG)
1162 o = ((struct tag*) o)->tagged;
1163 else if (o->type == OBJ_COMMIT)
1164 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1165 else {
1166 if (name)
1167 error("%.*s: expected %s type, but the object "
1168 "dereferences to %s type",
1169 namelen, name, type_name(expected_type),
1170 type_name(o->type));
1171 return NULL;
1176 static int peel_onion(struct repository *r, const char *name, int len,
1177 struct object_id *oid, unsigned lookup_flags)
1179 struct object_id outer;
1180 const char *sp;
1181 unsigned int expected_type = 0;
1182 struct object *o;
1185 * "ref^{type}" dereferences ref repeatedly until you cannot
1186 * dereference anymore, or you get an object of given type,
1187 * whichever comes first. "ref^{}" means just dereference
1188 * tags until you get a non-tag. "ref^0" is a shorthand for
1189 * "ref^{commit}". "commit^{tree}" could be used to find the
1190 * top-level tree of the given commit.
1192 if (len < 4 || name[len-1] != '}')
1193 return -1;
1195 for (sp = name + len - 1; name <= sp; sp--) {
1196 int ch = *sp;
1197 if (ch == '{' && name < sp && sp[-1] == '^')
1198 break;
1200 if (sp <= name)
1201 return -1;
1203 sp++; /* beginning of type name, or closing brace for empty */
1204 if (starts_with(sp, "commit}"))
1205 expected_type = OBJ_COMMIT;
1206 else if (starts_with(sp, "tag}"))
1207 expected_type = OBJ_TAG;
1208 else if (starts_with(sp, "tree}"))
1209 expected_type = OBJ_TREE;
1210 else if (starts_with(sp, "blob}"))
1211 expected_type = OBJ_BLOB;
1212 else if (starts_with(sp, "object}"))
1213 expected_type = OBJ_ANY;
1214 else if (sp[0] == '}')
1215 expected_type = OBJ_NONE;
1216 else if (sp[0] == '/')
1217 expected_type = OBJ_COMMIT;
1218 else
1219 return -1;
1221 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1222 if (expected_type == OBJ_COMMIT)
1223 lookup_flags |= GET_OID_COMMITTISH;
1224 else if (expected_type == OBJ_TREE)
1225 lookup_flags |= GET_OID_TREEISH;
1227 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1228 return -1;
1230 o = parse_object(r, &outer);
1231 if (!o)
1232 return -1;
1233 if (!expected_type) {
1234 o = deref_tag(r, o, name, sp - name - 2);
1235 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1236 return -1;
1237 oidcpy(oid, &o->oid);
1238 return 0;
1242 * At this point, the syntax look correct, so
1243 * if we do not get the needed object, we should
1244 * barf.
1246 o = repo_peel_to_type(r, name, len, o, expected_type);
1247 if (!o)
1248 return -1;
1250 oidcpy(oid, &o->oid);
1251 if (sp[0] == '/') {
1252 /* "$commit^{/foo}" */
1253 char *prefix;
1254 int ret;
1255 struct commit_list *list = NULL;
1258 * $commit^{/}. Some regex implementation may reject.
1259 * We don't need regex anyway. '' pattern always matches.
1261 if (sp[1] == '}')
1262 return 0;
1264 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1265 commit_list_insert((struct commit *)o, &list);
1266 ret = get_oid_oneline(r, prefix, oid, list);
1268 free_commit_list(list);
1269 free(prefix);
1270 return ret;
1272 return 0;
1276 * Documentation/revisions.txt says:
1277 * '<describeOutput>', e.g. 'v1.7.4.2-679-g3bee7fb'::
1278 * Output from `git describe`; i.e. a closest tag, optionally
1279 * followed by a dash and a number of commits, followed by a dash, a
1280 * 'g', and an abbreviated object name.
1282 * which means that the stuff before '-g${HASH}' needs to be a valid
1283 * refname, a dash, and a non-negative integer. This function verifies
1284 * that.
1286 * In particular, we do not want to treat
1287 * branchname:path/to/file/named/i-gaffed
1288 * as a request for commit affed.
1290 * More generally, we should probably not treat
1291 * 'refs/heads/./../.../ ~^:/?*[////\\\&}/busted.lock-g050e0ef6ead'
1292 * as a request for object 050e0ef6ead either.
1294 * We are called with name[len] == '-' and name[len+1] == 'g', i.e.
1295 * we are verifying ${REFNAME}-{INTEGER} part of the name.
1297 static int ref_and_count_parts_valid(const char *name, int len)
1299 struct strbuf sb;
1300 const char *cp;
1301 int flags = REFNAME_ALLOW_ONELEVEL;
1302 int ret = 1;
1304 /* Ensure we have at least one digit */
1305 if (!isxdigit(name[len-1]))
1306 return 0;
1308 /* Skip over digits backwards until we get to the dash */
1309 for (cp = name + len - 2; name < cp; cp--) {
1310 if (*cp == '-')
1311 break;
1312 if (!isxdigit(*cp))
1313 return 0;
1315 /* Ensure we found the leading dash */
1316 if (*cp != '-')
1317 return 0;
1319 len = cp - name;
1320 strbuf_init(&sb, len);
1321 strbuf_add(&sb, name, len);
1322 ret = !check_refname_format(sb.buf, flags);
1323 strbuf_release(&sb);
1324 return ret;
1327 static int get_describe_name(struct repository *r,
1328 const char *name, int len,
1329 struct object_id *oid)
1331 const char *cp;
1332 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1334 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1335 char ch = *cp;
1336 if (!isxdigit(ch)) {
1337 /* We must be looking at g in "SOMETHING-g"
1338 * for it to be describe output.
1340 if (ch == 'g' && cp[-1] == '-' &&
1341 ref_and_count_parts_valid(name, cp - 1 - name)) {
1342 cp++;
1343 len -= cp - name;
1344 return get_short_oid(r,
1345 cp, len, oid, flags);
1349 return -1;
1352 static enum get_oid_result get_oid_1(struct repository *r,
1353 const char *name, int len,
1354 struct object_id *oid,
1355 unsigned lookup_flags)
1357 int ret, has_suffix;
1358 const char *cp;
1361 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1363 has_suffix = 0;
1364 for (cp = name + len - 1; name <= cp; cp--) {
1365 int ch = *cp;
1366 if ('0' <= ch && ch <= '9')
1367 continue;
1368 if (ch == '~' || ch == '^')
1369 has_suffix = ch;
1370 break;
1373 if (has_suffix) {
1374 unsigned int num = 0;
1375 int len1 = cp - name;
1376 cp++;
1377 while (cp < name + len) {
1378 unsigned int digit = *cp++ - '0';
1379 if (unsigned_mult_overflows(num, 10))
1380 return MISSING_OBJECT;
1381 num *= 10;
1382 if (unsigned_add_overflows(num, digit))
1383 return MISSING_OBJECT;
1384 num += digit;
1386 if (!num && len1 == len - 1)
1387 num = 1;
1388 else if (num > INT_MAX)
1389 return MISSING_OBJECT;
1390 if (has_suffix == '^')
1391 return get_parent(r, name, len1, oid, num);
1392 /* else if (has_suffix == '~') -- goes without saying */
1393 return get_nth_ancestor(r, name, len1, oid, num);
1396 ret = peel_onion(r, name, len, oid, lookup_flags);
1397 if (!ret)
1398 return FOUND;
1400 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1401 if (!ret)
1402 return FOUND;
1404 /* It could be describe output that is "SOMETHING-gXXXX" */
1405 ret = get_describe_name(r, name, len, oid);
1406 if (!ret)
1407 return FOUND;
1409 return get_short_oid(r, name, len, oid, lookup_flags);
1413 * This interprets names like ':/Initial revision of "git"' by searching
1414 * through history and returning the first commit whose message starts
1415 * the given regular expression.
1417 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1419 * For a literal '!' character at the beginning of a pattern, you have to repeat
1420 * that, like: ':/!!foo'
1422 * For future extension, all other sequences beginning with ':/!' are reserved.
1425 /* Remember to update object flag allocation in object.h */
1426 #define ONELINE_SEEN (1u<<20)
1428 struct handle_one_ref_cb {
1429 struct repository *repo;
1430 struct commit_list **list;
1433 static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
1434 int flag UNUSED,
1435 void *cb_data)
1437 struct handle_one_ref_cb *cb = cb_data;
1438 struct commit_list **list = cb->list;
1439 struct object *object = parse_object(cb->repo, oid);
1440 if (!object)
1441 return 0;
1442 if (object->type == OBJ_TAG) {
1443 object = deref_tag(cb->repo, object, path,
1444 strlen(path));
1445 if (!object)
1446 return 0;
1448 if (object->type != OBJ_COMMIT)
1449 return 0;
1450 commit_list_insert((struct commit *)object, list);
1451 return 0;
1454 static int get_oid_oneline(struct repository *r,
1455 const char *prefix, struct object_id *oid,
1456 const struct commit_list *list)
1458 struct commit_list *copy = NULL, **copy_tail = &copy;
1459 const struct commit_list *l;
1460 int found = 0;
1461 int negative = 0;
1462 regex_t regex;
1464 if (prefix[0] == '!') {
1465 prefix++;
1467 if (prefix[0] == '-') {
1468 prefix++;
1469 negative = 1;
1470 } else if (prefix[0] != '!') {
1471 return -1;
1475 if (regcomp(&regex, prefix, REG_EXTENDED))
1476 return -1;
1478 for (l = list; l; l = l->next) {
1479 l->item->object.flags |= ONELINE_SEEN;
1480 copy_tail = &commit_list_insert(l->item, copy_tail)->next;
1482 while (copy) {
1483 const char *p, *buf;
1484 struct commit *commit;
1485 int matches;
1487 commit = pop_most_recent_commit(&copy, ONELINE_SEEN);
1488 if (!parse_object(r, &commit->object.oid))
1489 continue;
1490 buf = repo_get_commit_buffer(r, commit, NULL);
1491 p = strstr(buf, "\n\n");
1492 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1493 repo_unuse_commit_buffer(r, commit, buf);
1495 if (matches) {
1496 oidcpy(oid, &commit->object.oid);
1497 found = 1;
1498 break;
1501 regfree(&regex);
1502 for (l = list; l; l = l->next)
1503 clear_commit_marks(l->item, ONELINE_SEEN);
1504 free_commit_list(copy);
1505 return found ? 0 : -1;
1508 struct grab_nth_branch_switch_cbdata {
1509 int remaining;
1510 struct strbuf *sb;
1513 static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1514 struct object_id *noid UNUSED,
1515 const char *email UNUSED,
1516 timestamp_t timestamp UNUSED,
1517 int tz UNUSED,
1518 const char *message, void *cb_data)
1520 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1521 const char *match = NULL, *target = NULL;
1522 size_t len;
1524 if (skip_prefix(message, "checkout: moving from ", &match))
1525 target = strstr(match, " to ");
1527 if (!match || !target)
1528 return 0;
1529 if (--(cb->remaining) == 0) {
1530 len = target - match;
1531 strbuf_reset(cb->sb);
1532 strbuf_add(cb->sb, match, len);
1533 return 1; /* we are done */
1535 return 0;
1539 * Parse @{-N} syntax, return the number of characters parsed
1540 * if successful; otherwise signal an error with negative value.
1542 static int interpret_nth_prior_checkout(struct repository *r,
1543 const char *name, int namelen,
1544 struct strbuf *buf)
1546 long nth;
1547 int retval;
1548 struct grab_nth_branch_switch_cbdata cb;
1549 const char *brace;
1550 char *num_end;
1552 if (namelen < 4)
1553 return -1;
1554 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1555 return -1;
1556 brace = memchr(name, '}', namelen);
1557 if (!brace)
1558 return -1;
1559 nth = strtol(name + 3, &num_end, 10);
1560 if (num_end != brace)
1561 return -1;
1562 if (nth <= 0)
1563 return -1;
1564 cb.remaining = nth;
1565 cb.sb = buf;
1567 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1568 "HEAD", grab_nth_branch_switch, &cb);
1569 if (0 < retval) {
1570 retval = brace - name + 1;
1571 } else
1572 retval = 0;
1574 return retval;
1577 int repo_get_oid_mb(struct repository *r,
1578 const char *name,
1579 struct object_id *oid)
1581 struct commit *one, *two;
1582 struct commit_list *mbs = NULL;
1583 struct object_id oid_tmp;
1584 const char *dots;
1585 int st;
1587 dots = strstr(name, "...");
1588 if (!dots)
1589 return repo_get_oid(r, name, oid);
1590 if (dots == name)
1591 st = repo_get_oid(r, "HEAD", &oid_tmp);
1592 else {
1593 struct strbuf sb;
1594 strbuf_init(&sb, dots - name);
1595 strbuf_add(&sb, name, dots - name);
1596 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1597 strbuf_release(&sb);
1599 if (st)
1600 return st;
1601 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1602 if (!one)
1603 return -1;
1605 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1606 return -1;
1607 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1608 if (!two)
1609 return -1;
1610 if (repo_get_merge_bases(r, one, two, &mbs) < 0) {
1611 free_commit_list(mbs);
1612 return -1;
1614 if (!mbs || mbs->next)
1615 st = -1;
1616 else {
1617 st = 0;
1618 oidcpy(oid, &mbs->item->object.oid);
1620 free_commit_list(mbs);
1621 return st;
1624 /* parse @something syntax, when 'something' is not {.*} */
1625 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1627 const char *next;
1629 if (len || name[1] == '{')
1630 return -1;
1632 /* make sure it's a single @, or @@{.*}, not @foo */
1633 next = memchr(name + len + 1, '@', namelen - len - 1);
1634 if (next && next[1] != '{')
1635 return -1;
1636 if (!next)
1637 next = name + namelen;
1638 if (next != name + 1)
1639 return -1;
1641 strbuf_reset(buf);
1642 strbuf_add(buf, "HEAD", 4);
1643 return 1;
1646 static int reinterpret(struct repository *r,
1647 const char *name, int namelen, int len,
1648 struct strbuf *buf, unsigned allowed)
1650 /* we have extra data, which might need further processing */
1651 struct strbuf tmp = STRBUF_INIT;
1652 int used = buf->len;
1653 int ret;
1654 struct interpret_branch_name_options options = {
1655 .allowed = allowed
1658 strbuf_add(buf, name + len, namelen - len);
1659 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1660 /* that data was not interpreted, remove our cruft */
1661 if (ret < 0) {
1662 strbuf_setlen(buf, used);
1663 return len;
1665 strbuf_reset(buf);
1666 strbuf_addbuf(buf, &tmp);
1667 strbuf_release(&tmp);
1668 /* tweak for size of {-N} versus expanded ref name */
1669 return ret - used + len;
1672 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1674 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1675 strbuf_reset(buf);
1676 strbuf_addstr(buf, s);
1677 free(s);
1680 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1682 if (!allowed)
1683 return 1;
1685 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1686 starts_with(refname, "refs/heads/"))
1687 return 1;
1688 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1689 starts_with(refname, "refs/remotes/"))
1690 return 1;
1692 return 0;
1695 static int interpret_branch_mark(struct repository *r,
1696 const char *name, int namelen,
1697 int at, struct strbuf *buf,
1698 int (*get_mark)(const char *, int),
1699 const char *(*get_data)(struct branch *,
1700 struct strbuf *),
1701 const struct interpret_branch_name_options *options)
1703 int len;
1704 struct branch *branch;
1705 struct strbuf err = STRBUF_INIT;
1706 const char *value;
1708 len = get_mark(name + at, namelen - at);
1709 if (!len)
1710 return -1;
1712 if (memchr(name, ':', at))
1713 return -1;
1715 if (at) {
1716 char *name_str = xmemdupz(name, at);
1717 branch = branch_get(name_str);
1718 free(name_str);
1719 } else
1720 branch = branch_get(NULL);
1722 value = get_data(branch, &err);
1723 if (!value) {
1724 if (options->nonfatal_dangling_mark) {
1725 strbuf_release(&err);
1726 return -1;
1727 } else {
1728 die("%s", err.buf);
1732 if (!branch_interpret_allowed(value, options->allowed))
1733 return -1;
1735 set_shortened_ref(r, buf, value);
1736 return len + at;
1739 int repo_interpret_branch_name(struct repository *r,
1740 const char *name, int namelen,
1741 struct strbuf *buf,
1742 const struct interpret_branch_name_options *options)
1744 char *at;
1745 const char *start;
1746 int len;
1748 if (!namelen)
1749 namelen = strlen(name);
1751 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1752 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1753 if (!len) {
1754 return len; /* syntax Ok, not enough switches */
1755 } else if (len > 0) {
1756 if (len == namelen)
1757 return len; /* consumed all */
1758 else
1759 return reinterpret(r, name, namelen, len, buf,
1760 options->allowed);
1764 for (start = name;
1765 (at = memchr(start, '@', namelen - (start - name)));
1766 start = at + 1) {
1768 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1769 len = interpret_empty_at(name, namelen, at - name, buf);
1770 if (len > 0)
1771 return reinterpret(r, name, namelen, len, buf,
1772 options->allowed);
1775 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1776 upstream_mark, branch_get_upstream,
1777 options);
1778 if (len > 0)
1779 return len;
1781 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1782 push_mark, branch_get_push,
1783 options);
1784 if (len > 0)
1785 return len;
1788 return -1;
1791 void object_context_release(struct object_context *ctx)
1793 free(ctx->path);
1794 strbuf_release(&ctx->symlink_path);
1798 * This is like "get_oid_basic()", except it allows "object ID expressions",
1799 * notably "xyz^" for "parent of xyz"
1801 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1803 struct object_context unused;
1804 int ret = get_oid_with_context(r, name, 0, oid, &unused);
1805 object_context_release(&unused);
1806 return ret;
1810 * This returns a non-zero value if the string (built using printf
1811 * format and the given arguments) is not a valid object.
1813 int get_oidf(struct object_id *oid, const char *fmt, ...)
1815 va_list ap;
1816 int ret;
1817 struct strbuf sb = STRBUF_INIT;
1819 va_start(ap, fmt);
1820 strbuf_vaddf(&sb, fmt, ap);
1821 va_end(ap);
1823 ret = repo_get_oid(the_repository, sb.buf, oid);
1824 strbuf_release(&sb);
1826 return ret;
1830 * Many callers know that the user meant to name a commit-ish by
1831 * syntactical positions where the object name appears. Calling this
1832 * function allows the machinery to disambiguate shorter-than-unique
1833 * abbreviated object names between commit-ish and others.
1835 * Note that this does NOT error out when the named object is not a
1836 * commit-ish. It is merely to give a hint to the disambiguation
1837 * machinery.
1839 int repo_get_oid_committish(struct repository *r,
1840 const char *name,
1841 struct object_id *oid)
1843 struct object_context unused;
1844 int ret = get_oid_with_context(r, name, GET_OID_COMMITTISH,
1845 oid, &unused);
1846 object_context_release(&unused);
1847 return ret;
1850 int repo_get_oid_treeish(struct repository *r,
1851 const char *name,
1852 struct object_id *oid)
1854 struct object_context unused;
1855 int ret = get_oid_with_context(r, name, GET_OID_TREEISH,
1856 oid, &unused);
1857 object_context_release(&unused);
1858 return ret;
1861 int repo_get_oid_commit(struct repository *r,
1862 const char *name,
1863 struct object_id *oid)
1865 struct object_context unused;
1866 int ret = get_oid_with_context(r, name, GET_OID_COMMIT,
1867 oid, &unused);
1868 object_context_release(&unused);
1869 return ret;
1872 int repo_get_oid_tree(struct repository *r,
1873 const char *name,
1874 struct object_id *oid)
1876 struct object_context unused;
1877 int ret = get_oid_with_context(r, name, GET_OID_TREE,
1878 oid, &unused);
1879 object_context_release(&unused);
1880 return ret;
1883 int repo_get_oid_blob(struct repository *r,
1884 const char *name,
1885 struct object_id *oid)
1887 struct object_context unused;
1888 int ret = get_oid_with_context(r, name, GET_OID_BLOB,
1889 oid, &unused);
1890 object_context_release(&unused);
1891 return ret;
1894 /* Must be called only when object_name:filename doesn't exist. */
1895 static void diagnose_invalid_oid_path(struct repository *r,
1896 const char *prefix,
1897 const char *filename,
1898 const struct object_id *tree_oid,
1899 const char *object_name,
1900 int object_name_len)
1902 struct object_id oid;
1903 unsigned short mode;
1905 if (!prefix)
1906 prefix = "";
1908 if (file_exists(filename))
1909 die(_("path '%s' exists on disk, but not in '%.*s'"),
1910 filename, object_name_len, object_name);
1911 if (is_missing_file_error(errno)) {
1912 char *fullname = xstrfmt("%s%s", prefix, filename);
1914 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1915 die(_("path '%s' exists, but not '%s'\n"
1916 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1917 fullname,
1918 filename,
1919 object_name_len, object_name,
1920 fullname,
1921 object_name_len, object_name,
1922 filename);
1924 die(_("path '%s' does not exist in '%.*s'"),
1925 filename, object_name_len, object_name);
1929 /* Must be called only when :stage:filename doesn't exist. */
1930 static void diagnose_invalid_index_path(struct repository *r,
1931 int stage,
1932 const char *prefix,
1933 const char *filename)
1935 struct index_state *istate = r->index;
1936 const struct cache_entry *ce;
1937 int pos;
1938 unsigned namelen = strlen(filename);
1939 struct strbuf fullname = STRBUF_INIT;
1941 if (!prefix)
1942 prefix = "";
1944 /* Wrong stage number? */
1945 pos = index_name_pos(istate, filename, namelen);
1946 if (pos < 0)
1947 pos = -pos - 1;
1948 if (pos < istate->cache_nr) {
1949 ce = istate->cache[pos];
1950 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1951 ce_namelen(ce) == namelen &&
1952 !memcmp(ce->name, filename, namelen))
1953 die(_("path '%s' is in the index, but not at stage %d\n"
1954 "hint: Did you mean ':%d:%s'?"),
1955 filename, stage,
1956 ce_stage(ce), filename);
1959 /* Confusion between relative and absolute filenames? */
1960 strbuf_addstr(&fullname, prefix);
1961 strbuf_addstr(&fullname, filename);
1962 pos = index_name_pos(istate, fullname.buf, fullname.len);
1963 if (pos < 0)
1964 pos = -pos - 1;
1965 if (pos < istate->cache_nr) {
1966 ce = istate->cache[pos];
1967 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1968 ce_namelen(ce) == fullname.len &&
1969 !memcmp(ce->name, fullname.buf, fullname.len))
1970 die(_("path '%s' is in the index, but not '%s'\n"
1971 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1972 fullname.buf, filename,
1973 ce_stage(ce), fullname.buf,
1974 ce_stage(ce), filename);
1977 if (repo_file_exists(r, filename))
1978 die(_("path '%s' exists on disk, but not in the index"), filename);
1979 if (is_missing_file_error(errno))
1980 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1981 filename);
1983 strbuf_release(&fullname);
1987 static char *resolve_relative_path(struct repository *r, const char *rel)
1989 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1990 return NULL;
1992 if (r != the_repository || !is_inside_work_tree())
1993 die(_("relative path syntax can't be used outside working tree"));
1995 /* die() inside prefix_path() if resolved path is outside worktree */
1996 return prefix_path(startup_info->prefix,
1997 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1998 rel);
2001 static int reject_tree_in_index(struct repository *repo,
2002 int only_to_die,
2003 const struct cache_entry *ce,
2004 int stage,
2005 const char *prefix,
2006 const char *cp)
2008 if (!S_ISSPARSEDIR(ce->ce_mode))
2009 return 0;
2010 if (only_to_die)
2011 diagnose_invalid_index_path(repo, stage, prefix, cp);
2012 return -1;
2015 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
2016 const char *name,
2017 unsigned flags,
2018 const char *prefix,
2019 struct object_id *oid,
2020 struct object_context *oc)
2022 int ret, bracket_depth;
2023 int namelen = strlen(name);
2024 const char *cp;
2025 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
2027 memset(oc, 0, sizeof(*oc));
2028 oc->mode = S_IFINVALID;
2029 strbuf_init(&oc->symlink_path, 0);
2030 ret = get_oid_1(repo, name, namelen, oid, flags);
2031 if (!ret && flags & GET_OID_REQUIRE_PATH)
2032 die(_("<object>:<path> required, only <object> '%s' given"),
2033 name);
2034 if (!ret)
2035 return ret;
2037 * tree:path --> object name of path in tree
2038 * :path -> object name of absolute path in index
2039 * :./path -> object name of path relative to cwd in index
2040 * :[0-3]:path -> object name of path in index at stage
2041 * :/foo -> recent commit matching foo
2043 if (name[0] == ':') {
2044 int stage = 0;
2045 const struct cache_entry *ce;
2046 char *new_path = NULL;
2047 int pos;
2048 if (!only_to_die && namelen > 2 && name[1] == '/') {
2049 struct handle_one_ref_cb cb;
2050 struct commit_list *list = NULL;
2052 cb.repo = repo;
2053 cb.list = &list;
2054 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2055 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2056 commit_list_sort_by_date(&list);
2057 ret = get_oid_oneline(repo, name + 2, oid, list);
2059 free_commit_list(list);
2060 return ret;
2062 if (namelen < 3 ||
2063 name[2] != ':' ||
2064 name[1] < '0' || '3' < name[1])
2065 cp = name + 1;
2066 else {
2067 stage = name[1] - '0';
2068 cp = name + 3;
2070 new_path = resolve_relative_path(repo, cp);
2071 if (!new_path) {
2072 namelen = namelen - (cp - name);
2073 } else {
2074 cp = new_path;
2075 namelen = strlen(cp);
2078 if (flags & GET_OID_RECORD_PATH)
2079 oc->path = xstrdup(cp);
2081 if (!repo->index || !repo->index->cache)
2082 repo_read_index(repo);
2083 pos = index_name_pos(repo->index, cp, namelen);
2084 if (pos < 0)
2085 pos = -pos - 1;
2086 while (pos < repo->index->cache_nr) {
2087 ce = repo->index->cache[pos];
2088 if (ce_namelen(ce) != namelen ||
2089 memcmp(ce->name, cp, namelen))
2090 break;
2091 if (ce_stage(ce) == stage) {
2092 free(new_path);
2093 if (reject_tree_in_index(repo, only_to_die, ce,
2094 stage, prefix, cp))
2095 return -1;
2096 oidcpy(oid, &ce->oid);
2097 oc->mode = ce->ce_mode;
2098 return 0;
2100 pos++;
2102 if (only_to_die && name[1] && name[1] != '/')
2103 diagnose_invalid_index_path(repo, stage, prefix, cp);
2104 free(new_path);
2105 return -1;
2107 for (cp = name, bracket_depth = 0; *cp; cp++) {
2108 if (strchr("@^", *cp) && cp[1] == '{') {
2109 cp++;
2110 bracket_depth++;
2111 } else if (bracket_depth && *cp == '}') {
2112 bracket_depth--;
2113 } else if (!bracket_depth && *cp == ':') {
2114 break;
2117 if (*cp == ':') {
2118 struct object_id tree_oid;
2119 int len = cp - name;
2120 unsigned sub_flags = flags;
2122 sub_flags &= ~GET_OID_DISAMBIGUATORS;
2123 sub_flags |= GET_OID_TREEISH;
2125 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
2126 const char *filename = cp+1;
2127 char *new_filename = NULL;
2129 new_filename = resolve_relative_path(repo, filename);
2130 if (new_filename)
2131 filename = new_filename;
2132 if (flags & GET_OID_FOLLOW_SYMLINKS) {
2133 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
2134 filename, oid, &oc->symlink_path,
2135 &oc->mode);
2136 } else {
2137 ret = get_tree_entry(repo, &tree_oid, filename, oid,
2138 &oc->mode);
2139 if (ret && only_to_die) {
2140 diagnose_invalid_oid_path(repo, prefix,
2141 filename,
2142 &tree_oid,
2143 name, len);
2146 if (flags & GET_OID_RECORD_PATH)
2147 oc->path = xstrdup(filename);
2149 free(new_filename);
2150 return ret;
2151 } else {
2152 if (only_to_die)
2153 die(_("invalid object name '%.*s'."), len, name);
2156 return ret;
2160 * Call this function when you know "name" given by the end user must
2161 * name an object but it doesn't; the function _may_ die with a better
2162 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2163 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2164 * you have a chance to diagnose the error further.
2166 void maybe_die_on_misspelt_object_name(struct repository *r,
2167 const char *name,
2168 const char *prefix)
2170 struct object_context oc;
2171 struct object_id oid;
2172 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY,
2173 prefix, &oid, &oc);
2174 object_context_release(&oc);
2177 enum get_oid_result get_oid_with_context(struct repository *repo,
2178 const char *str,
2179 unsigned flags,
2180 struct object_id *oid,
2181 struct object_context *oc)
2183 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
2184 BUG("incompatible flags for get_oid_with_context");
2185 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);