4 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
6 struct alternate_object_database
*alt
;
9 static struct alternate_object_database
*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] = '/';
20 fakeent
->next
= alt_odb_list
;
22 sprintf(hex
, "%.2s", name
);
23 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
26 sprintf(alt
->name
, "%.2s/", name
);
27 dir
= opendir(alt
->base
);
30 while ((de
= readdir(dir
)) != NULL
) {
31 if (strlen(de
->d_name
) != 38)
33 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
36 memcpy(hex
+ 2, de
->d_name
, 38);
39 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
47 return get_sha1_hex(hex
, sha1
) == 0;
51 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
66 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
69 unsigned char found_sha1
[20];
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];
81 nth_packed_object_sha1(p
, mid
, now
);
82 cmp
= memcmp(match
, now
, 20);
94 unsigned char now
[20], next
[20];
95 nth_packed_object_sha1(p
, first
, now
);
96 if (match_sha(len
, match
, now
)) {
97 if (nth_packed_object_sha1(p
, first
+1, next
) ||
98 !match_sha(len
, match
, next
)) {
99 /* unique within this pack */
101 memcpy(found_sha1
, now
, 20);
104 else if (memcmp(found_sha1
, now
, 20)) {
110 /* not even unique within this pack */
118 memcpy(sha1
, found_sha1
, 20);
122 #define SHORT_NAME_NOT_FOUND (-1)
123 #define SHORT_NAME_AMBIGUOUS (-2)
125 static int find_unique_short_object(int len
, char *canonical
,
126 unsigned char *res
, unsigned char *sha1
)
128 int has_unpacked
, has_packed
;
129 unsigned char unpacked_sha1
[20], packed_sha1
[20];
131 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
132 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
133 if (!has_unpacked
&& !has_packed
)
134 return SHORT_NAME_NOT_FOUND
;
135 if (1 < has_unpacked
|| 1 < has_packed
)
136 return SHORT_NAME_AMBIGUOUS
;
137 if (has_unpacked
!= has_packed
) {
138 memcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
), 20);
141 /* Both have unique ones -- do they match? */
142 if (memcmp(packed_sha1
, unpacked_sha1
, 20))
144 memcpy(sha1
, packed_sha1
, 20);
148 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
153 unsigned char res
[20];
158 memset(canonical
, 'x', 40);
159 for (i
= 0; i
< len
;i
++) {
160 unsigned char c
= name
[i
];
162 if (c
>= '0' && c
<= '9')
164 else if (c
>= 'a' && c
<= 'f')
166 else if (c
>= 'A' && c
<='F') {
178 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
179 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
180 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
184 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
188 memcpy(hex
, sha1_to_hex(sha1
), 40);
190 unsigned char sha1_ret
[20];
191 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
196 if (status
!= SHORT_NAME_AMBIGUOUS
)
203 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
205 static const char *prefix
[] = {
214 if (len
== 40 && !get_sha1_hex(str
, sha1
))
217 for (p
= prefix
; *p
; p
++) {
218 char *pathname
= git_path("%s/%.*s", *p
, len
, str
);
219 if (!read_ref(pathname
, sha1
))
226 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
228 static int get_parent(const char *name
, int len
,
229 unsigned char *result
, int idx
)
231 unsigned char sha1
[20];
232 int ret
= get_sha1_1(name
, len
, sha1
);
233 struct commit
*commit
;
234 struct commit_list
*p
;
238 commit
= lookup_commit_reference(sha1
);
241 if (parse_commit(commit
))
244 memcpy(result
, commit
->object
.sha1
, 20);
250 memcpy(result
, p
->item
->object
.sha1
, 20);
258 static int get_nth_ancestor(const char *name
, int len
,
259 unsigned char *result
, int generation
)
261 unsigned char sha1
[20];
262 int ret
= get_sha1_1(name
, len
, sha1
);
266 while (generation
--) {
267 struct commit
*commit
= lookup_commit_reference(sha1
);
269 if (!commit
|| parse_commit(commit
) || !commit
->parents
)
271 memcpy(sha1
, commit
->parents
->item
->object
.sha1
, 20);
273 memcpy(result
, sha1
, 20);
277 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
282 /* foo^[0-9] or foo^ (== foo^1); we do not do more than 9 parents. */
283 if (len
> 2 && name
[len
-2] == '^' &&
284 name
[len
-1] >= '0' && name
[len
-1] <= '9') {
285 parent
= name
[len
-1] - '0';
288 else if (len
> 1 && name
[len
-1] == '^') {
295 return get_parent(name
, len
, sha1
, parent
);
297 /* "name~3" is "name^^^",
298 * "name~12" is "name^^^^^^^^^^^^", and
299 * "name~" and "name~0" are name -- not "name^0"!
302 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
304 if ('0' <= ch
&& ch
<= '9')
310 if (!parent
&& *cp
== '~') {
311 int len1
= cp
- name
;
313 while (cp
< name
+ len
)
314 parent
= parent
* 10 + *cp
++ - '0';
315 return get_nth_ancestor(name
, len1
, sha1
, parent
);
318 ret
= get_sha1_basic(name
, len
, sha1
);
321 return get_short_sha1(name
, len
, sha1
, 0);
325 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
326 * notably "xyz^" for "parent of xyz"
328 int get_sha1(const char *name
, unsigned char *sha1
)
331 return get_sha1_1(name
, strlen(name
), sha1
);