Merge branch 'fixes'
[git.git] / sha1_name.c
1 #include "cache.h"
2 #include "commit.h"
3
4 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
5 {
6         struct alternate_object_database *alt;
7         char hex[40];
8         int found = 0;
9         static struct alternate_object_database *fakeent;
10
11         if (!fakeent) {
12                 const char *objdir = get_object_directory();
13                 int objdir_len = strlen(objdir);
14                 int entlen = objdir_len + 43;
15                 fakeent = xmalloc(sizeof(*fakeent) + entlen);
16                 memcpy(fakeent->base, objdir, objdir_len);
17                 fakeent->name = fakeent->base + objdir_len + 1;
18                 fakeent->name[-1] = '/';
19         }
20         fakeent->next = alt_odb_list;
21
22         sprintf(hex, "%.2s", name);
23         for (alt = fakeent; alt && found < 2; alt = alt->next) {
24                 struct dirent *de;
25                 DIR *dir;
26                 sprintf(alt->name, "%.2s/", name);
27                 dir = opendir(alt->base);
28                 if (!dir)
29                         continue;
30                 while ((de = readdir(dir)) != NULL) {
31                         if (strlen(de->d_name) != 38)
32                                 continue;
33                         if (memcmp(de->d_name, name + 2, len - 2))
34                                 continue;
35                         if (!found) {
36                                 memcpy(hex + 2, de->d_name, 38);
37                                 found++;
38                         }
39                         else if (memcmp(hex + 2, de->d_name, 38)) {
40                                 found = 2;
41                                 break;
42                         }
43                 }
44                 closedir(dir);
45         }
46         if (found == 1)
47                 return get_sha1_hex(hex, sha1) == 0;
48         return found;
49 }
50
51 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
52 {
53         do {
54                 if (*a != *b)
55                         return 0;
56                 a++;
57                 b++;
58                 len -= 2;
59         } while (len > 1);
60         if (len)
61                 if ((*a ^ *b) & 0xf0)
62                         return 0;
63         return 1;
64 }
65
66 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
67 {
68         struct packed_git *p;
69         unsigned char found_sha1[20];
70         int found = 0;
71
72         prepare_packed_git();
73         for (p = packed_git; p && found < 2; p = p->next) {
74                 unsigned num = num_packed_objects(p);
75                 unsigned first = 0, last = num;
76                 while (first < last) {
77                         unsigned mid = (first + last) / 2;
78                         unsigned char now[20];
79                         int cmp;
80
81                         nth_packed_object_sha1(p, mid, now);
82                         cmp = memcmp(match, now, 20);
83                         if (!cmp) {
84                                 first = mid;
85                                 break;
86                         }
87                         if (cmp > 0) {
88                                 first = mid+1;
89                                 continue;
90                         }
91                         last = mid;
92                 }
93                 if (first < num) {
94                         unsigned char now[20];
95                         nth_packed_object_sha1(p, first, now);
96                         if (match_sha(len, match, now)) {
97                                 if (!found) {
98                                         memcpy(found_sha1, now, 20);
99                                         found++;
100                                 }
101                                 else if (memcmp(found_sha1, now, 20)) {
102                                         found = 2;
103                                         break;
104                                 }
105                         }
106                 }
107         }
108         if (found == 1)
109                 memcpy(sha1, found_sha1, 20);
110         return found;
111 }
112
113 static int find_unique_short_object(int len, char *canonical,
114                                     unsigned char *res, unsigned char *sha1)
115 {
116         int has_unpacked, has_packed;
117         unsigned char unpacked_sha1[20], packed_sha1[20];
118
119         has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
120         has_packed = find_short_packed_object(len, res, packed_sha1);
121         if (!has_unpacked && !has_packed)
122                 return -1;
123         if (1 < has_unpacked || 1 < has_packed)
124                 return -1;
125         if (has_unpacked != has_packed) {
126                 memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
127                 return 0;
128         }
129         /* Both have unique ones -- do they match? */
130         if (memcmp(packed_sha1, unpacked_sha1, 20))
131                 return error("short SHA1 %.*s is ambiguous.", len, canonical);
132         memcpy(sha1, packed_sha1, 20);
133         return 0;
134 }
135
136 static int get_short_sha1(const char *name, int len, unsigned char *sha1)
137 {
138         int i;
139         char canonical[40];
140         unsigned char res[20];
141
142         if (len < 4)
143                 return -1;
144         memset(res, 0, 20);
145         memset(canonical, 'x', 40);
146         for (i = 0; i < len ;i++) {
147                 unsigned char c = name[i];
148                 unsigned char val;
149                 if (c >= '0' && c <= '9')
150                         val = c - '0';
151                 else if (c >= 'a' && c <= 'f')
152                         val = c - 'a' + 10;
153                 else if (c >= 'A' && c <='F') {
154                         val = c - 'A' + 10;
155                         c -= 'A' - 'a';
156                 }
157                 else
158                         return -1;
159                 canonical[i] = c;
160                 if (!(i & 1))
161                         val <<= 4;
162                 res[i >> 1] |= val;
163         }
164
165         return find_unique_short_object(i, canonical, res, sha1);
166 }
167
168 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
169 {
170         static const char *prefix[] = {
171                 "",
172                 "refs",
173                 "refs/tags",
174                 "refs/heads",
175                 NULL
176         };
177         const char **p;
178
179         if (len == 40 && !get_sha1_hex(str, sha1))
180                 return 0;
181
182         for (p = prefix; *p; p++) {
183                 char *pathname = git_path("%s/%.*s", *p, len, str);
184                 if (!read_ref(pathname, sha1))
185                         return 0;
186         }
187
188         return -1;
189 }
190
191 static int get_sha1_1(const char *name, int len, unsigned char *sha1);
192
193 static int get_parent(const char *name, int len,
194                       unsigned char *result, int idx)
195 {
196         unsigned char sha1[20];
197         int ret = get_sha1_1(name, len, sha1);
198         struct commit *commit;
199         struct commit_list *p;
200
201         if (ret)
202                 return ret;
203         commit = lookup_commit_reference(sha1);
204         if (!commit)
205                 return -1;
206         if (parse_commit(commit))
207                 return -1;
208         if (!idx) {
209                 memcpy(result, commit->object.sha1, 20);
210                 return 0;
211         }
212         p = commit->parents;
213         while (p) {
214                 if (!--idx) {
215                         memcpy(result, p->item->object.sha1, 20);
216                         return 0;
217                 }
218                 p = p->next;
219         }
220         return -1;
221 }
222
223 static int get_nth_ancestor(const char *name, int len,
224                             unsigned char *result, int generation)
225 {
226         unsigned char sha1[20];
227         int ret = get_sha1_1(name, len, sha1);
228         if (ret)
229                 return ret;
230
231         while (generation--) {
232                 struct commit *commit = lookup_commit_reference(sha1);
233
234                 if (!commit || parse_commit(commit) || !commit->parents)
235                         return -1;
236                 memcpy(sha1, commit->parents->item->object.sha1, 20);
237         }
238         memcpy(result, sha1, 20);
239         return 0;
240 }
241
242 static int get_sha1_1(const char *name, int len, unsigned char *sha1)
243 {
244         int parent, ret;
245         const char *cp;
246
247         /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
248         if (len > 2 && name[len-2] == '^' &&
249             name[len-1] >= '0' && name[len-1] <= '9') {
250                 parent = name[len-1] - '0';
251                 len -= 2;
252         }
253         else if (len > 1 && name[len-1] == '^') {
254                 parent = 1;
255                 len--;
256         } else
257                 parent = -1;
258
259         if (parent >= 0)
260                 return get_parent(name, len, sha1, parent);
261
262         /* "name~3" is "name^^^",
263          * "name~12" is "name^^^^^^^^^^^^", and
264          * "name~" and "name~0" are name -- not "name^0"!
265          */
266         parent = 0;
267         for (cp = name + len - 1; name <= cp; cp--) {
268                 int ch = *cp;
269                 if ('0' <= ch && ch <= '9')
270                         continue;
271                 if (ch != '~')
272                         parent = -1;
273                 break;
274         }
275         if (!parent && *cp == '~') {
276                 int len1 = cp - name;
277                 cp++;
278                 while (cp < name + len)
279                         parent = parent * 10 + *cp++ - '0';
280                 return get_nth_ancestor(name, len1, sha1, parent);
281         }
282
283         ret = get_sha1_basic(name, len, sha1);
284         if (!ret)
285                 return 0;
286         return get_short_sha1(name, len, sha1);
287 }
288
289 /*
290  * This is like "get_sha1_basic()", except it allows "sha1 expressions",
291  * notably "xyz^" for "parent of xyz"
292  */
293 int get_sha1(const char *name, unsigned char *sha1)
294 {
295         prepare_alt_odb();
296         return get_sha1_1(name, strlen(name), sha1);
297 }