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