Documentation/git-archimport: document -o, -a, f, -D options
[git/jnareb-git.git] / sha1_name.c
blob67b69a54fb730548f668d274ec58ff4ffc544f37
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "blob.h"
7 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
9 struct alternate_object_database *alt;
10 char hex[40];
11 int found = 0;
12 static struct alternate_object_database *fakeent;
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] = '/';
23 fakeent->next = alt_odb_list;
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++;
42 else if (memcmp(hex + 2, de->d_name, 38)) {
43 found = 2;
44 break;
47 closedir(dir);
49 if (found == 1)
50 return get_sha1_hex(hex, sha1) == 0;
51 return found;
54 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
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;
69 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
71 struct packed_git *p;
72 unsigned char found_sha1[20];
73 int found = 0;
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;
84 nth_packed_object_sha1(p, mid, now);
85 cmp = memcmp(match, now, 20);
86 if (!cmp) {
87 first = mid;
88 break;
90 if (cmp > 0) {
91 first = mid+1;
92 continue;
94 last = mid;
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++;
107 else if (memcmp(found_sha1, now, 20)) {
108 found = 2;
109 break;
112 else {
113 /* not even unique within this pack */
114 found = 2;
115 break;
120 if (found == 1)
121 memcpy(sha1, found_sha1, 20);
122 return found;
125 #define SHORT_NAME_NOT_FOUND (-1)
126 #define SHORT_NAME_AMBIGUOUS (-2)
128 static int find_unique_short_object(int len, char *canonical,
129 unsigned char *res, unsigned char *sha1)
131 int has_unpacked, has_packed;
132 unsigned char unpacked_sha1[20], packed_sha1[20];
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;
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;
151 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
152 int quietly)
154 int i, status;
155 char canonical[40];
156 unsigned char res[20];
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';
173 else
174 return -1;
175 canonical[i] = c;
176 if (!(i & 1))
177 val <<= 4;
178 res[i >> 1] |= val;
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;
187 const char *find_unique_abbrev(const unsigned char *sha1, int len)
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;
199 if (status != SHORT_NAME_AMBIGUOUS)
200 return NULL;
201 len++;
203 return NULL;
206 static int ambiguous_path(const char *path, int len)
208 int slash = 1;
209 int cnt;
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;
226 break;
228 return slash;
231 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
233 static const char *prefix[] = {
235 "refs",
236 "refs/tags",
237 "refs/heads",
238 NULL
240 const char **p;
242 if (len == 40 && !get_sha1_hex(str, sha1))
243 return 0;
245 /* Accept only unambiguous ref paths. */
246 if (ambiguous_path(str, len))
247 return -1;
249 for (p = prefix; *p; p++) {
250 char *pathname = git_path("%s/%.*s", *p, len, str);
251 if (!read_ref(pathname, sha1))
252 return 0;
254 return -1;
257 static int get_sha1_1(const char *name, int len, unsigned char *sha1);
259 static int get_parent(const char *name, int len,
260 unsigned char *result, int idx)
262 unsigned char sha1[20];
263 int ret = get_sha1_1(name, len, sha1);
264 struct commit *commit;
265 struct commit_list *p;
267 if (ret)
268 return ret;
269 commit = lookup_commit_reference(sha1);
270 if (!commit)
271 return -1;
272 if (parse_commit(commit))
273 return -1;
274 if (!idx) {
275 memcpy(result, commit->object.sha1, 20);
276 return 0;
278 p = commit->parents;
279 while (p) {
280 if (!--idx) {
281 memcpy(result, p->item->object.sha1, 20);
282 return 0;
284 p = p->next;
286 return -1;
289 static int get_nth_ancestor(const char *name, int len,
290 unsigned char *result, int generation)
292 unsigned char sha1[20];
293 int ret = get_sha1_1(name, len, sha1);
294 if (ret)
295 return ret;
297 while (generation--) {
298 struct commit *commit = lookup_commit_reference(sha1);
300 if (!commit || parse_commit(commit) || !commit->parents)
301 return -1;
302 memcpy(sha1, commit->parents->item->object.sha1, 20);
304 memcpy(result, sha1, 20);
305 return 0;
308 static int peel_onion(const char *name, int len, unsigned char *sha1)
310 unsigned char outer[20];
311 const char *sp;
312 const char *type_string = NULL;
313 struct object *o;
316 * "ref^{type}" dereferences ref repeatedly until you cannot
317 * dereference anymore, or you get an object of given type,
318 * whichever comes first. "ref^{}" means just dereference
319 * tags until you get a non-tag. "ref^0" is a shorthand for
320 * "ref^{commit}". "commit^{tree}" could be used to find the
321 * top-level tree of the given commit.
323 if (len < 4 || name[len-1] != '}')
324 return -1;
326 for (sp = name + len - 1; name <= sp; sp--) {
327 int ch = *sp;
328 if (ch == '{' && name < sp && sp[-1] == '^')
329 break;
331 if (sp <= name)
332 return -1;
334 sp++; /* beginning of type name, or closing brace for empty */
335 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
336 type_string = commit_type;
337 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
338 type_string = tree_type;
339 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
340 type_string = blob_type;
341 else if (sp[0] == '}')
342 type_string = NULL;
343 else
344 return -1;
346 if (get_sha1_1(name, sp - name - 2, outer))
347 return -1;
349 o = parse_object(outer);
350 if (!o)
351 return -1;
352 if (!type_string) {
353 o = deref_tag(o, name, sp - name - 2);
354 if (!o || (!o->parsed && !parse_object(o->sha1)))
355 return -1;
356 memcpy(sha1, o->sha1, 20);
358 else {
359 /* At this point, the syntax look correct, so
360 * if we do not get the needed object, we should
361 * barf.
364 while (1) {
365 if (!o || (!o->parsed && !parse_object(o->sha1)))
366 return -1;
367 if (o->type == type_string) {
368 memcpy(sha1, o->sha1, 20);
369 return 0;
371 if (o->type == tag_type)
372 o = ((struct tag*) o)->tagged;
373 else if (o->type == commit_type)
374 o = &(((struct commit *) o)->tree->object);
375 else
376 return error("%.*s: expected %s type, but the object dereferences to %s type",
377 len, name, type_string,
378 o->type);
379 if (!o->parsed)
380 parse_object(o->sha1);
383 return 0;
386 static int get_sha1_1(const char *name, int len, unsigned char *sha1)
388 int parent, ret;
389 const char *cp;
391 /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
392 if (len > 2 && name[len-2] == '^' &&
393 name[len-1] >= '0' && name[len-1] <= '9') {
394 parent = name[len-1] - '0';
395 len -= 2;
397 else if (len > 1 && name[len-1] == '^') {
398 parent = 1;
399 len--;
400 } else
401 parent = -1;
403 if (parent >= 0)
404 return get_parent(name, len, sha1, parent);
406 /* "name~3" is "name^^^",
407 * "name~12" is "name^^^^^^^^^^^^", and
408 * "name~" and "name~0" are name -- not "name^0"!
410 parent = 0;
411 for (cp = name + len - 1; name <= cp; cp--) {
412 int ch = *cp;
413 if ('0' <= ch && ch <= '9')
414 continue;
415 if (ch != '~')
416 parent = -1;
417 break;
419 if (!parent && *cp == '~') {
420 int len1 = cp - name;
421 cp++;
422 while (cp < name + len)
423 parent = parent * 10 + *cp++ - '0';
424 return get_nth_ancestor(name, len1, sha1, parent);
427 ret = peel_onion(name, len, sha1);
428 if (!ret)
429 return 0;
431 ret = get_sha1_basic(name, len, sha1);
432 if (!ret)
433 return 0;
434 return get_short_sha1(name, len, sha1, 0);
438 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
439 * notably "xyz^" for "parent of xyz"
441 int get_sha1(const char *name, unsigned char *sha1)
443 prepare_alt_odb();
444 return get_sha1_1(name, strlen(name), sha1);