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