get_sha1_basic(): corner case ambiguity fix
[git.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
7 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
8 {
9         struct alternate_object_database *alt;
10         char hex[40];
11         int found = 0;
12         static struct alternate_object_database *fakeent;
13
14         if (!fakeent) {
15                 const char *objdir = get_object_directory();
16                 int objdir_len = strlen(objdir);
17                 int entlen = objdir_len + 43;
18                 fakeent = xmalloc(sizeof(*fakeent) + entlen);
19                 memcpy(fakeent->base, objdir, objdir_len);
20                 fakeent->name = fakeent->base + objdir_len + 1;
21                 fakeent->name[-1] = '/';
22         }
23         fakeent->next = alt_odb_list;
24
25         sprintf(hex, "%.2s", name);
26         for (alt = fakeent; alt && found < 2; alt = alt->next) {
27                 struct dirent *de;
28                 DIR *dir;
29                 sprintf(alt->name, "%.2s/", name);
30                 dir = opendir(alt->base);
31                 if (!dir)
32                         continue;
33                 while ((de = readdir(dir)) != NULL) {
34                         if (strlen(de->d_name) != 38)
35                                 continue;
36                         if (memcmp(de->d_name, name + 2, len - 2))
37                                 continue;
38                         if (!found) {
39                                 memcpy(hex + 2, de->d_name, 38);
40                                 found++;
41                         }
42                         else if (memcmp(hex + 2, de->d_name, 38)) {
43                                 found = 2;
44                                 break;
45                         }
46                 }
47                 closedir(dir);
48         }
49         if (found == 1)
50                 return get_sha1_hex(hex, sha1) == 0;
51         return found;
52 }
53
54 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
55 {
56         do {
57                 if (*a != *b)
58                         return 0;
59                 a++;
60                 b++;
61                 len -= 2;
62         } while (len > 1);
63         if (len)
64                 if ((*a ^ *b) & 0xf0)
65                         return 0;
66         return 1;
67 }
68
69 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
70 {
71         struct packed_git *p;
72         unsigned char found_sha1[20];
73         int found = 0;
74
75         prepare_packed_git();
76         for (p = packed_git; p && found < 2; p = p->next) {
77                 unsigned num = num_packed_objects(p);
78                 unsigned first = 0, last = num;
79                 while (first < last) {
80                         unsigned mid = (first + last) / 2;
81                         unsigned char now[20];
82                         int cmp;
83
84                         nth_packed_object_sha1(p, mid, now);
85                         cmp = memcmp(match, now, 20);
86                         if (!cmp) {
87                                 first = mid;
88                                 break;
89                         }
90                         if (cmp > 0) {
91                                 first = mid+1;
92                                 continue;
93                         }
94                         last = mid;
95                 }
96                 if (first < num) {
97                         unsigned char now[20], next[20];
98                         nth_packed_object_sha1(p, first, now);
99                         if (match_sha(len, match, now)) {
100                                 if (nth_packed_object_sha1(p, first+1, next) ||
101                                     !match_sha(len, match, next)) {
102                                         /* unique within this pack */
103                                         if (!found) {
104                                                 memcpy(found_sha1, now, 20);
105                                                 found++;
106                                         }
107                                         else if (memcmp(found_sha1, now, 20)) {
108                                                 found = 2;
109                                                 break;
110                                         }
111                                 }
112                                 else {
113                                         /* not even unique within this pack */
114                                         found = 2;
115                                         break;
116                                 }
117                         }
118                 }
119         }
120         if (found == 1)
121                 memcpy(sha1, found_sha1, 20);
122         return found;
123 }
124
125 #define SHORT_NAME_NOT_FOUND (-1)
126 #define SHORT_NAME_AMBIGUOUS (-2)
127
128 static int find_unique_short_object(int len, char *canonical,
129                                     unsigned char *res, unsigned char *sha1)
130 {
131         int has_unpacked, has_packed;
132         unsigned char unpacked_sha1[20], packed_sha1[20];
133
134         has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
135         has_packed = find_short_packed_object(len, res, packed_sha1);
136         if (!has_unpacked && !has_packed)
137                 return SHORT_NAME_NOT_FOUND;
138         if (1 < has_unpacked || 1 < has_packed)
139                 return SHORT_NAME_AMBIGUOUS;
140         if (has_unpacked != has_packed) {
141                 memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
142                 return 0;
143         }
144         /* Both have unique ones -- do they match? */
145         if (memcmp(packed_sha1, unpacked_sha1, 20))
146                 return -2;
147         memcpy(sha1, packed_sha1, 20);
148         return 0;
149 }
150
151 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
152                           int quietly)
153 {
154         int i, status;
155         char canonical[40];
156         unsigned char res[20];
157
158         if (len < 4)
159                 return -1;
160         memset(res, 0, 20);
161         memset(canonical, 'x', 40);
162         for (i = 0; i < len ;i++) {
163                 unsigned char c = name[i];
164                 unsigned char val;
165                 if (c >= '0' && c <= '9')
166                         val = c - '0';
167                 else if (c >= 'a' && c <= 'f')
168                         val = c - 'a' + 10;
169                 else if (c >= 'A' && c <='F') {
170                         val = c - 'A' + 10;
171                         c -= 'A' - 'a';
172                 }
173                 else
174                         return -1;
175                 canonical[i] = c;
176                 if (!(i & 1))
177                         val <<= 4;
178                 res[i >> 1] |= val;
179         }
180
181         status = find_unique_short_object(i, canonical, res, sha1);
182         if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
183                 return error("short SHA1 %.*s is ambiguous.", len, canonical);
184         return status;
185 }
186
187 const char *find_unique_abbrev(const unsigned char *sha1, int len)
188 {
189         int status;
190         static char hex[41];
191         memcpy(hex, sha1_to_hex(sha1), 40);
192         while (len < 40) {
193                 unsigned char sha1_ret[20];
194                 status = get_short_sha1(hex, len, sha1_ret, 1);
195                 if (!status) {
196                         hex[len] = 0;
197                         return hex;
198                 }
199                 if (status != SHORT_NAME_AMBIGUOUS)
200                         return NULL;
201                 len++;
202         }
203         return NULL;
204 }
205
206 static int ambiguous_path(const char *path, int len)
207 {
208         int slash = 1;
209         int cnt;
210
211         for (cnt = 0; cnt < len; cnt++) {
212                 switch (*path++) {
213                 case '\0':
214                         break;
215                 case '/':
216                         if (slash)
217                                 break;
218                         slash = 1;
219                         continue;
220                 case '.':
221                         continue;
222                 default:
223                         slash = 0;
224                         continue;
225                 }
226                 return slash;
227         }
228         return slash;
229 }
230
231 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
232 {
233         static const char *prefix[] = {
234                 "",
235                 "refs",
236                 "refs/tags",
237                 "refs/heads",
238                 NULL
239         };
240         const char **p;
241         int found = 0;
242
243         if (len == 40 && !get_sha1_hex(str, sha1))
244                 return 0;
245
246         /* Accept only unambiguous ref paths. */
247         if (ambiguous_path(str, len))
248                 return -1;
249
250         for (p = prefix; *p; p++) {
251                 char *pathname = git_path("%s/%.*s", *p, len, str);
252
253                 if (!read_ref(pathname, sha1)) {
254                         /* Must be unique; i.e. when heads/foo and
255                          * tags/foo are both present, reject "foo".
256                          */
257                         if (1 < found++)
258                                 return -1;
259                 }
260
261                 /* We want to allow .git/description file and
262                  * "description" branch to exist at the same time.
263                  * "git-rev-parse description" should silently skip
264                  * .git/description file as a candidate for
265                  * get_sha1().  However, having garbage file anywhere
266                  * under refs/ is not OK, and we would not have caught
267                  * ambiguous heads and tags with the above test.
268                  */
269                 else if (**p && !access(pathname, F_OK)) {
270                         /* Garbage exists under .git/refs */
271                         return error("garbage ref found '%s'", pathname);
272                 }
273         }
274         switch (found) {
275         case 0:
276                 return -1;
277         case 1:
278                 return 0;
279         default:
280                 return error("ambiguous refname '%.*s'", len, str);
281         }
282 }
283
284 static int get_sha1_1(const char *name, int len, unsigned char *sha1);
285
286 static int get_parent(const char *name, int len,
287                       unsigned char *result, int idx)
288 {
289         unsigned char sha1[20];
290         int ret = get_sha1_1(name, len, sha1);
291         struct commit *commit;
292         struct commit_list *p;
293
294         if (ret)
295                 return ret;
296         commit = lookup_commit_reference(sha1);
297         if (!commit)
298                 return -1;
299         if (parse_commit(commit))
300                 return -1;
301         if (!idx) {
302                 memcpy(result, commit->object.sha1, 20);
303                 return 0;
304         }
305         p = commit->parents;
306         while (p) {
307                 if (!--idx) {
308                         memcpy(result, p->item->object.sha1, 20);
309                         return 0;
310                 }
311                 p = p->next;
312         }
313         return -1;
314 }
315
316 static int get_nth_ancestor(const char *name, int len,
317                             unsigned char *result, int generation)
318 {
319         unsigned char sha1[20];
320         int ret = get_sha1_1(name, len, sha1);
321         if (ret)
322                 return ret;
323
324         while (generation--) {
325                 struct commit *commit = lookup_commit_reference(sha1);
326
327                 if (!commit || parse_commit(commit) || !commit->parents)
328                         return -1;
329                 memcpy(sha1, commit->parents->item->object.sha1, 20);
330         }
331         memcpy(result, sha1, 20);
332         return 0;
333 }
334
335 static int peel_onion(const char *name, int len, unsigned char *sha1)
336 {
337         unsigned char outer[20];
338         const char *sp;
339         const char *type_string = NULL;
340         struct object *o;
341
342         /*
343          * "ref^{type}" dereferences ref repeatedly until you cannot
344          * dereference anymore, or you get an object of given type,
345          * whichever comes first.  "ref^{}" means just dereference
346          * tags until you get a non-tag.  "ref^0" is a shorthand for
347          * "ref^{commit}".  "commit^{tree}" could be used to find the
348          * top-level tree of the given commit.
349          */
350         if (len < 4 || name[len-1] != '}')
351                 return -1;
352
353         for (sp = name + len - 1; name <= sp; sp--) {
354                 int ch = *sp;
355                 if (ch == '{' && name < sp && sp[-1] == '^')
356                         break;
357         }
358         if (sp <= name)
359                 return -1;
360
361         sp++; /* beginning of type name, or closing brace for empty */
362         if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
363                 type_string = commit_type;
364         else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
365                 type_string = tree_type;
366         else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
367                 type_string = blob_type;
368         else if (sp[0] == '}')
369                 type_string = NULL;
370         else
371                 return -1;
372
373         if (get_sha1_1(name, sp - name - 2, outer))
374                 return -1;
375
376         o = parse_object(outer);
377         if (!o)
378                 return -1;
379         if (!type_string) {
380                 o = deref_tag(o, name, sp - name - 2);
381                 if (!o || (!o->parsed && !parse_object(o->sha1)))
382                         return -1;
383                 memcpy(sha1, o->sha1, 20);
384         }
385         else {
386                 /* At this point, the syntax look correct, so
387                  * if we do not get the needed object, we should
388                  * barf.
389                  */
390
391                 while (1) {
392                         if (!o || (!o->parsed && !parse_object(o->sha1)))
393                                 return -1;
394                         if (o->type == type_string) {
395                                 memcpy(sha1, o->sha1, 20);
396                                 return 0;
397                         }
398                         if (o->type == tag_type)
399                                 o = ((struct tag*) o)->tagged;
400                         else if (o->type == commit_type)
401                                 o = &(((struct commit *) o)->tree->object);
402                         else
403                                 return error("%.*s: expected %s type, but the object dereferences to %s type",
404                                              len, name, type_string,
405                                              o->type);
406                         if (!o->parsed)
407                                 parse_object(o->sha1);
408                 }
409         }
410         return 0;
411 }
412
413 static int get_sha1_1(const char *name, int len, unsigned char *sha1)
414 {
415         int parent, ret;
416         const char *cp;
417
418         /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
419         if (len > 2 && name[len-2] == '^' &&
420             name[len-1] >= '0' && name[len-1] <= '9') {
421                 parent = name[len-1] - '0';
422                 len -= 2;
423         }
424         else if (len > 1 && name[len-1] == '^') {
425                 parent = 1;
426                 len--;
427         } else
428                 parent = -1;
429
430         if (parent >= 0)
431                 return get_parent(name, len, sha1, parent);
432
433         /* "name~3" is "name^^^",
434          * "name~12" is "name^^^^^^^^^^^^", and
435          * "name~" and "name~0" are name -- not "name^0"!
436          */
437         parent = 0;
438         for (cp = name + len - 1; name <= cp; cp--) {
439                 int ch = *cp;
440                 if ('0' <= ch && ch <= '9')
441                         continue;
442                 if (ch != '~')
443                         parent = -1;
444                 break;
445         }
446         if (!parent && *cp == '~') {
447                 int len1 = cp - name;
448                 cp++;
449                 while (cp < name + len)
450                         parent = parent * 10 + *cp++ - '0';
451                 return get_nth_ancestor(name, len1, sha1, parent);
452         }
453
454         ret = peel_onion(name, len, sha1);
455         if (!ret)
456                 return 0;
457
458         ret = get_sha1_basic(name, len, sha1);
459         if (!ret)
460                 return 0;
461         return get_short_sha1(name, len, sha1, 0);
462 }
463
464 /*
465  * This is like "get_sha1_basic()", except it allows "sha1 expressions",
466  * notably "xyz^" for "parent of xyz"
467  */
468 int get_sha1(const char *name, unsigned char *sha1)
469 {
470         prepare_alt_odb();
471         return get_sha1_1(name, strlen(name), sha1);
472 }