http-push: free transfer_request dest field
[git/gitster.git] / object-name.c
blobc892fbe80aa7173dfcc1995de5a75bc322c6adb7
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 "repo-settings.h"
24 #include "repository.h"
25 #include "setup.h"
26 #include "midx.h"
27 #include "commit-reach.h"
28 #include "date.h"
29 #include "object-file-convert.h"
31 static int get_oid_oneline(struct repository *r, const char *, struct object_id *,
32 const struct commit_list *);
34 typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *);
36 struct disambiguate_state {
37 int len; /* length of prefix in hex chars */
38 char hex_pfx[GIT_MAX_HEXSZ + 1];
39 struct object_id bin_pfx;
41 struct repository *repo;
42 disambiguate_hint_fn fn;
43 void *cb_data;
44 struct object_id candidate;
45 unsigned candidate_exists:1;
46 unsigned candidate_checked:1;
47 unsigned candidate_ok:1;
48 unsigned disambiguate_fn_used:1;
49 unsigned ambiguous:1;
50 unsigned always_call_fn:1;
53 static void update_candidates(struct disambiguate_state *ds, const struct object_id *current)
55 /* The hash algorithm of current has already been filtered */
56 if (ds->always_call_fn) {
57 ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0;
58 return;
60 if (!ds->candidate_exists) {
61 /* this is the first candidate */
62 oidcpy(&ds->candidate, current);
63 ds->candidate_exists = 1;
64 return;
65 } else if (oideq(&ds->candidate, current)) {
66 /* the same as what we already have seen */
67 return;
70 if (!ds->fn) {
71 /* cannot disambiguate between ds->candidate and current */
72 ds->ambiguous = 1;
73 return;
76 if (!ds->candidate_checked) {
77 ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data);
78 ds->disambiguate_fn_used = 1;
79 ds->candidate_checked = 1;
82 if (!ds->candidate_ok) {
83 /* discard the candidate; we know it does not satisfy fn */
84 oidcpy(&ds->candidate, current);
85 ds->candidate_checked = 0;
86 return;
89 /* if we reach this point, we know ds->candidate satisfies fn */
90 if (ds->fn(ds->repo, current, ds->cb_data)) {
92 * if both current and candidate satisfy fn, we cannot
93 * disambiguate.
95 ds->candidate_ok = 0;
96 ds->ambiguous = 1;
99 /* otherwise, current can be discarded and candidate is still good */
102 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
104 static enum cb_next match_prefix(const struct object_id *oid, void *arg)
106 struct disambiguate_state *ds = arg;
107 /* no need to call match_hash, oidtree_each did prefix match */
108 update_candidates(ds, oid);
109 return ds->ambiguous ? CB_BREAK : CB_CONTINUE;
112 static void find_short_object_filename(struct disambiguate_state *ds)
114 struct object_directory *odb;
116 for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next)
117 oidtree_each(odb_loose_cache(odb, &ds->bin_pfx),
118 &ds->bin_pfx, ds->len, match_prefix, ds);
121 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
123 do {
124 if (*a != *b)
125 return 0;
126 a++;
127 b++;
128 len -= 2;
129 } while (len > 1);
130 if (len)
131 if ((*a ^ *b) & 0xf0)
132 return 0;
133 return 1;
136 static void unique_in_midx(struct multi_pack_index *m,
137 struct disambiguate_state *ds)
139 for (; m; m = m->base_midx) {
140 uint32_t num, i, first = 0;
141 const struct object_id *current = NULL;
142 int len = ds->len > ds->repo->hash_algo->hexsz ?
143 ds->repo->hash_algo->hexsz : ds->len;
145 if (!m->num_objects)
146 continue;
148 num = m->num_objects + m->num_objects_in_base;
150 bsearch_one_midx(&ds->bin_pfx, m, &first);
153 * At this point, "first" is the location of the lowest
154 * object with an object name that could match
155 * "bin_pfx". See if we have 0, 1 or more objects that
156 * actually match(es).
158 for (i = first; i < num && !ds->ambiguous; i++) {
159 struct object_id oid;
160 current = nth_midxed_object_oid(&oid, m, i);
161 if (!match_hash(len, ds->bin_pfx.hash, current->hash))
162 break;
163 update_candidates(ds, current);
168 static void unique_in_pack(struct packed_git *p,
169 struct disambiguate_state *ds)
171 uint32_t num, i, first = 0;
172 int len = ds->len > ds->repo->hash_algo->hexsz ?
173 ds->repo->hash_algo->hexsz : ds->len;
175 if (p->multi_pack_index)
176 return;
178 if (open_pack_index(p) || !p->num_objects)
179 return;
181 num = p->num_objects;
182 bsearch_pack(&ds->bin_pfx, p, &first);
185 * At this point, "first" is the location of the lowest object
186 * with an object name that could match "bin_pfx". See if we have
187 * 0, 1 or more objects that actually match(es).
189 for (i = first; i < num && !ds->ambiguous; i++) {
190 struct object_id oid;
191 nth_packed_object_id(&oid, p, i);
192 if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
193 break;
194 update_candidates(ds, &oid);
198 static void find_short_packed_object(struct disambiguate_state *ds)
200 struct multi_pack_index *m;
201 struct packed_git *p;
203 /* Skip, unless oids from the storage hash algorithm are wanted */
204 if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
205 return;
207 for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous;
208 m = m->next)
209 unique_in_midx(m, ds);
210 for (p = get_packed_git(ds->repo); p && !ds->ambiguous;
211 p = p->next)
212 unique_in_pack(p, ds);
215 static int finish_object_disambiguation(struct disambiguate_state *ds,
216 struct object_id *oid)
218 if (ds->ambiguous)
219 return SHORT_NAME_AMBIGUOUS;
221 if (!ds->candidate_exists)
222 return MISSING_OBJECT;
224 if (!ds->candidate_checked)
226 * If this is the only candidate, there is no point
227 * calling the disambiguation hint callback.
229 * On the other hand, if the current candidate
230 * replaced an earlier candidate that did _not_ pass
231 * the disambiguation hint callback, then we do have
232 * more than one objects that match the short name
233 * given, so we should make sure this one matches;
234 * otherwise, if we discovered this one and the one
235 * that we previously discarded in the reverse order,
236 * we would end up showing different results in the
237 * same repository!
239 ds->candidate_ok = (!ds->disambiguate_fn_used ||
240 ds->fn(ds->repo, &ds->candidate, ds->cb_data));
242 if (!ds->candidate_ok)
243 return SHORT_NAME_AMBIGUOUS;
245 oidcpy(oid, &ds->candidate);
246 return 0;
249 static int disambiguate_commit_only(struct repository *r,
250 const struct object_id *oid,
251 void *cb_data UNUSED)
253 int kind = oid_object_info(r, oid, NULL);
254 return kind == OBJ_COMMIT;
257 static int disambiguate_committish_only(struct repository *r,
258 const struct object_id *oid,
259 void *cb_data UNUSED)
261 struct object *obj;
262 int kind;
264 kind = oid_object_info(r, oid, NULL);
265 if (kind == OBJ_COMMIT)
266 return 1;
267 if (kind != OBJ_TAG)
268 return 0;
270 /* We need to do this the hard way... */
271 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
272 if (obj && obj->type == OBJ_COMMIT)
273 return 1;
274 return 0;
277 static int disambiguate_tree_only(struct repository *r,
278 const struct object_id *oid,
279 void *cb_data UNUSED)
281 int kind = oid_object_info(r, oid, NULL);
282 return kind == OBJ_TREE;
285 static int disambiguate_treeish_only(struct repository *r,
286 const struct object_id *oid,
287 void *cb_data UNUSED)
289 struct object *obj;
290 int kind;
292 kind = oid_object_info(r, oid, NULL);
293 if (kind == OBJ_TREE || kind == OBJ_COMMIT)
294 return 1;
295 if (kind != OBJ_TAG)
296 return 0;
298 /* We need to do this the hard way... */
299 obj = deref_tag(r, parse_object(r, oid), NULL, 0);
300 if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
301 return 1;
302 return 0;
305 static int disambiguate_blob_only(struct repository *r,
306 const struct object_id *oid,
307 void *cb_data UNUSED)
309 int kind = oid_object_info(r, oid, NULL);
310 return kind == OBJ_BLOB;
313 static disambiguate_hint_fn default_disambiguate_hint;
315 int set_disambiguate_hint_config(const char *var, const char *value)
317 static const struct {
318 const char *name;
319 disambiguate_hint_fn fn;
320 } hints[] = {
321 { "none", NULL },
322 { "commit", disambiguate_commit_only },
323 { "committish", disambiguate_committish_only },
324 { "tree", disambiguate_tree_only },
325 { "treeish", disambiguate_treeish_only },
326 { "blob", disambiguate_blob_only }
328 int i;
330 if (!value)
331 return config_error_nonbool(var);
333 for (i = 0; i < ARRAY_SIZE(hints); i++) {
334 if (!strcasecmp(value, hints[i].name)) {
335 default_disambiguate_hint = hints[i].fn;
336 return 0;
340 return error("unknown hint type for '%s': %s", var, value);
343 static int init_object_disambiguation(struct repository *r,
344 const char *name, int len,
345 const struct git_hash_algo *algo,
346 struct disambiguate_state *ds)
348 int i;
350 if (len < MINIMUM_ABBREV || len > GIT_MAX_HEXSZ)
351 return -1;
353 memset(ds, 0, sizeof(*ds));
355 for (i = 0; i < len ;i++) {
356 unsigned char c = name[i];
357 unsigned char val;
358 if (c >= '0' && c <= '9')
359 val = c - '0';
360 else if (c >= 'a' && c <= 'f')
361 val = c - 'a' + 10;
362 else if (c >= 'A' && c <='F') {
363 val = c - 'A' + 10;
364 c -= 'A' - 'a';
366 else
367 return -1;
368 ds->hex_pfx[i] = c;
369 if (!(i & 1))
370 val <<= 4;
371 ds->bin_pfx.hash[i >> 1] |= val;
374 ds->len = len;
375 ds->hex_pfx[len] = '\0';
376 ds->repo = r;
377 ds->bin_pfx.algo = algo ? hash_algo_by_ptr(algo) : GIT_HASH_UNKNOWN;
378 prepare_alt_odb(r);
379 return 0;
382 struct ambiguous_output {
383 const struct disambiguate_state *ds;
384 struct strbuf advice;
385 struct strbuf sb;
388 static int show_ambiguous_object(const struct object_id *oid, void *data)
390 struct ambiguous_output *state = data;
391 const struct disambiguate_state *ds = state->ds;
392 struct strbuf *advice = &state->advice;
393 struct strbuf *sb = &state->sb;
394 int type;
395 const char *hash;
397 if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data))
398 return 0;
400 hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV);
401 type = oid_object_info(ds->repo, oid, NULL);
403 if (type < 0) {
405 * TRANSLATORS: This is a line of ambiguous object
406 * output shown when we cannot look up or parse the
407 * object in question. E.g. "deadbeef [bad object]".
409 strbuf_addf(sb, _("%s [bad object]"), hash);
410 goto out;
413 assert(type == OBJ_TREE || type == OBJ_COMMIT ||
414 type == OBJ_BLOB || type == OBJ_TAG);
416 if (type == OBJ_COMMIT) {
417 struct strbuf date = STRBUF_INIT;
418 struct strbuf msg = STRBUF_INIT;
419 struct commit *commit = lookup_commit(ds->repo, oid);
421 if (commit) {
422 struct pretty_print_context pp = {0};
423 pp.date_mode.type = DATE_SHORT;
424 repo_format_commit_message(the_repository, commit,
425 "%ad", &date, &pp);
426 repo_format_commit_message(the_repository, commit,
427 "%s", &msg, &pp);
431 * TRANSLATORS: This is a line of ambiguous commit
432 * object output. E.g.:
434 * "deadbeef commit 2021-01-01 - Some Commit Message"
436 strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf,
437 msg.buf);
439 strbuf_release(&date);
440 strbuf_release(&msg);
441 } else if (type == OBJ_TAG) {
442 struct tag *tag = lookup_tag(ds->repo, oid);
444 if (!parse_tag(tag) && tag->tag) {
446 * TRANSLATORS: This is a line of ambiguous
447 * tag object output. E.g.:
449 * "deadbeef tag 2022-01-01 - Some Tag Message"
451 * The second argument is the YYYY-MM-DD found
452 * in the tag.
454 * The third argument is the "tag" string
455 * from object.c.
457 strbuf_addf(sb, _("%s tag %s - %s"), hash,
458 show_date(tag->date, 0, DATE_MODE(SHORT)),
459 tag->tag);
460 } else {
462 * TRANSLATORS: This is a line of ambiguous
463 * tag object output where we couldn't parse
464 * the tag itself. E.g.:
466 * "deadbeef [bad tag, could not parse it]"
468 strbuf_addf(sb, _("%s [bad tag, could not parse it]"),
469 hash);
471 } else if (type == OBJ_TREE) {
473 * TRANSLATORS: This is a line of ambiguous <type>
474 * object output. E.g. "deadbeef tree".
476 strbuf_addf(sb, _("%s tree"), hash);
477 } else if (type == OBJ_BLOB) {
479 * TRANSLATORS: This is a line of ambiguous <type>
480 * object output. E.g. "deadbeef blob".
482 strbuf_addf(sb, _("%s blob"), hash);
486 out:
488 * TRANSLATORS: This is line item of ambiguous object output
489 * from describe_ambiguous_object() above. For RTL languages
490 * you'll probably want to swap the "%s" and leading " " space
491 * around.
493 strbuf_addf(advice, _(" %s\n"), sb->buf);
495 strbuf_reset(sb);
496 return 0;
499 static int collect_ambiguous(const struct object_id *oid, void *data)
501 oid_array_append(data, oid);
502 return 0;
505 static int repo_collect_ambiguous(struct repository *r UNUSED,
506 const struct object_id *oid,
507 void *data)
509 return collect_ambiguous(oid, data);
512 static int sort_ambiguous(const void *va, const void *vb, void *ctx)
514 struct repository *sort_ambiguous_repo = ctx;
515 const struct object_id *a = va, *b = vb;
516 int a_type = oid_object_info(sort_ambiguous_repo, a, NULL);
517 int b_type = oid_object_info(sort_ambiguous_repo, b, NULL);
518 int a_type_sort;
519 int b_type_sort;
522 * Sorts by hash within the same object type, just as
523 * oid_array_for_each_unique() would do.
525 if (a_type == b_type) {
526 if (a->algo == b->algo)
527 return oidcmp(a, b);
528 else
529 return a->algo > b->algo ? 1 : -1;
533 * Between object types show tags, then commits, and finally
534 * trees and blobs.
536 * The object_type enum is commit, tree, blob, tag, but we
537 * want tag, commit, tree blob. Cleverly (perhaps too
538 * cleverly) do that with modulus, since the enum assigns 1 to
539 * commit, so tag becomes 0.
541 a_type_sort = a_type % 4;
542 b_type_sort = b_type % 4;
543 return a_type_sort > b_type_sort ? 1 : -1;
546 static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a)
548 QSORT_S(a->oid, a->nr, sort_ambiguous, r);
551 static enum get_oid_result get_short_oid(struct repository *r,
552 const char *name, int len,
553 struct object_id *oid,
554 unsigned flags)
556 int status;
557 struct disambiguate_state ds;
558 int quietly = !!(flags & GET_OID_QUIETLY);
559 const struct git_hash_algo *algo = r->hash_algo;
561 if (flags & GET_OID_HASH_ANY)
562 algo = NULL;
564 if (init_object_disambiguation(r, name, len, algo, &ds) < 0)
565 return -1;
567 if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS))
568 BUG("multiple get_short_oid disambiguator flags");
570 if (flags & GET_OID_COMMIT)
571 ds.fn = disambiguate_commit_only;
572 else if (flags & GET_OID_COMMITTISH)
573 ds.fn = disambiguate_committish_only;
574 else if (flags & GET_OID_TREE)
575 ds.fn = disambiguate_tree_only;
576 else if (flags & GET_OID_TREEISH)
577 ds.fn = disambiguate_treeish_only;
578 else if (flags & GET_OID_BLOB)
579 ds.fn = disambiguate_blob_only;
580 else
581 ds.fn = default_disambiguate_hint;
583 find_short_object_filename(&ds);
584 find_short_packed_object(&ds);
585 status = finish_object_disambiguation(&ds, oid);
588 * If we didn't find it, do the usual reprepare() slow-path,
589 * since the object may have recently been added to the repository
590 * or migrated from loose to packed.
592 if (status == MISSING_OBJECT) {
593 reprepare_packed_git(r);
594 find_short_object_filename(&ds);
595 find_short_packed_object(&ds);
596 status = finish_object_disambiguation(&ds, oid);
599 if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) {
600 struct oid_array collect = OID_ARRAY_INIT;
601 struct ambiguous_output out = {
602 .ds = &ds,
603 .sb = STRBUF_INIT,
604 .advice = STRBUF_INIT,
607 error(_("short object ID %s is ambiguous"), ds.hex_pfx);
610 * We may still have ambiguity if we simply saw a series of
611 * candidates that did not satisfy our hint function. In
612 * that case, we still want to show them, so disable the hint
613 * function entirely.
615 if (!ds.ambiguous)
616 ds.fn = NULL;
618 repo_for_each_abbrev(r, ds.hex_pfx, algo, collect_ambiguous, &collect);
619 sort_ambiguous_oid_array(r, &collect);
621 if (oid_array_for_each(&collect, show_ambiguous_object, &out))
622 BUG("show_ambiguous_object shouldn't return non-zero");
625 * TRANSLATORS: The argument is the list of ambiguous
626 * objects composed in show_ambiguous_object(). See
627 * its "TRANSLATORS" comments for details.
629 advise(_("The candidates are:\n%s"), out.advice.buf);
631 oid_array_clear(&collect);
632 strbuf_release(&out.advice);
633 strbuf_release(&out.sb);
636 return status;
639 int repo_for_each_abbrev(struct repository *r, const char *prefix,
640 const struct git_hash_algo *algo,
641 each_abbrev_fn fn, void *cb_data)
643 struct oid_array collect = OID_ARRAY_INIT;
644 struct disambiguate_state ds;
645 int ret;
647 if (init_object_disambiguation(r, prefix, strlen(prefix), algo, &ds) < 0)
648 return -1;
650 ds.always_call_fn = 1;
651 ds.fn = repo_collect_ambiguous;
652 ds.cb_data = &collect;
653 find_short_object_filename(&ds);
654 find_short_packed_object(&ds);
656 ret = oid_array_for_each_unique(&collect, fn, cb_data);
657 oid_array_clear(&collect);
658 return ret;
662 * Return the slot of the most-significant bit set in "val". There are various
663 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
664 * probably not a big deal here.
666 static unsigned msb(unsigned long val)
668 unsigned r = 0;
669 while (val >>= 1)
670 r++;
671 return r;
674 struct min_abbrev_data {
675 unsigned int init_len;
676 unsigned int cur_len;
677 char *hex;
678 struct repository *repo;
679 const struct object_id *oid;
682 static inline char get_hex_char_from_oid(const struct object_id *oid,
683 unsigned int pos)
685 static const char hex[] = "0123456789abcdef";
687 if ((pos & 1) == 0)
688 return hex[oid->hash[pos >> 1] >> 4];
689 else
690 return hex[oid->hash[pos >> 1] & 0xf];
693 static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
695 struct min_abbrev_data *mad = cb_data;
697 unsigned int i = mad->init_len;
698 while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
699 i++;
701 if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
702 mad->cur_len = i + 1;
704 return 0;
707 static int repo_extend_abbrev_len(struct repository *r UNUSED,
708 const struct object_id *oid,
709 void *cb_data)
711 return extend_abbrev_len(oid, cb_data);
714 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
715 struct min_abbrev_data *mad)
717 for (; m; m = m->base_midx) {
718 int match = 0;
719 uint32_t num, first = 0;
720 struct object_id oid;
721 const struct object_id *mad_oid;
723 if (!m->num_objects)
724 continue;
726 num = m->num_objects + m->num_objects_in_base;
727 mad_oid = mad->oid;
728 match = bsearch_one_midx(mad_oid, m, &first);
731 * first is now the position in the packfile where we
732 * would insert mad->hash if it does not exist (or the
733 * position of mad->hash if it does exist). Hence, we
734 * consider a maximum of two objects nearby for the
735 * abbreviation length.
737 mad->init_len = 0;
738 if (!match) {
739 if (nth_midxed_object_oid(&oid, m, first))
740 extend_abbrev_len(&oid, mad);
741 } else if (first < num - 1) {
742 if (nth_midxed_object_oid(&oid, m, first + 1))
743 extend_abbrev_len(&oid, mad);
745 if (first > 0) {
746 if (nth_midxed_object_oid(&oid, m, first - 1))
747 extend_abbrev_len(&oid, mad);
749 mad->init_len = mad->cur_len;
753 static void find_abbrev_len_for_pack(struct packed_git *p,
754 struct min_abbrev_data *mad)
756 int match = 0;
757 uint32_t num, first = 0;
758 struct object_id oid;
759 const struct object_id *mad_oid;
761 if (p->multi_pack_index)
762 return;
764 if (open_pack_index(p) || !p->num_objects)
765 return;
767 num = p->num_objects;
768 mad_oid = mad->oid;
769 match = bsearch_pack(mad_oid, p, &first);
772 * first is now the position in the packfile where we would insert
773 * mad->hash if it does not exist (or the position of mad->hash if
774 * it does exist). Hence, we consider a maximum of two objects
775 * nearby for the abbreviation length.
777 mad->init_len = 0;
778 if (!match) {
779 if (!nth_packed_object_id(&oid, p, first))
780 extend_abbrev_len(&oid, mad);
781 } else if (first < num - 1) {
782 if (!nth_packed_object_id(&oid, p, first + 1))
783 extend_abbrev_len(&oid, mad);
785 if (first > 0) {
786 if (!nth_packed_object_id(&oid, p, first - 1))
787 extend_abbrev_len(&oid, mad);
789 mad->init_len = mad->cur_len;
792 static void find_abbrev_len_packed(struct min_abbrev_data *mad)
794 struct multi_pack_index *m;
795 struct packed_git *p;
797 for (m = get_multi_pack_index(mad->repo); m; m = m->next)
798 find_abbrev_len_for_midx(m, mad);
799 for (p = get_packed_git(mad->repo); p; p = p->next)
800 find_abbrev_len_for_pack(p, mad);
803 void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
804 const struct object_id *oid, int abbrev_len)
806 int r;
807 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
808 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
809 strbuf_setlen(sb, sb->len + r);
812 void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
813 int abbrev_len)
815 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
818 int repo_find_unique_abbrev_r(struct repository *r, char *hex,
819 const struct object_id *oid, int len)
821 const struct git_hash_algo *algo =
822 oid->algo ? &hash_algos[oid->algo] : r->hash_algo;
823 struct disambiguate_state ds;
824 struct min_abbrev_data mad;
825 struct object_id oid_ret;
826 const unsigned hexsz = algo->hexsz;
828 if (len < 0) {
829 unsigned long count = repo_approximate_object_count(r);
831 * Add one because the MSB only tells us the highest bit set,
832 * not including the value of all the _other_ bits (so "15"
833 * is only one off of 2^4, but the MSB is the 3rd bit.
835 len = msb(count) + 1;
837 * We now know we have on the order of 2^len objects, which
838 * expects a collision at 2^(len/2). But we also care about hex
839 * chars, not bits, and there are 4 bits per hex. So all
840 * together we need to divide by 2 and round up.
842 len = DIV_ROUND_UP(len, 2);
844 * For very small repos, we stick with our regular fallback.
846 if (len < FALLBACK_DEFAULT_ABBREV)
847 len = FALLBACK_DEFAULT_ABBREV;
850 oid_to_hex_r(hex, oid);
851 if (len >= hexsz || !len)
852 return hexsz;
854 mad.repo = r;
855 mad.init_len = len;
856 mad.cur_len = len;
857 mad.hex = hex;
858 mad.oid = oid;
860 find_abbrev_len_packed(&mad);
862 if (init_object_disambiguation(r, hex, mad.cur_len, algo, &ds) < 0)
863 return -1;
865 ds.fn = repo_extend_abbrev_len;
866 ds.always_call_fn = 1;
867 ds.cb_data = (void *)&mad;
869 find_short_object_filename(&ds);
870 (void)finish_object_disambiguation(&ds, &oid_ret);
872 hex[mad.cur_len] = 0;
873 return mad.cur_len;
876 const char *repo_find_unique_abbrev(struct repository *r,
877 const struct object_id *oid,
878 int len)
880 static int bufno;
881 static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
882 char *hex = hexbuffer[bufno];
883 bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
884 repo_find_unique_abbrev_r(r, hex, oid, len);
885 return hex;
888 static int ambiguous_path(const char *path, int len)
890 int slash = 1;
891 int cnt;
893 for (cnt = 0; cnt < len; cnt++) {
894 switch (*path++) {
895 case '\0':
896 break;
897 case '/':
898 if (slash)
899 break;
900 slash = 1;
901 continue;
902 case '.':
903 continue;
904 default:
905 slash = 0;
906 continue;
908 break;
910 return slash;
913 static inline int at_mark(const char *string, int len,
914 const char **suffix, int nr)
916 int i;
918 for (i = 0; i < nr; i++) {
919 int suffix_len = strlen(suffix[i]);
920 if (suffix_len <= len
921 && !strncasecmp(string, suffix[i], suffix_len))
922 return suffix_len;
924 return 0;
927 static inline int upstream_mark(const char *string, int len)
929 const char *suffix[] = { "@{upstream}", "@{u}" };
930 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
933 static inline int push_mark(const char *string, int len)
935 const char *suffix[] = { "@{push}" };
936 return at_mark(string, len, suffix, ARRAY_SIZE(suffix));
939 static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags);
940 static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf);
942 static int get_oid_basic(struct repository *r, const char *str, int len,
943 struct object_id *oid, unsigned int flags)
945 static const char *warn_msg = "refname '%.*s' is ambiguous.";
946 static const char *object_name_msg = N_(
947 "Git normally never creates a ref that ends with 40 hex characters\n"
948 "because it will be ignored when you just specify 40-hex. These refs\n"
949 "may be created by mistake. For example,\n"
950 "\n"
951 " git switch -c $br $(git rev-parse ...)\n"
952 "\n"
953 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
954 "examine these refs and maybe delete them. Turn this message off by\n"
955 "running \"git config advice.objectNameWarning false\"");
956 struct object_id tmp_oid;
957 char *real_ref = NULL;
958 int refs_found = 0;
959 int at, reflog_len, nth_prior = 0;
960 int fatal = !(flags & GET_OID_QUIETLY);
962 if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) {
963 if (repo_settings_get_warn_ambiguous_refs(r) && warn_on_object_refname_ambiguity) {
964 refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0);
965 if (refs_found > 0) {
966 warning(warn_msg, len, str);
967 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING))
968 fprintf(stderr, "%s\n", _(object_name_msg));
970 free(real_ref);
972 return 0;
975 /* basic@{time or number or -number} format to query ref-log */
976 reflog_len = at = 0;
977 if (len && str[len-1] == '}') {
978 for (at = len-4; at >= 0; at--) {
979 if (str[at] == '@' && str[at+1] == '{') {
980 if (str[at+2] == '-') {
981 if (at != 0)
982 /* @{-N} not at start */
983 return -1;
984 nth_prior = 1;
985 continue;
987 if (!upstream_mark(str + at, len - at) &&
988 !push_mark(str + at, len - at)) {
989 reflog_len = (len-1) - (at+2);
990 len = at;
992 break;
997 /* Accept only unambiguous ref paths. */
998 if (len && ambiguous_path(str, len))
999 return -1;
1001 if (nth_prior) {
1002 struct strbuf buf = STRBUF_INIT;
1003 int detached;
1005 if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) {
1006 detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid));
1007 strbuf_release(&buf);
1008 if (detached)
1009 return 0;
1013 if (!len && reflog_len)
1014 /* allow "@{...}" to mean the current branch reflog */
1015 refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal);
1016 else if (reflog_len)
1017 refs_found = repo_dwim_log(r, str, len, oid, &real_ref);
1018 else
1019 refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal);
1021 if (!refs_found)
1022 return -1;
1024 if (repo_settings_get_warn_ambiguous_refs(r) && !(flags & GET_OID_QUIETLY) &&
1025 (refs_found > 1 ||
1026 !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY)))
1027 warning(warn_msg, len, str);
1029 if (reflog_len) {
1030 int nth, i;
1031 timestamp_t at_time;
1032 timestamp_t co_time;
1033 int co_tz, co_cnt;
1035 /* Is it asking for N-th entry, or approxidate? */
1036 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
1037 char ch = str[at+2+i];
1038 if ('0' <= ch && ch <= '9')
1039 nth = nth * 10 + ch - '0';
1040 else
1041 nth = -1;
1043 if (100000000 <= nth) {
1044 at_time = nth;
1045 nth = -1;
1046 } else if (0 <= nth)
1047 at_time = 0;
1048 else {
1049 int errors = 0;
1050 char *tmp = xstrndup(str + at + 2, reflog_len);
1051 at_time = approxidate_careful(tmp, &errors);
1052 free(tmp);
1053 if (errors) {
1054 free(real_ref);
1055 return -1;
1058 if (read_ref_at(get_main_ref_store(r),
1059 real_ref, flags, at_time, nth, oid, NULL,
1060 &co_time, &co_tz, &co_cnt)) {
1061 if (!len) {
1062 if (!skip_prefix(real_ref, "refs/heads/", &str))
1063 str = "HEAD";
1064 len = strlen(str);
1066 if (at_time) {
1067 if (!(flags & GET_OID_QUIETLY)) {
1068 warning(_("log for '%.*s' only goes back to %s"),
1069 len, str,
1070 show_date(co_time, co_tz, DATE_MODE(RFC2822)));
1072 } else if (nth == co_cnt && !is_null_oid(oid)) {
1074 * We were asked for the Nth reflog (counting
1075 * from 0), but there were only N entries.
1076 * read_ref_at() will have returned "1" to tell
1077 * us it did not find an entry, but it did
1078 * still fill in the oid with the "old" value,
1079 * which we can use.
1081 } else {
1082 if (flags & GET_OID_QUIETLY) {
1083 exit(128);
1085 die(_("log for '%.*s' only has %d entries"),
1086 len, str, co_cnt);
1091 free(real_ref);
1092 return 0;
1095 static enum get_oid_result get_parent(struct repository *r,
1096 const char *name, int len,
1097 struct object_id *result, int idx)
1099 struct object_id oid;
1100 enum get_oid_result ret = get_oid_1(r, name, len, &oid,
1101 GET_OID_COMMITTISH);
1102 struct commit *commit;
1103 struct commit_list *p;
1105 if (ret)
1106 return ret;
1107 commit = lookup_commit_reference(r, &oid);
1108 if (repo_parse_commit(r, commit))
1109 return MISSING_OBJECT;
1110 if (!idx) {
1111 oidcpy(result, &commit->object.oid);
1112 return FOUND;
1114 p = commit->parents;
1115 while (p) {
1116 if (!--idx) {
1117 oidcpy(result, &p->item->object.oid);
1118 return FOUND;
1120 p = p->next;
1122 return MISSING_OBJECT;
1125 static enum get_oid_result get_nth_ancestor(struct repository *r,
1126 const char *name, int len,
1127 struct object_id *result,
1128 int generation)
1130 struct object_id oid;
1131 struct commit *commit;
1132 int ret;
1134 ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH);
1135 if (ret)
1136 return ret;
1137 commit = lookup_commit_reference(r, &oid);
1138 if (!commit)
1139 return MISSING_OBJECT;
1141 while (generation--) {
1142 if (repo_parse_commit(r, commit) || !commit->parents)
1143 return MISSING_OBJECT;
1144 commit = commit->parents->item;
1146 oidcpy(result, &commit->object.oid);
1147 return FOUND;
1150 struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen,
1151 struct object *o, enum object_type expected_type)
1153 if (name && !namelen)
1154 namelen = strlen(name);
1155 while (1) {
1156 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1157 return NULL;
1158 if (expected_type == OBJ_ANY || o->type == expected_type)
1159 return o;
1160 if (o->type == OBJ_TAG)
1161 o = ((struct tag*) o)->tagged;
1162 else if (o->type == OBJ_COMMIT)
1163 o = &(repo_get_commit_tree(r, ((struct commit *)o))->object);
1164 else {
1165 if (name)
1166 error("%.*s: expected %s type, but the object "
1167 "dereferences to %s type",
1168 namelen, name, type_name(expected_type),
1169 type_name(o->type));
1170 return NULL;
1175 static int peel_onion(struct repository *r, const char *name, int len,
1176 struct object_id *oid, unsigned lookup_flags)
1178 struct object_id outer;
1179 const char *sp;
1180 unsigned int expected_type = 0;
1181 struct object *o;
1184 * "ref^{type}" dereferences ref repeatedly until you cannot
1185 * dereference anymore, or you get an object of given type,
1186 * whichever comes first. "ref^{}" means just dereference
1187 * tags until you get a non-tag. "ref^0" is a shorthand for
1188 * "ref^{commit}". "commit^{tree}" could be used to find the
1189 * top-level tree of the given commit.
1191 if (len < 4 || name[len-1] != '}')
1192 return -1;
1194 for (sp = name + len - 1; name <= sp; sp--) {
1195 int ch = *sp;
1196 if (ch == '{' && name < sp && sp[-1] == '^')
1197 break;
1199 if (sp <= name)
1200 return -1;
1202 sp++; /* beginning of type name, or closing brace for empty */
1203 if (starts_with(sp, "commit}"))
1204 expected_type = OBJ_COMMIT;
1205 else if (starts_with(sp, "tag}"))
1206 expected_type = OBJ_TAG;
1207 else if (starts_with(sp, "tree}"))
1208 expected_type = OBJ_TREE;
1209 else if (starts_with(sp, "blob}"))
1210 expected_type = OBJ_BLOB;
1211 else if (starts_with(sp, "object}"))
1212 expected_type = OBJ_ANY;
1213 else if (sp[0] == '}')
1214 expected_type = OBJ_NONE;
1215 else if (sp[0] == '/')
1216 expected_type = OBJ_COMMIT;
1217 else
1218 return -1;
1220 lookup_flags &= ~GET_OID_DISAMBIGUATORS;
1221 if (expected_type == OBJ_COMMIT)
1222 lookup_flags |= GET_OID_COMMITTISH;
1223 else if (expected_type == OBJ_TREE)
1224 lookup_flags |= GET_OID_TREEISH;
1226 if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags))
1227 return -1;
1229 o = parse_object(r, &outer);
1230 if (!o)
1231 return -1;
1232 if (!expected_type) {
1233 o = deref_tag(r, o, name, sp - name - 2);
1234 if (!o || (!o->parsed && !parse_object(r, &o->oid)))
1235 return -1;
1236 oidcpy(oid, &o->oid);
1237 return 0;
1241 * At this point, the syntax look correct, so
1242 * if we do not get the needed object, we should
1243 * barf.
1245 o = repo_peel_to_type(r, name, len, o, expected_type);
1246 if (!o)
1247 return -1;
1249 oidcpy(oid, &o->oid);
1250 if (sp[0] == '/') {
1251 /* "$commit^{/foo}" */
1252 char *prefix;
1253 int ret;
1254 struct commit_list *list = NULL;
1257 * $commit^{/}. Some regex implementation may reject.
1258 * We don't need regex anyway. '' pattern always matches.
1260 if (sp[1] == '}')
1261 return 0;
1263 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
1264 commit_list_insert((struct commit *)o, &list);
1265 ret = get_oid_oneline(r, prefix, oid, list);
1267 free_commit_list(list);
1268 free(prefix);
1269 return ret;
1271 return 0;
1274 static int get_describe_name(struct repository *r,
1275 const char *name, int len,
1276 struct object_id *oid)
1278 const char *cp;
1279 unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT;
1281 for (cp = name + len - 1; name + 2 <= cp; cp--) {
1282 char ch = *cp;
1283 if (!isxdigit(ch)) {
1284 /* We must be looking at g in "SOMETHING-g"
1285 * for it to be describe output.
1287 if (ch == 'g' && cp[-1] == '-') {
1288 cp++;
1289 len -= cp - name;
1290 return get_short_oid(r,
1291 cp, len, oid, flags);
1295 return -1;
1298 static enum get_oid_result get_oid_1(struct repository *r,
1299 const char *name, int len,
1300 struct object_id *oid,
1301 unsigned lookup_flags)
1303 int ret, has_suffix;
1304 const char *cp;
1307 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1309 has_suffix = 0;
1310 for (cp = name + len - 1; name <= cp; cp--) {
1311 int ch = *cp;
1312 if ('0' <= ch && ch <= '9')
1313 continue;
1314 if (ch == '~' || ch == '^')
1315 has_suffix = ch;
1316 break;
1319 if (has_suffix) {
1320 unsigned int num = 0;
1321 int len1 = cp - name;
1322 cp++;
1323 while (cp < name + len) {
1324 unsigned int digit = *cp++ - '0';
1325 if (unsigned_mult_overflows(num, 10))
1326 return MISSING_OBJECT;
1327 num *= 10;
1328 if (unsigned_add_overflows(num, digit))
1329 return MISSING_OBJECT;
1330 num += digit;
1332 if (!num && len1 == len - 1)
1333 num = 1;
1334 else if (num > INT_MAX)
1335 return MISSING_OBJECT;
1336 if (has_suffix == '^')
1337 return get_parent(r, name, len1, oid, num);
1338 /* else if (has_suffix == '~') -- goes without saying */
1339 return get_nth_ancestor(r, name, len1, oid, num);
1342 ret = peel_onion(r, name, len, oid, lookup_flags);
1343 if (!ret)
1344 return FOUND;
1346 ret = get_oid_basic(r, name, len, oid, lookup_flags);
1347 if (!ret)
1348 return FOUND;
1350 /* It could be describe output that is "SOMETHING-gXXXX" */
1351 ret = get_describe_name(r, name, len, oid);
1352 if (!ret)
1353 return FOUND;
1355 return get_short_oid(r, name, len, oid, lookup_flags);
1359 * This interprets names like ':/Initial revision of "git"' by searching
1360 * through history and returning the first commit whose message starts
1361 * the given regular expression.
1363 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1365 * For a literal '!' character at the beginning of a pattern, you have to repeat
1366 * that, like: ':/!!foo'
1368 * For future extension, all other sequences beginning with ':/!' are reserved.
1371 /* Remember to update object flag allocation in object.h */
1372 #define ONELINE_SEEN (1u<<20)
1374 struct handle_one_ref_cb {
1375 struct repository *repo;
1376 struct commit_list **list;
1379 static int handle_one_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
1380 int flag UNUSED,
1381 void *cb_data)
1383 struct handle_one_ref_cb *cb = cb_data;
1384 struct commit_list **list = cb->list;
1385 struct object *object = parse_object(cb->repo, oid);
1386 if (!object)
1387 return 0;
1388 if (object->type == OBJ_TAG) {
1389 object = deref_tag(cb->repo, object, path,
1390 strlen(path));
1391 if (!object)
1392 return 0;
1394 if (object->type != OBJ_COMMIT)
1395 return 0;
1396 commit_list_insert((struct commit *)object, list);
1397 return 0;
1400 static int get_oid_oneline(struct repository *r,
1401 const char *prefix, struct object_id *oid,
1402 const struct commit_list *list)
1404 struct commit_list *copy = NULL;
1405 const struct commit_list *l;
1406 int found = 0;
1407 int negative = 0;
1408 regex_t regex;
1410 if (prefix[0] == '!') {
1411 prefix++;
1413 if (prefix[0] == '-') {
1414 prefix++;
1415 negative = 1;
1416 } else if (prefix[0] != '!') {
1417 return -1;
1421 if (regcomp(&regex, prefix, REG_EXTENDED))
1422 return -1;
1424 for (l = list; l; l = l->next) {
1425 l->item->object.flags |= ONELINE_SEEN;
1426 commit_list_insert(l->item, &copy);
1428 while (copy) {
1429 const char *p, *buf;
1430 struct commit *commit;
1431 int matches;
1433 commit = pop_most_recent_commit(&copy, ONELINE_SEEN);
1434 if (!parse_object(r, &commit->object.oid))
1435 continue;
1436 buf = repo_get_commit_buffer(r, commit, NULL);
1437 p = strstr(buf, "\n\n");
1438 matches = negative ^ (p && !regexec(&regex, p + 2, 0, NULL, 0));
1439 repo_unuse_commit_buffer(r, commit, buf);
1441 if (matches) {
1442 oidcpy(oid, &commit->object.oid);
1443 found = 1;
1444 break;
1447 regfree(&regex);
1448 for (l = list; l; l = l->next)
1449 clear_commit_marks(l->item, ONELINE_SEEN);
1450 free_commit_list(copy);
1451 return found ? 0 : -1;
1454 struct grab_nth_branch_switch_cbdata {
1455 int remaining;
1456 struct strbuf *sb;
1459 static int grab_nth_branch_switch(struct object_id *ooid UNUSED,
1460 struct object_id *noid UNUSED,
1461 const char *email UNUSED,
1462 timestamp_t timestamp UNUSED,
1463 int tz UNUSED,
1464 const char *message, void *cb_data)
1466 struct grab_nth_branch_switch_cbdata *cb = cb_data;
1467 const char *match = NULL, *target = NULL;
1468 size_t len;
1470 if (skip_prefix(message, "checkout: moving from ", &match))
1471 target = strstr(match, " to ");
1473 if (!match || !target)
1474 return 0;
1475 if (--(cb->remaining) == 0) {
1476 len = target - match;
1477 strbuf_reset(cb->sb);
1478 strbuf_add(cb->sb, match, len);
1479 return 1; /* we are done */
1481 return 0;
1485 * Parse @{-N} syntax, return the number of characters parsed
1486 * if successful; otherwise signal an error with negative value.
1488 static int interpret_nth_prior_checkout(struct repository *r,
1489 const char *name, int namelen,
1490 struct strbuf *buf)
1492 long nth;
1493 int retval;
1494 struct grab_nth_branch_switch_cbdata cb;
1495 const char *brace;
1496 char *num_end;
1498 if (namelen < 4)
1499 return -1;
1500 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
1501 return -1;
1502 brace = memchr(name, '}', namelen);
1503 if (!brace)
1504 return -1;
1505 nth = strtol(name + 3, &num_end, 10);
1506 if (num_end != brace)
1507 return -1;
1508 if (nth <= 0)
1509 return -1;
1510 cb.remaining = nth;
1511 cb.sb = buf;
1513 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1514 "HEAD", grab_nth_branch_switch, &cb);
1515 if (0 < retval) {
1516 retval = brace - name + 1;
1517 } else
1518 retval = 0;
1520 return retval;
1523 int repo_get_oid_mb(struct repository *r,
1524 const char *name,
1525 struct object_id *oid)
1527 struct commit *one, *two;
1528 struct commit_list *mbs = NULL;
1529 struct object_id oid_tmp;
1530 const char *dots;
1531 int st;
1533 dots = strstr(name, "...");
1534 if (!dots)
1535 return repo_get_oid(r, name, oid);
1536 if (dots == name)
1537 st = repo_get_oid(r, "HEAD", &oid_tmp);
1538 else {
1539 struct strbuf sb;
1540 strbuf_init(&sb, dots - name);
1541 strbuf_add(&sb, name, dots - name);
1542 st = repo_get_oid_committish(r, sb.buf, &oid_tmp);
1543 strbuf_release(&sb);
1545 if (st)
1546 return st;
1547 one = lookup_commit_reference_gently(r, &oid_tmp, 0);
1548 if (!one)
1549 return -1;
1551 if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp))
1552 return -1;
1553 two = lookup_commit_reference_gently(r, &oid_tmp, 0);
1554 if (!two)
1555 return -1;
1556 if (repo_get_merge_bases(r, one, two, &mbs) < 0) {
1557 free_commit_list(mbs);
1558 return -1;
1560 if (!mbs || mbs->next)
1561 st = -1;
1562 else {
1563 st = 0;
1564 oidcpy(oid, &mbs->item->object.oid);
1566 free_commit_list(mbs);
1567 return st;
1570 /* parse @something syntax, when 'something' is not {.*} */
1571 static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf)
1573 const char *next;
1575 if (len || name[1] == '{')
1576 return -1;
1578 /* make sure it's a single @, or @@{.*}, not @foo */
1579 next = memchr(name + len + 1, '@', namelen - len - 1);
1580 if (next && next[1] != '{')
1581 return -1;
1582 if (!next)
1583 next = name + namelen;
1584 if (next != name + 1)
1585 return -1;
1587 strbuf_reset(buf);
1588 strbuf_add(buf, "HEAD", 4);
1589 return 1;
1592 static int reinterpret(struct repository *r,
1593 const char *name, int namelen, int len,
1594 struct strbuf *buf, unsigned allowed)
1596 /* we have extra data, which might need further processing */
1597 struct strbuf tmp = STRBUF_INIT;
1598 int used = buf->len;
1599 int ret;
1600 struct interpret_branch_name_options options = {
1601 .allowed = allowed
1604 strbuf_add(buf, name + len, namelen - len);
1605 ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options);
1606 /* that data was not interpreted, remove our cruft */
1607 if (ret < 0) {
1608 strbuf_setlen(buf, used);
1609 return len;
1611 strbuf_reset(buf);
1612 strbuf_addbuf(buf, &tmp);
1613 strbuf_release(&tmp);
1614 /* tweak for size of {-N} versus expanded ref name */
1615 return ret - used + len;
1618 static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref)
1620 char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0);
1621 strbuf_reset(buf);
1622 strbuf_addstr(buf, s);
1623 free(s);
1626 static int branch_interpret_allowed(const char *refname, unsigned allowed)
1628 if (!allowed)
1629 return 1;
1631 if ((allowed & INTERPRET_BRANCH_LOCAL) &&
1632 starts_with(refname, "refs/heads/"))
1633 return 1;
1634 if ((allowed & INTERPRET_BRANCH_REMOTE) &&
1635 starts_with(refname, "refs/remotes/"))
1636 return 1;
1638 return 0;
1641 static int interpret_branch_mark(struct repository *r,
1642 const char *name, int namelen,
1643 int at, struct strbuf *buf,
1644 int (*get_mark)(const char *, int),
1645 const char *(*get_data)(struct branch *,
1646 struct strbuf *),
1647 const struct interpret_branch_name_options *options)
1649 int len;
1650 struct branch *branch;
1651 struct strbuf err = STRBUF_INIT;
1652 const char *value;
1654 len = get_mark(name + at, namelen - at);
1655 if (!len)
1656 return -1;
1658 if (memchr(name, ':', at))
1659 return -1;
1661 if (at) {
1662 char *name_str = xmemdupz(name, at);
1663 branch = branch_get(name_str);
1664 free(name_str);
1665 } else
1666 branch = branch_get(NULL);
1668 value = get_data(branch, &err);
1669 if (!value) {
1670 if (options->nonfatal_dangling_mark) {
1671 strbuf_release(&err);
1672 return -1;
1673 } else {
1674 die("%s", err.buf);
1678 if (!branch_interpret_allowed(value, options->allowed))
1679 return -1;
1681 set_shortened_ref(r, buf, value);
1682 return len + at;
1685 int repo_interpret_branch_name(struct repository *r,
1686 const char *name, int namelen,
1687 struct strbuf *buf,
1688 const struct interpret_branch_name_options *options)
1690 char *at;
1691 const char *start;
1692 int len;
1694 if (!namelen)
1695 namelen = strlen(name);
1697 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) {
1698 len = interpret_nth_prior_checkout(r, name, namelen, buf);
1699 if (!len) {
1700 return len; /* syntax Ok, not enough switches */
1701 } else if (len > 0) {
1702 if (len == namelen)
1703 return len; /* consumed all */
1704 else
1705 return reinterpret(r, name, namelen, len, buf,
1706 options->allowed);
1710 for (start = name;
1711 (at = memchr(start, '@', namelen - (start - name)));
1712 start = at + 1) {
1714 if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) {
1715 len = interpret_empty_at(name, namelen, at - name, buf);
1716 if (len > 0)
1717 return reinterpret(r, name, namelen, len, buf,
1718 options->allowed);
1721 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1722 upstream_mark, branch_get_upstream,
1723 options);
1724 if (len > 0)
1725 return len;
1727 len = interpret_branch_mark(r, name, namelen, at - name, buf,
1728 push_mark, branch_get_push,
1729 options);
1730 if (len > 0)
1731 return len;
1734 return -1;
1737 void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1739 int len = strlen(name);
1740 struct interpret_branch_name_options options = {
1741 .allowed = allowed
1743 int used = repo_interpret_branch_name(the_repository, name, len, sb,
1744 &options);
1746 if (used < 0)
1747 used = 0;
1748 strbuf_add(sb, name + used, len - used);
1751 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1753 if (startup_info->have_repository)
1754 strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1755 else
1756 strbuf_addstr(sb, name);
1759 * This splice must be done even if we end up rejecting the
1760 * name; builtin/branch.c::copy_or_rename_branch() still wants
1761 * to see what the name expanded to so that "branch -m" can be
1762 * used as a tool to correct earlier mistakes.
1764 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1766 if (*name == '-' ||
1767 !strcmp(sb->buf, "refs/heads/HEAD"))
1768 return -1;
1770 return check_refname_format(sb->buf, 0);
1773 void object_context_release(struct object_context *ctx)
1775 free(ctx->path);
1776 strbuf_release(&ctx->symlink_path);
1780 * This is like "get_oid_basic()", except it allows "object ID expressions",
1781 * notably "xyz^" for "parent of xyz"
1783 int repo_get_oid(struct repository *r, const char *name, struct object_id *oid)
1785 struct object_context unused;
1786 int ret = get_oid_with_context(r, name, 0, oid, &unused);
1787 object_context_release(&unused);
1788 return ret;
1792 * This returns a non-zero value if the string (built using printf
1793 * format and the given arguments) is not a valid object.
1795 int get_oidf(struct object_id *oid, const char *fmt, ...)
1797 va_list ap;
1798 int ret;
1799 struct strbuf sb = STRBUF_INIT;
1801 va_start(ap, fmt);
1802 strbuf_vaddf(&sb, fmt, ap);
1803 va_end(ap);
1805 ret = repo_get_oid(the_repository, sb.buf, oid);
1806 strbuf_release(&sb);
1808 return ret;
1812 * Many callers know that the user meant to name a commit-ish by
1813 * syntactical positions where the object name appears. Calling this
1814 * function allows the machinery to disambiguate shorter-than-unique
1815 * abbreviated object names between commit-ish and others.
1817 * Note that this does NOT error out when the named object is not a
1818 * commit-ish. It is merely to give a hint to the disambiguation
1819 * machinery.
1821 int repo_get_oid_committish(struct repository *r,
1822 const char *name,
1823 struct object_id *oid)
1825 struct object_context unused;
1826 int ret = get_oid_with_context(r, name, GET_OID_COMMITTISH,
1827 oid, &unused);
1828 object_context_release(&unused);
1829 return ret;
1832 int repo_get_oid_treeish(struct repository *r,
1833 const char *name,
1834 struct object_id *oid)
1836 struct object_context unused;
1837 int ret = get_oid_with_context(r, name, GET_OID_TREEISH,
1838 oid, &unused);
1839 object_context_release(&unused);
1840 return ret;
1843 int repo_get_oid_commit(struct repository *r,
1844 const char *name,
1845 struct object_id *oid)
1847 struct object_context unused;
1848 int ret = get_oid_with_context(r, name, GET_OID_COMMIT,
1849 oid, &unused);
1850 object_context_release(&unused);
1851 return ret;
1854 int repo_get_oid_tree(struct repository *r,
1855 const char *name,
1856 struct object_id *oid)
1858 struct object_context unused;
1859 int ret = get_oid_with_context(r, name, GET_OID_TREE,
1860 oid, &unused);
1861 object_context_release(&unused);
1862 return ret;
1865 int repo_get_oid_blob(struct repository *r,
1866 const char *name,
1867 struct object_id *oid)
1869 struct object_context unused;
1870 int ret = get_oid_with_context(r, name, GET_OID_BLOB,
1871 oid, &unused);
1872 object_context_release(&unused);
1873 return ret;
1876 /* Must be called only when object_name:filename doesn't exist. */
1877 static void diagnose_invalid_oid_path(struct repository *r,
1878 const char *prefix,
1879 const char *filename,
1880 const struct object_id *tree_oid,
1881 const char *object_name,
1882 int object_name_len)
1884 struct object_id oid;
1885 unsigned short mode;
1887 if (!prefix)
1888 prefix = "";
1890 if (file_exists(filename))
1891 die(_("path '%s' exists on disk, but not in '%.*s'"),
1892 filename, object_name_len, object_name);
1893 if (is_missing_file_error(errno)) {
1894 char *fullname = xstrfmt("%s%s", prefix, filename);
1896 if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) {
1897 die(_("path '%s' exists, but not '%s'\n"
1898 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1899 fullname,
1900 filename,
1901 object_name_len, object_name,
1902 fullname,
1903 object_name_len, object_name,
1904 filename);
1906 die(_("path '%s' does not exist in '%.*s'"),
1907 filename, object_name_len, object_name);
1911 /* Must be called only when :stage:filename doesn't exist. */
1912 static void diagnose_invalid_index_path(struct repository *r,
1913 int stage,
1914 const char *prefix,
1915 const char *filename)
1917 struct index_state *istate = r->index;
1918 const struct cache_entry *ce;
1919 int pos;
1920 unsigned namelen = strlen(filename);
1921 struct strbuf fullname = STRBUF_INIT;
1923 if (!prefix)
1924 prefix = "";
1926 /* Wrong stage number? */
1927 pos = index_name_pos(istate, filename, namelen);
1928 if (pos < 0)
1929 pos = -pos - 1;
1930 if (pos < istate->cache_nr) {
1931 ce = istate->cache[pos];
1932 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1933 ce_namelen(ce) == namelen &&
1934 !memcmp(ce->name, filename, namelen))
1935 die(_("path '%s' is in the index, but not at stage %d\n"
1936 "hint: Did you mean ':%d:%s'?"),
1937 filename, stage,
1938 ce_stage(ce), filename);
1941 /* Confusion between relative and absolute filenames? */
1942 strbuf_addstr(&fullname, prefix);
1943 strbuf_addstr(&fullname, filename);
1944 pos = index_name_pos(istate, fullname.buf, fullname.len);
1945 if (pos < 0)
1946 pos = -pos - 1;
1947 if (pos < istate->cache_nr) {
1948 ce = istate->cache[pos];
1949 if (!S_ISSPARSEDIR(ce->ce_mode) &&
1950 ce_namelen(ce) == fullname.len &&
1951 !memcmp(ce->name, fullname.buf, fullname.len))
1952 die(_("path '%s' is in the index, but not '%s'\n"
1953 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1954 fullname.buf, filename,
1955 ce_stage(ce), fullname.buf,
1956 ce_stage(ce), filename);
1959 if (repo_file_exists(r, filename))
1960 die(_("path '%s' exists on disk, but not in the index"), filename);
1961 if (is_missing_file_error(errno))
1962 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1963 filename);
1965 strbuf_release(&fullname);
1969 static char *resolve_relative_path(struct repository *r, const char *rel)
1971 if (!starts_with(rel, "./") && !starts_with(rel, "../"))
1972 return NULL;
1974 if (r != the_repository || !is_inside_work_tree())
1975 die(_("relative path syntax can't be used outside working tree"));
1977 /* die() inside prefix_path() if resolved path is outside worktree */
1978 return prefix_path(startup_info->prefix,
1979 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1980 rel);
1983 static int reject_tree_in_index(struct repository *repo,
1984 int only_to_die,
1985 const struct cache_entry *ce,
1986 int stage,
1987 const char *prefix,
1988 const char *cp)
1990 if (!S_ISSPARSEDIR(ce->ce_mode))
1991 return 0;
1992 if (only_to_die)
1993 diagnose_invalid_index_path(repo, stage, prefix, cp);
1994 return -1;
1997 static enum get_oid_result get_oid_with_context_1(struct repository *repo,
1998 const char *name,
1999 unsigned flags,
2000 const char *prefix,
2001 struct object_id *oid,
2002 struct object_context *oc)
2004 int ret, bracket_depth;
2005 int namelen = strlen(name);
2006 const char *cp;
2007 int only_to_die = flags & GET_OID_ONLY_TO_DIE;
2009 memset(oc, 0, sizeof(*oc));
2010 oc->mode = S_IFINVALID;
2011 strbuf_init(&oc->symlink_path, 0);
2012 ret = get_oid_1(repo, name, namelen, oid, flags);
2013 if (!ret && flags & GET_OID_REQUIRE_PATH)
2014 die(_("<object>:<path> required, only <object> '%s' given"),
2015 name);
2016 if (!ret)
2017 return ret;
2019 * tree:path --> object name of path in tree
2020 * :path -> object name of absolute path in index
2021 * :./path -> object name of path relative to cwd in index
2022 * :[0-3]:path -> object name of path in index at stage
2023 * :/foo -> recent commit matching foo
2025 if (name[0] == ':') {
2026 int stage = 0;
2027 const struct cache_entry *ce;
2028 char *new_path = NULL;
2029 int pos;
2030 if (!only_to_die && namelen > 2 && name[1] == '/') {
2031 struct handle_one_ref_cb cb;
2032 struct commit_list *list = NULL;
2034 cb.repo = repo;
2035 cb.list = &list;
2036 refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2037 refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb);
2038 commit_list_sort_by_date(&list);
2039 ret = get_oid_oneline(repo, name + 2, oid, list);
2041 free_commit_list(list);
2042 return ret;
2044 if (namelen < 3 ||
2045 name[2] != ':' ||
2046 name[1] < '0' || '3' < name[1])
2047 cp = name + 1;
2048 else {
2049 stage = name[1] - '0';
2050 cp = name + 3;
2052 new_path = resolve_relative_path(repo, cp);
2053 if (!new_path) {
2054 namelen = namelen - (cp - name);
2055 } else {
2056 cp = new_path;
2057 namelen = strlen(cp);
2060 if (flags & GET_OID_RECORD_PATH)
2061 oc->path = xstrdup(cp);
2063 if (!repo->index || !repo->index->cache)
2064 repo_read_index(repo);
2065 pos = index_name_pos(repo->index, cp, namelen);
2066 if (pos < 0)
2067 pos = -pos - 1;
2068 while (pos < repo->index->cache_nr) {
2069 ce = repo->index->cache[pos];
2070 if (ce_namelen(ce) != namelen ||
2071 memcmp(ce->name, cp, namelen))
2072 break;
2073 if (ce_stage(ce) == stage) {
2074 free(new_path);
2075 if (reject_tree_in_index(repo, only_to_die, ce,
2076 stage, prefix, cp))
2077 return -1;
2078 oidcpy(oid, &ce->oid);
2079 oc->mode = ce->ce_mode;
2080 return 0;
2082 pos++;
2084 if (only_to_die && name[1] && name[1] != '/')
2085 diagnose_invalid_index_path(repo, stage, prefix, cp);
2086 free(new_path);
2087 return -1;
2089 for (cp = name, bracket_depth = 0; *cp; cp++) {
2090 if (*cp == '{')
2091 bracket_depth++;
2092 else if (bracket_depth && *cp == '}')
2093 bracket_depth--;
2094 else if (!bracket_depth && *cp == ':')
2095 break;
2097 if (*cp == ':') {
2098 struct object_id tree_oid;
2099 int len = cp - name;
2100 unsigned sub_flags = flags;
2102 sub_flags &= ~GET_OID_DISAMBIGUATORS;
2103 sub_flags |= GET_OID_TREEISH;
2105 if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) {
2106 const char *filename = cp+1;
2107 char *new_filename = NULL;
2109 new_filename = resolve_relative_path(repo, filename);
2110 if (new_filename)
2111 filename = new_filename;
2112 if (flags & GET_OID_FOLLOW_SYMLINKS) {
2113 ret = get_tree_entry_follow_symlinks(repo, &tree_oid,
2114 filename, oid, &oc->symlink_path,
2115 &oc->mode);
2116 } else {
2117 ret = get_tree_entry(repo, &tree_oid, filename, oid,
2118 &oc->mode);
2119 if (ret && only_to_die) {
2120 diagnose_invalid_oid_path(repo, prefix,
2121 filename,
2122 &tree_oid,
2123 name, len);
2126 if (flags & GET_OID_RECORD_PATH)
2127 oc->path = xstrdup(filename);
2129 free(new_filename);
2130 return ret;
2131 } else {
2132 if (only_to_die)
2133 die(_("invalid object name '%.*s'."), len, name);
2136 return ret;
2140 * Call this function when you know "name" given by the end user must
2141 * name an object but it doesn't; the function _may_ die with a better
2142 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2143 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2144 * you have a chance to diagnose the error further.
2146 void maybe_die_on_misspelt_object_name(struct repository *r,
2147 const char *name,
2148 const char *prefix)
2150 struct object_context oc;
2151 struct object_id oid;
2152 get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY,
2153 prefix, &oid, &oc);
2154 object_context_release(&oc);
2157 enum get_oid_result get_oid_with_context(struct repository *repo,
2158 const char *str,
2159 unsigned flags,
2160 struct object_id *oid,
2161 struct object_context *oc)
2163 if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE)
2164 BUG("incompatible flags for get_oid_with_context");
2165 return get_oid_with_context_1(repo, str, flags, NULL, oid, oc);