t: migrate helper/test-oidmap.c to unit-tests/t-oidmap.c
[git/debian.git] / object-name.c
blob1be2ad1a16c6a49736e2fdd25c4ba723dbaf5267
1 #include "git-compat-util.h"
2 #include "object-name.h"
3 #include "advice.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "tag.h"
9 #include "commit.h"
10 #include "tree.h"
11 #include "tree-walk.h"
12 #include "refs.h"
13 #include "remote.h"
14 #include "dir.h"
15 #include "oid-array.h"
16 #include "oidtree.h"
17 #include "packfile.h"
18 #include "pretty.h"
19 #include "object-store-ll.h"
20 #include "read-cache-ll.h"
21 #include "repository.h"
22 #include "setup.h"
23 #include "midx.h"
24 #include "commit-reach.h"
25 #include "date.h"
26 #include "object-file-convert.h"
28 static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);
30 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
32 struct disambiguate_state {
33 int len; /* length of prefix in hex chars */
34 char hex_pfx[GIT_MAX_HEXSZ + 1];
35 struct object_id bin_pfx;
37 struct repository *repo;
38 disambiguate_hint_fn fn;
39 void *cb_data;
40 struct object_id candidate;
41 unsigned candidate_exists:1;
42 unsigned candidate_checked:1;
43 unsigned candidate_ok:1;
44 unsigned disambiguate_fn_used:1;
45 unsigned ambiguous:1;
46 unsigned always_call_fn:1;
49 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
51 /* The hash algorithm of current has already been filtered */
52 if (ds->always_call_fn) {
53 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
54 return;
56 if (!ds->candidate_exists) {
57 /* this is the first candidate */
58 oidcpy(&ds->candidate, current);
59 ds->candidate_exists = 1;
60 return;
61 } else if (oideq(&ds->candidate, current)) {
62 /* the same as what we already have seen */
63 return;
66 if (!ds->fn) {
67 /* cannot disambiguate between ds->candidate and current */
68 ds->ambiguous = 1;
69 return;
72 if (!ds->candidate_checked) {
73 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
74 ds->disambiguate_fn_used = 1;
75 ds->candidate_checked = 1;
78 if (!ds->candidate_ok) {
79 /* discard the candidate; we know it does not satisfy fn */
80 oidcpy(&ds->candidate, current);
81 ds->candidate_checked = 0;
82 return;
85 /* if we reach this point, we know ds->candidate satisfies fn */
86 if (ds->fn(ds->repo, current, ds->cb_data)) {
88 * if both current and candidate satisfy fn, we cannot
89 * disambiguate.
91 ds->candidate_ok = 0;
92 ds->ambiguous = 1;
95 /* otherwise, current can be discarded and candidate is still good */
98 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
100 static enum cb_next match_prefix(const struct object_id *oid, void *arg)
102 struct disambiguate_state *ds = arg;
103 /* no need to call match_hash, oidtree_each did prefix match */
104 update_candidates(ds, oid);
105 return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
108 static void find_short_object_filename(struct disambiguate_state *ds)
110 struct object_directory *odb;
112 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next)
113 oidtree_each(odb_loose_cache(odb, &ds->bin_pfx),
114 &ds->bin_pfx, ds->len, match_prefix, ds);
117 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
119 do {
120 if (*a != *b)
121 return 0;
122 a++;
123 b++;
124 len -= 2;
125 } while (len > 1);
126 if (len)
127 if ((*a ^ *b) & 0xf0)
128 return 0;
129 return 1;
132 static void unique_in_midx(struct multi_pack_index *m,
133 struct disambiguate_state *ds)
135 uint32_t num, i, first = 0;
136 const struct object_id *current = NULL;
137 int len = ds->len > ds->repo->hash_algo->hexsz ?
138 ds->repo->hash_algo->hexsz : ds->len;
139 num = m->num_objects;
141 if (!num)
142 return;
144 bsearch_midx(&ds->bin_pfx, m, &first);
147 * At this point, "first" is the location of the lowest object
148 * with an object name that could match "bin_pfx". See if we have
149 * 0, 1 or more objects that actually match(es).
151 for (i = first; i < num && !ds->ambiguous; i++) {
152 struct object_id oid;
153 current = nth_midxed_object_oid(&oid, m, i);
154 if (!match_hash(len, ds->bin_pfx.hash, current->hash))
155 break;
156 update_candidates(ds, current);
160 static void unique_in_pack(struct packed_git *p,
161 struct disambiguate_state *ds)
163 uint32_t num, i, first = 0;
164 int len = ds->len > ds->repo->hash_algo->hexsz ?
165 ds->repo->hash_algo->hexsz : ds->len;
167 if (p->multi_pack_index)
168 return;
170 if (open_pack_index(p) || !p->num_objects)
171 return;
173 num = p->num_objects;
174 bsearch_pack(&ds->bin_pfx, p, &first);
177 * At this point, "first" is the location of the lowest object
178 * with an object name that could match "bin_pfx". See if we have
179 * 0, 1 or more objects that actually match(es).
181 for (i = first; i < num && !ds->ambiguous; i++) {
182 struct object_id oid;
183 nth_packed_object_id(&oid, p, i);
184 if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
185 break;
186 update_candidates(ds, &oid);
190 static void find_short_packed_object(struct disambiguate_state *ds)
192 struct multi_pack_index *m;
193 struct packed_git *p;
195 /* Skip, unless oids from the storage hash algorithm are wanted */
196 if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
197 return;
199 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
200 m = m->next)
201 unique_in_midx(m, ds);
202 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
203 p = p->next)
204 unique_in_pack(p, ds);
207 static int finish_object_disambiguation(struct disambiguate_state *ds,
208 struct object_id *oid)
210 if (ds->ambiguous)
211 return SHORT_NAME_AMBIGUOUS;
213 if (!ds->candidate_exists)
214 return MISSING_OBJECT;
216 if (!ds->candidate_checked)
218 * If this is the only candidate, there is no point
219 * calling the disambiguation hint callback.
221 * On the other hand, if the current candidate
222 * replaced an earlier candidate that did _not_ pass
223 * the disambiguation hint callback, then we do have
224 * more than one objects that match the short name
225 * given, so we should make sure this one matches;
226 * otherwise, if we discovered this one and the one
227 * that we previously discarded in the reverse order,
228 * we would end up showing different results in the
229 * same repository!
231 ds->candidate_ok = (!ds->disambiguate_fn_used ||
232 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
234 if (!ds->candidate_ok)
235 return SHORT_NAME_AMBIGUOUS;
237 oidcpy(oid, &ds->candidate);
238 return 0;
241 static int disambiguate_commit_only(struct repository *r,
242 const struct object_id *oid,
243 void *cb_data UNUSED)
245 int kind = oid_object_info(r, oid, NULL);
246 return kind == OBJ_COMMIT;
249 static int disambiguate_committish_only(struct repository *r,
250 const struct object_id *oid,
251 void *cb_data UNUSED)
253 struct object *obj;
254 int kind;
256 kind = oid_object_info(r, oid, NULL);
257 if (kind == OBJ_COMMIT)
258 return 1;
259 if (kind != OBJ_TAG)
260 return 0;
262 /* We need to do this the hard way... */
263 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
264 if (obj && obj->type == OBJ_COMMIT)
265 return 1;
266 return 0;
269 static int disambiguate_tree_only(struct repository *r,
270 const struct object_id *oid,
271 void *cb_data UNUSED)
273 int kind = oid_object_info(r, oid, NULL);
274 return kind == OBJ_TREE;
277 static int disambiguate_treeish_only(struct repository *r,
278 const struct object_id *oid,
279 void *cb_data UNUSED)
281 struct object *obj;
282 int kind;
284 kind = oid_object_info(r, oid, NULL);
285 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
286 return 1;
287 if (kind != OBJ_TAG)
288 return 0;
290 /* We need to do this the hard way... */
291 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
292 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
293 return 1;
294 return 0;
297 static int disambiguate_blob_only(struct repository *r,
298 const struct object_id *oid,
299 void *cb_data UNUSED)
301 int kind = oid_object_info(r, oid, NULL);
302 return kind == OBJ_BLOB;
305 static disambiguate_hint_fn default_disambiguate_hint;
307 int set_disambiguate_hint_config(const char *var, const char *value)
309 static const struct {
310 const char *name;
311 disambiguate_hint_fn fn;
312 } hints[] = {
313 { "none", NULL },
314 { "commit", disambiguate_commit_only },
315 { "committish", disambiguate_committish_only },
316 { "tree", disambiguate_tree_only },
317 { "treeish", disambiguate_treeish_only },
318 { "blob", disambiguate_blob_only }
320 int i;
322 if (!value)
323 return config_error_nonbool(var);
325 for (i = 0; i < ARRAY_SIZE(hints); i++) {
326 if (!strcasecmp(value, hints[i].name)) {
327 default_disambiguate_hint = hints[i].fn;
328 return 0;
332 return error("unknown hint type for '%s': %s", var, value);
335 static int init_object_disambiguation(struct repository *r,
336 const char *name, int len,
337 const struct git_hash_algo *algo,
338 struct disambiguate_state *ds)
340 int i;
342 if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
343 return -1;
345 memset(ds, 0, sizeof(*ds));
347 for (i = 0; i < len ;i++) {
348 unsigned char c = name[i];
349 unsigned char val;
350 if (c >= '0' && c <= '9')
351 val = c - '0';
352 else if (c >= 'a' && c <= 'f')
353 val = c - 'a' + 10;
354 else if (c >= 'A' && c <='F') {
355 val = c - 'A' + 10;
356 c -= 'A' - 'a';
358 else
359 return -1;
360 ds->hex_pfx[i] = c;
361 if (!(i & 1))
362 val <<= 4;
363 ds->bin_pfx.hash[i >> 1] |= val;
366 ds->len = len;
367 ds->hex_pfx[len] = '\0';
368 ds->repo = r;
369 ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
370 prepare_alt_odb(r);
371 return 0;
374 struct ambiguous_output {
375 const struct disambiguate_state *ds;
376 struct strbuf advice;
377 struct strbuf sb;
380 static int show_ambiguous_object(const struct object_id *oid, void *data)
382 struct ambiguous_output *state = data;
383 const struct disambiguate_state *ds = state->ds;
384 struct strbuf *advice = &state->advice;
385 struct strbuf *sb = &state->sb;
386 int type;
387 const char *hash;
389 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
390 return 0;
392 hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
393 type = oid_object_info(ds->repo, oid, NULL);
395 if (type < 0) {
397 * TRANSLATORS: This is a line of ambiguous object
398 * output shown when we cannot look up or parse the
399 * object in question. E.g. "deadbeef [bad object]".
401 strbuf_addf(sb, _("%s [bad object]"), hash);
402 goto out;
405 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
406 type == OBJ_BLOB || type == OBJ_TAG);
408 if (type == OBJ_COMMIT) {
409 struct strbuf date = STRBUF_INIT;
410 struct strbuf msg = STRBUF_INIT;
411 struct commit *commit = lookup_commit(ds->repo, oid);
413 if (commit) {
414 struct pretty_print_context pp = {0};
415 pp.date_mode.type = DATE_SHORT;
416 repo_format_commit_message(the_repository, commit,
417 "%ad", &date, &pp);
418 repo_format_commit_message(the_repository, commit,
419 "%s", &msg, &pp);
423 * TRANSLATORS: This is a line of ambiguous commit
424 * object output. E.g.:
426 * "deadbeef commit 2021-01-01 - Some Commit Message"
428 strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
429 msg.buf);
431 strbuf_release(&date);
432 strbuf_release(&msg);
433 } else if (type == OBJ_TAG) {
434 struct tag *tag = lookup_tag(ds->repo, oid);
436 if (!parse_tag(tag) && tag->tag) {
438 * TRANSLATORS: This is a line of ambiguous
439 * tag object output. E.g.:
441 * "deadbeef tag 2022-01-01 - Some Tag Message"
443 * The second argument is the YYYY-MM-DD found
444 * in the tag.
446 * The third argument is the "tag" string
447 * from object.c.
449 strbuf_addf(sb, _("%s tag %s - %s"), hash,
450 show_date(tag->date, 0, DATE_MODE(SHORT)),
451 tag->tag);
452 } else {
454 * TRANSLATORS: This is a line of ambiguous
455 * tag object output where we couldn't parse
456 * the tag itself. E.g.:
458 * "deadbeef [bad tag, could not parse it]"
460 strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
461 hash);
463 } else if (type == OBJ_TREE) {
465 * TRANSLATORS: This is a line of ambiguous <type>
466 * object output. E.g. "deadbeef tree".
468 strbuf_addf(sb, _("%s tree"), hash);
469 } else if (type == OBJ_BLOB) {
471 * TRANSLATORS: This is a line of ambiguous <type>
472 * object output. E.g. "deadbeef blob".
474 strbuf_addf(sb, _("%s blob"), hash);
478 out:
480 * TRANSLATORS: This is line item of ambiguous object output
481 * from describe_ambiguous_object() above. For RTL languages
482 * you'll probably want to swap the "%s" and leading " " space
483 * around.
485 strbuf_addf(advice, _(" %s\n"), sb->buf);
487 strbuf_reset(sb);
488 return 0;
491 static int collect_ambiguous(const struct object_id *oid, void *data)
493 oid_array_append(data, oid);
494 return 0;
497 static int repo_collect_ambiguous(struct repository *r UNUSED,
498 const struct object_id *oid,
499 void *data)
501 return collect_ambiguous(oid, data);
504 static int sort_ambiguous(const void *va, const void *vb, void *ctx)
506 struct repository *sort_ambiguous_repo = ctx;
507 const struct object_id *a = va, *b = vb;
508 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
509 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
510 int a_type_sort;
511 int b_type_sort;
514 * Sorts by hash within the same object type, just as
515 * oid_array_for_each_unique() would do.
517 if (a_type == b_type) {
518 if (a->algo == b->algo)
519 return oidcmp(a, b);
520 else
521 return a->algo > b->algo ? 1 : -1;
525 * Between object types show tags, then commits, and finally
526 * trees and blobs.
528 * The object_type enum is commit, tree, blob, tag, but we
529 * want tag, commit, tree blob. Cleverly (perhaps too
530 * cleverly) do that with modulus, since the enum assigns 1 to
531 * commit, so tag becomes 0.
533 a_type_sort = a_type % 4;
534 b_type_sort = b_type % 4;
535 return a_type_sort > b_type_sort ? 1 : -1;
538 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
540 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
543 static enum get_oid_result get_short_oid(struct repository *r,
544 const char *name, int len,
545 struct object_id *oid,
546 unsigned flags)
548 int status;
549 struct disambiguate_state ds;
550 int quietly = !!(flags & GET_OID_QUIETLY);
551 const struct git_hash_algo *algo = r->hash_algo;
553 if (flags & GET_OID_HASH_ANY)
554 algo = NULL;
556 if (init_object_disambiguation(r, name, len, algo, &ds) < 0)
557 return -1;
559 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
560 BUG("multiple get_short_oid disambiguator flags");
562 if (flags & GET_OID_COMMIT)
563 ds.fn = disambiguate_commit_only;
564 else if (flags & GET_OID_COMMITTISH)
565 ds.fn = disambiguate_committish_only;
566 else if (flags & GET_OID_TREE)
567 ds.fn = disambiguate_tree_only;
568 else if (flags & GET_OID_TREEISH)
569 ds.fn = disambiguate_treeish_only;
570 else if (flags & GET_OID_BLOB)
571 ds.fn = disambiguate_blob_only;
572 else
573 ds.fn = default_disambiguate_hint;
575 find_short_object_filename(&ds);
576 find_short_packed_object(&ds);
577 status = finish_object_disambiguation(&ds, oid);
580 * If we didn't find it, do the usual reprepare() slow-path,
581 * since the object may have recently been added to the repository
582 * or migrated from loose to packed.
584 if (status == MISSING_OBJECT) {
585 reprepare_packed_git(r);
586 find_short_object_filename(&ds);
587 find_short_packed_object(&ds);
588 status = finish_object_disambiguation(&ds, oid);
591 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
592 struct oid_array collect = OID_ARRAY_INIT;
593 struct ambiguous_output out = {
594 .ds = &ds,
595 .sb = STRBUF_INIT,
596 .advice = STRBUF_INIT,
599 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
602 * We may still have ambiguity if we simply saw a series of
603 * candidates that did not satisfy our hint function. In
604 * that case, we still want to show them, so disable the hint
605 * function entirely.
607 if (!ds.ambiguous)
608 ds.fn = NULL;
610 repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect);
611 sort_ambiguous_oid_array(r, &collect);
613 if (oid_array_for_each(&collect, show_ambiguous_object, &out))
614 BUG("show_ambiguous_object shouldn't return non-zero");
617 * TRANSLATORS: The argument is the list of ambiguous
618 * objects composed in show_ambiguous_object(). See
619 * its "TRANSLATORS" comments for details.
621 advise(_("The candidates are:\n%s"), out.advice.buf);
623 oid_array_clear(&collect);
624 strbuf_release(&out.advice);
625 strbuf_release(&out.sb);
628 return status;
631 int repo_for_each_abbrev(struct repository *r, const char *prefix,
632 const struct git_hash_algo *algo,
633 each_abbrev_fn fn, void *cb_data)
635 struct oid_array collect = OID_ARRAY_INIT;
636 struct disambiguate_state ds;
637 int ret;
639 if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0)
640 return -1;
642 ds.always_call_fn = 1;
643 ds.fn = repo_collect_ambiguous;
644 ds.cb_data = &collect;
645 find_short_object_filename(&ds);
646 find_short_packed_object(&ds);
648 ret = oid_array_for_each_unique(&collect, fn, cb_data);
649 oid_array_clear(&collect);
650 return ret;
654 * Return the slot of the most-significant bit set in "val". There are various
655 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
656 * probably not a big deal here.
658 static unsigned msb(unsigned long val)
660 unsigned r = 0;
661 while (val >>= 1)
662 r++;
663 return r;
666 struct min_abbrev_data {
667 unsigned int init_len;
668 unsigned int cur_len;
669 char *hex;
670 struct repository *repo;
671 const struct object_id *oid;
674 static inline char get_hex_char_from_oid(const struct object_id *oid,
675 unsigned int pos)
677 static const char hex[] = "0123456789abcdef";
679 if ((pos & 1) == 0)
680 return hex[oid->hash[pos >> 1] >> 4];
681 else
682 return hex[oid->hash[pos >> 1] & 0xf];
685 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
687 struct min_abbrev_data *mad = cb_data;
689 unsigned int i = mad->init_len;
690 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
691 i++;
693 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
694 mad->cur_len = i + 1;
696 return 0;
699 static int repo_extend_abbrev_len(struct repository *r UNUSED,
700 const struct object_id *oid,
701 void *cb_data)
703 return extend_abbrev_len(oid, cb_data);
706 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
707 struct min_abbrev_data *mad)
709 int match = 0;
710 uint32_t num, first = 0;
711 struct object_id oid;
712 const struct object_id *mad_oid;
714 if (!m->num_objects)
715 return;
717 num = m->num_objects;
718 mad_oid = mad->oid;
719 match = bsearch_midx(mad_oid, m, &first);
722 * first is now the position in the packfile where we would insert
723 * mad->hash if it does not exist (or the position of mad->hash if
724 * it does exist). Hence, we consider a maximum of two objects
725 * nearby for the abbreviation length.
727 mad->init_len = 0;
728 if (!match) {
729 if (nth_midxed_object_oid(&oid, m, first))
730 extend_abbrev_len(&oid, mad);
731 } else if (first < num - 1) {
732 if (nth_midxed_object_oid(&oid, m, first + 1))
733 extend_abbrev_len(&oid, mad);
735 if (first > 0) {
736 if (nth_midxed_object_oid(&oid, m, first - 1))
737 extend_abbrev_len(&oid, mad);
739 mad->init_len = mad->cur_len;
742 static void find_abbrev_len_for_pack(struct packed_git *p,
743 struct min_abbrev_data *mad)
745 int match = 0;
746 uint32_t num, first = 0;
747 struct object_id oid;
748 const struct object_id *mad_oid;
750 if (p->multi_pack_index)
751 return;
753 if (open_pack_index(p) || !p->num_objects)
754 return;
756 num = p->num_objects;
757 mad_oid = mad->oid;
758 match = bsearch_pack(mad_oid, p, &first);
761 * first is now the position in the packfile where we would insert
762 * mad->hash if it does not exist (or the position of mad->hash if
763 * it does exist). Hence, we consider a maximum of two objects
764 * nearby for the abbreviation length.
766 mad->init_len = 0;
767 if (!match) {
768 if (!nth_packed_object_id(&oid, p, first))
769 extend_abbrev_len(&oid, mad);
770 } else if (first < num - 1) {
771 if (!nth_packed_object_id(&oid, p, first + 1))
772 extend_abbrev_len(&oid, mad);
774 if (first > 0) {
775 if (!nth_packed_object_id(&oid, p, first - 1))
776 extend_abbrev_len(&oid, mad);
778 mad->init_len = mad->cur_len;
781 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
783 struct multi_pack_index *m;
784 struct packed_git *p;
786 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
787 find_abbrev_len_for_midx(m, mad);
788 for (p = get_packed_git(mad->repo); p; p = p->next)
789 find_abbrev_len_for_pack(p, mad);
792 void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
793 const struct object_id *oid, int abbrev_len)
795 int r;
796 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
797 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
798 strbuf_setlen(sb, sb->len + r);
801 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
802 int abbrev_len)
804 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
807 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
808 const struct object_id *oid, int len)
810 const struct git_hash_algo *algo =
811 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
812 struct disambiguate_state ds;
813 struct min_abbrev_data mad;
814 struct object_id oid_ret;
815 const unsigned hexsz = algo->hexsz;
817 if (len < 0) {
818 unsigned long count = repo_approximate_object_count(r);
820 * Add one because the MSB only tells us the highest bit set,
821 * not including the value of all the _other_ bits (so "15"
822 * is only one off of 2^4, but the MSB is the 3rd bit.
824 len = msb(count) + 1;
826 * We now know we have on the order of 2^len objects, which
827 * expects a collision at 2^(len/2). But we also care about hex
828 * chars, not bits, and there are 4 bits per hex. So all
829 * together we need to divide by 2 and round up.
831 len = DIV_ROUND_UP(len, 2);
833 * For very small repos, we stick with our regular fallback.
835 if (len < FALLBACK_DEFAULT_ABBREV)
836 len = FALLBACK_DEFAULT_ABBREV;
839 oid_to_hex_r(hex, oid);
840 if (len >= hexsz || !len)
841 return hexsz;
843 mad.repo = r;
844 mad.init_len = len;
845 mad.cur_len = len;
846 mad.hex = hex;
847 mad.oid = oid;
849 find_abbrev_len_packed(&mad);
851 if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
852 return -1;
854 ds.fn = repo_extend_abbrev_len;
855 ds.always_call_fn = 1;
856 ds.cb_data = (void *)&mad;
858 find_short_object_filename(&ds);
859 (void)finish_object_disambiguation(&ds, &oid_ret);
861 hex[mad.cur_len] = 0;
862 return mad.cur_len;
865 const char *repo_find_unique_abbrev(struct repository *r,
866 const struct object_id *oid,
867 int len)
869 static int bufno;
870 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
871 char *hex = hexbuffer[bufno];
872 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
873 repo_find_unique_abbrev_r(r, hex, oid, len);
874 return hex;
877 static int ambiguous_path(const char *path, int len)
879 int slash = 1;
880 int cnt;
882 for (cnt = 0; cnt < len; cnt++) {
883 switch (*path++) {
884 case '\0':
885 break;
886 case '/':
887 if (slash)
888 break;
889 slash = 1;
890 continue;
891 case '.':
892 continue;
893 default:
894 slash = 0;
895 continue;
897 break;
899 return slash;
902 static inline int at_mark(const char *string, int len,
903 const char **suffix, int nr)
905 int i;
907 for (i = 0; i < nr; i++) {
908 int suffix_len = strlen(suffix[i]);
909 if (suffix_len <= len
910 && !strncasecmp(string, suffix[i], suffix_len))
911 return suffix_len;
913 return 0;
916 static inline int upstream_mark(const char *string, int len)
918 const char *suffix[] = { "@{upstream}", "@{u}" };
919 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
922 static inline int push_mark(const char *string, int len)
924 const char *suffix[] = { "@{push}" };
925 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
928 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
929 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
931 static int get_oid_basic(struct repository *r, const char *str, int len,
932 struct object_id *oid, unsigned int flags)
934 static const char *warn_msg = "refname '%.*s' is ambiguous.";
935 static const char *object_name_msg = N_(
936 "Git normally never creates a ref that ends with 40 hex characters\n"
937 "because it will be ignored when you just specify 40-hex. These refs\n"
938 "may be created by mistake. For example,\n"
939 "\n"
940 " git switch -c $br $(git rev-parse ...)\n"
941 "\n"
942 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
943 "examine these refs and maybe delete them. Turn this message off by\n"
944 "running \"git config advice.objectNameWarning false\"");
945 struct object_id tmp_oid;
946 char *real_ref = NULL;
947 int refs_found = 0;
948 int at, reflog_len, nth_prior = 0;
949 int fatal = !(flags & GET_OID_QUIETLY);
951 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
952 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
953 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
954 if (refs_found > 0) {
955 warning(warn_msg, len, str);
956 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
957 fprintf(stderr, "%s\n", _(object_name_msg));
959 free(real_ref);
961 return 0;
964 /* basic@{time or number or -number} format to query ref-log */
965 reflog_len = at = 0;
966 if (len && str[len-1] == '}') {
967 for (at = len-4; at >= 0; at--) {
968 if (str[at] == '@' && str[at+1] == '{') {
969 if (str[at+2] == '-') {
970 if (at != 0)
971 /* @{-N} not at start */
972 return -1;
973 nth_prior = 1;
974 continue;
976 if (!upstream_mark(str + at, len - at) &&
977 !push_mark(str + at, len - at)) {
978 reflog_len = (len-1) - (at+2);
979 len = at;
981 break;
986 /* Accept only unambiguous ref paths. */
987 if (len && ambiguous_path(str, len))
988 return -1;
990 if (nth_prior) {
991 struct strbuf buf = STRBUF_INIT;
992 int detached;
994 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
995 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
996 strbuf_release(&buf);
997 if (detached)
998 return 0;
1002 if (!len && reflog_len)
1003 /* allow "@{...}" to mean the current branch reflog */
1004 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
1005 else if (reflog_len)
1006 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
1007 else
1008 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
1010 if (!refs_found)
1011 return -1;
1013 if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
1014 (refs_found > 1 ||
1015 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
1016 warning(warn_msg, len, str);
1018 if (reflog_len) {
1019 int nth, i;
1020 timestamp_t at_time;
1021 timestamp_t co_time;
1022 int co_tz, co_cnt;
1024 /* Is it asking for N-th entry, or approxidate? */
1025 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
1026 char ch = str[at+2+i];
1027 if ('0' <= ch && ch <= '9')
1028 nth = nth * 10 + ch - '0';
1029 else
1030 nth = -1;
1032 if (100000000 <= nth) {
1033 at_time = nth;
1034 nth = -1;
1035 } else if (0 <= nth)
1036 at_time = 0;
1037 else {
1038 int errors = 0;
1039 char *tmp = xstrndup(str + at + 2, reflog_len);
1040 at_time = approxidate_careful(tmp, &errors);
1041 free(tmp);
1042 if (errors) {
1043 free(real_ref);
1044 return -1;
1047 if (read_ref_at(get_main_ref_store(r),
1048 real_ref, flags, at_time, nth, oid, NULL,
1049 &co_time, &co_tz, &co_cnt)) {
1050 if (!len) {
1051 if (!skip_prefix(real_ref, "refs/heads/", &str))
1052 str = "HEAD";
1053 len = strlen(str);
1055 if (at_time) {
1056 if (!(flags & GET_OID_QUIETLY)) {
1057 warning(_("log for '%.*s' only goes back to %s"),
1058 len, str,
1059 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
1061 } else if (nth == co_cnt && !is_null_oid(oid)) {
1063 * We were asked for the Nth reflog (counting
1064 * from 0), but there were only N entries.
1065 * read_ref_at() will have returned "1" to tell
1066 * us it did not find an entry, but it did
1067 * still fill in the oid with the "old" value,
1068 * which we can use.
1070 } else {
1071 if (flags & GET_OID_QUIETLY) {
1072 exit(128);
1074 die(_("log for '%.*s' only has %d entries"),
1075 len, str, co_cnt);
1080 free(real_ref);
1081 return 0;
1084 static enum get_oid_result get_parent(struct repository *r,
1085 const char *name, int len,
1086 struct object_id *result, int idx)
1088 struct object_id oid;
1089 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
1090 GET_OID_COMMITTISH);
1091 struct commit *commit;
1092 struct commit_list *p;
1094 if (ret)
1095 return ret;
1096 commit = lookup_commit_reference(r, &oid);
1097 if (repo_parse_commit(r, commit))
1098 return MISSING_OBJECT;
1099 if (!idx) {
1100 oidcpy(result, &commit->object.oid);
1101 return FOUND;
1103 p = commit->parents;
1104 while (p) {
1105 if (!--idx) {
1106 oidcpy(result, &p->item->object.oid);
1107 return FOUND;
1109 p = p->next;
1111 return MISSING_OBJECT;
1114 static enum get_oid_result get_nth_ancestor(struct repository *r,
1115 const char *name, int len,
1116 struct object_id *result,
1117 int generation)
1119 struct object_id oid;
1120 struct commit *commit;
1121 int ret;
1123 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
1124 if (ret)
1125 return ret;
1126 commit = lookup_commit_reference(r, &oid);
1127 if (!commit)
1128 return MISSING_OBJECT;
1130 while (generation--) {
1131 if (repo_parse_commit(r, commit) || !commit->parents)
1132 return MISSING_OBJECT;
1133 commit = commit->parents->item;
1135 oidcpy(result, &commit->object.oid);
1136 return FOUND;
1139 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
1140 struct object *o, enum object_type expected_type)
1142 if (name && !namelen)
1143 namelen = strlen(name);
1144 while (1) {
1145 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1146 return NULL;
1147 if (expected_type == OBJ_ANY || o->type == expected_type)
1148 return o;
1149 if (o->type == OBJ_TAG)
1150 o = ((struct tag*) o)->tagged;
1151 else if (o->type == OBJ_COMMIT)
1152 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1153 else {
1154 if (name)
1155 error("%.*s: expected %s type, but the object "
1156 "dereferences to %s type",
1157 namelen, name, type_name(expected_type),
1158 type_name(o->type));
1159 return NULL;
1164 static int peel_onion(struct repository *r, const char *name, int len,
1165 struct object_id *oid, unsigned lookup_flags)
1167 struct object_id outer;
1168 const char *sp;
1169 unsigned int expected_type = 0;
1170 struct object *o;
1173 * "ref^{type}" dereferences ref repeatedly until you cannot
1174 * dereference anymore, or you get an object of given type,
1175 * whichever comes first. "ref^{}" means just dereference
1176 * tags until you get a non-tag. "ref^0" is a shorthand for
1177 * "ref^{commit}". "commit^{tree}" could be used to find the
1178 * top-level tree of the given commit.
1180 if (len < 4 || name[len-1] != '}')
1181 return -1;
1183 for (sp = name + len - 1; name <= sp; sp--) {
1184 int ch = *sp;
1185 if (ch == '{' && name < sp && sp[-1] == '^')
1186 break;
1188 if (sp <= name)
1189 return -1;
1191 sp++; /* beginning of type name, or closing brace for empty */
1192 if (starts_with(sp, "commit}"))
1193 expected_type = OBJ_COMMIT;
1194 else if (starts_with(sp, "tag}"))
1195 expected_type = OBJ_TAG;
1196 else if (starts_with(sp, "tree}"))
1197 expected_type = OBJ_TREE;
1198 else if (starts_with(sp, "blob}"))
1199 expected_type = OBJ_BLOB;
1200 else if (starts_with(sp, "object}"))
1201 expected_type = OBJ_ANY;
1202 else if (sp[0] == '}')
1203 expected_type = OBJ_NONE;
1204 else if (sp[0] == '/')
1205 expected_type = OBJ_COMMIT;
1206 else
1207 return -1;
1209 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1210 if (expected_type == OBJ_COMMIT)
1211 lookup_flags |= GET_OID_COMMITTISH;
1212 else if (expected_type == OBJ_TREE)
1213 lookup_flags |= GET_OID_TREEISH;
1215 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1216 return -1;
1218 o = parse_object(r, &outer);
1219 if (!o)
1220 return -1;
1221 if (!expected_type) {
1222 o = deref_tag(r, o, name, sp - name - 2);
1223 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1224 return -1;
1225 oidcpy(oid, &o->oid);
1226 return 0;
1230 * At this point, the syntax look correct, so
1231 * if we do not get the needed object, we should
1232 * barf.
1234 o = repo_peel_to_type(r, name, len, o, expected_type);
1235 if (!o)
1236 return -1;
1238 oidcpy(oid, &o->oid);
1239 if (sp[0] == '/') {
1240 /* "$commit^{/foo}" */
1241 char *prefix;
1242 int ret;
1243 struct commit_list *list = NULL;
1246 * $commit^{/}. Some regex implementation may reject.
1247 * We don't need regex anyway. '' pattern always matches.
1249 if (sp[1] == '}')
1250 return 0;
1252 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1253 commit_list_insert((struct commit *)o, &list);
1254 ret = get_oid_oneline(r, prefix, oid, list);
1255 free(prefix);
1256 return ret;
1258 return 0;
1261 static int get_describe_name(struct repository *r,
1262 const char *name, int len,
1263 struct object_id *oid)
1265 const char *cp;
1266 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1268 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1269 char ch = *cp;
1270 if (!isxdigit(ch)) {
1271 /* We must be looking at g in "SOMETHING-g"
1272 * for it to be describe output.
1274 if (ch == 'g' && cp[-1] == '-') {
1275 cp++;
1276 len -= cp - name;
1277 return get_short_oid(r,
1278 cp, len, oid, flags);
1282 return -1;
1285 static enum get_oid_result get_oid_1(struct repository *r,
1286 const char *name, int len,
1287 struct object_id *oid,
1288 unsigned lookup_flags)
1290 int ret, has_suffix;
1291 const char *cp;
1294 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1296 has_suffix = 0;
1297 for (cp = name + len - 1; name <= cp; cp--) {
1298 int ch = *cp;
1299 if ('0' <= ch && ch <= '9')
1300 continue;
1301 if (ch == '~' || ch == '^')
1302 has_suffix = ch;
1303 break;
1306 if (has_suffix) {
1307 unsigned int num = 0;
1308 int len1 = cp - name;
1309 cp++;
1310 while (cp < name + len) {
1311 unsigned int digit = *cp++ - '0';
1312 if (unsigned_mult_overflows(num, 10))
1313 return MISSING_OBJECT;
1314 num *= 10;
1315 if (unsigned_add_overflows(num, digit))
1316 return MISSING_OBJECT;
1317 num += digit;
1319 if (!num && len1 == len - 1)
1320 num = 1;
1321 else if (num > INT_MAX)
1322 return MISSING_OBJECT;
1323 if (has_suffix == '^')
1324 return get_parent(r, name, len1, oid, num);
1325 /* else if (has_suffix == '~') -- goes without saying */
1326 return get_nth_ancestor(r, name, len1, oid, num);
1329 ret = peel_onion(r, name, len, oid, lookup_flags);
1330 if (!ret)
1331 return FOUND;
1333 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1334 if (!ret)
1335 return FOUND;
1337 /* It could be describe output that is "SOMETHING-gXXXX" */
1338 ret = get_describe_name(r, name, len, oid);
1339 if (!ret)
1340 return FOUND;
1342 return get_short_oid(r, name, len, oid, lookup_flags);
1346 * This interprets names like ':/Initial revision of "git"' by searching
1347 * through history and returning the first commit whose message starts
1348 * the given regular expression.
1350 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1352 * For a literal '!' character at the beginning of a pattern, you have to repeat
1353 * that, like: ':/!!foo'
1355 * For future extension, all other sequences beginning with ':/!' are reserved.
1358 /* Remember to update object flag allocation in object.h */
1359 #define ONELINE_SEEN (1u<<20)
1361 struct handle_one_ref_cb {
1362 struct repository *repo;
1363 struct commit_list **list;
1366 static int handle_one_ref(const char *path, const struct object_id *oid,
1367 int flag UNUSED,
1368 void *cb_data)
1370 struct handle_one_ref_cb *cb = cb_data;
1371 struct commit_list **list = cb->list;
1372 struct object *object = parse_object(cb->repo, oid);
1373 if (!object)
1374 return 0;
1375 if (object->type == OBJ_TAG) {
1376 object = deref_tag(cb->repo, object, path,
1377 strlen(path));
1378 if (!object)
1379 return 0;
1381 if (object->type != OBJ_COMMIT)
1382 return 0;
1383 commit_list_insert((struct commit *)object, list);
1384 return 0;
1387 static int get_oid_oneline(struct repository *r,
1388 const char *prefix, struct object_id *oid,
1389 struct commit_list *list)
1391 struct commit_list *backup = NULL, *l;
1392 int found = 0;
1393 int negative = 0;
1394 regex_t regex;
1396 if (prefix[0] == '!') {
1397 prefix++;
1399 if (prefix[0] == '-') {
1400 prefix++;
1401 negative = 1;
1402 } else if (prefix[0] != '!') {
1403 return -1;
1407 if (regcomp(&regex, prefix, REG_EXTENDED))
1408 return -1;
1410 for (l = list; l; l = l->next) {
1411 l->item->object.flags |= ONELINE_SEEN;
1412 commit_list_insert(l->item, &backup);
1414 while (list) {
1415 const char *p, *buf;
1416 struct commit *commit;
1417 int matches;
1419 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1420 if (!parse_object(r, &commit->object.oid))
1421 continue;
1422 buf = repo_get_commit_buffer(r, commit, NULL);
1423 p = strstr(buf, "\n\n");
1424 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1425 repo_unuse_commit_buffer(r, commit, buf);
1427 if (matches) {
1428 oidcpy(oid, &commit->object.oid);
1429 found = 1;
1430 break;
1433 regfree(&regex);
1434 free_commit_list(list);
1435 for (l = backup; l; l = l->next)
1436 clear_commit_marks(l->item, ONELINE_SEEN);
1437 free_commit_list(backup);
1438 return found ? 0 : -1;
1441 struct grab_nth_branch_switch_cbdata {
1442 int remaining;
1443 struct strbuf *sb;
1446 static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1447 struct object_id *noid UNUSED,
1448 const char *email UNUSED,
1449 timestamp_t timestamp UNUSED,
1450 int tz UNUSED,
1451 const char *message, void *cb_data)
1453 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1454 const char *match = NULL, *target = NULL;
1455 size_t len;
1457 if (skip_prefix(message, "checkout: moving from ", &match))
1458 target = strstr(match, " to ");
1460 if (!match || !target)
1461 return 0;
1462 if (--(cb->remaining) == 0) {
1463 len = target - match;
1464 strbuf_reset(cb->sb);
1465 strbuf_add(cb->sb, match, len);
1466 return 1; /* we are done */
1468 return 0;
1472 * Parse @{-N} syntax, return the number of characters parsed
1473 * if successful; otherwise signal an error with negative value.
1475 static int interpret_nth_prior_checkout(struct repository *r,
1476 const char *name, int namelen,
1477 struct strbuf *buf)
1479 long nth;
1480 int retval;
1481 struct grab_nth_branch_switch_cbdata cb;
1482 const char *brace;
1483 char *num_end;
1485 if (namelen < 4)
1486 return -1;
1487 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1488 return -1;
1489 brace = memchr(name, '}', namelen);
1490 if (!brace)
1491 return -1;
1492 nth = strtol(name + 3, &num_end, 10);
1493 if (num_end != brace)
1494 return -1;
1495 if (nth <= 0)
1496 return -1;
1497 cb.remaining = nth;
1498 cb.sb = buf;
1500 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1501 "HEAD", grab_nth_branch_switch, &cb);
1502 if (0 < retval) {
1503 retval = brace - name + 1;
1504 } else
1505 retval = 0;
1507 return retval;
1510 int repo_get_oid_mb(struct repository *r,
1511 const char *name,
1512 struct object_id *oid)
1514 struct commit *one, *two;
1515 struct commit_list *mbs = NULL;
1516 struct object_id oid_tmp;
1517 const char *dots;
1518 int st;
1520 dots = strstr(name, "...");
1521 if (!dots)
1522 return repo_get_oid(r, name, oid);
1523 if (dots == name)
1524 st = repo_get_oid(r, "HEAD", &oid_tmp);
1525 else {
1526 struct strbuf sb;
1527 strbuf_init(&sb, dots - name);
1528 strbuf_add(&sb, name, dots - name);
1529 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1530 strbuf_release(&sb);
1532 if (st)
1533 return st;
1534 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1535 if (!one)
1536 return -1;
1538 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1539 return -1;
1540 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1541 if (!two)
1542 return -1;
1543 if (repo_get_merge_bases(r, one, two, &mbs) < 0) {
1544 free_commit_list(mbs);
1545 return -1;
1547 if (!mbs || mbs->next)
1548 st = -1;
1549 else {
1550 st = 0;
1551 oidcpy(oid, &mbs->item->object.oid);
1553 free_commit_list(mbs);
1554 return st;
1557 /* parse @something syntax, when 'something' is not {.*} */
1558 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1560 const char *next;
1562 if (len || name[1] == '{')
1563 return -1;
1565 /* make sure it's a single @, or @@{.*}, not @foo */
1566 next = memchr(name + len + 1, '@', namelen - len - 1);
1567 if (next && next[1] != '{')
1568 return -1;
1569 if (!next)
1570 next = name + namelen;
1571 if (next != name + 1)
1572 return -1;
1574 strbuf_reset(buf);
1575 strbuf_add(buf, "HEAD", 4);
1576 return 1;
1579 static int reinterpret(struct repository *r,
1580 const char *name, int namelen, int len,
1581 struct strbuf *buf, unsigned allowed)
1583 /* we have extra data, which might need further processing */
1584 struct strbuf tmp = STRBUF_INIT;
1585 int used = buf->len;
1586 int ret;
1587 struct interpret_branch_name_options options = {
1588 .allowed = allowed
1591 strbuf_add(buf, name + len, namelen - len);
1592 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1593 /* that data was not interpreted, remove our cruft */
1594 if (ret < 0) {
1595 strbuf_setlen(buf, used);
1596 return len;
1598 strbuf_reset(buf);
1599 strbuf_addbuf(buf, &tmp);
1600 strbuf_release(&tmp);
1601 /* tweak for size of {-N} versus expanded ref name */
1602 return ret - used + len;
1605 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1607 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1608 strbuf_reset(buf);
1609 strbuf_addstr(buf, s);
1610 free(s);
1613 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1615 if (!allowed)
1616 return 1;
1618 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1619 starts_with(refname, "refs/heads/"))
1620 return 1;
1621 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1622 starts_with(refname, "refs/remotes/"))
1623 return 1;
1625 return 0;
1628 static int interpret_branch_mark(struct repository *r,
1629 const char *name, int namelen,
1630 int at, struct strbuf *buf,
1631 int (*get_mark)(const char *, int),
1632 const char *(*get_data)(struct branch *,
1633 struct strbuf *),
1634 const struct interpret_branch_name_options *options)
1636 int len;
1637 struct branch *branch;
1638 struct strbuf err = STRBUF_INIT;
1639 const char *value;
1641 len = get_mark(name + at, namelen - at);
1642 if (!len)
1643 return -1;
1645 if (memchr(name, ':', at))
1646 return -1;
1648 if (at) {
1649 char *name_str = xmemdupz(name, at);
1650 branch = branch_get(name_str);
1651 free(name_str);
1652 } else
1653 branch = branch_get(NULL);
1655 value = get_data(branch, &err);
1656 if (!value) {
1657 if (options->nonfatal_dangling_mark) {
1658 strbuf_release(&err);
1659 return -1;
1660 } else {
1661 die("%s", err.buf);
1665 if (!branch_interpret_allowed(value, options->allowed))
1666 return -1;
1668 set_shortened_ref(r, buf, value);
1669 return len + at;
1672 int repo_interpret_branch_name(struct repository *r,
1673 const char *name, int namelen,
1674 struct strbuf *buf,
1675 const struct interpret_branch_name_options *options)
1677 char *at;
1678 const char *start;
1679 int len;
1681 if (!namelen)
1682 namelen = strlen(name);
1684 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1685 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1686 if (!len) {
1687 return len; /* syntax Ok, not enough switches */
1688 } else if (len > 0) {
1689 if (len == namelen)
1690 return len; /* consumed all */
1691 else
1692 return reinterpret(r, name, namelen, len, buf,
1693 options->allowed);
1697 for (start = name;
1698 (at = memchr(start, '@', namelen - (start - name)));
1699 start = at + 1) {
1701 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1702 len = interpret_empty_at(name, namelen, at - name, buf);
1703 if (len > 0)
1704 return reinterpret(r, name, namelen, len, buf,
1705 options->allowed);
1708 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1709 upstream_mark, branch_get_upstream,
1710 options);
1711 if (len > 0)
1712 return len;
1714 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1715 push_mark, branch_get_push,
1716 options);
1717 if (len > 0)
1718 return len;
1721 return -1;
1724 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1726 int len = strlen(name);
1727 struct interpret_branch_name_options options = {
1728 .allowed = allowed
1730 int used = repo_interpret_branch_name(the_repository, name, len, sb,
1731 &options);
1733 if (used < 0)
1734 used = 0;
1735 strbuf_add(sb, name + used, len - used);
1738 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1740 if (startup_info->have_repository)
1741 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1742 else
1743 strbuf_addstr(sb, name);
1746 * This splice must be done even if we end up rejecting the
1747 * name; builtin/branch.c::copy_or_rename_branch() still wants
1748 * to see what the name expanded to so that "branch -m" can be
1749 * used as a tool to correct earlier mistakes.
1751 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1753 if (*name == '-' ||
1754 !strcmp(sb->buf, "refs/heads/HEAD"))
1755 return -1;
1757 return check_refname_format(sb->buf, 0);
1761 * This is like "get_oid_basic()", except it allows "object ID expressions",
1762 * notably "xyz^" for "parent of xyz"
1764 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1766 struct object_context unused;
1767 return get_oid_with_context(r, name, 0, oid, &unused);
1771 * This returns a non-zero value if the string (built using printf
1772 * format and the given arguments) is not a valid object.
1774 int get_oidf(struct object_id *oid, const char *fmt, ...)
1776 va_list ap;
1777 int ret;
1778 struct strbuf sb = STRBUF_INIT;
1780 va_start(ap, fmt);
1781 strbuf_vaddf(&sb, fmt, ap);
1782 va_end(ap);
1784 ret = repo_get_oid(the_repository, sb.buf, oid);
1785 strbuf_release(&sb);
1787 return ret;
1791 * Many callers know that the user meant to name a commit-ish by
1792 * syntactical positions where the object name appears. Calling this
1793 * function allows the machinery to disambiguate shorter-than-unique
1794 * abbreviated object names between commit-ish and others.
1796 * Note that this does NOT error out when the named object is not a
1797 * commit-ish. It is merely to give a hint to the disambiguation
1798 * machinery.
1800 int repo_get_oid_committish(struct repository *r,
1801 const char *name,
1802 struct object_id *oid)
1804 struct object_context unused;
1805 return get_oid_with_context(r, name, GET_OID_COMMITTISH,
1806 oid, &unused);
1809 int repo_get_oid_treeish(struct repository *r,
1810 const char *name,
1811 struct object_id *oid)
1813 struct object_context unused;
1814 return get_oid_with_context(r, name, GET_OID_TREEISH,
1815 oid, &unused);
1818 int repo_get_oid_commit(struct repository *r,
1819 const char *name,
1820 struct object_id *oid)
1822 struct object_context unused;
1823 return get_oid_with_context(r, name, GET_OID_COMMIT,
1824 oid, &unused);
1827 int repo_get_oid_tree(struct repository *r,
1828 const char *name,
1829 struct object_id *oid)
1831 struct object_context unused;
1832 return get_oid_with_context(r, name, GET_OID_TREE,
1833 oid, &unused);
1836 int repo_get_oid_blob(struct repository *r,
1837 const char *name,
1838 struct object_id *oid)
1840 struct object_context unused;
1841 return get_oid_with_context(r, name, GET_OID_BLOB,
1842 oid, &unused);
1845 /* Must be called only when object_name:filename doesn't exist. */
1846 static void diagnose_invalid_oid_path(struct repository *r,
1847 const char *prefix,
1848 const char *filename,
1849 const struct object_id *tree_oid,
1850 const char *object_name,
1851 int object_name_len)
1853 struct object_id oid;
1854 unsigned short mode;
1856 if (!prefix)
1857 prefix = "";
1859 if (file_exists(filename))
1860 die(_("path '%s' exists on disk, but not in '%.*s'"),
1861 filename, object_name_len, object_name);
1862 if (is_missing_file_error(errno)) {
1863 char *fullname = xstrfmt("%s%s", prefix, filename);
1865 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1866 die(_("path '%s' exists, but not '%s'\n"
1867 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1868 fullname,
1869 filename,
1870 object_name_len, object_name,
1871 fullname,
1872 object_name_len, object_name,
1873 filename);
1875 die(_("path '%s' does not exist in '%.*s'"),
1876 filename, object_name_len, object_name);
1880 /* Must be called only when :stage:filename doesn't exist. */
1881 static void diagnose_invalid_index_path(struct repository *r,
1882 int stage,
1883 const char *prefix,
1884 const char *filename)
1886 struct index_state *istate = r->index;
1887 const struct cache_entry *ce;
1888 int pos;
1889 unsigned namelen = strlen(filename);
1890 struct strbuf fullname = STRBUF_INIT;
1892 if (!prefix)
1893 prefix = "";
1895 /* Wrong stage number? */
1896 pos = index_name_pos(istate, filename, namelen);
1897 if (pos < 0)
1898 pos = -pos - 1;
1899 if (pos < istate->cache_nr) {
1900 ce = istate->cache[pos];
1901 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1902 ce_namelen(ce) == namelen &&
1903 !memcmp(ce->name, filename, namelen))
1904 die(_("path '%s' is in the index, but not at stage %d\n"
1905 "hint: Did you mean ':%d:%s'?"),
1906 filename, stage,
1907 ce_stage(ce), filename);
1910 /* Confusion between relative and absolute filenames? */
1911 strbuf_addstr(&fullname, prefix);
1912 strbuf_addstr(&fullname, filename);
1913 pos = index_name_pos(istate, fullname.buf, fullname.len);
1914 if (pos < 0)
1915 pos = -pos - 1;
1916 if (pos < istate->cache_nr) {
1917 ce = istate->cache[pos];
1918 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1919 ce_namelen(ce) == fullname.len &&
1920 !memcmp(ce->name, fullname.buf, fullname.len))
1921 die(_("path '%s' is in the index, but not '%s'\n"
1922 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1923 fullname.buf, filename,
1924 ce_stage(ce), fullname.buf,
1925 ce_stage(ce), filename);
1928 if (repo_file_exists(r, filename))
1929 die(_("path '%s' exists on disk, but not in the index"), filename);
1930 if (is_missing_file_error(errno))
1931 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1932 filename);
1934 strbuf_release(&fullname);
1938 static char *resolve_relative_path(struct repository *r, const char *rel)
1940 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1941 return NULL;
1943 if (r != the_repository || !is_inside_work_tree())
1944 die(_("relative path syntax can't be used outside working tree"));
1946 /* die() inside prefix_path() if resolved path is outside worktree */
1947 return prefix_path(startup_info->prefix,
1948 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1949 rel);
1952 static int reject_tree_in_index(struct repository *repo,
1953 int only_to_die,
1954 const struct cache_entry *ce,
1955 int stage,
1956 const char *prefix,
1957 const char *cp)
1959 if (!S_ISSPARSEDIR(ce->ce_mode))
1960 return 0;
1961 if (only_to_die)
1962 diagnose_invalid_index_path(repo, stage, prefix, cp);
1963 return -1;
1966 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1967 const char *name,
1968 unsigned flags,
1969 const char *prefix,
1970 struct object_id *oid,
1971 struct object_context *oc)
1973 int ret, bracket_depth;
1974 int namelen = strlen(name);
1975 const char *cp;
1976 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
1978 memset(oc, 0, sizeof(*oc));
1979 oc->mode = S_IFINVALID;
1980 strbuf_init(&oc->symlink_path, 0);
1981 ret = get_oid_1(repo, name, namelen, oid, flags);
1982 if (!ret && flags & GET_OID_REQUIRE_PATH)
1983 die(_("<object>:<path> required, only <object> '%s' given"),
1984 name);
1985 if (!ret)
1986 return ret;
1988 * tree:path --> object name of path in tree
1989 * :path -> object name of absolute path in index
1990 * :./path -> object name of path relative to cwd in index
1991 * :[0-3]:path -> object name of path in index at stage
1992 * :/foo -> recent commit matching foo
1994 if (name[0] == ':') {
1995 int stage = 0;
1996 const struct cache_entry *ce;
1997 char *new_path = NULL;
1998 int pos;
1999 if (!only_to_die && namelen > 2 && name[1] == '/') {
2000 struct handle_one_ref_cb cb;
2001 struct commit_list *list = NULL;
2003 cb.repo = repo;
2004 cb.list = &list;
2005 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2006 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2007 commit_list_sort_by_date(&list);
2008 return get_oid_oneline(repo, name + 2, oid, list);
2010 if (namelen < 3 ||
2011 name[2] != ':' ||
2012 name[1] < '0' || '3' < name[1])
2013 cp = name + 1;
2014 else {
2015 stage = name[1] - '0';
2016 cp = name + 3;
2018 new_path = resolve_relative_path(repo, cp);
2019 if (!new_path) {
2020 namelen = namelen - (cp - name);
2021 } else {
2022 cp = new_path;
2023 namelen = strlen(cp);
2026 if (flags & GET_OID_RECORD_PATH)
2027 oc->path = xstrdup(cp);
2029 if (!repo->index || !repo->index->cache)
2030 repo_read_index(repo);
2031 pos = index_name_pos(repo->index, cp, namelen);
2032 if (pos < 0)
2033 pos = -pos - 1;
2034 while (pos < repo->index->cache_nr) {
2035 ce = repo->index->cache[pos];
2036 if (ce_namelen(ce) != namelen ||
2037 memcmp(ce->name, cp, namelen))
2038 break;
2039 if (ce_stage(ce) == stage) {
2040 free(new_path);
2041 if (reject_tree_in_index(repo, only_to_die, ce,
2042 stage, prefix, cp))
2043 return -1;
2044 oidcpy(oid, &ce->oid);
2045 oc->mode = ce->ce_mode;
2046 return 0;
2048 pos++;
2050 if (only_to_die && name[1] && name[1] != '/')
2051 diagnose_invalid_index_path(repo, stage, prefix, cp);
2052 free(new_path);
2053 return -1;
2055 for (cp = name, bracket_depth = 0; *cp; cp++) {
2056 if (*cp == '{')
2057 bracket_depth++;
2058 else if (bracket_depth && *cp == '}')
2059 bracket_depth--;
2060 else if (!bracket_depth && *cp == ':')
2061 break;
2063 if (*cp == ':') {
2064 struct object_id tree_oid;
2065 int len = cp - name;
2066 unsigned sub_flags = flags;
2068 sub_flags &= ~GET_OID_DISAMBIGUATORS;
2069 sub_flags |= GET_OID_TREEISH;
2071 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
2072 const char *filename = cp+1;
2073 char *new_filename = NULL;
2075 new_filename = resolve_relative_path(repo, filename);
2076 if (new_filename)
2077 filename = new_filename;
2078 if (flags & GET_OID_FOLLOW_SYMLINKS) {
2079 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
2080 filename, oid, &oc->symlink_path,
2081 &oc->mode);
2082 } else {
2083 ret = get_tree_entry(repo, &tree_oid, filename, oid,
2084 &oc->mode);
2085 if (ret && only_to_die) {
2086 diagnose_invalid_oid_path(repo, prefix,
2087 filename,
2088 &tree_oid,
2089 name, len);
2092 if (flags & GET_OID_RECORD_PATH)
2093 oc->path = xstrdup(filename);
2095 free(new_filename);
2096 return ret;
2097 } else {
2098 if (only_to_die)
2099 die(_("invalid object name '%.*s'."), len, name);
2102 return ret;
2106 * Call this function when you know "name" given by the end user must
2107 * name an object but it doesn't; the function _may_ die with a better
2108 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2109 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2110 * you have a chance to diagnose the error further.
2112 void maybe_die_on_misspelt_object_name(struct repository *r,
2113 const char *name,
2114 const char *prefix)
2116 struct object_context oc;
2117 struct object_id oid;
2118 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY,
2119 prefix, &oid, &oc);
2122 enum get_oid_result get_oid_with_context(struct repository *repo,
2123 const char *str,
2124 unsigned flags,
2125 struct object_id *oid,
2126 struct object_context *oc)
2128 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
2129 BUG("incompatible flags for get_oid_with_context");
2130 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);