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