7 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
9 struct alternate_object_database
*alt
;
12 static struct alternate_object_database
*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
) {
29 sprintf(alt
->name
, "%.2s/", name
);
30 dir
= opendir(alt
->base
);
33 while ((de
= readdir(dir
)) != NULL
) {
34 if (strlen(de
->d_name
) != 38)
36 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
39 memcpy(hex
+ 2, de
->d_name
, 38);
42 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
50 return get_sha1_hex(hex
, sha1
) == 0;
54 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
69 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
72 unsigned char found_sha1
[20];
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];
84 nth_packed_object_sha1(p
, mid
, now
);
85 cmp
= memcmp(match
, now
, 20);
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 */
104 memcpy(found_sha1
, now
, 20);
107 else if (memcmp(found_sha1
, now
, 20)) {
113 /* not even unique within this pack */
121 memcpy(sha1
, found_sha1
, 20);
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);
144 /* Both have unique ones -- do they match? */
145 if (memcmp(packed_sha1
, unpacked_sha1
, 20))
147 memcpy(sha1
, packed_sha1
, 20);
151 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
156 unsigned char res
[20];
161 memset(canonical
, 'x', 40);
162 for (i
= 0; i
< len
;i
++) {
163 unsigned char c
= name
[i
];
165 if (c
>= '0' && c
<= '9')
167 else if (c
>= 'a' && c
<= 'f')
169 else if (c
>= 'A' && c
<='F') {
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
);
187 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
191 memcpy(hex
, sha1_to_hex(sha1
), 40);
193 unsigned char sha1_ret
[20];
194 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
199 if (status
!= SHORT_NAME_AMBIGUOUS
)
206 static int ambiguous_path(const char *path
)
229 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
231 static const char *prefix
[] = {
240 if (len
== 40 && !get_sha1_hex(str
, sha1
))
243 /* Accept only unambiguous ref paths. */
244 if (ambiguous_path(str
))
247 for (p
= prefix
; *p
; p
++) {
248 char *pathname
= git_path("%s/%.*s", *p
, len
, str
);
249 if (!read_ref(pathname
, sha1
))
256 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
258 static int get_parent(const char *name
, int len
,
259 unsigned char *result
, int idx
)
261 unsigned char sha1
[20];
262 int ret
= get_sha1_1(name
, len
, sha1
);
263 struct commit
*commit
;
264 struct commit_list
*p
;
268 commit
= lookup_commit_reference(sha1
);
271 if (parse_commit(commit
))
274 memcpy(result
, commit
->object
.sha1
, 20);
280 memcpy(result
, p
->item
->object
.sha1
, 20);
288 static int get_nth_ancestor(const char *name
, int len
,
289 unsigned char *result
, int generation
)
291 unsigned char sha1
[20];
292 int ret
= get_sha1_1(name
, len
, sha1
);
296 while (generation
--) {
297 struct commit
*commit
= lookup_commit_reference(sha1
);
299 if (!commit
|| parse_commit(commit
) || !commit
->parents
)
301 memcpy(sha1
, commit
->parents
->item
->object
.sha1
, 20);
303 memcpy(result
, sha1
, 20);
307 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
309 unsigned char outer
[20];
311 const char *type_string
= NULL
;
315 * "ref^{type}" dereferences ref repeatedly until you cannot
316 * dereference anymore, or you get an object of given type,
317 * whichever comes first. "ref^{}" means just dereference
318 * tags until you get a non-tag. "ref^0" is a shorthand for
319 * "ref^{commit}". "commit^{tree}" could be used to find the
320 * top-level tree of the given commit.
322 if (len
< 4 || name
[len
-1] != '}')
325 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
327 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
333 sp
++; /* beginning of type name, or closing brace for empty */
334 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
335 type_string
= commit_type
;
336 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
337 type_string
= tree_type
;
338 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
339 type_string
= blob_type
;
340 else if (sp
[0] == '}')
345 if (get_sha1_1(name
, sp
- name
- 2, outer
))
348 o
= parse_object(outer
);
353 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
355 memcpy(sha1
, o
->sha1
, 20);
358 /* At this point, the syntax look correct, so
359 * if we do not get the needed object, we should
364 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
366 if (o
->type
== type_string
) {
367 memcpy(sha1
, o
->sha1
, 20);
370 if (o
->type
== tag_type
)
371 o
= ((struct tag
*) o
)->tagged
;
372 else if (o
->type
== commit_type
)
373 o
= &(((struct commit
*) o
)->tree
->object
);
375 return error("%.*s: expected %s type, but the object dereferences to %s type",
376 len
, name
, type_string
,
379 parse_object(o
->sha1
);
385 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
390 /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
391 if (len
> 2 && name
[len
-2] == '^' &&
392 name
[len
-1] >= '0' && name
[len
-1] <= '9') {
393 parent
= name
[len
-1] - '0';
396 else if (len
> 1 && name
[len
-1] == '^') {
403 return get_parent(name
, len
, sha1
, parent
);
405 /* "name~3" is "name^^^",
406 * "name~12" is "name^^^^^^^^^^^^", and
407 * "name~" and "name~0" are name -- not "name^0"!
410 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
412 if ('0' <= ch
&& ch
<= '9')
418 if (!parent
&& *cp
== '~') {
419 int len1
= cp
- name
;
421 while (cp
< name
+ len
)
422 parent
= parent
* 10 + *cp
++ - '0';
423 return get_nth_ancestor(name
, len1
, sha1
, parent
);
426 ret
= peel_onion(name
, len
, sha1
);
430 ret
= get_sha1_basic(name
, len
, sha1
);
433 return get_short_sha1(name
, len
, sha1
, 0);
437 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
438 * notably "xyz^" for "parent of xyz"
440 int get_sha1(const char *name
, unsigned char *sha1
)
443 return get_sha1_1(name
, strlen(name
), sha1
);