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