Merge branch 'ps/reftable-drop-generic'
[git/gitster.git] / object-name.c
blob09c1bd93a3158b8d786374717093dcebc1fac50b
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "object-name.h"
5 #include "advice.h"
6 #include "config.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "tag.h"
11 #include "commit.h"
12 #include "tree.h"
13 #include "tree-walk.h"
14 #include "refs.h"
15 #include "remote.h"
16 #include "dir.h"
17 #include "oid-array.h"
18 #include "oidtree.h"
19 #include "packfile.h"
20 #include "pretty.h"
21 #include "object-store-ll.h"
22 #include "read-cache-ll.h"
23 #include "repository.h"
24 #include "setup.h"
25 #include "midx.h"
26 #include "commit-reach.h"
27 #include "date.h"
28 #include "object-file-convert.h"
30 static int get_oid_oneline(struct repository *r, const char *, struct object_id *,
31 const struct commit_list *);
33 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
35 struct disambiguate_state {
36 int len; /* length of prefix in hex chars */
37 char hex_pfx[GIT_MAX_HEXSZ + 1];
38 struct object_id bin_pfx;
40 struct repository *repo;
41 disambiguate_hint_fn fn;
42 void *cb_data;
43 struct object_id candidate;
44 unsigned candidate_exists:1;
45 unsigned candidate_checked:1;
46 unsigned candidate_ok:1;
47 unsigned disambiguate_fn_used:1;
48 unsigned ambiguous:1;
49 unsigned always_call_fn:1;
52 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
54 /* The hash algorithm of current has already been filtered */
55 if (ds->always_call_fn) {
56 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
57 return;
59 if (!ds->candidate_exists) {
60 /* this is the first candidate */
61 oidcpy(&ds->candidate, current);
62 ds->candidate_exists = 1;
63 return;
64 } else if (oideq(&ds->candidate, current)) {
65 /* the same as what we already have seen */
66 return;
69 if (!ds->fn) {
70 /* cannot disambiguate between ds->candidate and current */
71 ds->ambiguous = 1;
72 return;
75 if (!ds->candidate_checked) {
76 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
77 ds->disambiguate_fn_used = 1;
78 ds->candidate_checked = 1;
81 if (!ds->candidate_ok) {
82 /* discard the candidate; we know it does not satisfy fn */
83 oidcpy(&ds->candidate, current);
84 ds->candidate_checked = 0;
85 return;
88 /* if we reach this point, we know ds->candidate satisfies fn */
89 if (ds->fn(ds->repo, current, ds->cb_data)) {
91 * if both current and candidate satisfy fn, we cannot
92 * disambiguate.
94 ds->candidate_ok = 0;
95 ds->ambiguous = 1;
98 /* otherwise, current can be discarded and candidate is still good */
101 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
103 static enum cb_next match_prefix(const struct object_id *oid, void *arg)
105 struct disambiguate_state *ds = arg;
106 /* no need to call match_hash, oidtree_each did prefix match */
107 update_candidates(ds, oid);
108 return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
111 static void find_short_object_filename(struct disambiguate_state *ds)
113 struct object_directory *odb;
115 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next)
116 oidtree_each(odb_loose_cache(odb, &ds->bin_pfx),
117 &ds->bin_pfx, ds->len, match_prefix, ds);
120 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
122 do {
123 if (*a != *b)
124 return 0;
125 a++;
126 b++;
127 len -= 2;
128 } while (len > 1);
129 if (len)
130 if ((*a ^ *b) & 0xf0)
131 return 0;
132 return 1;
135 static void unique_in_midx(struct multi_pack_index *m,
136 struct disambiguate_state *ds)
138 for (; m; m = m->base_midx) {
139 uint32_t num, i, first = 0;
140 const struct object_id *current = NULL;
141 int len = ds->len > ds->repo->hash_algo->hexsz ?
142 ds->repo->hash_algo->hexsz : ds->len;
144 if (!m->num_objects)
145 continue;
147 num = m->num_objects + m->num_objects_in_base;
149 bsearch_one_midx(&ds->bin_pfx, m, &first);
152 * At this point, "first" is the location of the lowest
153 * object with an object name that could match
154 * "bin_pfx". See if we have 0, 1 or more objects that
155 * actually match(es).
157 for (i = first; i < num && !ds->ambiguous; i++) {
158 struct object_id oid;
159 current = nth_midxed_object_oid(&oid, m, i);
160 if (!match_hash(len, ds->bin_pfx.hash, current->hash))
161 break;
162 update_candidates(ds, current);
167 static void unique_in_pack(struct packed_git *p,
168 struct disambiguate_state *ds)
170 uint32_t num, i, first = 0;
171 int len = ds->len > ds->repo->hash_algo->hexsz ?
172 ds->repo->hash_algo->hexsz : ds->len;
174 if (p->multi_pack_index)
175 return;
177 if (open_pack_index(p) || !p->num_objects)
178 return;
180 num = p->num_objects;
181 bsearch_pack(&ds->bin_pfx, p, &first);
184 * At this point, "first" is the location of the lowest object
185 * with an object name that could match "bin_pfx". See if we have
186 * 0, 1 or more objects that actually match(es).
188 for (i = first; i < num && !ds->ambiguous; i++) {
189 struct object_id oid;
190 nth_packed_object_id(&oid, p, i);
191 if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
192 break;
193 update_candidates(ds, &oid);
197 static void find_short_packed_object(struct disambiguate_state *ds)
199 struct multi_pack_index *m;
200 struct packed_git *p;
202 /* Skip, unless oids from the storage hash algorithm are wanted */
203 if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
204 return;
206 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
207 m = m->next)
208 unique_in_midx(m, ds);
209 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
210 p = p->next)
211 unique_in_pack(p, ds);
214 static int finish_object_disambiguation(struct disambiguate_state *ds,
215 struct object_id *oid)
217 if (ds->ambiguous)
218 return SHORT_NAME_AMBIGUOUS;
220 if (!ds->candidate_exists)
221 return MISSING_OBJECT;
223 if (!ds->candidate_checked)
225 * If this is the only candidate, there is no point
226 * calling the disambiguation hint callback.
228 * On the other hand, if the current candidate
229 * replaced an earlier candidate that did _not_ pass
230 * the disambiguation hint callback, then we do have
231 * more than one objects that match the short name
232 * given, so we should make sure this one matches;
233 * otherwise, if we discovered this one and the one
234 * that we previously discarded in the reverse order,
235 * we would end up showing different results in the
236 * same repository!
238 ds->candidate_ok = (!ds->disambiguate_fn_used ||
239 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
241 if (!ds->candidate_ok)
242 return SHORT_NAME_AMBIGUOUS;
244 oidcpy(oid, &ds->candidate);
245 return 0;
248 static int disambiguate_commit_only(struct repository *r,
249 const struct object_id *oid,
250 void *cb_data UNUSED)
252 int kind = oid_object_info(r, oid, NULL);
253 return kind == OBJ_COMMIT;
256 static int disambiguate_committish_only(struct repository *r,
257 const struct object_id *oid,
258 void *cb_data UNUSED)
260 struct object *obj;
261 int kind;
263 kind = oid_object_info(r, oid, NULL);
264 if (kind == OBJ_COMMIT)
265 return 1;
266 if (kind != OBJ_TAG)
267 return 0;
269 /* We need to do this the hard way... */
270 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
271 if (obj && obj->type == OBJ_COMMIT)
272 return 1;
273 return 0;
276 static int disambiguate_tree_only(struct repository *r,
277 const struct object_id *oid,
278 void *cb_data UNUSED)
280 int kind = oid_object_info(r, oid, NULL);
281 return kind == OBJ_TREE;
284 static int disambiguate_treeish_only(struct repository *r,
285 const struct object_id *oid,
286 void *cb_data UNUSED)
288 struct object *obj;
289 int kind;
291 kind = oid_object_info(r, oid, NULL);
292 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
293 return 1;
294 if (kind != OBJ_TAG)
295 return 0;
297 /* We need to do this the hard way... */
298 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
299 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
300 return 1;
301 return 0;
304 static int disambiguate_blob_only(struct repository *r,
305 const struct object_id *oid,
306 void *cb_data UNUSED)
308 int kind = oid_object_info(r, oid, NULL);
309 return kind == OBJ_BLOB;
312 static disambiguate_hint_fn default_disambiguate_hint;
314 int set_disambiguate_hint_config(const char *var, const char *value)
316 static const struct {
317 const char *name;
318 disambiguate_hint_fn fn;
319 } hints[] = {
320 { "none", NULL },
321 { "commit", disambiguate_commit_only },
322 { "committish", disambiguate_committish_only },
323 { "tree", disambiguate_tree_only },
324 { "treeish", disambiguate_treeish_only },
325 { "blob", disambiguate_blob_only }
327 int i;
329 if (!value)
330 return config_error_nonbool(var);
332 for (i = 0; i < ARRAY_SIZE(hints); i++) {
333 if (!strcasecmp(value, hints[i].name)) {
334 default_disambiguate_hint = hints[i].fn;
335 return 0;
339 return error("unknown hint type for '%s': %s", var, value);
342 static int init_object_disambiguation(struct repository *r,
343 const char *name, int len,
344 const struct git_hash_algo *algo,
345 struct disambiguate_state *ds)
347 int i;
349 if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
350 return -1;
352 memset(ds, 0, sizeof(*ds));
354 for (i = 0; i < len ;i++) {
355 unsigned char c = name[i];
356 unsigned char val;
357 if (c >= '0' && c <= '9')
358 val = c - '0';
359 else if (c >= 'a' && c <= 'f')
360 val = c - 'a' + 10;
361 else if (c >= 'A' && c <='F') {
362 val = c - 'A' + 10;
363 c -= 'A' - 'a';
365 else
366 return -1;
367 ds->hex_pfx[i] = c;
368 if (!(i & 1))
369 val <<= 4;
370 ds->bin_pfx.hash[i >> 1] |= val;
373 ds->len = len;
374 ds->hex_pfx[len] = '\0';
375 ds->repo = r;
376 ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
377 prepare_alt_odb(r);
378 return 0;
381 struct ambiguous_output {
382 const struct disambiguate_state *ds;
383 struct strbuf advice;
384 struct strbuf sb;
387 static int show_ambiguous_object(const struct object_id *oid, void *data)
389 struct ambiguous_output *state = data;
390 const struct disambiguate_state *ds = state->ds;
391 struct strbuf *advice = &state->advice;
392 struct strbuf *sb = &state->sb;
393 int type;
394 const char *hash;
396 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
397 return 0;
399 hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
400 type = oid_object_info(ds->repo, oid, NULL);
402 if (type < 0) {
404 * TRANSLATORS: This is a line of ambiguous object
405 * output shown when we cannot look up or parse the
406 * object in question. E.g. "deadbeef [bad object]".
408 strbuf_addf(sb, _("%s [bad object]"), hash);
409 goto out;
412 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
413 type == OBJ_BLOB || type == OBJ_TAG);
415 if (type == OBJ_COMMIT) {
416 struct strbuf date = STRBUF_INIT;
417 struct strbuf msg = STRBUF_INIT;
418 struct commit *commit = lookup_commit(ds->repo, oid);
420 if (commit) {
421 struct pretty_print_context pp = {0};
422 pp.date_mode.type = DATE_SHORT;
423 repo_format_commit_message(the_repository, commit,
424 "%ad", &date, &pp);
425 repo_format_commit_message(the_repository, commit,
426 "%s", &msg, &pp);
430 * TRANSLATORS: This is a line of ambiguous commit
431 * object output. E.g.:
433 * "deadbeef commit 2021-01-01 - Some Commit Message"
435 strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
436 msg.buf);
438 strbuf_release(&date);
439 strbuf_release(&msg);
440 } else if (type == OBJ_TAG) {
441 struct tag *tag = lookup_tag(ds->repo, oid);
443 if (!parse_tag(tag) && tag->tag) {
445 * TRANSLATORS: This is a line of ambiguous
446 * tag object output. E.g.:
448 * "deadbeef tag 2022-01-01 - Some Tag Message"
450 * The second argument is the YYYY-MM-DD found
451 * in the tag.
453 * The third argument is the "tag" string
454 * from object.c.
456 strbuf_addf(sb, _("%s tag %s - %s"), hash,
457 show_date(tag->date, 0, DATE_MODE(SHORT)),
458 tag->tag);
459 } else {
461 * TRANSLATORS: This is a line of ambiguous
462 * tag object output where we couldn't parse
463 * the tag itself. E.g.:
465 * "deadbeef [bad tag, could not parse it]"
467 strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
468 hash);
470 } else if (type == OBJ_TREE) {
472 * TRANSLATORS: This is a line of ambiguous <type>
473 * object output. E.g. "deadbeef tree".
475 strbuf_addf(sb, _("%s tree"), hash);
476 } else if (type == OBJ_BLOB) {
478 * TRANSLATORS: This is a line of ambiguous <type>
479 * object output. E.g. "deadbeef blob".
481 strbuf_addf(sb, _("%s blob"), hash);
485 out:
487 * TRANSLATORS: This is line item of ambiguous object output
488 * from describe_ambiguous_object() above. For RTL languages
489 * you'll probably want to swap the "%s" and leading " " space
490 * around.
492 strbuf_addf(advice, _(" %s\n"), sb->buf);
494 strbuf_reset(sb);
495 return 0;
498 static int collect_ambiguous(const struct object_id *oid, void *data)
500 oid_array_append(data, oid);
501 return 0;
504 static int repo_collect_ambiguous(struct repository *r UNUSED,
505 const struct object_id *oid,
506 void *data)
508 return collect_ambiguous(oid, data);
511 static int sort_ambiguous(const void *va, const void *vb, void *ctx)
513 struct repository *sort_ambiguous_repo = ctx;
514 const struct object_id *a = va, *b = vb;
515 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
516 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
517 int a_type_sort;
518 int b_type_sort;
521 * Sorts by hash within the same object type, just as
522 * oid_array_for_each_unique() would do.
524 if (a_type == b_type) {
525 if (a->algo == b->algo)
526 return oidcmp(a, b);
527 else
528 return a->algo > b->algo ? 1 : -1;
532 * Between object types show tags, then commits, and finally
533 * trees and blobs.
535 * The object_type enum is commit, tree, blob, tag, but we
536 * want tag, commit, tree blob. Cleverly (perhaps too
537 * cleverly) do that with modulus, since the enum assigns 1 to
538 * commit, so tag becomes 0.
540 a_type_sort = a_type % 4;
541 b_type_sort = b_type % 4;
542 return a_type_sort > b_type_sort ? 1 : -1;
545 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
547 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
550 static enum get_oid_result get_short_oid(struct repository *r,
551 const char *name, int len,
552 struct object_id *oid,
553 unsigned flags)
555 int status;
556 struct disambiguate_state ds;
557 int quietly = !!(flags & GET_OID_QUIETLY);
558 const struct git_hash_algo *algo = r->hash_algo;
560 if (flags & GET_OID_HASH_ANY)
561 algo = NULL;
563 if (init_object_disambiguation(r, name, len, algo, &ds) < 0)
564 return -1;
566 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
567 BUG("multiple get_short_oid disambiguator flags");
569 if (flags & GET_OID_COMMIT)
570 ds.fn = disambiguate_commit_only;
571 else if (flags & GET_OID_COMMITTISH)
572 ds.fn = disambiguate_committish_only;
573 else if (flags & GET_OID_TREE)
574 ds.fn = disambiguate_tree_only;
575 else if (flags & GET_OID_TREEISH)
576 ds.fn = disambiguate_treeish_only;
577 else if (flags & GET_OID_BLOB)
578 ds.fn = disambiguate_blob_only;
579 else
580 ds.fn = default_disambiguate_hint;
582 find_short_object_filename(&ds);
583 find_short_packed_object(&ds);
584 status = finish_object_disambiguation(&ds, oid);
587 * If we didn't find it, do the usual reprepare() slow-path,
588 * since the object may have recently been added to the repository
589 * or migrated from loose to packed.
591 if (status == MISSING_OBJECT) {
592 reprepare_packed_git(r);
593 find_short_object_filename(&ds);
594 find_short_packed_object(&ds);
595 status = finish_object_disambiguation(&ds, oid);
598 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
599 struct oid_array collect = OID_ARRAY_INIT;
600 struct ambiguous_output out = {
601 .ds = &ds,
602 .sb = STRBUF_INIT,
603 .advice = STRBUF_INIT,
606 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
609 * We may still have ambiguity if we simply saw a series of
610 * candidates that did not satisfy our hint function. In
611 * that case, we still want to show them, so disable the hint
612 * function entirely.
614 if (!ds.ambiguous)
615 ds.fn = NULL;
617 repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect);
618 sort_ambiguous_oid_array(r, &collect);
620 if (oid_array_for_each(&collect, show_ambiguous_object, &out))
621 BUG("show_ambiguous_object shouldn't return non-zero");
624 * TRANSLATORS: The argument is the list of ambiguous
625 * objects composed in show_ambiguous_object(). See
626 * its "TRANSLATORS" comments for details.
628 advise(_("The candidates are:\n%s"), out.advice.buf);
630 oid_array_clear(&collect);
631 strbuf_release(&out.advice);
632 strbuf_release(&out.sb);
635 return status;
638 int repo_for_each_abbrev(struct repository *r, const char *prefix,
639 const struct git_hash_algo *algo,
640 each_abbrev_fn fn, void *cb_data)
642 struct oid_array collect = OID_ARRAY_INIT;
643 struct disambiguate_state ds;
644 int ret;
646 if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0)
647 return -1;
649 ds.always_call_fn = 1;
650 ds.fn = repo_collect_ambiguous;
651 ds.cb_data = &collect;
652 find_short_object_filename(&ds);
653 find_short_packed_object(&ds);
655 ret = oid_array_for_each_unique(&collect, fn, cb_data);
656 oid_array_clear(&collect);
657 return ret;
661 * Return the slot of the most-significant bit set in "val". There are various
662 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
663 * probably not a big deal here.
665 static unsigned msb(unsigned long val)
667 unsigned r = 0;
668 while (val >>= 1)
669 r++;
670 return r;
673 struct min_abbrev_data {
674 unsigned int init_len;
675 unsigned int cur_len;
676 char *hex;
677 struct repository *repo;
678 const struct object_id *oid;
681 static inline char get_hex_char_from_oid(const struct object_id *oid,
682 unsigned int pos)
684 static const char hex[] = "0123456789abcdef";
686 if ((pos & 1) == 0)
687 return hex[oid->hash[pos >> 1] >> 4];
688 else
689 return hex[oid->hash[pos >> 1] & 0xf];
692 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
694 struct min_abbrev_data *mad = cb_data;
696 unsigned int i = mad->init_len;
697 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
698 i++;
700 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
701 mad->cur_len = i + 1;
703 return 0;
706 static int repo_extend_abbrev_len(struct repository *r UNUSED,
707 const struct object_id *oid,
708 void *cb_data)
710 return extend_abbrev_len(oid, cb_data);
713 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
714 struct min_abbrev_data *mad)
716 for (; m; m = m->base_midx) {
717 int match = 0;
718 uint32_t num, first = 0;
719 struct object_id oid;
720 const struct object_id *mad_oid;
722 if (!m->num_objects)
723 continue;
725 num = m->num_objects + m->num_objects_in_base;
726 mad_oid = mad->oid;
727 match = bsearch_one_midx(mad_oid, m, &first);
730 * first is now the position in the packfile where we
731 * would insert mad->hash if it does not exist (or the
732 * position of mad->hash if it does exist). Hence, we
733 * consider a maximum of two objects nearby for the
734 * abbreviation length.
736 mad->init_len = 0;
737 if (!match) {
738 if (nth_midxed_object_oid(&oid, m, first))
739 extend_abbrev_len(&oid, mad);
740 } else if (first < num - 1) {
741 if (nth_midxed_object_oid(&oid, m, first + 1))
742 extend_abbrev_len(&oid, mad);
744 if (first > 0) {
745 if (nth_midxed_object_oid(&oid, m, first - 1))
746 extend_abbrev_len(&oid, mad);
748 mad->init_len = mad->cur_len;
752 static void find_abbrev_len_for_pack(struct packed_git *p,
753 struct min_abbrev_data *mad)
755 int match = 0;
756 uint32_t num, first = 0;
757 struct object_id oid;
758 const struct object_id *mad_oid;
760 if (p->multi_pack_index)
761 return;
763 if (open_pack_index(p) || !p->num_objects)
764 return;
766 num = p->num_objects;
767 mad_oid = mad->oid;
768 match = bsearch_pack(mad_oid, p, &first);
771 * first is now the position in the packfile where we would insert
772 * mad->hash if it does not exist (or the position of mad->hash if
773 * it does exist). Hence, we consider a maximum of two objects
774 * nearby for the abbreviation length.
776 mad->init_len = 0;
777 if (!match) {
778 if (!nth_packed_object_id(&oid, p, first))
779 extend_abbrev_len(&oid, mad);
780 } else if (first < num - 1) {
781 if (!nth_packed_object_id(&oid, p, first + 1))
782 extend_abbrev_len(&oid, mad);
784 if (first > 0) {
785 if (!nth_packed_object_id(&oid, p, first - 1))
786 extend_abbrev_len(&oid, mad);
788 mad->init_len = mad->cur_len;
791 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
793 struct multi_pack_index *m;
794 struct packed_git *p;
796 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
797 find_abbrev_len_for_midx(m, mad);
798 for (p = get_packed_git(mad->repo); p; p = p->next)
799 find_abbrev_len_for_pack(p, mad);
802 void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
803 const struct object_id *oid, int abbrev_len)
805 int r;
806 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
807 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
808 strbuf_setlen(sb, sb->len + r);
811 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
812 int abbrev_len)
814 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
817 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
818 const struct object_id *oid, int len)
820 const struct git_hash_algo *algo =
821 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
822 struct disambiguate_state ds;
823 struct min_abbrev_data mad;
824 struct object_id oid_ret;
825 const unsigned hexsz = algo->hexsz;
827 if (len < 0) {
828 unsigned long count = repo_approximate_object_count(r);
830 * Add one because the MSB only tells us the highest bit set,
831 * not including the value of all the _other_ bits (so "15"
832 * is only one off of 2^4, but the MSB is the 3rd bit.
834 len = msb(count) + 1;
836 * We now know we have on the order of 2^len objects, which
837 * expects a collision at 2^(len/2). But we also care about hex
838 * chars, not bits, and there are 4 bits per hex. So all
839 * together we need to divide by 2 and round up.
841 len = DIV_ROUND_UP(len, 2);
843 * For very small repos, we stick with our regular fallback.
845 if (len < FALLBACK_DEFAULT_ABBREV)
846 len = FALLBACK_DEFAULT_ABBREV;
849 oid_to_hex_r(hex, oid);
850 if (len >= hexsz || !len)
851 return hexsz;
853 mad.repo = r;
854 mad.init_len = len;
855 mad.cur_len = len;
856 mad.hex = hex;
857 mad.oid = oid;
859 find_abbrev_len_packed(&mad);
861 if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
862 return -1;
864 ds.fn = repo_extend_abbrev_len;
865 ds.always_call_fn = 1;
866 ds.cb_data = (void *)&mad;
868 find_short_object_filename(&ds);
869 (void)finish_object_disambiguation(&ds, &oid_ret);
871 hex[mad.cur_len] = 0;
872 return mad.cur_len;
875 const char *repo_find_unique_abbrev(struct repository *r,
876 const struct object_id *oid,
877 int len)
879 static int bufno;
880 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
881 char *hex = hexbuffer[bufno];
882 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
883 repo_find_unique_abbrev_r(r, hex, oid, len);
884 return hex;
887 static int ambiguous_path(const char *path, int len)
889 int slash = 1;
890 int cnt;
892 for (cnt = 0; cnt < len; cnt++) {
893 switch (*path++) {
894 case '\0':
895 break;
896 case '/':
897 if (slash)
898 break;
899 slash = 1;
900 continue;
901 case '.':
902 continue;
903 default:
904 slash = 0;
905 continue;
907 break;
909 return slash;
912 static inline int at_mark(const char *string, int len,
913 const char **suffix, int nr)
915 int i;
917 for (i = 0; i < nr; i++) {
918 int suffix_len = strlen(suffix[i]);
919 if (suffix_len <= len
920 && !strncasecmp(string, suffix[i], suffix_len))
921 return suffix_len;
923 return 0;
926 static inline int upstream_mark(const char *string, int len)
928 const char *suffix[] = { "@{upstream}", "@{u}" };
929 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
932 static inline int push_mark(const char *string, int len)
934 const char *suffix[] = { "@{push}" };
935 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
938 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
939 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
941 static int get_oid_basic(struct repository *r, const char *str, int len,
942 struct object_id *oid, unsigned int flags)
944 static const char *warn_msg = "refname '%.*s' is ambiguous.";
945 static const char *object_name_msg = N_(
946 "Git normally never creates a ref that ends with 40 hex characters\n"
947 "because it will be ignored when you just specify 40-hex. These refs\n"
948 "may be created by mistake. For example,\n"
949 "\n"
950 " git switch -c $br $(git rev-parse ...)\n"
951 "\n"
952 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
953 "examine these refs and maybe delete them. Turn this message off by\n"
954 "running \"git config advice.objectNameWarning false\"");
955 struct object_id tmp_oid;
956 char *real_ref = NULL;
957 int refs_found = 0;
958 int at, reflog_len, nth_prior = 0;
959 int fatal = !(flags & GET_OID_QUIETLY);
961 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
962 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
963 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
964 if (refs_found > 0) {
965 warning(warn_msg, len, str);
966 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
967 fprintf(stderr, "%s\n", _(object_name_msg));
969 free(real_ref);
971 return 0;
974 /* basic@{time or number or -number} format to query ref-log */
975 reflog_len = at = 0;
976 if (len && str[len-1] == '}') {
977 for (at = len-4; at >= 0; at--) {
978 if (str[at] == '@' && str[at+1] == '{') {
979 if (str[at+2] == '-') {
980 if (at != 0)
981 /* @{-N} not at start */
982 return -1;
983 nth_prior = 1;
984 continue;
986 if (!upstream_mark(str + at, len - at) &&
987 !push_mark(str + at, len - at)) {
988 reflog_len = (len-1) - (at+2);
989 len = at;
991 break;
996 /* Accept only unambiguous ref paths. */
997 if (len && ambiguous_path(str, len))
998 return -1;
1000 if (nth_prior) {
1001 struct strbuf buf = STRBUF_INIT;
1002 int detached;
1004 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
1005 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
1006 strbuf_release(&buf);
1007 if (detached)
1008 return 0;
1012 if (!len && reflog_len)
1013 /* allow "@{...}" to mean the current branch reflog */
1014 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
1015 else if (reflog_len)
1016 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
1017 else
1018 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
1020 if (!refs_found)
1021 return -1;
1023 if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
1024 (refs_found > 1 ||
1025 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
1026 warning(warn_msg, len, str);
1028 if (reflog_len) {
1029 int nth, i;
1030 timestamp_t at_time;
1031 timestamp_t co_time;
1032 int co_tz, co_cnt;
1034 /* Is it asking for N-th entry, or approxidate? */
1035 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
1036 char ch = str[at+2+i];
1037 if ('0' <= ch && ch <= '9')
1038 nth = nth * 10 + ch - '0';
1039 else
1040 nth = -1;
1042 if (100000000 <= nth) {
1043 at_time = nth;
1044 nth = -1;
1045 } else if (0 <= nth)
1046 at_time = 0;
1047 else {
1048 int errors = 0;
1049 char *tmp = xstrndup(str + at + 2, reflog_len);
1050 at_time = approxidate_careful(tmp, &errors);
1051 free(tmp);
1052 if (errors) {
1053 free(real_ref);
1054 return -1;
1057 if (read_ref_at(get_main_ref_store(r),
1058 real_ref, flags, at_time, nth, oid, NULL,
1059 &co_time, &co_tz, &co_cnt)) {
1060 if (!len) {
1061 if (!skip_prefix(real_ref, "refs/heads/", &str))
1062 str = "HEAD";
1063 len = strlen(str);
1065 if (at_time) {
1066 if (!(flags & GET_OID_QUIETLY)) {
1067 warning(_("log for '%.*s' only goes back to %s"),
1068 len, str,
1069 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
1071 } else if (nth == co_cnt && !is_null_oid(oid)) {
1073 * We were asked for the Nth reflog (counting
1074 * from 0), but there were only N entries.
1075 * read_ref_at() will have returned "1" to tell
1076 * us it did not find an entry, but it did
1077 * still fill in the oid with the "old" value,
1078 * which we can use.
1080 } else {
1081 if (flags & GET_OID_QUIETLY) {
1082 exit(128);
1084 die(_("log for '%.*s' only has %d entries"),
1085 len, str, co_cnt);
1090 free(real_ref);
1091 return 0;
1094 static enum get_oid_result get_parent(struct repository *r,
1095 const char *name, int len,
1096 struct object_id *result, int idx)
1098 struct object_id oid;
1099 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
1100 GET_OID_COMMITTISH);
1101 struct commit *commit;
1102 struct commit_list *p;
1104 if (ret)
1105 return ret;
1106 commit = lookup_commit_reference(r, &oid);
1107 if (repo_parse_commit(r, commit))
1108 return MISSING_OBJECT;
1109 if (!idx) {
1110 oidcpy(result, &commit->object.oid);
1111 return FOUND;
1113 p = commit->parents;
1114 while (p) {
1115 if (!--idx) {
1116 oidcpy(result, &p->item->object.oid);
1117 return FOUND;
1119 p = p->next;
1121 return MISSING_OBJECT;
1124 static enum get_oid_result get_nth_ancestor(struct repository *r,
1125 const char *name, int len,
1126 struct object_id *result,
1127 int generation)
1129 struct object_id oid;
1130 struct commit *commit;
1131 int ret;
1133 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
1134 if (ret)
1135 return ret;
1136 commit = lookup_commit_reference(r, &oid);
1137 if (!commit)
1138 return MISSING_OBJECT;
1140 while (generation--) {
1141 if (repo_parse_commit(r, commit) || !commit->parents)
1142 return MISSING_OBJECT;
1143 commit = commit->parents->item;
1145 oidcpy(result, &commit->object.oid);
1146 return FOUND;
1149 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
1150 struct object *o, enum object_type expected_type)
1152 if (name && !namelen)
1153 namelen = strlen(name);
1154 while (1) {
1155 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1156 return NULL;
1157 if (expected_type == OBJ_ANY || o->type == expected_type)
1158 return o;
1159 if (o->type == OBJ_TAG)
1160 o = ((struct tag*) o)->tagged;
1161 else if (o->type == OBJ_COMMIT)
1162 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1163 else {
1164 if (name)
1165 error("%.*s: expected %s type, but the object "
1166 "dereferences to %s type",
1167 namelen, name, type_name(expected_type),
1168 type_name(o->type));
1169 return NULL;
1174 static int peel_onion(struct repository *r, const char *name, int len,
1175 struct object_id *oid, unsigned lookup_flags)
1177 struct object_id outer;
1178 const char *sp;
1179 unsigned int expected_type = 0;
1180 struct object *o;
1183 * "ref^{type}" dereferences ref repeatedly until you cannot
1184 * dereference anymore, or you get an object of given type,
1185 * whichever comes first. "ref^{}" means just dereference
1186 * tags until you get a non-tag. "ref^0" is a shorthand for
1187 * "ref^{commit}". "commit^{tree}" could be used to find the
1188 * top-level tree of the given commit.
1190 if (len < 4 || name[len-1] != '}')
1191 return -1;
1193 for (sp = name + len - 1; name <= sp; sp--) {
1194 int ch = *sp;
1195 if (ch == '{' && name < sp && sp[-1] == '^')
1196 break;
1198 if (sp <= name)
1199 return -1;
1201 sp++; /* beginning of type name, or closing brace for empty */
1202 if (starts_with(sp, "commit}"))
1203 expected_type = OBJ_COMMIT;
1204 else if (starts_with(sp, "tag}"))
1205 expected_type = OBJ_TAG;
1206 else if (starts_with(sp, "tree}"))
1207 expected_type = OBJ_TREE;
1208 else if (starts_with(sp, "blob}"))
1209 expected_type = OBJ_BLOB;
1210 else if (starts_with(sp, "object}"))
1211 expected_type = OBJ_ANY;
1212 else if (sp[0] == '}')
1213 expected_type = OBJ_NONE;
1214 else if (sp[0] == '/')
1215 expected_type = OBJ_COMMIT;
1216 else
1217 return -1;
1219 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1220 if (expected_type == OBJ_COMMIT)
1221 lookup_flags |= GET_OID_COMMITTISH;
1222 else if (expected_type == OBJ_TREE)
1223 lookup_flags |= GET_OID_TREEISH;
1225 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1226 return -1;
1228 o = parse_object(r, &outer);
1229 if (!o)
1230 return -1;
1231 if (!expected_type) {
1232 o = deref_tag(r, o, name, sp - name - 2);
1233 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1234 return -1;
1235 oidcpy(oid, &o->oid);
1236 return 0;
1240 * At this point, the syntax look correct, so
1241 * if we do not get the needed object, we should
1242 * barf.
1244 o = repo_peel_to_type(r, name, len, o, expected_type);
1245 if (!o)
1246 return -1;
1248 oidcpy(oid, &o->oid);
1249 if (sp[0] == '/') {
1250 /* "$commit^{/foo}" */
1251 char *prefix;
1252 int ret;
1253 struct commit_list *list = NULL;
1256 * $commit^{/}. Some regex implementation may reject.
1257 * We don't need regex anyway. '' pattern always matches.
1259 if (sp[1] == '}')
1260 return 0;
1262 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1263 commit_list_insert((struct commit *)o, &list);
1264 ret = get_oid_oneline(r, prefix, oid, list);
1266 free_commit_list(list);
1267 free(prefix);
1268 return ret;
1270 return 0;
1273 static int get_describe_name(struct repository *r,
1274 const char *name, int len,
1275 struct object_id *oid)
1277 const char *cp;
1278 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1280 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1281 char ch = *cp;
1282 if (!isxdigit(ch)) {
1283 /* We must be looking at g in "SOMETHING-g"
1284 * for it to be describe output.
1286 if (ch == 'g' && cp[-1] == '-') {
1287 cp++;
1288 len -= cp - name;
1289 return get_short_oid(r,
1290 cp, len, oid, flags);
1294 return -1;
1297 static enum get_oid_result get_oid_1(struct repository *r,
1298 const char *name, int len,
1299 struct object_id *oid,
1300 unsigned lookup_flags)
1302 int ret, has_suffix;
1303 const char *cp;
1306 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1308 has_suffix = 0;
1309 for (cp = name + len - 1; name <= cp; cp--) {
1310 int ch = *cp;
1311 if ('0' <= ch && ch <= '9')
1312 continue;
1313 if (ch == '~' || ch == '^')
1314 has_suffix = ch;
1315 break;
1318 if (has_suffix) {
1319 unsigned int num = 0;
1320 int len1 = cp - name;
1321 cp++;
1322 while (cp < name + len) {
1323 unsigned int digit = *cp++ - '0';
1324 if (unsigned_mult_overflows(num, 10))
1325 return MISSING_OBJECT;
1326 num *= 10;
1327 if (unsigned_add_overflows(num, digit))
1328 return MISSING_OBJECT;
1329 num += digit;
1331 if (!num && len1 == len - 1)
1332 num = 1;
1333 else if (num > INT_MAX)
1334 return MISSING_OBJECT;
1335 if (has_suffix == '^')
1336 return get_parent(r, name, len1, oid, num);
1337 /* else if (has_suffix == '~') -- goes without saying */
1338 return get_nth_ancestor(r, name, len1, oid, num);
1341 ret = peel_onion(r, name, len, oid, lookup_flags);
1342 if (!ret)
1343 return FOUND;
1345 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1346 if (!ret)
1347 return FOUND;
1349 /* It could be describe output that is "SOMETHING-gXXXX" */
1350 ret = get_describe_name(r, name, len, oid);
1351 if (!ret)
1352 return FOUND;
1354 return get_short_oid(r, name, len, oid, lookup_flags);
1358 * This interprets names like ':/Initial revision of "git"' by searching
1359 * through history and returning the first commit whose message starts
1360 * the given regular expression.
1362 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1364 * For a literal '!' character at the beginning of a pattern, you have to repeat
1365 * that, like: ':/!!foo'
1367 * For future extension, all other sequences beginning with ':/!' are reserved.
1370 /* Remember to update object flag allocation in object.h */
1371 #define ONELINE_SEEN (1u<<20)
1373 struct handle_one_ref_cb {
1374 struct repository *repo;
1375 struct commit_list **list;
1378 static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
1379 int flag UNUSED,
1380 void *cb_data)
1382 struct handle_one_ref_cb *cb = cb_data;
1383 struct commit_list **list = cb->list;
1384 struct object *object = parse_object(cb->repo, oid);
1385 if (!object)
1386 return 0;
1387 if (object->type == OBJ_TAG) {
1388 object = deref_tag(cb->repo, object, path,
1389 strlen(path));
1390 if (!object)
1391 return 0;
1393 if (object->type != OBJ_COMMIT)
1394 return 0;
1395 commit_list_insert((struct commit *)object, list);
1396 return 0;
1399 static int get_oid_oneline(struct repository *r,
1400 const char *prefix, struct object_id *oid,
1401 const struct commit_list *list)
1403 struct commit_list *copy = NULL;
1404 const struct commit_list *l;
1405 int found = 0;
1406 int negative = 0;
1407 regex_t regex;
1409 if (prefix[0] == '!') {
1410 prefix++;
1412 if (prefix[0] == '-') {
1413 prefix++;
1414 negative = 1;
1415 } else if (prefix[0] != '!') {
1416 return -1;
1420 if (regcomp(&regex, prefix, REG_EXTENDED))
1421 return -1;
1423 for (l = list; l; l = l->next) {
1424 l->item->object.flags |= ONELINE_SEEN;
1425 commit_list_insert(l->item, &copy);
1427 while (copy) {
1428 const char *p, *buf;
1429 struct commit *commit;
1430 int matches;
1432 commit = pop_most_recent_commit(&copy, ONELINE_SEEN);
1433 if (!parse_object(r, &commit->object.oid))
1434 continue;
1435 buf = repo_get_commit_buffer(r, commit, NULL);
1436 p = strstr(buf, "\n\n");
1437 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1438 repo_unuse_commit_buffer(r, commit, buf);
1440 if (matches) {
1441 oidcpy(oid, &commit->object.oid);
1442 found = 1;
1443 break;
1446 regfree(&regex);
1447 for (l = list; l; l = l->next)
1448 clear_commit_marks(l->item, ONELINE_SEEN);
1449 free_commit_list(copy);
1450 return found ? 0 : -1;
1453 struct grab_nth_branch_switch_cbdata {
1454 int remaining;
1455 struct strbuf *sb;
1458 static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1459 struct object_id *noid UNUSED,
1460 const char *email UNUSED,
1461 timestamp_t timestamp UNUSED,
1462 int tz UNUSED,
1463 const char *message, void *cb_data)
1465 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1466 const char *match = NULL, *target = NULL;
1467 size_t len;
1469 if (skip_prefix(message, "checkout: moving from ", &match))
1470 target = strstr(match, " to ");
1472 if (!match || !target)
1473 return 0;
1474 if (--(cb->remaining) == 0) {
1475 len = target - match;
1476 strbuf_reset(cb->sb);
1477 strbuf_add(cb->sb, match, len);
1478 return 1; /* we are done */
1480 return 0;
1484 * Parse @{-N} syntax, return the number of characters parsed
1485 * if successful; otherwise signal an error with negative value.
1487 static int interpret_nth_prior_checkout(struct repository *r,
1488 const char *name, int namelen,
1489 struct strbuf *buf)
1491 long nth;
1492 int retval;
1493 struct grab_nth_branch_switch_cbdata cb;
1494 const char *brace;
1495 char *num_end;
1497 if (namelen < 4)
1498 return -1;
1499 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1500 return -1;
1501 brace = memchr(name, '}', namelen);
1502 if (!brace)
1503 return -1;
1504 nth = strtol(name + 3, &num_end, 10);
1505 if (num_end != brace)
1506 return -1;
1507 if (nth <= 0)
1508 return -1;
1509 cb.remaining = nth;
1510 cb.sb = buf;
1512 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1513 "HEAD", grab_nth_branch_switch, &cb);
1514 if (0 < retval) {
1515 retval = brace - name + 1;
1516 } else
1517 retval = 0;
1519 return retval;
1522 int repo_get_oid_mb(struct repository *r,
1523 const char *name,
1524 struct object_id *oid)
1526 struct commit *one, *two;
1527 struct commit_list *mbs = NULL;
1528 struct object_id oid_tmp;
1529 const char *dots;
1530 int st;
1532 dots = strstr(name, "...");
1533 if (!dots)
1534 return repo_get_oid(r, name, oid);
1535 if (dots == name)
1536 st = repo_get_oid(r, "HEAD", &oid_tmp);
1537 else {
1538 struct strbuf sb;
1539 strbuf_init(&sb, dots - name);
1540 strbuf_add(&sb, name, dots - name);
1541 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1542 strbuf_release(&sb);
1544 if (st)
1545 return st;
1546 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1547 if (!one)
1548 return -1;
1550 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1551 return -1;
1552 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1553 if (!two)
1554 return -1;
1555 if (repo_get_merge_bases(r, one, two, &mbs) < 0) {
1556 free_commit_list(mbs);
1557 return -1;
1559 if (!mbs || mbs->next)
1560 st = -1;
1561 else {
1562 st = 0;
1563 oidcpy(oid, &mbs->item->object.oid);
1565 free_commit_list(mbs);
1566 return st;
1569 /* parse @something syntax, when 'something' is not {.*} */
1570 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1572 const char *next;
1574 if (len || name[1] == '{')
1575 return -1;
1577 /* make sure it's a single @, or @@{.*}, not @foo */
1578 next = memchr(name + len + 1, '@', namelen - len - 1);
1579 if (next && next[1] != '{')
1580 return -1;
1581 if (!next)
1582 next = name + namelen;
1583 if (next != name + 1)
1584 return -1;
1586 strbuf_reset(buf);
1587 strbuf_add(buf, "HEAD", 4);
1588 return 1;
1591 static int reinterpret(struct repository *r,
1592 const char *name, int namelen, int len,
1593 struct strbuf *buf, unsigned allowed)
1595 /* we have extra data, which might need further processing */
1596 struct strbuf tmp = STRBUF_INIT;
1597 int used = buf->len;
1598 int ret;
1599 struct interpret_branch_name_options options = {
1600 .allowed = allowed
1603 strbuf_add(buf, name + len, namelen - len);
1604 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1605 /* that data was not interpreted, remove our cruft */
1606 if (ret < 0) {
1607 strbuf_setlen(buf, used);
1608 return len;
1610 strbuf_reset(buf);
1611 strbuf_addbuf(buf, &tmp);
1612 strbuf_release(&tmp);
1613 /* tweak for size of {-N} versus expanded ref name */
1614 return ret - used + len;
1617 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1619 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1620 strbuf_reset(buf);
1621 strbuf_addstr(buf, s);
1622 free(s);
1625 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1627 if (!allowed)
1628 return 1;
1630 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1631 starts_with(refname, "refs/heads/"))
1632 return 1;
1633 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1634 starts_with(refname, "refs/remotes/"))
1635 return 1;
1637 return 0;
1640 static int interpret_branch_mark(struct repository *r,
1641 const char *name, int namelen,
1642 int at, struct strbuf *buf,
1643 int (*get_mark)(const char *, int),
1644 const char *(*get_data)(struct branch *,
1645 struct strbuf *),
1646 const struct interpret_branch_name_options *options)
1648 int len;
1649 struct branch *branch;
1650 struct strbuf err = STRBUF_INIT;
1651 const char *value;
1653 len = get_mark(name + at, namelen - at);
1654 if (!len)
1655 return -1;
1657 if (memchr(name, ':', at))
1658 return -1;
1660 if (at) {
1661 char *name_str = xmemdupz(name, at);
1662 branch = branch_get(name_str);
1663 free(name_str);
1664 } else
1665 branch = branch_get(NULL);
1667 value = get_data(branch, &err);
1668 if (!value) {
1669 if (options->nonfatal_dangling_mark) {
1670 strbuf_release(&err);
1671 return -1;
1672 } else {
1673 die("%s", err.buf);
1677 if (!branch_interpret_allowed(value, options->allowed))
1678 return -1;
1680 set_shortened_ref(r, buf, value);
1681 return len + at;
1684 int repo_interpret_branch_name(struct repository *r,
1685 const char *name, int namelen,
1686 struct strbuf *buf,
1687 const struct interpret_branch_name_options *options)
1689 char *at;
1690 const char *start;
1691 int len;
1693 if (!namelen)
1694 namelen = strlen(name);
1696 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1697 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1698 if (!len) {
1699 return len; /* syntax Ok, not enough switches */
1700 } else if (len > 0) {
1701 if (len == namelen)
1702 return len; /* consumed all */
1703 else
1704 return reinterpret(r, name, namelen, len, buf,
1705 options->allowed);
1709 for (start = name;
1710 (at = memchr(start, '@', namelen - (start - name)));
1711 start = at + 1) {
1713 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1714 len = interpret_empty_at(name, namelen, at - name, buf);
1715 if (len > 0)
1716 return reinterpret(r, name, namelen, len, buf,
1717 options->allowed);
1720 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1721 upstream_mark, branch_get_upstream,
1722 options);
1723 if (len > 0)
1724 return len;
1726 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1727 push_mark, branch_get_push,
1728 options);
1729 if (len > 0)
1730 return len;
1733 return -1;
1736 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1738 int len = strlen(name);
1739 struct interpret_branch_name_options options = {
1740 .allowed = allowed
1742 int used = repo_interpret_branch_name(the_repository, name, len, sb,
1743 &options);
1745 if (used < 0)
1746 used = 0;
1747 strbuf_add(sb, name + used, len - used);
1750 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1752 if (startup_info->have_repository)
1753 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1754 else
1755 strbuf_addstr(sb, name);
1758 * This splice must be done even if we end up rejecting the
1759 * name; builtin/branch.c::copy_or_rename_branch() still wants
1760 * to see what the name expanded to so that "branch -m" can be
1761 * used as a tool to correct earlier mistakes.
1763 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1765 if (*name == '-' ||
1766 !strcmp(sb->buf, "refs/heads/HEAD"))
1767 return -1;
1769 return check_refname_format(sb->buf, 0);
1772 void object_context_release(struct object_context *ctx)
1774 free(ctx->path);
1775 strbuf_release(&ctx->symlink_path);
1779 * This is like "get_oid_basic()", except it allows "object ID expressions",
1780 * notably "xyz^" for "parent of xyz"
1782 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1784 struct object_context unused;
1785 int ret = get_oid_with_context(r, name, 0, oid, &unused);
1786 object_context_release(&unused);
1787 return ret;
1791 * This returns a non-zero value if the string (built using printf
1792 * format and the given arguments) is not a valid object.
1794 int get_oidf(struct object_id *oid, const char *fmt, ...)
1796 va_list ap;
1797 int ret;
1798 struct strbuf sb = STRBUF_INIT;
1800 va_start(ap, fmt);
1801 strbuf_vaddf(&sb, fmt, ap);
1802 va_end(ap);
1804 ret = repo_get_oid(the_repository, sb.buf, oid);
1805 strbuf_release(&sb);
1807 return ret;
1811 * Many callers know that the user meant to name a commit-ish by
1812 * syntactical positions where the object name appears. Calling this
1813 * function allows the machinery to disambiguate shorter-than-unique
1814 * abbreviated object names between commit-ish and others.
1816 * Note that this does NOT error out when the named object is not a
1817 * commit-ish. It is merely to give a hint to the disambiguation
1818 * machinery.
1820 int repo_get_oid_committish(struct repository *r,
1821 const char *name,
1822 struct object_id *oid)
1824 struct object_context unused;
1825 int ret = get_oid_with_context(r, name, GET_OID_COMMITTISH,
1826 oid, &unused);
1827 object_context_release(&unused);
1828 return ret;
1831 int repo_get_oid_treeish(struct repository *r,
1832 const char *name,
1833 struct object_id *oid)
1835 struct object_context unused;
1836 int ret = get_oid_with_context(r, name, GET_OID_TREEISH,
1837 oid, &unused);
1838 object_context_release(&unused);
1839 return ret;
1842 int repo_get_oid_commit(struct repository *r,
1843 const char *name,
1844 struct object_id *oid)
1846 struct object_context unused;
1847 int ret = get_oid_with_context(r, name, GET_OID_COMMIT,
1848 oid, &unused);
1849 object_context_release(&unused);
1850 return ret;
1853 int repo_get_oid_tree(struct repository *r,
1854 const char *name,
1855 struct object_id *oid)
1857 struct object_context unused;
1858 int ret = get_oid_with_context(r, name, GET_OID_TREE,
1859 oid, &unused);
1860 object_context_release(&unused);
1861 return ret;
1864 int repo_get_oid_blob(struct repository *r,
1865 const char *name,
1866 struct object_id *oid)
1868 struct object_context unused;
1869 int ret = get_oid_with_context(r, name, GET_OID_BLOB,
1870 oid, &unused);
1871 object_context_release(&unused);
1872 return ret;
1875 /* Must be called only when object_name:filename doesn't exist. */
1876 static void diagnose_invalid_oid_path(struct repository *r,
1877 const char *prefix,
1878 const char *filename,
1879 const struct object_id *tree_oid,
1880 const char *object_name,
1881 int object_name_len)
1883 struct object_id oid;
1884 unsigned short mode;
1886 if (!prefix)
1887 prefix = "";
1889 if (file_exists(filename))
1890 die(_("path '%s' exists on disk, but not in '%.*s'"),
1891 filename, object_name_len, object_name);
1892 if (is_missing_file_error(errno)) {
1893 char *fullname = xstrfmt("%s%s", prefix, filename);
1895 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1896 die(_("path '%s' exists, but not '%s'\n"
1897 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1898 fullname,
1899 filename,
1900 object_name_len, object_name,
1901 fullname,
1902 object_name_len, object_name,
1903 filename);
1905 die(_("path '%s' does not exist in '%.*s'"),
1906 filename, object_name_len, object_name);
1910 /* Must be called only when :stage:filename doesn't exist. */
1911 static void diagnose_invalid_index_path(struct repository *r,
1912 int stage,
1913 const char *prefix,
1914 const char *filename)
1916 struct index_state *istate = r->index;
1917 const struct cache_entry *ce;
1918 int pos;
1919 unsigned namelen = strlen(filename);
1920 struct strbuf fullname = STRBUF_INIT;
1922 if (!prefix)
1923 prefix = "";
1925 /* Wrong stage number? */
1926 pos = index_name_pos(istate, filename, namelen);
1927 if (pos < 0)
1928 pos = -pos - 1;
1929 if (pos < istate->cache_nr) {
1930 ce = istate->cache[pos];
1931 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1932 ce_namelen(ce) == namelen &&
1933 !memcmp(ce->name, filename, namelen))
1934 die(_("path '%s' is in the index, but not at stage %d\n"
1935 "hint: Did you mean ':%d:%s'?"),
1936 filename, stage,
1937 ce_stage(ce), filename);
1940 /* Confusion between relative and absolute filenames? */
1941 strbuf_addstr(&fullname, prefix);
1942 strbuf_addstr(&fullname, filename);
1943 pos = index_name_pos(istate, fullname.buf, fullname.len);
1944 if (pos < 0)
1945 pos = -pos - 1;
1946 if (pos < istate->cache_nr) {
1947 ce = istate->cache[pos];
1948 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1949 ce_namelen(ce) == fullname.len &&
1950 !memcmp(ce->name, fullname.buf, fullname.len))
1951 die(_("path '%s' is in the index, but not '%s'\n"
1952 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1953 fullname.buf, filename,
1954 ce_stage(ce), fullname.buf,
1955 ce_stage(ce), filename);
1958 if (repo_file_exists(r, filename))
1959 die(_("path '%s' exists on disk, but not in the index"), filename);
1960 if (is_missing_file_error(errno))
1961 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1962 filename);
1964 strbuf_release(&fullname);
1968 static char *resolve_relative_path(struct repository *r, const char *rel)
1970 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1971 return NULL;
1973 if (r != the_repository || !is_inside_work_tree())
1974 die(_("relative path syntax can't be used outside working tree"));
1976 /* die() inside prefix_path() if resolved path is outside worktree */
1977 return prefix_path(startup_info->prefix,
1978 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1979 rel);
1982 static int reject_tree_in_index(struct repository *repo,
1983 int only_to_die,
1984 const struct cache_entry *ce,
1985 int stage,
1986 const char *prefix,
1987 const char *cp)
1989 if (!S_ISSPARSEDIR(ce->ce_mode))
1990 return 0;
1991 if (only_to_die)
1992 diagnose_invalid_index_path(repo, stage, prefix, cp);
1993 return -1;
1996 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1997 const char *name,
1998 unsigned flags,
1999 const char *prefix,
2000 struct object_id *oid,
2001 struct object_context *oc)
2003 int ret, bracket_depth;
2004 int namelen = strlen(name);
2005 const char *cp;
2006 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
2008 memset(oc, 0, sizeof(*oc));
2009 oc->mode = S_IFINVALID;
2010 strbuf_init(&oc->symlink_path, 0);
2011 ret = get_oid_1(repo, name, namelen, oid, flags);
2012 if (!ret && flags & GET_OID_REQUIRE_PATH)
2013 die(_("<object>:<path> required, only <object> '%s' given"),
2014 name);
2015 if (!ret)
2016 return ret;
2018 * tree:path --> object name of path in tree
2019 * :path -> object name of absolute path in index
2020 * :./path -> object name of path relative to cwd in index
2021 * :[0-3]:path -> object name of path in index at stage
2022 * :/foo -> recent commit matching foo
2024 if (name[0] == ':') {
2025 int stage = 0;
2026 const struct cache_entry *ce;
2027 char *new_path = NULL;
2028 int pos;
2029 if (!only_to_die && namelen > 2 && name[1] == '/') {
2030 struct handle_one_ref_cb cb;
2031 struct commit_list *list = NULL;
2033 cb.repo = repo;
2034 cb.list = &list;
2035 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2036 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2037 commit_list_sort_by_date(&list);
2038 ret = get_oid_oneline(repo, name + 2, oid, list);
2040 free_commit_list(list);
2041 return ret;
2043 if (namelen < 3 ||
2044 name[2] != ':' ||
2045 name[1] < '0' || '3' < name[1])
2046 cp = name + 1;
2047 else {
2048 stage = name[1] - '0';
2049 cp = name + 3;
2051 new_path = resolve_relative_path(repo, cp);
2052 if (!new_path) {
2053 namelen = namelen - (cp - name);
2054 } else {
2055 cp = new_path;
2056 namelen = strlen(cp);
2059 if (flags & GET_OID_RECORD_PATH)
2060 oc->path = xstrdup(cp);
2062 if (!repo->index || !repo->index->cache)
2063 repo_read_index(repo);
2064 pos = index_name_pos(repo->index, cp, namelen);
2065 if (pos < 0)
2066 pos = -pos - 1;
2067 while (pos < repo->index->cache_nr) {
2068 ce = repo->index->cache[pos];
2069 if (ce_namelen(ce) != namelen ||
2070 memcmp(ce->name, cp, namelen))
2071 break;
2072 if (ce_stage(ce) == stage) {
2073 free(new_path);
2074 if (reject_tree_in_index(repo, only_to_die, ce,
2075 stage, prefix, cp))
2076 return -1;
2077 oidcpy(oid, &ce->oid);
2078 oc->mode = ce->ce_mode;
2079 return 0;
2081 pos++;
2083 if (only_to_die && name[1] && name[1] != '/')
2084 diagnose_invalid_index_path(repo, stage, prefix, cp);
2085 free(new_path);
2086 return -1;
2088 for (cp = name, bracket_depth = 0; *cp; cp++) {
2089 if (*cp == '{')
2090 bracket_depth++;
2091 else if (bracket_depth && *cp == '}')
2092 bracket_depth--;
2093 else if (!bracket_depth && *cp == ':')
2094 break;
2096 if (*cp == ':') {
2097 struct object_id tree_oid;
2098 int len = cp - name;
2099 unsigned sub_flags = flags;
2101 sub_flags &= ~GET_OID_DISAMBIGUATORS;
2102 sub_flags |= GET_OID_TREEISH;
2104 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
2105 const char *filename = cp+1;
2106 char *new_filename = NULL;
2108 new_filename = resolve_relative_path(repo, filename);
2109 if (new_filename)
2110 filename = new_filename;
2111 if (flags & GET_OID_FOLLOW_SYMLINKS) {
2112 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
2113 filename, oid, &oc->symlink_path,
2114 &oc->mode);
2115 } else {
2116 ret = get_tree_entry(repo, &tree_oid, filename, oid,
2117 &oc->mode);
2118 if (ret && only_to_die) {
2119 diagnose_invalid_oid_path(repo, prefix,
2120 filename,
2121 &tree_oid,
2122 name, len);
2125 if (flags & GET_OID_RECORD_PATH)
2126 oc->path = xstrdup(filename);
2128 free(new_filename);
2129 return ret;
2130 } else {
2131 if (only_to_die)
2132 die(_("invalid object name '%.*s'."), len, name);
2135 return ret;
2139 * Call this function when you know "name" given by the end user must
2140 * name an object but it doesn't; the function _may_ die with a better
2141 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2142 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2143 * you have a chance to diagnose the error further.
2145 void maybe_die_on_misspelt_object_name(struct repository *r,
2146 const char *name,
2147 const char *prefix)
2149 struct object_context oc;
2150 struct object_id oid;
2151 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY,
2152 prefix, &oid, &oc);
2153 object_context_release(&oc);
2156 enum get_oid_result get_oid_with_context(struct repository *repo,
2157 const char *str,
2158 unsigned flags,
2159 struct object_id *oid,
2160 struct object_context *oc)
2162 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
2163 BUG("incompatible flags for get_oid_with_context");
2164 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);