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