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