Fix bogus tests on rev-list output.
[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
192         memcpy(hex, sha1_to_hex(sha1), 40);
193         if (len == 40)
194                 return hex;
195         while (len < 40) {
196                 unsigned char sha1_ret[20];
197                 status = get_short_sha1(hex, len, sha1_ret, 1);
198                 if (!status) {
199                         hex[len] = 0;
200                         return hex;
201                 }
202                 if (status != SHORT_NAME_AMBIGUOUS)
203                         return NULL;
204                 len++;
205         }
206         return NULL;
207 }
208
209 static int ambiguous_path(const char *path, int len)
210 {
211         int slash = 1;
212         int cnt;
213
214         for (cnt = 0; cnt < len; cnt++) {
215                 switch (*path++) {
216                 case '\0':
217                         break;
218                 case '/':
219                         if (slash)
220                                 break;
221                         slash = 1;
222                         continue;
223                 case '.':
224                         continue;
225                 default:
226                         slash = 0;
227                         continue;
228                 }
229                 break;
230         }
231         return slash;
232 }
233
234 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
235 {
236         static const char *prefix[] = {
237                 "",
238                 "refs",
239                 "refs/tags",
240                 "refs/heads",
241                 NULL
242         };
243         const char **p;
244
245         if (len == 40 && !get_sha1_hex(str, sha1))
246                 return 0;
247
248         /* Accept only unambiguous ref paths. */
249         if (ambiguous_path(str, len))
250                 return -1;
251
252         for (p = prefix; *p; p++) {
253                 char *pathname = git_path("%s/%.*s", *p, len, str);
254                 if (!read_ref(pathname, sha1))
255                         return 0;
256         }
257         return -1;
258 }
259
260 static int get_sha1_1(const char *name, int len, unsigned char *sha1);
261
262 static int get_parent(const char *name, int len,
263                       unsigned char *result, int idx)
264 {
265         unsigned char sha1[20];
266         int ret = get_sha1_1(name, len, sha1);
267         struct commit *commit;
268         struct commit_list *p;
269
270         if (ret)
271                 return ret;
272         commit = lookup_commit_reference(sha1);
273         if (!commit)
274                 return -1;
275         if (parse_commit(commit))
276                 return -1;
277         if (!idx) {
278                 memcpy(result, commit->object.sha1, 20);
279                 return 0;
280         }
281         p = commit->parents;
282         while (p) {
283                 if (!--idx) {
284                         memcpy(result, p->item->object.sha1, 20);
285                         return 0;
286                 }
287                 p = p->next;
288         }
289         return -1;
290 }
291
292 static int get_nth_ancestor(const char *name, int len,
293                             unsigned char *result, int generation)
294 {
295         unsigned char sha1[20];
296         int ret = get_sha1_1(name, len, sha1);
297         if (ret)
298                 return ret;
299
300         while (generation--) {
301                 struct commit *commit = lookup_commit_reference(sha1);
302
303                 if (!commit || parse_commit(commit) || !commit->parents)
304                         return -1;
305                 memcpy(sha1, commit->parents->item->object.sha1, 20);
306         }
307         memcpy(result, sha1, 20);
308         return 0;
309 }
310
311 static int peel_onion(const char *name, int len, unsigned char *sha1)
312 {
313         unsigned char outer[20];
314         const char *sp;
315         const char *type_string = NULL;
316         struct object *o;
317
318         /*
319          * "ref^{type}" dereferences ref repeatedly until you cannot
320          * dereference anymore, or you get an object of given type,
321          * whichever comes first.  "ref^{}" means just dereference
322          * tags until you get a non-tag.  "ref^0" is a shorthand for
323          * "ref^{commit}".  "commit^{tree}" could be used to find the
324          * top-level tree of the given commit.
325          */
326         if (len < 4 || name[len-1] != '}')
327                 return -1;
328
329         for (sp = name + len - 1; name <= sp; sp--) {
330                 int ch = *sp;
331                 if (ch == '{' && name < sp && sp[-1] == '^')
332                         break;
333         }
334         if (sp <= name)
335                 return -1;
336
337         sp++; /* beginning of type name, or closing brace for empty */
338         if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
339                 type_string = commit_type;
340         else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
341                 type_string = tree_type;
342         else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
343                 type_string = blob_type;
344         else if (sp[0] == '}')
345                 type_string = NULL;
346         else
347                 return -1;
348
349         if (get_sha1_1(name, sp - name - 2, outer))
350                 return -1;
351
352         o = parse_object(outer);
353         if (!o)
354                 return -1;
355         if (!type_string) {
356                 o = deref_tag(o, name, sp - name - 2);
357                 if (!o || (!o->parsed && !parse_object(o->sha1)))
358                         return -1;
359                 memcpy(sha1, o->sha1, 20);
360         }
361         else {
362                 /* At this point, the syntax look correct, so
363                  * if we do not get the needed object, we should
364                  * barf.
365                  */
366
367                 while (1) {
368                         if (!o || (!o->parsed && !parse_object(o->sha1)))
369                                 return -1;
370                         if (o->type == type_string) {
371                                 memcpy(sha1, o->sha1, 20);
372                                 return 0;
373                         }
374                         if (o->type == tag_type)
375                                 o = ((struct tag*) o)->tagged;
376                         else if (o->type == commit_type)
377                                 o = &(((struct commit *) o)->tree->object);
378                         else
379                                 return error("%.*s: expected %s type, but the object dereferences to %s type",
380                                              len, name, type_string,
381                                              o->type);
382                         if (!o->parsed)
383                                 parse_object(o->sha1);
384                 }
385         }
386         return 0;
387 }
388
389 static int get_sha1_1(const char *name, int len, unsigned char *sha1)
390 {
391         int parent, ret;
392         const char *cp;
393
394         /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
395         if (len > 2 && name[len-2] == '^' &&
396             name[len-1] >= '0' && name[len-1] <= '9') {
397                 parent = name[len-1] - '0';
398                 len -= 2;
399         }
400         else if (len > 1 && name[len-1] == '^') {
401                 parent = 1;
402                 len--;
403         } else
404                 parent = -1;
405
406         if (parent >= 0)
407                 return get_parent(name, len, sha1, parent);
408
409         /* "name~3" is "name^^^",
410          * "name~12" is "name^^^^^^^^^^^^", and
411          * "name~" and "name~0" are name -- not "name^0"!
412          */
413         parent = 0;
414         for (cp = name + len - 1; name <= cp; cp--) {
415                 int ch = *cp;
416                 if ('0' <= ch && ch <= '9')
417                         continue;
418                 if (ch != '~')
419                         parent = -1;
420                 break;
421         }
422         if (!parent && *cp == '~') {
423                 int len1 = cp - name;
424                 cp++;
425                 while (cp < name + len)
426                         parent = parent * 10 + *cp++ - '0';
427                 return get_nth_ancestor(name, len1, sha1, parent);
428         }
429
430         ret = peel_onion(name, len, sha1);
431         if (!ret)
432                 return 0;
433
434         ret = get_sha1_basic(name, len, sha1);
435         if (!ret)
436                 return 0;
437         return get_short_sha1(name, len, sha1, 0);
438 }
439
440 /*
441  * This is like "get_sha1_basic()", except it allows "sha1 expressions",
442  * notably "xyz^" for "parent of xyz"
443  */
444 int get_sha1(const char *name, unsigned char *sha1)
445 {
446         prepare_alt_odb();
447         return get_sha1_1(name, strlen(name), sha1);
448 }