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