perl: fix a typo
[git/gitster.git] / object-name.c
blob527b853ac456cb1ba243106a3a165eec6d247cf8
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 *, struct commit_list *);
32 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
34 struct disambiguate_state {
35 int len; /* length of prefix in hex chars */
36 char hex_pfx[GIT_MAX_HEXSZ + 1];
37 struct object_id bin_pfx;
39 struct repository *repo;
40 disambiguate_hint_fn fn;
41 void *cb_data;
42 struct object_id candidate;
43 unsigned candidate_exists:1;
44 unsigned candidate_checked:1;
45 unsigned candidate_ok:1;
46 unsigned disambiguate_fn_used:1;
47 unsigned ambiguous:1;
48 unsigned always_call_fn:1;
51 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
53 /* The hash algorithm of current has already been filtered */
54 if (ds->always_call_fn) {
55 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
56 return;
58 if (!ds->candidate_exists) {
59 /* this is the first candidate */
60 oidcpy(&ds->candidate, current);
61 ds->candidate_exists = 1;
62 return;
63 } else if (oideq(&ds->candidate, current)) {
64 /* the same as what we already have seen */
65 return;
68 if (!ds->fn) {
69 /* cannot disambiguate between ds->candidate and current */
70 ds->ambiguous = 1;
71 return;
74 if (!ds->candidate_checked) {
75 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
76 ds->disambiguate_fn_used = 1;
77 ds->candidate_checked = 1;
80 if (!ds->candidate_ok) {
81 /* discard the candidate; we know it does not satisfy fn */
82 oidcpy(&ds->candidate, current);
83 ds->candidate_checked = 0;
84 return;
87 /* if we reach this point, we know ds->candidate satisfies fn */
88 if (ds->fn(ds->repo, current, ds->cb_data)) {
90 * if both current and candidate satisfy fn, we cannot
91 * disambiguate.
93 ds->candidate_ok = 0;
94 ds->ambiguous = 1;
97 /* otherwise, current can be discarded and candidate is still good */
100 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
102 static enum cb_next match_prefix(const struct object_id *oid, void *arg)
104 struct disambiguate_state *ds = arg;
105 /* no need to call match_hash, oidtree_each did prefix match */
106 update_candidates(ds, oid);
107 return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
110 static void find_short_object_filename(struct disambiguate_state *ds)
112 struct object_directory *odb;
114 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next)
115 oidtree_each(odb_loose_cache(odb, &ds->bin_pfx),
116 &ds->bin_pfx, ds->len, match_prefix, ds);
119 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
121 do {
122 if (*a != *b)
123 return 0;
124 a++;
125 b++;
126 len -= 2;
127 } while (len > 1);
128 if (len)
129 if ((*a ^ *b) & 0xf0)
130 return 0;
131 return 1;
134 static void unique_in_midx(struct multi_pack_index *m,
135 struct disambiguate_state *ds)
137 uint32_t num, i, first = 0;
138 const struct object_id *current = NULL;
139 int len = ds->len > ds->repo->hash_algo->hexsz ?
140 ds->repo->hash_algo->hexsz : ds->len;
141 num = m->num_objects;
143 if (!num)
144 return;
146 bsearch_midx(&ds->bin_pfx, m, &first);
149 * At this point, "first" is the location of the lowest object
150 * with an object name that could match "bin_pfx". See if we have
151 * 0, 1 or more objects that actually match(es).
153 for (i = first; i < num && !ds->ambiguous; i++) {
154 struct object_id oid;
155 current = nth_midxed_object_oid(&oid, m, i);
156 if (!match_hash(len, ds->bin_pfx.hash, current->hash))
157 break;
158 update_candidates(ds, current);
162 static void unique_in_pack(struct packed_git *p,
163 struct disambiguate_state *ds)
165 uint32_t num, i, first = 0;
166 int len = ds->len > ds->repo->hash_algo->hexsz ?
167 ds->repo->hash_algo->hexsz : ds->len;
169 if (p->multi_pack_index)
170 return;
172 if (open_pack_index(p) || !p->num_objects)
173 return;
175 num = p->num_objects;
176 bsearch_pack(&ds->bin_pfx, p, &first);
179 * At this point, "first" is the location of the lowest object
180 * with an object name that could match "bin_pfx". See if we have
181 * 0, 1 or more objects that actually match(es).
183 for (i = first; i < num && !ds->ambiguous; i++) {
184 struct object_id oid;
185 nth_packed_object_id(&oid, p, i);
186 if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
187 break;
188 update_candidates(ds, &oid);
192 static void find_short_packed_object(struct disambiguate_state *ds)
194 struct multi_pack_index *m;
195 struct packed_git *p;
197 /* Skip, unless oids from the storage hash algorithm are wanted */
198 if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
199 return;
201 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
202 m = m->next)
203 unique_in_midx(m, ds);
204 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
205 p = p->next)
206 unique_in_pack(p, ds);
209 static int finish_object_disambiguation(struct disambiguate_state *ds,
210 struct object_id *oid)
212 if (ds->ambiguous)
213 return SHORT_NAME_AMBIGUOUS;
215 if (!ds->candidate_exists)
216 return MISSING_OBJECT;
218 if (!ds->candidate_checked)
220 * If this is the only candidate, there is no point
221 * calling the disambiguation hint callback.
223 * On the other hand, if the current candidate
224 * replaced an earlier candidate that did _not_ pass
225 * the disambiguation hint callback, then we do have
226 * more than one objects that match the short name
227 * given, so we should make sure this one matches;
228 * otherwise, if we discovered this one and the one
229 * that we previously discarded in the reverse order,
230 * we would end up showing different results in the
231 * same repository!
233 ds->candidate_ok = (!ds->disambiguate_fn_used ||
234 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
236 if (!ds->candidate_ok)
237 return SHORT_NAME_AMBIGUOUS;
239 oidcpy(oid, &ds->candidate);
240 return 0;
243 static int disambiguate_commit_only(struct repository *r,
244 const struct object_id *oid,
245 void *cb_data UNUSED)
247 int kind = oid_object_info(r, oid, NULL);
248 return kind == OBJ_COMMIT;
251 static int disambiguate_committish_only(struct repository *r,
252 const struct object_id *oid,
253 void *cb_data UNUSED)
255 struct object *obj;
256 int kind;
258 kind = oid_object_info(r, oid, NULL);
259 if (kind == OBJ_COMMIT)
260 return 1;
261 if (kind != OBJ_TAG)
262 return 0;
264 /* We need to do this the hard way... */
265 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
266 if (obj && obj->type == OBJ_COMMIT)
267 return 1;
268 return 0;
271 static int disambiguate_tree_only(struct repository *r,
272 const struct object_id *oid,
273 void *cb_data UNUSED)
275 int kind = oid_object_info(r, oid, NULL);
276 return kind == OBJ_TREE;
279 static int disambiguate_treeish_only(struct repository *r,
280 const struct object_id *oid,
281 void *cb_data UNUSED)
283 struct object *obj;
284 int kind;
286 kind = oid_object_info(r, oid, NULL);
287 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
288 return 1;
289 if (kind != OBJ_TAG)
290 return 0;
292 /* We need to do this the hard way... */
293 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
294 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
295 return 1;
296 return 0;
299 static int disambiguate_blob_only(struct repository *r,
300 const struct object_id *oid,
301 void *cb_data UNUSED)
303 int kind = oid_object_info(r, oid, NULL);
304 return kind == OBJ_BLOB;
307 static disambiguate_hint_fn default_disambiguate_hint;
309 int set_disambiguate_hint_config(const char *var, const char *value)
311 static const struct {
312 const char *name;
313 disambiguate_hint_fn fn;
314 } hints[] = {
315 { "none", NULL },
316 { "commit", disambiguate_commit_only },
317 { "committish", disambiguate_committish_only },
318 { "tree", disambiguate_tree_only },
319 { "treeish", disambiguate_treeish_only },
320 { "blob", disambiguate_blob_only }
322 int i;
324 if (!value)
325 return config_error_nonbool(var);
327 for (i = 0; i < ARRAY_SIZE(hints); i++) {
328 if (!strcasecmp(value, hints[i].name)) {
329 default_disambiguate_hint = hints[i].fn;
330 return 0;
334 return error("unknown hint type for '%s': %s", var, value);
337 static int init_object_disambiguation(struct repository *r,
338 const char *name, int len,
339 const struct git_hash_algo *algo,
340 struct disambiguate_state *ds)
342 int i;
344 if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
345 return -1;
347 memset(ds, 0, sizeof(*ds));
349 for (i = 0; i < len ;i++) {
350 unsigned char c = name[i];
351 unsigned char val;
352 if (c >= '0' && c <= '9')
353 val = c - '0';
354 else if (c >= 'a' && c <= 'f')
355 val = c - 'a' + 10;
356 else if (c >= 'A' && c <='F') {
357 val = c - 'A' + 10;
358 c -= 'A' - 'a';
360 else
361 return -1;
362 ds->hex_pfx[i] = c;
363 if (!(i & 1))
364 val <<= 4;
365 ds->bin_pfx.hash[i >> 1] |= val;
368 ds->len = len;
369 ds->hex_pfx[len] = '\0';
370 ds->repo = r;
371 ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
372 prepare_alt_odb(r);
373 return 0;
376 struct ambiguous_output {
377 const struct disambiguate_state *ds;
378 struct strbuf advice;
379 struct strbuf sb;
382 static int show_ambiguous_object(const struct object_id *oid, void *data)
384 struct ambiguous_output *state = data;
385 const struct disambiguate_state *ds = state->ds;
386 struct strbuf *advice = &state->advice;
387 struct strbuf *sb = &state->sb;
388 int type;
389 const char *hash;
391 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
392 return 0;
394 hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
395 type = oid_object_info(ds->repo, oid, NULL);
397 if (type < 0) {
399 * TRANSLATORS: This is a line of ambiguous object
400 * output shown when we cannot look up or parse the
401 * object in question. E.g. "deadbeef [bad object]".
403 strbuf_addf(sb, _("%s [bad object]"), hash);
404 goto out;
407 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
408 type == OBJ_BLOB || type == OBJ_TAG);
410 if (type == OBJ_COMMIT) {
411 struct strbuf date = STRBUF_INIT;
412 struct strbuf msg = STRBUF_INIT;
413 struct commit *commit = lookup_commit(ds->repo, oid);
415 if (commit) {
416 struct pretty_print_context pp = {0};
417 pp.date_mode.type = DATE_SHORT;
418 repo_format_commit_message(the_repository, commit,
419 "%ad", &date, &pp);
420 repo_format_commit_message(the_repository, commit,
421 "%s", &msg, &pp);
425 * TRANSLATORS: This is a line of ambiguous commit
426 * object output. E.g.:
428 * "deadbeef commit 2021-01-01 - Some Commit Message"
430 strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
431 msg.buf);
433 strbuf_release(&date);
434 strbuf_release(&msg);
435 } else if (type == OBJ_TAG) {
436 struct tag *tag = lookup_tag(ds->repo, oid);
438 if (!parse_tag(tag) && tag->tag) {
440 * TRANSLATORS: This is a line of ambiguous
441 * tag object output. E.g.:
443 * "deadbeef tag 2022-01-01 - Some Tag Message"
445 * The second argument is the YYYY-MM-DD found
446 * in the tag.
448 * The third argument is the "tag" string
449 * from object.c.
451 strbuf_addf(sb, _("%s tag %s - %s"), hash,
452 show_date(tag->date, 0, DATE_MODE(SHORT)),
453 tag->tag);
454 } else {
456 * TRANSLATORS: This is a line of ambiguous
457 * tag object output where we couldn't parse
458 * the tag itself. E.g.:
460 * "deadbeef [bad tag, could not parse it]"
462 strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
463 hash);
465 } else if (type == OBJ_TREE) {
467 * TRANSLATORS: This is a line of ambiguous <type>
468 * object output. E.g. "deadbeef tree".
470 strbuf_addf(sb, _("%s tree"), hash);
471 } else if (type == OBJ_BLOB) {
473 * TRANSLATORS: This is a line of ambiguous <type>
474 * object output. E.g. "deadbeef blob".
476 strbuf_addf(sb, _("%s blob"), hash);
480 out:
482 * TRANSLATORS: This is line item of ambiguous object output
483 * from describe_ambiguous_object() above. For RTL languages
484 * you'll probably want to swap the "%s" and leading " " space
485 * around.
487 strbuf_addf(advice, _(" %s\n"), sb->buf);
489 strbuf_reset(sb);
490 return 0;
493 static int collect_ambiguous(const struct object_id *oid, void *data)
495 oid_array_append(data, oid);
496 return 0;
499 static int repo_collect_ambiguous(struct repository *r UNUSED,
500 const struct object_id *oid,
501 void *data)
503 return collect_ambiguous(oid, data);
506 static int sort_ambiguous(const void *va, const void *vb, void *ctx)
508 struct repository *sort_ambiguous_repo = ctx;
509 const struct object_id *a = va, *b = vb;
510 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
511 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
512 int a_type_sort;
513 int b_type_sort;
516 * Sorts by hash within the same object type, just as
517 * oid_array_for_each_unique() would do.
519 if (a_type == b_type) {
520 if (a->algo == b->algo)
521 return oidcmp(a, b);
522 else
523 return a->algo > b->algo ? 1 : -1;
527 * Between object types show tags, then commits, and finally
528 * trees and blobs.
530 * The object_type enum is commit, tree, blob, tag, but we
531 * want tag, commit, tree blob. Cleverly (perhaps too
532 * cleverly) do that with modulus, since the enum assigns 1 to
533 * commit, so tag becomes 0.
535 a_type_sort = a_type % 4;
536 b_type_sort = b_type % 4;
537 return a_type_sort > b_type_sort ? 1 : -1;
540 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
542 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
545 static enum get_oid_result get_short_oid(struct repository *r,
546 const char *name, int len,
547 struct object_id *oid,
548 unsigned flags)
550 int status;
551 struct disambiguate_state ds;
552 int quietly = !!(flags & GET_OID_QUIETLY);
553 const struct git_hash_algo *algo = r->hash_algo;
555 if (flags & GET_OID_HASH_ANY)
556 algo = NULL;
558 if (init_object_disambiguation(r, name, len, algo, &ds) < 0)
559 return -1;
561 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
562 BUG("multiple get_short_oid disambiguator flags");
564 if (flags & GET_OID_COMMIT)
565 ds.fn = disambiguate_commit_only;
566 else if (flags & GET_OID_COMMITTISH)
567 ds.fn = disambiguate_committish_only;
568 else if (flags & GET_OID_TREE)
569 ds.fn = disambiguate_tree_only;
570 else if (flags & GET_OID_TREEISH)
571 ds.fn = disambiguate_treeish_only;
572 else if (flags & GET_OID_BLOB)
573 ds.fn = disambiguate_blob_only;
574 else
575 ds.fn = default_disambiguate_hint;
577 find_short_object_filename(&ds);
578 find_short_packed_object(&ds);
579 status = finish_object_disambiguation(&ds, oid);
582 * If we didn't find it, do the usual reprepare() slow-path,
583 * since the object may have recently been added to the repository
584 * or migrated from loose to packed.
586 if (status == MISSING_OBJECT) {
587 reprepare_packed_git(r);
588 find_short_object_filename(&ds);
589 find_short_packed_object(&ds);
590 status = finish_object_disambiguation(&ds, oid);
593 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
594 struct oid_array collect = OID_ARRAY_INIT;
595 struct ambiguous_output out = {
596 .ds = &ds,
597 .sb = STRBUF_INIT,
598 .advice = STRBUF_INIT,
601 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
604 * We may still have ambiguity if we simply saw a series of
605 * candidates that did not satisfy our hint function. In
606 * that case, we still want to show them, so disable the hint
607 * function entirely.
609 if (!ds.ambiguous)
610 ds.fn = NULL;
612 repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect);
613 sort_ambiguous_oid_array(r, &collect);
615 if (oid_array_for_each(&collect, show_ambiguous_object, &out))
616 BUG("show_ambiguous_object shouldn't return non-zero");
619 * TRANSLATORS: The argument is the list of ambiguous
620 * objects composed in show_ambiguous_object(). See
621 * its "TRANSLATORS" comments for details.
623 advise(_("The candidates are:\n%s"), out.advice.buf);
625 oid_array_clear(&collect);
626 strbuf_release(&out.advice);
627 strbuf_release(&out.sb);
630 return status;
633 int repo_for_each_abbrev(struct repository *r, const char *prefix,
634 const struct git_hash_algo *algo,
635 each_abbrev_fn fn, void *cb_data)
637 struct oid_array collect = OID_ARRAY_INIT;
638 struct disambiguate_state ds;
639 int ret;
641 if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0)
642 return -1;
644 ds.always_call_fn = 1;
645 ds.fn = repo_collect_ambiguous;
646 ds.cb_data = &collect;
647 find_short_object_filename(&ds);
648 find_short_packed_object(&ds);
650 ret = oid_array_for_each_unique(&collect, fn, cb_data);
651 oid_array_clear(&collect);
652 return ret;
656 * Return the slot of the most-significant bit set in "val". There are various
657 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
658 * probably not a big deal here.
660 static unsigned msb(unsigned long val)
662 unsigned r = 0;
663 while (val >>= 1)
664 r++;
665 return r;
668 struct min_abbrev_data {
669 unsigned int init_len;
670 unsigned int cur_len;
671 char *hex;
672 struct repository *repo;
673 const struct object_id *oid;
676 static inline char get_hex_char_from_oid(const struct object_id *oid,
677 unsigned int pos)
679 static const char hex[] = "0123456789abcdef";
681 if ((pos & 1) == 0)
682 return hex[oid->hash[pos >> 1] >> 4];
683 else
684 return hex[oid->hash[pos >> 1] & 0xf];
687 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
689 struct min_abbrev_data *mad = cb_data;
691 unsigned int i = mad->init_len;
692 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
693 i++;
695 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
696 mad->cur_len = i + 1;
698 return 0;
701 static int repo_extend_abbrev_len(struct repository *r UNUSED,
702 const struct object_id *oid,
703 void *cb_data)
705 return extend_abbrev_len(oid, cb_data);
708 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
709 struct min_abbrev_data *mad)
711 int match = 0;
712 uint32_t num, first = 0;
713 struct object_id oid;
714 const struct object_id *mad_oid;
716 if (!m->num_objects)
717 return;
719 num = m->num_objects;
720 mad_oid = mad->oid;
721 match = bsearch_midx(mad_oid, m, &first);
724 * first is now the position in the packfile where we would insert
725 * mad->hash if it does not exist (or the position of mad->hash if
726 * it does exist). Hence, we consider a maximum of two objects
727 * nearby for the abbreviation length.
729 mad->init_len = 0;
730 if (!match) {
731 if (nth_midxed_object_oid(&oid, m, first))
732 extend_abbrev_len(&oid, mad);
733 } else if (first < num - 1) {
734 if (nth_midxed_object_oid(&oid, m, first + 1))
735 extend_abbrev_len(&oid, mad);
737 if (first > 0) {
738 if (nth_midxed_object_oid(&oid, m, first - 1))
739 extend_abbrev_len(&oid, mad);
741 mad->init_len = mad->cur_len;
744 static void find_abbrev_len_for_pack(struct packed_git *p,
745 struct min_abbrev_data *mad)
747 int match = 0;
748 uint32_t num, first = 0;
749 struct object_id oid;
750 const struct object_id *mad_oid;
752 if (p->multi_pack_index)
753 return;
755 if (open_pack_index(p) || !p->num_objects)
756 return;
758 num = p->num_objects;
759 mad_oid = mad->oid;
760 match = bsearch_pack(mad_oid, p, &first);
763 * first is now the position in the packfile where we would insert
764 * mad->hash if it does not exist (or the position of mad->hash if
765 * it does exist). Hence, we consider a maximum of two objects
766 * nearby for the abbreviation length.
768 mad->init_len = 0;
769 if (!match) {
770 if (!nth_packed_object_id(&oid, p, first))
771 extend_abbrev_len(&oid, mad);
772 } else if (first < num - 1) {
773 if (!nth_packed_object_id(&oid, p, first + 1))
774 extend_abbrev_len(&oid, mad);
776 if (first > 0) {
777 if (!nth_packed_object_id(&oid, p, first - 1))
778 extend_abbrev_len(&oid, mad);
780 mad->init_len = mad->cur_len;
783 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
785 struct multi_pack_index *m;
786 struct packed_git *p;
788 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
789 find_abbrev_len_for_midx(m, mad);
790 for (p = get_packed_git(mad->repo); p; p = p->next)
791 find_abbrev_len_for_pack(p, mad);
794 void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
795 const struct object_id *oid, int abbrev_len)
797 int r;
798 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
799 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
800 strbuf_setlen(sb, sb->len + r);
803 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
804 int abbrev_len)
806 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
809 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
810 const struct object_id *oid, int len)
812 const struct git_hash_algo *algo =
813 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
814 struct disambiguate_state ds;
815 struct min_abbrev_data mad;
816 struct object_id oid_ret;
817 const unsigned hexsz = algo->hexsz;
819 if (len < 0) {
820 unsigned long count = repo_approximate_object_count(r);
822 * Add one because the MSB only tells us the highest bit set,
823 * not including the value of all the _other_ bits (so "15"
824 * is only one off of 2^4, but the MSB is the 3rd bit.
826 len = msb(count) + 1;
828 * We now know we have on the order of 2^len objects, which
829 * expects a collision at 2^(len/2). But we also care about hex
830 * chars, not bits, and there are 4 bits per hex. So all
831 * together we need to divide by 2 and round up.
833 len = DIV_ROUND_UP(len, 2);
835 * For very small repos, we stick with our regular fallback.
837 if (len < FALLBACK_DEFAULT_ABBREV)
838 len = FALLBACK_DEFAULT_ABBREV;
841 oid_to_hex_r(hex, oid);
842 if (len >= hexsz || !len)
843 return hexsz;
845 mad.repo = r;
846 mad.init_len = len;
847 mad.cur_len = len;
848 mad.hex = hex;
849 mad.oid = oid;
851 find_abbrev_len_packed(&mad);
853 if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
854 return -1;
856 ds.fn = repo_extend_abbrev_len;
857 ds.always_call_fn = 1;
858 ds.cb_data = (void *)&mad;
860 find_short_object_filename(&ds);
861 (void)finish_object_disambiguation(&ds, &oid_ret);
863 hex[mad.cur_len] = 0;
864 return mad.cur_len;
867 const char *repo_find_unique_abbrev(struct repository *r,
868 const struct object_id *oid,
869 int len)
871 static int bufno;
872 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
873 char *hex = hexbuffer[bufno];
874 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
875 repo_find_unique_abbrev_r(r, hex, oid, len);
876 return hex;
879 static int ambiguous_path(const char *path, int len)
881 int slash = 1;
882 int cnt;
884 for (cnt = 0; cnt < len; cnt++) {
885 switch (*path++) {
886 case '\0':
887 break;
888 case '/':
889 if (slash)
890 break;
891 slash = 1;
892 continue;
893 case '.':
894 continue;
895 default:
896 slash = 0;
897 continue;
899 break;
901 return slash;
904 static inline int at_mark(const char *string, int len,
905 const char **suffix, int nr)
907 int i;
909 for (i = 0; i < nr; i++) {
910 int suffix_len = strlen(suffix[i]);
911 if (suffix_len <= len
912 && !strncasecmp(string, suffix[i], suffix_len))
913 return suffix_len;
915 return 0;
918 static inline int upstream_mark(const char *string, int len)
920 const char *suffix[] = { "@{upstream}", "@{u}" };
921 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
924 static inline int push_mark(const char *string, int len)
926 const char *suffix[] = { "@{push}" };
927 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
930 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
931 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
933 static int get_oid_basic(struct repository *r, const char *str, int len,
934 struct object_id *oid, unsigned int flags)
936 static const char *warn_msg = "refname '%.*s' is ambiguous.";
937 static const char *object_name_msg = N_(
938 "Git normally never creates a ref that ends with 40 hex characters\n"
939 "because it will be ignored when you just specify 40-hex. These refs\n"
940 "may be created by mistake. For example,\n"
941 "\n"
942 " git switch -c $br $(git rev-parse ...)\n"
943 "\n"
944 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
945 "examine these refs and maybe delete them. Turn this message off by\n"
946 "running \"git config advice.objectNameWarning false\"");
947 struct object_id tmp_oid;
948 char *real_ref = NULL;
949 int refs_found = 0;
950 int at, reflog_len, nth_prior = 0;
951 int fatal = !(flags & GET_OID_QUIETLY);
953 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
954 if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) {
955 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
956 if (refs_found > 0) {
957 warning(warn_msg, len, str);
958 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
959 fprintf(stderr, "%s\n", _(object_name_msg));
961 free(real_ref);
963 return 0;
966 /* basic@{time or number or -number} format to query ref-log */
967 reflog_len = at = 0;
968 if (len && str[len-1] == '}') {
969 for (at = len-4; at >= 0; at--) {
970 if (str[at] == '@' && str[at+1] == '{') {
971 if (str[at+2] == '-') {
972 if (at != 0)
973 /* @{-N} not at start */
974 return -1;
975 nth_prior = 1;
976 continue;
978 if (!upstream_mark(str + at, len - at) &&
979 !push_mark(str + at, len - at)) {
980 reflog_len = (len-1) - (at+2);
981 len = at;
983 break;
988 /* Accept only unambiguous ref paths. */
989 if (len && ambiguous_path(str, len))
990 return -1;
992 if (nth_prior) {
993 struct strbuf buf = STRBUF_INIT;
994 int detached;
996 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
997 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
998 strbuf_release(&buf);
999 if (detached)
1000 return 0;
1004 if (!len && reflog_len)
1005 /* allow "@{...}" to mean the current branch reflog */
1006 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
1007 else if (reflog_len)
1008 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
1009 else
1010 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
1012 if (!refs_found)
1013 return -1;
1015 if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) &&
1016 (refs_found > 1 ||
1017 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
1018 warning(warn_msg, len, str);
1020 if (reflog_len) {
1021 int nth, i;
1022 timestamp_t at_time;
1023 timestamp_t co_time;
1024 int co_tz, co_cnt;
1026 /* Is it asking for N-th entry, or approxidate? */
1027 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
1028 char ch = str[at+2+i];
1029 if ('0' <= ch && ch <= '9')
1030 nth = nth * 10 + ch - '0';
1031 else
1032 nth = -1;
1034 if (100000000 <= nth) {
1035 at_time = nth;
1036 nth = -1;
1037 } else if (0 <= nth)
1038 at_time = 0;
1039 else {
1040 int errors = 0;
1041 char *tmp = xstrndup(str + at + 2, reflog_len);
1042 at_time = approxidate_careful(tmp, &errors);
1043 free(tmp);
1044 if (errors) {
1045 free(real_ref);
1046 return -1;
1049 if (read_ref_at(get_main_ref_store(r),
1050 real_ref, flags, at_time, nth, oid, NULL,
1051 &co_time, &co_tz, &co_cnt)) {
1052 if (!len) {
1053 if (!skip_prefix(real_ref, "refs/heads/", &str))
1054 str = "HEAD";
1055 len = strlen(str);
1057 if (at_time) {
1058 if (!(flags & GET_OID_QUIETLY)) {
1059 warning(_("log for '%.*s' only goes back to %s"),
1060 len, str,
1061 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
1063 } else if (nth == co_cnt && !is_null_oid(oid)) {
1065 * We were asked for the Nth reflog (counting
1066 * from 0), but there were only N entries.
1067 * read_ref_at() will have returned "1" to tell
1068 * us it did not find an entry, but it did
1069 * still fill in the oid with the "old" value,
1070 * which we can use.
1072 } else {
1073 if (flags & GET_OID_QUIETLY) {
1074 exit(128);
1076 die(_("log for '%.*s' only has %d entries"),
1077 len, str, co_cnt);
1082 free(real_ref);
1083 return 0;
1086 static enum get_oid_result get_parent(struct repository *r,
1087 const char *name, int len,
1088 struct object_id *result, int idx)
1090 struct object_id oid;
1091 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
1092 GET_OID_COMMITTISH);
1093 struct commit *commit;
1094 struct commit_list *p;
1096 if (ret)
1097 return ret;
1098 commit = lookup_commit_reference(r, &oid);
1099 if (repo_parse_commit(r, commit))
1100 return MISSING_OBJECT;
1101 if (!idx) {
1102 oidcpy(result, &commit->object.oid);
1103 return FOUND;
1105 p = commit->parents;
1106 while (p) {
1107 if (!--idx) {
1108 oidcpy(result, &p->item->object.oid);
1109 return FOUND;
1111 p = p->next;
1113 return MISSING_OBJECT;
1116 static enum get_oid_result get_nth_ancestor(struct repository *r,
1117 const char *name, int len,
1118 struct object_id *result,
1119 int generation)
1121 struct object_id oid;
1122 struct commit *commit;
1123 int ret;
1125 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
1126 if (ret)
1127 return ret;
1128 commit = lookup_commit_reference(r, &oid);
1129 if (!commit)
1130 return MISSING_OBJECT;
1132 while (generation--) {
1133 if (repo_parse_commit(r, commit) || !commit->parents)
1134 return MISSING_OBJECT;
1135 commit = commit->parents->item;
1137 oidcpy(result, &commit->object.oid);
1138 return FOUND;
1141 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
1142 struct object *o, enum object_type expected_type)
1144 if (name && !namelen)
1145 namelen = strlen(name);
1146 while (1) {
1147 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1148 return NULL;
1149 if (expected_type == OBJ_ANY || o->type == expected_type)
1150 return o;
1151 if (o->type == OBJ_TAG)
1152 o = ((struct tag*) o)->tagged;
1153 else if (o->type == OBJ_COMMIT)
1154 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1155 else {
1156 if (name)
1157 error("%.*s: expected %s type, but the object "
1158 "dereferences to %s type",
1159 namelen, name, type_name(expected_type),
1160 type_name(o->type));
1161 return NULL;
1166 static int peel_onion(struct repository *r, const char *name, int len,
1167 struct object_id *oid, unsigned lookup_flags)
1169 struct object_id outer;
1170 const char *sp;
1171 unsigned int expected_type = 0;
1172 struct object *o;
1175 * "ref^{type}" dereferences ref repeatedly until you cannot
1176 * dereference anymore, or you get an object of given type,
1177 * whichever comes first. "ref^{}" means just dereference
1178 * tags until you get a non-tag. "ref^0" is a shorthand for
1179 * "ref^{commit}". "commit^{tree}" could be used to find the
1180 * top-level tree of the given commit.
1182 if (len < 4 || name[len-1] != '}')
1183 return -1;
1185 for (sp = name + len - 1; name <= sp; sp--) {
1186 int ch = *sp;
1187 if (ch == '{' && name < sp && sp[-1] == '^')
1188 break;
1190 if (sp <= name)
1191 return -1;
1193 sp++; /* beginning of type name, or closing brace for empty */
1194 if (starts_with(sp, "commit}"))
1195 expected_type = OBJ_COMMIT;
1196 else if (starts_with(sp, "tag}"))
1197 expected_type = OBJ_TAG;
1198 else if (starts_with(sp, "tree}"))
1199 expected_type = OBJ_TREE;
1200 else if (starts_with(sp, "blob}"))
1201 expected_type = OBJ_BLOB;
1202 else if (starts_with(sp, "object}"))
1203 expected_type = OBJ_ANY;
1204 else if (sp[0] == '}')
1205 expected_type = OBJ_NONE;
1206 else if (sp[0] == '/')
1207 expected_type = OBJ_COMMIT;
1208 else
1209 return -1;
1211 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1212 if (expected_type == OBJ_COMMIT)
1213 lookup_flags |= GET_OID_COMMITTISH;
1214 else if (expected_type == OBJ_TREE)
1215 lookup_flags |= GET_OID_TREEISH;
1217 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1218 return -1;
1220 o = parse_object(r, &outer);
1221 if (!o)
1222 return -1;
1223 if (!expected_type) {
1224 o = deref_tag(r, o, name, sp - name - 2);
1225 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1226 return -1;
1227 oidcpy(oid, &o->oid);
1228 return 0;
1232 * At this point, the syntax look correct, so
1233 * if we do not get the needed object, we should
1234 * barf.
1236 o = repo_peel_to_type(r, name, len, o, expected_type);
1237 if (!o)
1238 return -1;
1240 oidcpy(oid, &o->oid);
1241 if (sp[0] == '/') {
1242 /* "$commit^{/foo}" */
1243 char *prefix;
1244 int ret;
1245 struct commit_list *list = NULL;
1248 * $commit^{/}. Some regex implementation may reject.
1249 * We don't need regex anyway. '' pattern always matches.
1251 if (sp[1] == '}')
1252 return 0;
1254 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1255 commit_list_insert((struct commit *)o, &list);
1256 ret = get_oid_oneline(r, prefix, oid, list);
1257 free(prefix);
1258 return ret;
1260 return 0;
1263 static int get_describe_name(struct repository *r,
1264 const char *name, int len,
1265 struct object_id *oid)
1267 const char *cp;
1268 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1270 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1271 char ch = *cp;
1272 if (!isxdigit(ch)) {
1273 /* We must be looking at g in "SOMETHING-g"
1274 * for it to be describe output.
1276 if (ch == 'g' && cp[-1] == '-') {
1277 cp++;
1278 len -= cp - name;
1279 return get_short_oid(r,
1280 cp, len, oid, flags);
1284 return -1;
1287 static enum get_oid_result get_oid_1(struct repository *r,
1288 const char *name, int len,
1289 struct object_id *oid,
1290 unsigned lookup_flags)
1292 int ret, has_suffix;
1293 const char *cp;
1296 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1298 has_suffix = 0;
1299 for (cp = name + len - 1; name <= cp; cp--) {
1300 int ch = *cp;
1301 if ('0' <= ch && ch <= '9')
1302 continue;
1303 if (ch == '~' || ch == '^')
1304 has_suffix = ch;
1305 break;
1308 if (has_suffix) {
1309 unsigned int num = 0;
1310 int len1 = cp - name;
1311 cp++;
1312 while (cp < name + len) {
1313 unsigned int digit = *cp++ - '0';
1314 if (unsigned_mult_overflows(num, 10))
1315 return MISSING_OBJECT;
1316 num *= 10;
1317 if (unsigned_add_overflows(num, digit))
1318 return MISSING_OBJECT;
1319 num += digit;
1321 if (!num && len1 == len - 1)
1322 num = 1;
1323 else if (num > INT_MAX)
1324 return MISSING_OBJECT;
1325 if (has_suffix == '^')
1326 return get_parent(r, name, len1, oid, num);
1327 /* else if (has_suffix == '~') -- goes without saying */
1328 return get_nth_ancestor(r, name, len1, oid, num);
1331 ret = peel_onion(r, name, len, oid, lookup_flags);
1332 if (!ret)
1333 return FOUND;
1335 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1336 if (!ret)
1337 return FOUND;
1339 /* It could be describe output that is "SOMETHING-gXXXX" */
1340 ret = get_describe_name(r, name, len, oid);
1341 if (!ret)
1342 return FOUND;
1344 return get_short_oid(r, name, len, oid, lookup_flags);
1348 * This interprets names like ':/Initial revision of "git"' by searching
1349 * through history and returning the first commit whose message starts
1350 * the given regular expression.
1352 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1354 * For a literal '!' character at the beginning of a pattern, you have to repeat
1355 * that, like: ':/!!foo'
1357 * For future extension, all other sequences beginning with ':/!' are reserved.
1360 /* Remember to update object flag allocation in object.h */
1361 #define ONELINE_SEEN (1u<<20)
1363 struct handle_one_ref_cb {
1364 struct repository *repo;
1365 struct commit_list **list;
1368 static int handle_one_ref(const char *path, const struct object_id *oid,
1369 int flag UNUSED,
1370 void *cb_data)
1372 struct handle_one_ref_cb *cb = cb_data;
1373 struct commit_list **list = cb->list;
1374 struct object *object = parse_object(cb->repo, oid);
1375 if (!object)
1376 return 0;
1377 if (object->type == OBJ_TAG) {
1378 object = deref_tag(cb->repo, object, path,
1379 strlen(path));
1380 if (!object)
1381 return 0;
1383 if (object->type != OBJ_COMMIT)
1384 return 0;
1385 commit_list_insert((struct commit *)object, list);
1386 return 0;
1389 static int get_oid_oneline(struct repository *r,
1390 const char *prefix, struct object_id *oid,
1391 struct commit_list *list)
1393 struct commit_list *backup = NULL, *l;
1394 int found = 0;
1395 int negative = 0;
1396 regex_t regex;
1398 if (prefix[0] == '!') {
1399 prefix++;
1401 if (prefix[0] == '-') {
1402 prefix++;
1403 negative = 1;
1404 } else if (prefix[0] != '!') {
1405 return -1;
1409 if (regcomp(&regex, prefix, REG_EXTENDED))
1410 return -1;
1412 for (l = list; l; l = l->next) {
1413 l->item->object.flags |= ONELINE_SEEN;
1414 commit_list_insert(l->item, &backup);
1416 while (list) {
1417 const char *p, *buf;
1418 struct commit *commit;
1419 int matches;
1421 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
1422 if (!parse_object(r, &commit->object.oid))
1423 continue;
1424 buf = repo_get_commit_buffer(r, commit, NULL);
1425 p = strstr(buf, "\n\n");
1426 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1427 repo_unuse_commit_buffer(r, commit, buf);
1429 if (matches) {
1430 oidcpy(oid, &commit->object.oid);
1431 found = 1;
1432 break;
1435 regfree(&regex);
1436 free_commit_list(list);
1437 for (l = backup; l; l = l->next)
1438 clear_commit_marks(l->item, ONELINE_SEEN);
1439 free_commit_list(backup);
1440 return found ? 0 : -1;
1443 struct grab_nth_branch_switch_cbdata {
1444 int remaining;
1445 struct strbuf *sb;
1448 static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1449 struct object_id *noid UNUSED,
1450 const char *email UNUSED,
1451 timestamp_t timestamp UNUSED,
1452 int tz UNUSED,
1453 const char *message, void *cb_data)
1455 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1456 const char *match = NULL, *target = NULL;
1457 size_t len;
1459 if (skip_prefix(message, "checkout: moving from ", &match))
1460 target = strstr(match, " to ");
1462 if (!match || !target)
1463 return 0;
1464 if (--(cb->remaining) == 0) {
1465 len = target - match;
1466 strbuf_reset(cb->sb);
1467 strbuf_add(cb->sb, match, len);
1468 return 1; /* we are done */
1470 return 0;
1474 * Parse @{-N} syntax, return the number of characters parsed
1475 * if successful; otherwise signal an error with negative value.
1477 static int interpret_nth_prior_checkout(struct repository *r,
1478 const char *name, int namelen,
1479 struct strbuf *buf)
1481 long nth;
1482 int retval;
1483 struct grab_nth_branch_switch_cbdata cb;
1484 const char *brace;
1485 char *num_end;
1487 if (namelen < 4)
1488 return -1;
1489 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1490 return -1;
1491 brace = memchr(name, '}', namelen);
1492 if (!brace)
1493 return -1;
1494 nth = strtol(name + 3, &num_end, 10);
1495 if (num_end != brace)
1496 return -1;
1497 if (nth <= 0)
1498 return -1;
1499 cb.remaining = nth;
1500 cb.sb = buf;
1502 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1503 "HEAD", grab_nth_branch_switch, &cb);
1504 if (0 < retval) {
1505 retval = brace - name + 1;
1506 } else
1507 retval = 0;
1509 return retval;
1512 int repo_get_oid_mb(struct repository *r,
1513 const char *name,
1514 struct object_id *oid)
1516 struct commit *one, *two;
1517 struct commit_list *mbs = NULL;
1518 struct object_id oid_tmp;
1519 const char *dots;
1520 int st;
1522 dots = strstr(name, "...");
1523 if (!dots)
1524 return repo_get_oid(r, name, oid);
1525 if (dots == name)
1526 st = repo_get_oid(r, "HEAD", &oid_tmp);
1527 else {
1528 struct strbuf sb;
1529 strbuf_init(&sb, dots - name);
1530 strbuf_add(&sb, name, dots - name);
1531 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1532 strbuf_release(&sb);
1534 if (st)
1535 return st;
1536 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1537 if (!one)
1538 return -1;
1540 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1541 return -1;
1542 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1543 if (!two)
1544 return -1;
1545 if (repo_get_merge_bases(r, one, two, &mbs) < 0) {
1546 free_commit_list(mbs);
1547 return -1;
1549 if (!mbs || mbs->next)
1550 st = -1;
1551 else {
1552 st = 0;
1553 oidcpy(oid, &mbs->item->object.oid);
1555 free_commit_list(mbs);
1556 return st;
1559 /* parse @something syntax, when 'something' is not {.*} */
1560 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1562 const char *next;
1564 if (len || name[1] == '{')
1565 return -1;
1567 /* make sure it's a single @, or @@{.*}, not @foo */
1568 next = memchr(name + len + 1, '@', namelen - len - 1);
1569 if (next && next[1] != '{')
1570 return -1;
1571 if (!next)
1572 next = name + namelen;
1573 if (next != name + 1)
1574 return -1;
1576 strbuf_reset(buf);
1577 strbuf_add(buf, "HEAD", 4);
1578 return 1;
1581 static int reinterpret(struct repository *r,
1582 const char *name, int namelen, int len,
1583 struct strbuf *buf, unsigned allowed)
1585 /* we have extra data, which might need further processing */
1586 struct strbuf tmp = STRBUF_INIT;
1587 int used = buf->len;
1588 int ret;
1589 struct interpret_branch_name_options options = {
1590 .allowed = allowed
1593 strbuf_add(buf, name + len, namelen - len);
1594 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1595 /* that data was not interpreted, remove our cruft */
1596 if (ret < 0) {
1597 strbuf_setlen(buf, used);
1598 return len;
1600 strbuf_reset(buf);
1601 strbuf_addbuf(buf, &tmp);
1602 strbuf_release(&tmp);
1603 /* tweak for size of {-N} versus expanded ref name */
1604 return ret - used + len;
1607 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1609 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1610 strbuf_reset(buf);
1611 strbuf_addstr(buf, s);
1612 free(s);
1615 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1617 if (!allowed)
1618 return 1;
1620 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1621 starts_with(refname, "refs/heads/"))
1622 return 1;
1623 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1624 starts_with(refname, "refs/remotes/"))
1625 return 1;
1627 return 0;
1630 static int interpret_branch_mark(struct repository *r,
1631 const char *name, int namelen,
1632 int at, struct strbuf *buf,
1633 int (*get_mark)(const char *, int),
1634 const char *(*get_data)(struct branch *,
1635 struct strbuf *),
1636 const struct interpret_branch_name_options *options)
1638 int len;
1639 struct branch *branch;
1640 struct strbuf err = STRBUF_INIT;
1641 const char *value;
1643 len = get_mark(name + at, namelen - at);
1644 if (!len)
1645 return -1;
1647 if (memchr(name, ':', at))
1648 return -1;
1650 if (at) {
1651 char *name_str = xmemdupz(name, at);
1652 branch = branch_get(name_str);
1653 free(name_str);
1654 } else
1655 branch = branch_get(NULL);
1657 value = get_data(branch, &err);
1658 if (!value) {
1659 if (options->nonfatal_dangling_mark) {
1660 strbuf_release(&err);
1661 return -1;
1662 } else {
1663 die("%s", err.buf);
1667 if (!branch_interpret_allowed(value, options->allowed))
1668 return -1;
1670 set_shortened_ref(r, buf, value);
1671 return len + at;
1674 int repo_interpret_branch_name(struct repository *r,
1675 const char *name, int namelen,
1676 struct strbuf *buf,
1677 const struct interpret_branch_name_options *options)
1679 char *at;
1680 const char *start;
1681 int len;
1683 if (!namelen)
1684 namelen = strlen(name);
1686 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1687 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1688 if (!len) {
1689 return len; /* syntax Ok, not enough switches */
1690 } else if (len > 0) {
1691 if (len == namelen)
1692 return len; /* consumed all */
1693 else
1694 return reinterpret(r, name, namelen, len, buf,
1695 options->allowed);
1699 for (start = name;
1700 (at = memchr(start, '@', namelen - (start - name)));
1701 start = at + 1) {
1703 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1704 len = interpret_empty_at(name, namelen, at - name, buf);
1705 if (len > 0)
1706 return reinterpret(r, name, namelen, len, buf,
1707 options->allowed);
1710 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1711 upstream_mark, branch_get_upstream,
1712 options);
1713 if (len > 0)
1714 return len;
1716 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1717 push_mark, branch_get_push,
1718 options);
1719 if (len > 0)
1720 return len;
1723 return -1;
1726 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1728 int len = strlen(name);
1729 struct interpret_branch_name_options options = {
1730 .allowed = allowed
1732 int used = repo_interpret_branch_name(the_repository, name, len, sb,
1733 &options);
1735 if (used < 0)
1736 used = 0;
1737 strbuf_add(sb, name + used, len - used);
1740 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1742 if (startup_info->have_repository)
1743 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1744 else
1745 strbuf_addstr(sb, name);
1748 * This splice must be done even if we end up rejecting the
1749 * name; builtin/branch.c::copy_or_rename_branch() still wants
1750 * to see what the name expanded to so that "branch -m" can be
1751 * used as a tool to correct earlier mistakes.
1753 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1755 if (*name == '-' ||
1756 !strcmp(sb->buf, "refs/heads/HEAD"))
1757 return -1;
1759 return check_refname_format(sb->buf, 0);
1762 void object_context_release(struct object_context *ctx)
1764 free(ctx->path);
1768 * This is like "get_oid_basic()", except it allows "object ID expressions",
1769 * notably "xyz^" for "parent of xyz"
1771 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1773 struct object_context unused;
1774 int ret = get_oid_with_context(r, name, 0, oid, &unused);
1775 object_context_release(&unused);
1776 return ret;
1780 * This returns a non-zero value if the string (built using printf
1781 * format and the given arguments) is not a valid object.
1783 int get_oidf(struct object_id *oid, const char *fmt, ...)
1785 va_list ap;
1786 int ret;
1787 struct strbuf sb = STRBUF_INIT;
1789 va_start(ap, fmt);
1790 strbuf_vaddf(&sb, fmt, ap);
1791 va_end(ap);
1793 ret = repo_get_oid(the_repository, sb.buf, oid);
1794 strbuf_release(&sb);
1796 return ret;
1800 * Many callers know that the user meant to name a commit-ish by
1801 * syntactical positions where the object name appears. Calling this
1802 * function allows the machinery to disambiguate shorter-than-unique
1803 * abbreviated object names between commit-ish and others.
1805 * Note that this does NOT error out when the named object is not a
1806 * commit-ish. It is merely to give a hint to the disambiguation
1807 * machinery.
1809 int repo_get_oid_committish(struct repository *r,
1810 const char *name,
1811 struct object_id *oid)
1813 struct object_context unused;
1814 int ret = get_oid_with_context(r, name, GET_OID_COMMITTISH,
1815 oid, &unused);
1816 object_context_release(&unused);
1817 return ret;
1820 int repo_get_oid_treeish(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_TREEISH,
1826 oid, &unused);
1827 object_context_release(&unused);
1828 return ret;
1831 int repo_get_oid_commit(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_COMMIT,
1837 oid, &unused);
1838 object_context_release(&unused);
1839 return ret;
1842 int repo_get_oid_tree(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_TREE,
1848 oid, &unused);
1849 object_context_release(&unused);
1850 return ret;
1853 int repo_get_oid_blob(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_BLOB,
1859 oid, &unused);
1860 object_context_release(&unused);
1861 return ret;
1864 /* Must be called only when object_name:filename doesn't exist. */
1865 static void diagnose_invalid_oid_path(struct repository *r,
1866 const char *prefix,
1867 const char *filename,
1868 const struct object_id *tree_oid,
1869 const char *object_name,
1870 int object_name_len)
1872 struct object_id oid;
1873 unsigned short mode;
1875 if (!prefix)
1876 prefix = "";
1878 if (file_exists(filename))
1879 die(_("path '%s' exists on disk, but not in '%.*s'"),
1880 filename, object_name_len, object_name);
1881 if (is_missing_file_error(errno)) {
1882 char *fullname = xstrfmt("%s%s", prefix, filename);
1884 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1885 die(_("path '%s' exists, but not '%s'\n"
1886 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1887 fullname,
1888 filename,
1889 object_name_len, object_name,
1890 fullname,
1891 object_name_len, object_name,
1892 filename);
1894 die(_("path '%s' does not exist in '%.*s'"),
1895 filename, object_name_len, object_name);
1899 /* Must be called only when :stage:filename doesn't exist. */
1900 static void diagnose_invalid_index_path(struct repository *r,
1901 int stage,
1902 const char *prefix,
1903 const char *filename)
1905 struct index_state *istate = r->index;
1906 const struct cache_entry *ce;
1907 int pos;
1908 unsigned namelen = strlen(filename);
1909 struct strbuf fullname = STRBUF_INIT;
1911 if (!prefix)
1912 prefix = "";
1914 /* Wrong stage number? */
1915 pos = index_name_pos(istate, filename, namelen);
1916 if (pos < 0)
1917 pos = -pos - 1;
1918 if (pos < istate->cache_nr) {
1919 ce = istate->cache[pos];
1920 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1921 ce_namelen(ce) == namelen &&
1922 !memcmp(ce->name, filename, namelen))
1923 die(_("path '%s' is in the index, but not at stage %d\n"
1924 "hint: Did you mean ':%d:%s'?"),
1925 filename, stage,
1926 ce_stage(ce), filename);
1929 /* Confusion between relative and absolute filenames? */
1930 strbuf_addstr(&fullname, prefix);
1931 strbuf_addstr(&fullname, filename);
1932 pos = index_name_pos(istate, fullname.buf, fullname.len);
1933 if (pos < 0)
1934 pos = -pos - 1;
1935 if (pos < istate->cache_nr) {
1936 ce = istate->cache[pos];
1937 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1938 ce_namelen(ce) == fullname.len &&
1939 !memcmp(ce->name, fullname.buf, fullname.len))
1940 die(_("path '%s' is in the index, but not '%s'\n"
1941 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1942 fullname.buf, filename,
1943 ce_stage(ce), fullname.buf,
1944 ce_stage(ce), filename);
1947 if (repo_file_exists(r, filename))
1948 die(_("path '%s' exists on disk, but not in the index"), filename);
1949 if (is_missing_file_error(errno))
1950 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1951 filename);
1953 strbuf_release(&fullname);
1957 static char *resolve_relative_path(struct repository *r, const char *rel)
1959 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1960 return NULL;
1962 if (r != the_repository || !is_inside_work_tree())
1963 die(_("relative path syntax can't be used outside working tree"));
1965 /* die() inside prefix_path() if resolved path is outside worktree */
1966 return prefix_path(startup_info->prefix,
1967 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1968 rel);
1971 static int reject_tree_in_index(struct repository *repo,
1972 int only_to_die,
1973 const struct cache_entry *ce,
1974 int stage,
1975 const char *prefix,
1976 const char *cp)
1978 if (!S_ISSPARSEDIR(ce->ce_mode))
1979 return 0;
1980 if (only_to_die)
1981 diagnose_invalid_index_path(repo, stage, prefix, cp);
1982 return -1;
1985 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1986 const char *name,
1987 unsigned flags,
1988 const char *prefix,
1989 struct object_id *oid,
1990 struct object_context *oc)
1992 int ret, bracket_depth;
1993 int namelen = strlen(name);
1994 const char *cp;
1995 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
1997 memset(oc, 0, sizeof(*oc));
1998 oc->mode = S_IFINVALID;
1999 strbuf_init(&oc->symlink_path, 0);
2000 ret = get_oid_1(repo, name, namelen, oid, flags);
2001 if (!ret && flags & GET_OID_REQUIRE_PATH)
2002 die(_("<object>:<path> required, only <object> '%s' given"),
2003 name);
2004 if (!ret)
2005 return ret;
2007 * tree:path --> object name of path in tree
2008 * :path -> object name of absolute path in index
2009 * :./path -> object name of path relative to cwd in index
2010 * :[0-3]:path -> object name of path in index at stage
2011 * :/foo -> recent commit matching foo
2013 if (name[0] == ':') {
2014 int stage = 0;
2015 const struct cache_entry *ce;
2016 char *new_path = NULL;
2017 int pos;
2018 if (!only_to_die && namelen > 2 && name[1] == '/') {
2019 struct handle_one_ref_cb cb;
2020 struct commit_list *list = NULL;
2022 cb.repo = repo;
2023 cb.list = &list;
2024 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2025 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2026 commit_list_sort_by_date(&list);
2027 return get_oid_oneline(repo, name + 2, oid, list);
2029 if (namelen < 3 ||
2030 name[2] != ':' ||
2031 name[1] < '0' || '3' < name[1])
2032 cp = name + 1;
2033 else {
2034 stage = name[1] - '0';
2035 cp = name + 3;
2037 new_path = resolve_relative_path(repo, cp);
2038 if (!new_path) {
2039 namelen = namelen - (cp - name);
2040 } else {
2041 cp = new_path;
2042 namelen = strlen(cp);
2045 if (flags & GET_OID_RECORD_PATH)
2046 oc->path = xstrdup(cp);
2048 if (!repo->index || !repo->index->cache)
2049 repo_read_index(repo);
2050 pos = index_name_pos(repo->index, cp, namelen);
2051 if (pos < 0)
2052 pos = -pos - 1;
2053 while (pos < repo->index->cache_nr) {
2054 ce = repo->index->cache[pos];
2055 if (ce_namelen(ce) != namelen ||
2056 memcmp(ce->name, cp, namelen))
2057 break;
2058 if (ce_stage(ce) == stage) {
2059 free(new_path);
2060 if (reject_tree_in_index(repo, only_to_die, ce,
2061 stage, prefix, cp))
2062 return -1;
2063 oidcpy(oid, &ce->oid);
2064 oc->mode = ce->ce_mode;
2065 return 0;
2067 pos++;
2069 if (only_to_die && name[1] && name[1] != '/')
2070 diagnose_invalid_index_path(repo, stage, prefix, cp);
2071 free(new_path);
2072 return -1;
2074 for (cp = name, bracket_depth = 0; *cp; cp++) {
2075 if (*cp == '{')
2076 bracket_depth++;
2077 else if (bracket_depth && *cp == '}')
2078 bracket_depth--;
2079 else if (!bracket_depth && *cp == ':')
2080 break;
2082 if (*cp == ':') {
2083 struct object_id tree_oid;
2084 int len = cp - name;
2085 unsigned sub_flags = flags;
2087 sub_flags &= ~GET_OID_DISAMBIGUATORS;
2088 sub_flags |= GET_OID_TREEISH;
2090 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
2091 const char *filename = cp+1;
2092 char *new_filename = NULL;
2094 new_filename = resolve_relative_path(repo, filename);
2095 if (new_filename)
2096 filename = new_filename;
2097 if (flags & GET_OID_FOLLOW_SYMLINKS) {
2098 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
2099 filename, oid, &oc->symlink_path,
2100 &oc->mode);
2101 } else {
2102 ret = get_tree_entry(repo, &tree_oid, filename, oid,
2103 &oc->mode);
2104 if (ret && only_to_die) {
2105 diagnose_invalid_oid_path(repo, prefix,
2106 filename,
2107 &tree_oid,
2108 name, len);
2111 if (flags & GET_OID_RECORD_PATH)
2112 oc->path = xstrdup(filename);
2114 free(new_filename);
2115 return ret;
2116 } else {
2117 if (only_to_die)
2118 die(_("invalid object name '%.*s'."), len, name);
2121 return ret;
2125 * Call this function when you know "name" given by the end user must
2126 * name an object but it doesn't; the function _may_ die with a better
2127 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2128 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2129 * you have a chance to diagnose the error further.
2131 void maybe_die_on_misspelt_object_name(struct repository *r,
2132 const char *name,
2133 const char *prefix)
2135 struct object_context oc;
2136 struct object_id oid;
2137 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY,
2138 prefix, &oid, &oc);
2139 object_context_release(&oc);
2142 enum get_oid_result get_oid_with_context(struct repository *repo,
2143 const char *str,
2144 unsigned flags,
2145 struct object_id *oid,
2146 struct object_context *oc)
2148 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
2149 BUG("incompatible flags for get_oid_with_context");
2150 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);