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