peel_onion: disambiguate to favor tree-ish when we know we want a tree-ish
[git] / sha1_name.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
6 #include "tree-walk.h"
7 #include "refs.h"
8 #include "remote.h"
9
10 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
11
12 typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
13
14 struct disambiguate_state {
15         disambiguate_hint_fn fn;
16         void *cb_data;
17         unsigned char candidate[20];
18         unsigned candidate_exists:1;
19         unsigned candidate_checked:1;
20         unsigned candidate_ok:1;
21         unsigned disambiguate_fn_used:1;
22         unsigned ambiguous:1;
23         unsigned always_call_fn:1;
24 };
25
26 static void update_candidates(struct disambiguate_state *ds, const unsigned char *current)
27 {
28         if (ds->always_call_fn) {
29                 ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0;
30                 return;
31         }
32         if (!ds->candidate_exists) {
33                 /* this is the first candidate */
34                 hashcpy(ds->candidate, current);
35                 ds->candidate_exists = 1;
36                 return;
37         } else if (!hashcmp(ds->candidate, current)) {
38                 /* the same as what we already have seen */
39                 return;
40         }
41
42         if (!ds->fn) {
43                 /* cannot disambiguate between ds->candidate and current */
44                 ds->ambiguous = 1;
45                 return;
46         }
47
48         if (!ds->candidate_checked) {
49                 ds->candidate_ok = ds->fn(ds->candidate, ds->cb_data);
50                 ds->disambiguate_fn_used = 1;
51                 ds->candidate_checked = 1;
52         }
53
54         if (!ds->candidate_ok) {
55                 /* discard the candidate; we know it does not satisify fn */
56                 hashcpy(ds->candidate, current);
57                 ds->candidate_checked = 0;
58                 return;
59         }
60
61         /* if we reach this point, we know ds->candidate satisfies fn */
62         if (ds->fn(current, ds->cb_data)) {
63                 /*
64                  * if both current and candidate satisfy fn, we cannot
65                  * disambiguate.
66                  */
67                 ds->candidate_ok = 0;
68                 ds->ambiguous = 1;
69         }
70
71         /* otherwise, current can be discarded and candidate is still good */
72 }
73
74 static void find_short_object_filename(int len, const char *hex_pfx, struct disambiguate_state *ds)
75 {
76         struct alternate_object_database *alt;
77         char hex[40];
78         static struct alternate_object_database *fakeent;
79
80         if (!fakeent) {
81                 /*
82                  * Create a "fake" alternate object database that
83                  * points to our own object database, to make it
84                  * easier to get a temporary working space in
85                  * alt->name/alt->base while iterating over the
86                  * object databases including our own.
87                  */
88                 const char *objdir = get_object_directory();
89                 int objdir_len = strlen(objdir);
90                 int entlen = objdir_len + 43;
91                 fakeent = xmalloc(sizeof(*fakeent) + entlen);
92                 memcpy(fakeent->base, objdir, objdir_len);
93                 fakeent->name = fakeent->base + objdir_len + 1;
94                 fakeent->name[-1] = '/';
95         }
96         fakeent->next = alt_odb_list;
97
98         sprintf(hex, "%.2s", hex_pfx);
99         for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) {
100                 struct dirent *de;
101                 DIR *dir;
102                 sprintf(alt->name, "%.2s/", hex_pfx);
103                 dir = opendir(alt->base);
104                 if (!dir)
105                         continue;
106
107                 while (!ds->ambiguous && (de = readdir(dir)) != NULL) {
108                         unsigned char sha1[20];
109
110                         if (strlen(de->d_name) != 38)
111                                 continue;
112                         if (memcmp(de->d_name, hex_pfx + 2, len - 2))
113                                 continue;
114                         memcpy(hex + 2, de->d_name, 38);
115                         if (!get_sha1_hex(hex, sha1))
116                                 update_candidates(ds, sha1);
117                 }
118                 closedir(dir);
119         }
120 }
121
122 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
123 {
124         do {
125                 if (*a != *b)
126                         return 0;
127                 a++;
128                 b++;
129                 len -= 2;
130         } while (len > 1);
131         if (len)
132                 if ((*a ^ *b) & 0xf0)
133                         return 0;
134         return 1;
135 }
136
137 static void unique_in_pack(int len,
138                           const unsigned char *bin_pfx,
139                            struct packed_git *p,
140                            struct disambiguate_state *ds)
141 {
142         uint32_t num, last, i, first = 0;
143         const unsigned char *current = NULL;
144
145         open_pack_index(p);
146         num = p->num_objects;
147         last = num;
148         while (first < last) {
149                 uint32_t mid = (first + last) / 2;
150                 const unsigned char *current;
151                 int cmp;
152
153                 current = nth_packed_object_sha1(p, mid);
154                 cmp = hashcmp(bin_pfx, current);
155                 if (!cmp) {
156                         first = mid;
157                         break;
158                 }
159                 if (cmp > 0) {
160                         first = mid+1;
161                         continue;
162                 }
163                 last = mid;
164         }
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                 current = nth_packed_object_sha1(p, i);
173                 if (!match_sha(len, bin_pfx, current))
174                         break;
175                 update_candidates(ds, current);
176         }
177 }
178
179 static void find_short_packed_object(int len, const unsigned char *bin_pfx,
180                                      struct disambiguate_state *ds)
181 {
182         struct packed_git *p;
183
184         prepare_packed_git();
185         for (p = packed_git; p && !ds->ambiguous; p = p->next)
186                 unique_in_pack(len, bin_pfx, p, ds);
187 }
188
189 #define SHORT_NAME_NOT_FOUND (-1)
190 #define SHORT_NAME_AMBIGUOUS (-2)
191
192 static int finish_object_disambiguation(struct disambiguate_state *ds,
193                                         unsigned char *sha1)
194 {
195         if (ds->ambiguous)
196                 return SHORT_NAME_AMBIGUOUS;
197
198         if (!ds->candidate_exists)
199                 return SHORT_NAME_NOT_FOUND;
200
201         if (!ds->candidate_checked)
202                 /*
203                  * If this is the only candidate, there is no point
204                  * calling the disambiguation hint callback.
205                  *
206                  * On the other hand, if the current candidate
207                  * replaced an earlier candidate that did _not_ pass
208                  * the disambiguation hint callback, then we do have
209                  * more than one objects that match the short name
210                  * given, so we should make sure this one matches;
211                  * otherwise, if we discovered this one and the one
212                  * that we previously discarded in the reverse order,
213                  * we would end up showing different results in the
214                  * same repository!
215                  */
216                 ds->candidate_ok = (!ds->disambiguate_fn_used ||
217                                     ds->fn(ds->candidate, ds->cb_data));
218
219         if (!ds->candidate_ok)
220                 return SHORT_NAME_AMBIGUOUS;
221
222         hashcpy(sha1, ds->candidate);
223         return 0;
224 }
225
226 static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unused)
227 {
228         int kind = sha1_object_info(sha1, NULL);
229         return kind == OBJ_COMMIT;
230 }
231
232 static int disambiguate_committish_only(const unsigned char *sha1, void *cb_data_unused)
233 {
234         struct object *obj;
235         int kind;
236
237         kind = sha1_object_info(sha1, NULL);
238         if (kind == OBJ_COMMIT)
239                 return 1;
240         if (kind != OBJ_TAG)
241                 return 0;
242
243         /* We need to do this the hard way... */
244         obj = deref_tag(lookup_object(sha1), NULL, 0);
245         if (obj && obj->type == OBJ_COMMIT)
246                 return 1;
247         return 0;
248 }
249
250 static int disambiguate_tree_only(const unsigned char *sha1, void *cb_data_unused)
251 {
252         int kind = sha1_object_info(sha1, NULL);
253         return kind == OBJ_TREE;
254 }
255
256 static int disambiguate_treeish_only(const unsigned char *sha1, void *cb_data_unused)
257 {
258         struct object *obj;
259         int kind;
260
261         kind = sha1_object_info(sha1, NULL);
262         if (kind == OBJ_TREE || kind == OBJ_COMMIT)
263                 return 1;
264         if (kind != OBJ_TAG)
265                 return 0;
266
267         /* We need to do this the hard way... */
268         obj = deref_tag(lookup_object(sha1), NULL, 0);
269         if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT))
270                 return 1;
271         return 0;
272 }
273
274 static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unused)
275 {
276         int kind = sha1_object_info(sha1, NULL);
277         return kind == OBJ_BLOB;
278 }
279
280 static int prepare_prefixes(const char *name, int len,
281                             unsigned char *bin_pfx,
282                             char *hex_pfx)
283 {
284         int i;
285
286         hashclr(bin_pfx);
287         memset(hex_pfx, 'x', 40);
288         for (i = 0; i < len ;i++) {
289                 unsigned char c = name[i];
290                 unsigned char val;
291                 if (c >= '0' && c <= '9')
292                         val = c - '0';
293                 else if (c >= 'a' && c <= 'f')
294                         val = c - 'a' + 10;
295                 else if (c >= 'A' && c <='F') {
296                         val = c - 'A' + 10;
297                         c -= 'A' - 'a';
298                 }
299                 else
300                         return -1;
301                 hex_pfx[i] = c;
302                 if (!(i & 1))
303                         val <<= 4;
304                 bin_pfx[i >> 1] |= val;
305         }
306         return 0;
307 }
308
309 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
310                           unsigned flags)
311 {
312         int status;
313         char hex_pfx[40];
314         unsigned char bin_pfx[20];
315         struct disambiguate_state ds;
316         int quietly = !!(flags & GET_SHA1_QUIETLY);
317
318         if (len < MINIMUM_ABBREV || len > 40)
319                 return -1;
320         if (prepare_prefixes(name, len, bin_pfx, hex_pfx) < 0)
321                 return -1;
322
323         prepare_alt_odb();
324
325         memset(&ds, 0, sizeof(ds));
326         if (flags & GET_SHA1_COMMIT)
327                 ds.fn = disambiguate_commit_only;
328         else if (flags & GET_SHA1_COMMITTISH)
329                 ds.fn = disambiguate_committish_only;
330         else if (flags & GET_SHA1_TREE)
331                 ds.fn = disambiguate_tree_only;
332         else if (flags & GET_SHA1_TREEISH)
333                 ds.fn = disambiguate_treeish_only;
334         else if (flags & GET_SHA1_BLOB)
335                 ds.fn = disambiguate_blob_only;
336
337         find_short_object_filename(len, hex_pfx, &ds);
338         find_short_packed_object(len, bin_pfx, &ds);
339         status = finish_object_disambiguation(&ds, sha1);
340
341         if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
342                 return error("short SHA1 %.*s is ambiguous.", len, hex_pfx);
343         return status;
344 }
345
346
347 int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
348 {
349         char hex_pfx[40];
350         unsigned char bin_pfx[20];
351         struct disambiguate_state ds;
352         int len = strlen(prefix);
353
354         if (len < MINIMUM_ABBREV || len > 40)
355                 return -1;
356         if (prepare_prefixes(prefix, len, bin_pfx, hex_pfx) < 0)
357                 return -1;
358
359         prepare_alt_odb();
360
361         memset(&ds, 0, sizeof(ds));
362         ds.always_call_fn = 1;
363         ds.cb_data = cb_data;
364         ds.fn = fn;
365
366         find_short_object_filename(len, hex_pfx, &ds);
367         find_short_packed_object(len, bin_pfx, &ds);
368         return ds.ambiguous;
369 }
370
371 const char *find_unique_abbrev(const unsigned char *sha1, int len)
372 {
373         int status, exists;
374         static char hex[41];
375
376         exists = has_sha1_file(sha1);
377         memcpy(hex, sha1_to_hex(sha1), 40);
378         if (len == 40 || !len)
379                 return hex;
380         while (len < 40) {
381                 unsigned char sha1_ret[20];
382                 status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
383                 if (exists
384                     ? !status
385                     : status == SHORT_NAME_NOT_FOUND) {
386                         hex[len] = 0;
387                         return hex;
388                 }
389                 len++;
390         }
391         return hex;
392 }
393
394 static int ambiguous_path(const char *path, int len)
395 {
396         int slash = 1;
397         int cnt;
398
399         for (cnt = 0; cnt < len; cnt++) {
400                 switch (*path++) {
401                 case '\0':
402                         break;
403                 case '/':
404                         if (slash)
405                                 break;
406                         slash = 1;
407                         continue;
408                 case '.':
409                         continue;
410                 default:
411                         slash = 0;
412                         continue;
413                 }
414                 break;
415         }
416         return slash;
417 }
418
419 static inline int upstream_mark(const char *string, int len)
420 {
421         const char *suffix[] = { "@{upstream}", "@{u}" };
422         int i;
423
424         for (i = 0; i < ARRAY_SIZE(suffix); i++) {
425                 int suffix_len = strlen(suffix[i]);
426                 if (suffix_len <= len
427                     && !memcmp(string, suffix[i], suffix_len))
428                         return suffix_len;
429         }
430         return 0;
431 }
432
433 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags);
434
435 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
436 {
437         static const char *warn_msg = "refname '%.*s' is ambiguous.";
438         char *real_ref = NULL;
439         int refs_found = 0;
440         int at, reflog_len;
441
442         if (len == 40 && !get_sha1_hex(str, sha1))
443                 return 0;
444
445         /* basic@{time or number or -number} format to query ref-log */
446         reflog_len = at = 0;
447         if (len && str[len-1] == '}') {
448                 for (at = len-2; at >= 0; at--) {
449                         if (str[at] == '@' && str[at+1] == '{') {
450                                 if (!upstream_mark(str + at, len - at)) {
451                                         reflog_len = (len-1) - (at+2);
452                                         len = at;
453                                 }
454                                 break;
455                         }
456                 }
457         }
458
459         /* Accept only unambiguous ref paths. */
460         if (len && ambiguous_path(str, len))
461                 return -1;
462
463         if (!len && reflog_len) {
464                 struct strbuf buf = STRBUF_INIT;
465                 int ret;
466                 /* try the @{-N} syntax for n-th checkout */
467                 ret = interpret_branch_name(str+at, &buf);
468                 if (ret > 0) {
469                         /* substitute this branch name and restart */
470                         return get_sha1_1(buf.buf, buf.len, sha1, 0);
471                 } else if (ret == 0) {
472                         return -1;
473                 }
474                 /* allow "@{...}" to mean the current branch reflog */
475                 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
476         } else if (reflog_len)
477                 refs_found = dwim_log(str, len, sha1, &real_ref);
478         else
479                 refs_found = dwim_ref(str, len, sha1, &real_ref);
480
481         if (!refs_found)
482                 return -1;
483
484         if (warn_ambiguous_refs && refs_found > 1)
485                 warning(warn_msg, len, str);
486
487         if (reflog_len) {
488                 int nth, i;
489                 unsigned long at_time;
490                 unsigned long co_time;
491                 int co_tz, co_cnt;
492
493                 /* a @{-N} placed anywhere except the start is an error */
494                 if (str[at+2] == '-')
495                         return -1;
496
497                 /* Is it asking for N-th entry, or approxidate? */
498                 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
499                         char ch = str[at+2+i];
500                         if ('0' <= ch && ch <= '9')
501                                 nth = nth * 10 + ch - '0';
502                         else
503                                 nth = -1;
504                 }
505                 if (100000000 <= nth) {
506                         at_time = nth;
507                         nth = -1;
508                 } else if (0 <= nth)
509                         at_time = 0;
510                 else {
511                         int errors = 0;
512                         char *tmp = xstrndup(str + at + 2, reflog_len);
513                         at_time = approxidate_careful(tmp, &errors);
514                         free(tmp);
515                         if (errors)
516                                 return -1;
517                 }
518                 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
519                                 &co_time, &co_tz, &co_cnt)) {
520                         if (at_time)
521                                 warning("Log for '%.*s' only goes "
522                                         "back to %s.", len, str,
523                                         show_date(co_time, co_tz, DATE_RFC2822));
524                         else {
525                                 free(real_ref);
526                                 die("Log for '%.*s' only has %d entries.",
527                                     len, str, co_cnt);
528                         }
529                 }
530         }
531
532         free(real_ref);
533         return 0;
534 }
535
536 static int get_parent(const char *name, int len,
537                       unsigned char *result, int idx)
538 {
539         unsigned char sha1[20];
540         int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
541         struct commit *commit;
542         struct commit_list *p;
543
544         if (ret)
545                 return ret;
546         commit = lookup_commit_reference(sha1);
547         if (!commit)
548                 return -1;
549         if (parse_commit(commit))
550                 return -1;
551         if (!idx) {
552                 hashcpy(result, commit->object.sha1);
553                 return 0;
554         }
555         p = commit->parents;
556         while (p) {
557                 if (!--idx) {
558                         hashcpy(result, p->item->object.sha1);
559                         return 0;
560                 }
561                 p = p->next;
562         }
563         return -1;
564 }
565
566 static int get_nth_ancestor(const char *name, int len,
567                             unsigned char *result, int generation)
568 {
569         unsigned char sha1[20];
570         struct commit *commit;
571         int ret;
572
573         ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH);
574         if (ret)
575                 return ret;
576         commit = lookup_commit_reference(sha1);
577         if (!commit)
578                 return -1;
579
580         while (generation--) {
581                 if (parse_commit(commit) || !commit->parents)
582                         return -1;
583                 commit = commit->parents->item;
584         }
585         hashcpy(result, commit->object.sha1);
586         return 0;
587 }
588
589 struct object *peel_to_type(const char *name, int namelen,
590                             struct object *o, enum object_type expected_type)
591 {
592         if (name && !namelen)
593                 namelen = strlen(name);
594         while (1) {
595                 if (!o || (!o->parsed && !parse_object(o->sha1)))
596                         return NULL;
597                 if (o->type == expected_type)
598                         return o;
599                 if (o->type == OBJ_TAG)
600                         o = ((struct tag*) o)->tagged;
601                 else if (o->type == OBJ_COMMIT)
602                         o = &(((struct commit *) o)->tree->object);
603                 else {
604                         if (name)
605                                 error("%.*s: expected %s type, but the object "
606                                       "dereferences to %s type",
607                                       namelen, name, typename(expected_type),
608                                       typename(o->type));
609                         return NULL;
610                 }
611         }
612 }
613
614 static int peel_onion(const char *name, int len, unsigned char *sha1)
615 {
616         unsigned char outer[20];
617         const char *sp;
618         unsigned int expected_type = 0;
619         unsigned lookup_flags = 0;
620         struct object *o;
621
622         /*
623          * "ref^{type}" dereferences ref repeatedly until you cannot
624          * dereference anymore, or you get an object of given type,
625          * whichever comes first.  "ref^{}" means just dereference
626          * tags until you get a non-tag.  "ref^0" is a shorthand for
627          * "ref^{commit}".  "commit^{tree}" could be used to find the
628          * top-level tree of the given commit.
629          */
630         if (len < 4 || name[len-1] != '}')
631                 return -1;
632
633         for (sp = name + len - 1; name <= sp; sp--) {
634                 int ch = *sp;
635                 if (ch == '{' && name < sp && sp[-1] == '^')
636                         break;
637         }
638         if (sp <= name)
639                 return -1;
640
641         sp++; /* beginning of type name, or closing brace for empty */
642         if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
643                 expected_type = OBJ_COMMIT;
644         else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
645                 expected_type = OBJ_TREE;
646         else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
647                 expected_type = OBJ_BLOB;
648         else if (sp[0] == '}')
649                 expected_type = OBJ_NONE;
650         else if (sp[0] == '/')
651                 expected_type = OBJ_COMMIT;
652         else
653                 return -1;
654
655         if (expected_type == OBJ_COMMIT)
656                 lookup_flags = GET_SHA1_COMMITTISH;
657         else if (expected_type == OBJ_TREE)
658                 lookup_flags = GET_SHA1_TREEISH;
659
660         if (get_sha1_1(name, sp - name - 2, outer, lookup_flags))
661                 return -1;
662
663         o = parse_object(outer);
664         if (!o)
665                 return -1;
666         if (!expected_type) {
667                 o = deref_tag(o, name, sp - name - 2);
668                 if (!o || (!o->parsed && !parse_object(o->sha1)))
669                         return -1;
670                 hashcpy(sha1, o->sha1);
671                 return 0;
672         }
673
674         /*
675          * At this point, the syntax look correct, so
676          * if we do not get the needed object, we should
677          * barf.
678          */
679         o = peel_to_type(name, len, o, expected_type);
680         if (!o)
681                 return -1;
682
683         hashcpy(sha1, o->sha1);
684         if (sp[0] == '/') {
685                 /* "$commit^{/foo}" */
686                 char *prefix;
687                 int ret;
688                 struct commit_list *list = NULL;
689
690                 /*
691                  * $commit^{/}. Some regex implementation may reject.
692                  * We don't need regex anyway. '' pattern always matches.
693                  */
694                 if (sp[1] == '}')
695                         return 0;
696
697                 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
698                 commit_list_insert((struct commit *)o, &list);
699                 ret = get_sha1_oneline(prefix, sha1, list);
700                 free(prefix);
701                 return ret;
702         }
703         return 0;
704 }
705
706 static int get_describe_name(const char *name, int len, unsigned char *sha1)
707 {
708         const char *cp;
709         unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT;
710
711         for (cp = name + len - 1; name + 2 <= cp; cp--) {
712                 char ch = *cp;
713                 if (hexval(ch) & ~0377) {
714                         /* We must be looking at g in "SOMETHING-g"
715                          * for it to be describe output.
716                          */
717                         if (ch == 'g' && cp[-1] == '-') {
718                                 cp++;
719                                 len -= cp - name;
720                                 return get_short_sha1(cp, len, sha1, flags);
721                         }
722                 }
723         }
724         return -1;
725 }
726
727 static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags)
728 {
729         int ret, has_suffix;
730         const char *cp;
731
732         /*
733          * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
734          */
735         has_suffix = 0;
736         for (cp = name + len - 1; name <= cp; cp--) {
737                 int ch = *cp;
738                 if ('0' <= ch && ch <= '9')
739                         continue;
740                 if (ch == '~' || ch == '^')
741                         has_suffix = ch;
742                 break;
743         }
744
745         if (has_suffix) {
746                 int num = 0;
747                 int len1 = cp - name;
748                 cp++;
749                 while (cp < name + len)
750                         num = num * 10 + *cp++ - '0';
751                 if (!num && len1 == len - 1)
752                         num = 1;
753                 if (has_suffix == '^')
754                         return get_parent(name, len1, sha1, num);
755                 /* else if (has_suffix == '~') -- goes without saying */
756                 return get_nth_ancestor(name, len1, sha1, num);
757         }
758
759         ret = peel_onion(name, len, sha1);
760         if (!ret)
761                 return 0;
762
763         ret = get_sha1_basic(name, len, sha1);
764         if (!ret)
765                 return 0;
766
767         /* It could be describe output that is "SOMETHING-gXXXX" */
768         ret = get_describe_name(name, len, sha1);
769         if (!ret)
770                 return 0;
771
772         return get_short_sha1(name, len, sha1, lookup_flags);
773 }
774
775 /*
776  * This interprets names like ':/Initial revision of "git"' by searching
777  * through history and returning the first commit whose message starts
778  * the given regular expression.
779  *
780  * For future extension, ':/!' is reserved. If you want to match a message
781  * beginning with a '!', you have to repeat the exclamation mark.
782  */
783 #define ONELINE_SEEN (1u<<20)
784
785 static int handle_one_ref(const char *path,
786                 const unsigned char *sha1, int flag, void *cb_data)
787 {
788         struct commit_list **list = cb_data;
789         struct object *object = parse_object(sha1);
790         if (!object)
791                 return 0;
792         if (object->type == OBJ_TAG) {
793                 object = deref_tag(object, path, strlen(path));
794                 if (!object)
795                         return 0;
796         }
797         if (object->type != OBJ_COMMIT)
798                 return 0;
799         commit_list_insert_by_date((struct commit *)object, list);
800         return 0;
801 }
802
803 static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
804                             struct commit_list *list)
805 {
806         struct commit_list *backup = NULL, *l;
807         int found = 0;
808         regex_t regex;
809
810         if (prefix[0] == '!') {
811                 if (prefix[1] != '!')
812                         die ("Invalid search pattern: %s", prefix);
813                 prefix++;
814         }
815
816         if (regcomp(&regex, prefix, REG_EXTENDED))
817                 die("Invalid search pattern: %s", prefix);
818
819         for (l = list; l; l = l->next) {
820                 l->item->object.flags |= ONELINE_SEEN;
821                 commit_list_insert(l->item, &backup);
822         }
823         while (list) {
824                 char *p, *to_free = NULL;
825                 struct commit *commit;
826                 enum object_type type;
827                 unsigned long size;
828                 int matches;
829
830                 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
831                 if (!parse_object(commit->object.sha1))
832                         continue;
833                 if (commit->buffer)
834                         p = commit->buffer;
835                 else {
836                         p = read_sha1_file(commit->object.sha1, &type, &size);
837                         if (!p)
838                                 continue;
839                         to_free = p;
840                 }
841
842                 p = strstr(p, "\n\n");
843                 matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
844                 free(to_free);
845
846                 if (matches) {
847                         hashcpy(sha1, commit->object.sha1);
848                         found = 1;
849                         break;
850                 }
851         }
852         regfree(&regex);
853         free_commit_list(list);
854         for (l = backup; l; l = l->next)
855                 clear_commit_marks(l->item, ONELINE_SEEN);
856         free_commit_list(backup);
857         return found ? 0 : -1;
858 }
859
860 struct grab_nth_branch_switch_cbdata {
861         long cnt, alloc;
862         struct strbuf *buf;
863 };
864
865 static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
866                                   const char *email, unsigned long timestamp, int tz,
867                                   const char *message, void *cb_data)
868 {
869         struct grab_nth_branch_switch_cbdata *cb = cb_data;
870         const char *match = NULL, *target = NULL;
871         size_t len;
872         int nth;
873
874         if (!prefixcmp(message, "checkout: moving from ")) {
875                 match = message + strlen("checkout: moving from ");
876                 target = strstr(match, " to ");
877         }
878
879         if (!match || !target)
880                 return 0;
881
882         len = target - match;
883         nth = cb->cnt++ % cb->alloc;
884         strbuf_reset(&cb->buf[nth]);
885         strbuf_add(&cb->buf[nth], match, len);
886         return 0;
887 }
888
889 /*
890  * Parse @{-N} syntax, return the number of characters parsed
891  * if successful; otherwise signal an error with negative value.
892  */
893 static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf)
894 {
895         long nth;
896         int i, retval;
897         struct grab_nth_branch_switch_cbdata cb;
898         const char *brace;
899         char *num_end;
900
901         if (name[0] != '@' || name[1] != '{' || name[2] != '-')
902                 return -1;
903         brace = strchr(name, '}');
904         if (!brace)
905                 return -1;
906         nth = strtol(name+3, &num_end, 10);
907         if (num_end != brace)
908                 return -1;
909         if (nth <= 0)
910                 return -1;
911         cb.alloc = nth;
912         cb.buf = xmalloc(nth * sizeof(struct strbuf));
913         for (i = 0; i < nth; i++)
914                 strbuf_init(&cb.buf[i], 20);
915         cb.cnt = 0;
916         retval = 0;
917         for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
918         if (cb.cnt < nth) {
919                 cb.cnt = 0;
920                 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
921         }
922         if (cb.cnt < nth)
923                 goto release_return;
924         i = cb.cnt % nth;
925         strbuf_reset(buf);
926         strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
927         retval = brace-name+1;
928
929 release_return:
930         for (i = 0; i < nth; i++)
931                 strbuf_release(&cb.buf[i]);
932         free(cb.buf);
933
934         return retval;
935 }
936
937 int get_sha1_mb(const char *name, unsigned char *sha1)
938 {
939         struct commit *one, *two;
940         struct commit_list *mbs;
941         unsigned char sha1_tmp[20];
942         const char *dots;
943         int st;
944
945         dots = strstr(name, "...");
946         if (!dots)
947                 return get_sha1(name, sha1);
948         if (dots == name)
949                 st = get_sha1("HEAD", sha1_tmp);
950         else {
951                 struct strbuf sb;
952                 strbuf_init(&sb, dots - name);
953                 strbuf_add(&sb, name, dots - name);
954                 st = get_sha1_committish(sb.buf, sha1_tmp);
955                 strbuf_release(&sb);
956         }
957         if (st)
958                 return st;
959         one = lookup_commit_reference_gently(sha1_tmp, 0);
960         if (!one)
961                 return -1;
962
963         if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
964                 return -1;
965         two = lookup_commit_reference_gently(sha1_tmp, 0);
966         if (!two)
967                 return -1;
968         mbs = get_merge_bases(one, two, 1);
969         if (!mbs || mbs->next)
970                 st = -1;
971         else {
972                 st = 0;
973                 hashcpy(sha1, mbs->item->object.sha1);
974         }
975         free_commit_list(mbs);
976         return st;
977 }
978
979 /*
980  * This reads short-hand syntax that not only evaluates to a commit
981  * object name, but also can act as if the end user spelled the name
982  * of the branch from the command line.
983  *
984  * - "@{-N}" finds the name of the Nth previous branch we were on, and
985  *   places the name of the branch in the given buf and returns the
986  *   number of characters parsed if successful.
987  *
988  * - "<branch>@{upstream}" finds the name of the other ref that
989  *   <branch> is configured to merge with (missing <branch> defaults
990  *   to the current branch), and places the name of the branch in the
991  *   given buf and returns the number of characters parsed if
992  *   successful.
993  *
994  * If the input is not of the accepted format, it returns a negative
995  * number to signal an error.
996  *
997  * If the input was ok but there are not N branch switches in the
998  * reflog, it returns 0.
999  */
1000 int interpret_branch_name(const char *name, struct strbuf *buf)
1001 {
1002         char *cp;
1003         struct branch *upstream;
1004         int namelen = strlen(name);
1005         int len = interpret_nth_prior_checkout(name, buf);
1006         int tmp_len;
1007
1008         if (!len)
1009                 return len; /* syntax Ok, not enough switches */
1010         if (0 < len && len == namelen)
1011                 return len; /* consumed all */
1012         else if (0 < len) {
1013                 /* we have extra data, which might need further processing */
1014                 struct strbuf tmp = STRBUF_INIT;
1015                 int used = buf->len;
1016                 int ret;
1017
1018                 strbuf_add(buf, name + len, namelen - len);
1019                 ret = interpret_branch_name(buf->buf, &tmp);
1020                 /* that data was not interpreted, remove our cruft */
1021                 if (ret < 0) {
1022                         strbuf_setlen(buf, used);
1023                         return len;
1024                 }
1025                 strbuf_reset(buf);
1026                 strbuf_addbuf(buf, &tmp);
1027                 strbuf_release(&tmp);
1028                 /* tweak for size of {-N} versus expanded ref name */
1029                 return ret - used + len;
1030         }
1031
1032         cp = strchr(name, '@');
1033         if (!cp)
1034                 return -1;
1035         tmp_len = upstream_mark(cp, namelen - (cp - name));
1036         if (!tmp_len)
1037                 return -1;
1038         len = cp + tmp_len - name;
1039         cp = xstrndup(name, cp - name);
1040         upstream = branch_get(*cp ? cp : NULL);
1041         /*
1042          * Upstream can be NULL only if cp refers to HEAD and HEAD
1043          * points to something different than a branch.
1044          */
1045         if (!upstream)
1046                 return error(_("HEAD does not point to a branch"));
1047         if (!upstream->merge || !upstream->merge[0]->dst) {
1048                 if (!ref_exists(upstream->refname))
1049                         return error(_("No such branch: '%s'"), cp);
1050                 if (!upstream->merge)
1051                         return error(_("No upstream configured for branch '%s'"),
1052                                      upstream->name);
1053                 return error(
1054                         _("Upstream branch '%s' not stored as a remote-tracking branch"),
1055                         upstream->merge[0]->src);
1056         }
1057         free(cp);
1058         cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
1059         strbuf_reset(buf);
1060         strbuf_addstr(buf, cp);
1061         free(cp);
1062         return len;
1063 }
1064
1065 int strbuf_branchname(struct strbuf *sb, const char *name)
1066 {
1067         int len = strlen(name);
1068         if (interpret_branch_name(name, sb) == len)
1069                 return 0;
1070         strbuf_add(sb, name, len);
1071         return len;
1072 }
1073
1074 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1075 {
1076         strbuf_branchname(sb, name);
1077         if (name[0] == '-')
1078                 return -1;
1079         strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1080         return check_refname_format(sb->buf, 0);
1081 }
1082
1083 /*
1084  * This is like "get_sha1_basic()", except it allows "sha1 expressions",
1085  * notably "xyz^" for "parent of xyz"
1086  */
1087 int get_sha1(const char *name, unsigned char *sha1)
1088 {
1089         struct object_context unused;
1090         return get_sha1_with_context(name, 0, sha1, &unused);
1091 }
1092
1093 /*
1094  * Many callers know that the user meant to name a committish by
1095  * syntactical positions where the object name appears.  Calling this
1096  * function allows the machinery to disambiguate shorter-than-unique
1097  * abbreviated object names between committish and others.
1098  *
1099  * Note that this does NOT error out when the named object is not a
1100  * committish. It is merely to give a hint to the disambiguation
1101  * machinery.
1102  */
1103 int get_sha1_committish(const char *name, unsigned char *sha1)
1104 {
1105         struct object_context unused;
1106         return get_sha1_with_context(name, GET_SHA1_COMMITTISH,
1107                                      sha1, &unused);
1108 }
1109
1110 int get_sha1_treeish(const char *name, unsigned char *sha1)
1111 {
1112         struct object_context unused;
1113         return get_sha1_with_context(name, GET_SHA1_TREEISH,
1114                                      sha1, &unused);
1115 }
1116
1117 int get_sha1_commit(const char *name, unsigned char *sha1)
1118 {
1119         struct object_context unused;
1120         return get_sha1_with_context(name, GET_SHA1_COMMIT,
1121                                      sha1, &unused);
1122 }
1123
1124 int get_sha1_tree(const char *name, unsigned char *sha1)
1125 {
1126         struct object_context unused;
1127         return get_sha1_with_context(name, GET_SHA1_TREE,
1128                                      sha1, &unused);
1129 }
1130
1131 int get_sha1_blob(const char *name, unsigned char *sha1)
1132 {
1133         struct object_context unused;
1134         return get_sha1_with_context(name, GET_SHA1_BLOB,
1135                                      sha1, &unused);
1136 }
1137
1138 /* Must be called only when object_name:filename doesn't exist. */
1139 static void diagnose_invalid_sha1_path(const char *prefix,
1140                                        const char *filename,
1141                                        const unsigned char *tree_sha1,
1142                                        const char *object_name,
1143                                        int object_name_len)
1144 {
1145         struct stat st;
1146         unsigned char sha1[20];
1147         unsigned mode;
1148
1149         if (!prefix)
1150                 prefix = "";
1151
1152         if (!lstat(filename, &st))
1153                 die("Path '%s' exists on disk, but not in '%.*s'.",
1154                     filename, object_name_len, object_name);
1155         if (errno == ENOENT || errno == ENOTDIR) {
1156                 char *fullname = xmalloc(strlen(filename)
1157                                              + strlen(prefix) + 1);
1158                 strcpy(fullname, prefix);
1159                 strcat(fullname, filename);
1160
1161                 if (!get_tree_entry(tree_sha1, fullname,
1162                                     sha1, &mode)) {
1163                         die("Path '%s' exists, but not '%s'.\n"
1164                             "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1165                             fullname,
1166                             filename,
1167                             object_name_len, object_name,
1168                             fullname,
1169                             object_name_len, object_name,
1170                             filename);
1171                 }
1172                 die("Path '%s' does not exist in '%.*s'",
1173                     filename, object_name_len, object_name);
1174         }
1175 }
1176
1177 /* Must be called only when :stage:filename doesn't exist. */
1178 static void diagnose_invalid_index_path(int stage,
1179                                         const char *prefix,
1180                                         const char *filename)
1181 {
1182         struct stat st;
1183         struct cache_entry *ce;
1184         int pos;
1185         unsigned namelen = strlen(filename);
1186         unsigned fullnamelen;
1187         char *fullname;
1188
1189         if (!prefix)
1190                 prefix = "";
1191
1192         /* Wrong stage number? */
1193         pos = cache_name_pos(filename, namelen);
1194         if (pos < 0)
1195                 pos = -pos - 1;
1196         if (pos < active_nr) {
1197                 ce = active_cache[pos];
1198                 if (ce_namelen(ce) == namelen &&
1199                     !memcmp(ce->name, filename, namelen))
1200                         die("Path '%s' is in the index, but not at stage %d.\n"
1201                             "Did you mean ':%d:%s'?",
1202                             filename, stage,
1203                             ce_stage(ce), filename);
1204         }
1205
1206         /* Confusion between relative and absolute filenames? */
1207         fullnamelen = namelen + strlen(prefix);
1208         fullname = xmalloc(fullnamelen + 1);
1209         strcpy(fullname, prefix);
1210         strcat(fullname, filename);
1211         pos = cache_name_pos(fullname, fullnamelen);
1212         if (pos < 0)
1213                 pos = -pos - 1;
1214         if (pos < active_nr) {
1215                 ce = active_cache[pos];
1216                 if (ce_namelen(ce) == fullnamelen &&
1217                     !memcmp(ce->name, fullname, fullnamelen))
1218                         die("Path '%s' is in the index, but not '%s'.\n"
1219                             "Did you mean ':%d:%s' aka ':%d:./%s'?",
1220                             fullname, filename,
1221                             ce_stage(ce), fullname,
1222                             ce_stage(ce), filename);
1223         }
1224
1225         if (!lstat(filename, &st))
1226                 die("Path '%s' exists on disk, but not in the index.", filename);
1227         if (errno == ENOENT || errno == ENOTDIR)
1228                 die("Path '%s' does not exist (neither on disk nor in the index).",
1229                     filename);
1230
1231         free(fullname);
1232 }
1233
1234
1235 static char *resolve_relative_path(const char *rel)
1236 {
1237         if (prefixcmp(rel, "./") && prefixcmp(rel, "../"))
1238                 return NULL;
1239
1240         if (!startup_info)
1241                 die("BUG: startup_info struct is not initialized.");
1242
1243         if (!is_inside_work_tree())
1244                 die("relative path syntax can't be used outside working tree.");
1245
1246         /* die() inside prefix_path() if resolved path is outside worktree */
1247         return prefix_path(startup_info->prefix,
1248                            startup_info->prefix ? strlen(startup_info->prefix) : 0,
1249                            rel);
1250 }
1251
1252 static int get_sha1_with_context_1(const char *name,
1253                                    unsigned flags,
1254                                    const char *prefix,
1255                                    unsigned char *sha1,
1256                                    struct object_context *oc)
1257 {
1258         int ret, bracket_depth;
1259         int namelen = strlen(name);
1260         const char *cp;
1261         int only_to_die = flags & GET_SHA1_ONLY_TO_DIE;
1262
1263         memset(oc, 0, sizeof(*oc));
1264         oc->mode = S_IFINVALID;
1265         ret = get_sha1_1(name, namelen, sha1, flags);
1266         if (!ret)
1267                 return ret;
1268         /*
1269          * sha1:path --> object name of path in ent sha1
1270          * :path -> object name of absolute path in index
1271          * :./path -> object name of path relative to cwd in index
1272          * :[0-3]:path -> object name of path in index at stage
1273          * :/foo -> recent commit matching foo
1274          */
1275         if (name[0] == ':') {
1276                 int stage = 0;
1277                 struct cache_entry *ce;
1278                 char *new_path = NULL;
1279                 int pos;
1280                 if (!only_to_die && namelen > 2 && name[1] == '/') {
1281                         struct commit_list *list = NULL;
1282                         for_each_ref(handle_one_ref, &list);
1283                         return get_sha1_oneline(name + 2, sha1, list);
1284                 }
1285                 if (namelen < 3 ||
1286                     name[2] != ':' ||
1287                     name[1] < '0' || '3' < name[1])
1288                         cp = name + 1;
1289                 else {
1290                         stage = name[1] - '0';
1291                         cp = name + 3;
1292                 }
1293                 new_path = resolve_relative_path(cp);
1294                 if (!new_path) {
1295                         namelen = namelen - (cp - name);
1296                 } else {
1297                         cp = new_path;
1298                         namelen = strlen(cp);
1299                 }
1300
1301                 strncpy(oc->path, cp,
1302                         sizeof(oc->path));
1303                 oc->path[sizeof(oc->path)-1] = '\0';
1304
1305                 if (!active_cache)
1306                         read_cache();
1307                 pos = cache_name_pos(cp, namelen);
1308                 if (pos < 0)
1309                         pos = -pos - 1;
1310                 while (pos < active_nr) {
1311                         ce = active_cache[pos];
1312                         if (ce_namelen(ce) != namelen ||
1313                             memcmp(ce->name, cp, namelen))
1314                                 break;
1315                         if (ce_stage(ce) == stage) {
1316                                 hashcpy(sha1, ce->sha1);
1317                                 oc->mode = ce->ce_mode;
1318                                 free(new_path);
1319                                 return 0;
1320                         }
1321                         pos++;
1322                 }
1323                 if (only_to_die && name[1] && name[1] != '/')
1324                         diagnose_invalid_index_path(stage, prefix, cp);
1325                 free(new_path);
1326                 return -1;
1327         }
1328         for (cp = name, bracket_depth = 0; *cp; cp++) {
1329                 if (*cp == '{')
1330                         bracket_depth++;
1331                 else if (bracket_depth && *cp == '}')
1332                         bracket_depth--;
1333                 else if (!bracket_depth && *cp == ':')
1334                         break;
1335         }
1336         if (*cp == ':') {
1337                 unsigned char tree_sha1[20];
1338                 int len = cp - name;
1339                 if (!get_sha1_1(name, len, tree_sha1, GET_SHA1_TREEISH)) {
1340                         const char *filename = cp+1;
1341                         char *new_filename = NULL;
1342
1343                         new_filename = resolve_relative_path(filename);
1344                         if (new_filename)
1345                                 filename = new_filename;
1346                         ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
1347                         if (ret && only_to_die) {
1348                                 diagnose_invalid_sha1_path(prefix, filename,
1349                                                            tree_sha1,
1350                                                            name, len);
1351                         }
1352                         hashcpy(oc->tree, tree_sha1);
1353                         strncpy(oc->path, filename,
1354                                 sizeof(oc->path));
1355                         oc->path[sizeof(oc->path)-1] = '\0';
1356
1357                         free(new_filename);
1358                         return ret;
1359                 } else {
1360                         if (only_to_die)
1361                                 die("Invalid object name '%.*s'.", len, name);
1362                 }
1363         }
1364         return ret;
1365 }
1366
1367 /*
1368  * Call this function when you know "name" given by the end user must
1369  * name an object but it doesn't; the function _may_ die with a better
1370  * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1371  * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1372  * you have a chance to diagnose the error further.
1373  */
1374 void maybe_die_on_misspelt_object_name(const char *name, const char *prefix)
1375 {
1376         struct object_context oc;
1377         unsigned char sha1[20];
1378         get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc);
1379 }
1380
1381 int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc)
1382 {
1383         return get_sha1_with_context_1(str, flags, NULL, sha1, orc);
1384 }