]> git.ipfire.org Git - thirdparty/git.git/blame - sha1_name.c
Be more careful tangling object chains while marking commits.
[thirdparty/git.git] / sha1_name.c
CommitLineData
9938af6a 1#include "cache.h"
5385f52d 2#include "tag.h"
9938af6a 3#include "commit.h"
5385f52d
JH
4#include "tree.h"
5#include "blob.h"
9938af6a
JH
6
7static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
8{
99a19b43 9 struct alternate_object_database *alt;
9938af6a 10 char hex[40];
99a19b43
JH
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;
9938af6a 24
9938af6a 25 sprintf(hex, "%.2s", name);
99a19b43 26 for (alt = fakeent; alt && found < 2; alt = alt->next) {
9938af6a 27 struct dirent *de;
99a19b43
JH
28 DIR *dir;
29 sprintf(alt->name, "%.2s/", name);
30 dir = opendir(alt->base);
31 if (!dir)
32 continue;
9938af6a
JH
33 while ((de = readdir(dir)) != NULL) {
34 if (strlen(de->d_name) != 38)
35 continue;
99a19b43 36 if (memcmp(de->d_name, name + 2, len - 2))
9938af6a 37 continue;
99a19b43
JH
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;
9938af6a 44 break;
99a19b43 45 }
9938af6a
JH
46 }
47 closedir(dir);
48 }
49 if (found == 1)
50 return get_sha1_hex(hex, sha1) == 0;
99a19b43 51 return found;
9938af6a
JH
52}
53
54static 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
69static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
70{
71 struct packed_git *p;
99a19b43
JH
72 unsigned char found_sha1[20];
73 int found = 0;
9938af6a
JH
74
75 prepare_packed_git();
99a19b43 76 for (p = packed_git; p && found < 2; p = p->next) {
9938af6a
JH
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) {
0bc45890 97 unsigned char now[20], next[20];
9938af6a
JH
98 nth_packed_object_sha1(p, first, now);
99 if (match_sha(len, match, now)) {
0bc45890
JH
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 }
99a19b43 111 }
0bc45890
JH
112 else {
113 /* not even unique within this pack */
99a19b43
JH
114 found = 2;
115 break;
9938af6a
JH
116 }
117 }
118 }
119 }
99a19b43
JH
120 if (found == 1)
121 memcpy(sha1, found_sha1, 20);
122 return found;
123}
124
013f276e
JH
125#define SHORT_NAME_NOT_FOUND (-1)
126#define SHORT_NAME_AMBIGUOUS (-2)
127
99a19b43
JH
128static 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)
013f276e 137 return SHORT_NAME_NOT_FOUND;
99a19b43 138 if (1 < has_unpacked || 1 < has_packed)
013f276e 139 return SHORT_NAME_AMBIGUOUS;
99a19b43
JH
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))
013f276e 146 return -2;
99a19b43 147 memcpy(sha1, packed_sha1, 20);
9938af6a
JH
148 return 0;
149}
150
013f276e
JH
151static int get_short_sha1(const char *name, int len, unsigned char *sha1,
152 int quietly)
9938af6a 153{
013f276e 154 int i, status;
9938af6a
JH
155 char canonical[40];
156 unsigned char res[20];
157
af61c6e0
LT
158 if (len < 4)
159 return -1;
9938af6a
JH
160 memset(res, 0, 20);
161 memset(canonical, 'x', 40);
af61c6e0 162 for (i = 0; i < len ;i++) {
9938af6a
JH
163 unsigned char c = name[i];
164 unsigned char val;
9938af6a
JH
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 }
99a19b43 180
013f276e
JH
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
187const 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;
9938af6a
JH
204}
205
9938af6a
JH
206static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
207{
208 static const char *prefix[] = {
209 "",
210 "refs",
211 "refs/tags",
212 "refs/heads",
9938af6a
JH
213 NULL
214 };
215 const char **p;
216
3c3852e3 217 if (len == 40 && !get_sha1_hex(str, sha1))
9938af6a
JH
218 return 0;
219
220 for (p = prefix; *p; p++) {
221 char *pathname = git_path("%s/%.*s", *p, len, str);
ca8db142 222 if (!read_ref(pathname, sha1))
9938af6a
JH
223 return 0;
224 }
225
226 return -1;
227}
228
229static int get_sha1_1(const char *name, int len, unsigned char *sha1);
230
231static int get_parent(const char *name, int len,
232 unsigned char *result, int idx)
233{
234 unsigned char sha1[20];
235 int ret = get_sha1_1(name, len, sha1);
236 struct commit *commit;
237 struct commit_list *p;
238
239 if (ret)
240 return ret;
241 commit = lookup_commit_reference(sha1);
242 if (!commit)
243 return -1;
244 if (parse_commit(commit))
245 return -1;
246 if (!idx) {
247 memcpy(result, commit->object.sha1, 20);
248 return 0;
249 }
250 p = commit->parents;
251 while (p) {
252 if (!--idx) {
253 memcpy(result, p->item->object.sha1, 20);
254 return 0;
255 }
256 p = p->next;
257 }
258 return -1;
259}
260
4f7599ac
JH
261static int get_nth_ancestor(const char *name, int len,
262 unsigned char *result, int generation)
263{
264 unsigned char sha1[20];
265 int ret = get_sha1_1(name, len, sha1);
266 if (ret)
267 return ret;
268
269 while (generation--) {
270 struct commit *commit = lookup_commit_reference(sha1);
271
272 if (!commit || parse_commit(commit) || !commit->parents)
273 return -1;
274 memcpy(sha1, commit->parents->item->object.sha1, 20);
275 }
276 memcpy(result, sha1, 20);
277 return 0;
278}
279
5385f52d
JH
280static int peel_onion(const char *name, int len, unsigned char *sha1)
281{
282 unsigned char outer[20];
283 const char *sp;
284 const char *type_string = NULL;
285 struct object *o;
286
287 /*
288 * "ref^{type}" dereferences ref repeatedly until you cannot
289 * dereference anymore, or you get an object of given type,
290 * whichever comes first. "ref^{}" means just dereference
291 * tags until you get a non-tag. "ref^0" is a shorthand for
292 * "ref^{commit}". "commit^{tree}" could be used to find the
293 * top-level tree of the given commit.
294 */
295 if (len < 4 || name[len-1] != '}')
296 return -1;
297
298 for (sp = name + len - 1; name <= sp; sp--) {
299 int ch = *sp;
300 if (ch == '{' && name < sp && sp[-1] == '^')
301 break;
302 }
303 if (sp <= name)
304 return -1;
305
306 sp++; /* beginning of type name, or closing brace for empty */
307 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
308 type_string = commit_type;
309 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
310 type_string = tree_type;
311 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
312 type_string = blob_type;
313 else if (sp[0] == '}')
314 type_string = NULL;
315 else
316 return -1;
317
318 if (get_sha1_1(name, sp - name - 2, outer))
319 return -1;
320
321 o = parse_object(outer);
322 if (!o)
323 return -1;
324 if (!type_string) {
325 o = deref_tag(o);
326 memcpy(sha1, o->sha1, 20);
327 }
328 else {
329 /* At this point, the syntax look correct, so
330 * if we do not get the needed object, we should
331 * barf.
332 */
333
334 while (1) {
335 if (!o)
336 return -1;
337 if (o->type == type_string) {
338 memcpy(sha1, o->sha1, 20);
339 return 0;
340 }
341 if (o->type == tag_type)
342 o = ((struct tag*) o)->tagged;
343 else if (o->type == commit_type)
344 o = &(((struct commit *) o)->tree->object);
345 else
346 return error("%.*s: expected %s type, but the object dereferences to %s type",
347 len, name, type_string,
348 o->type);
349 if (!o->parsed)
350 parse_object(o->sha1);
351 }
352 }
353 return 0;
354}
355
9938af6a
JH
356static int get_sha1_1(const char *name, int len, unsigned char *sha1)
357{
358 int parent, ret;
4f7599ac 359 const char *cp;
9938af6a
JH
360
361 /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
362 if (len > 2 && name[len-2] == '^' &&
363 name[len-1] >= '0' && name[len-1] <= '9') {
364 parent = name[len-1] - '0';
365 len -= 2;
366 }
ef0bd2e6 367 else if (len > 1 && name[len-1] == '^') {
9938af6a 368 parent = 1;
ef0bd2e6
JS
369 len--;
370 } else
9938af6a
JH
371 parent = -1;
372
02a4a32c
LT
373 if (parent >= 0)
374 return get_parent(name, len, sha1, parent);
375
4f7599ac
JH
376 /* "name~3" is "name^^^",
377 * "name~12" is "name^^^^^^^^^^^^", and
378 * "name~" and "name~0" are name -- not "name^0"!
379 */
380 parent = 0;
381 for (cp = name + len - 1; name <= cp; cp--) {
382 int ch = *cp;
383 if ('0' <= ch && ch <= '9')
384 continue;
385 if (ch != '~')
386 parent = -1;
387 break;
388 }
389 if (!parent && *cp == '~') {
390 int len1 = cp - name;
391 cp++;
392 while (cp < name + len)
393 parent = parent * 10 + *cp++ - '0';
394 return get_nth_ancestor(name, len1, sha1, parent);
395 }
396
5385f52d
JH
397 ret = peel_onion(name, len, sha1);
398 if (!ret)
399 return 0;
400
9938af6a
JH
401 ret = get_sha1_basic(name, len, sha1);
402 if (!ret)
403 return 0;
013f276e 404 return get_short_sha1(name, len, sha1, 0);
9938af6a
JH
405}
406
407/*
408 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
409 * notably "xyz^" for "parent of xyz"
410 */
411int get_sha1(const char *name, unsigned char *sha1)
412{
99a19b43 413 prepare_alt_odb();
9938af6a
JH
414 return get_sha1_1(name, strlen(name), sha1);
415}