8 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
10 struct alternate_object_database
*alt
;
13 static struct alternate_object_database
*fakeent
;
16 const char *objdir
= get_object_directory();
17 int objdir_len
= strlen(objdir
);
18 int entlen
= objdir_len
+ 43;
19 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
20 memcpy(fakeent
->base
, objdir
, objdir_len
);
21 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
22 fakeent
->name
[-1] = '/';
24 fakeent
->next
= alt_odb_list
;
26 sprintf(hex
, "%.2s", name
);
27 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
30 sprintf(alt
->name
, "%.2s/", name
);
31 dir
= opendir(alt
->base
);
34 while ((de
= readdir(dir
)) != NULL
) {
35 if (strlen(de
->d_name
) != 38)
37 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
40 memcpy(hex
+ 2, de
->d_name
, 38);
43 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
51 return get_sha1_hex(hex
, sha1
) == 0;
55 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
70 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
73 unsigned char found_sha1
[20];
77 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
) {
78 unsigned num
= num_packed_objects(p
);
79 unsigned first
= 0, last
= num
;
80 while (first
< last
) {
81 unsigned mid
= (first
+ last
) / 2;
82 unsigned char now
[20];
85 nth_packed_object_sha1(p
, mid
, now
);
86 cmp
= memcmp(match
, now
, 20);
98 unsigned char now
[20], next
[20];
99 nth_packed_object_sha1(p
, first
, now
);
100 if (match_sha(len
, match
, now
)) {
101 if (nth_packed_object_sha1(p
, first
+1, next
) ||
102 !match_sha(len
, match
, next
)) {
103 /* unique within this pack */
105 memcpy(found_sha1
, now
, 20);
108 else if (memcmp(found_sha1
, now
, 20)) {
114 /* not even unique within this pack */
122 memcpy(sha1
, found_sha1
, 20);
126 #define SHORT_NAME_NOT_FOUND (-1)
127 #define SHORT_NAME_AMBIGUOUS (-2)
129 static int find_unique_short_object(int len
, char *canonical
,
130 unsigned char *res
, unsigned char *sha1
)
132 int has_unpacked
, has_packed
;
133 unsigned char unpacked_sha1
[20], packed_sha1
[20];
135 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
136 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
137 if (!has_unpacked
&& !has_packed
)
138 return SHORT_NAME_NOT_FOUND
;
139 if (1 < has_unpacked
|| 1 < has_packed
)
140 return SHORT_NAME_AMBIGUOUS
;
141 if (has_unpacked
!= has_packed
) {
142 memcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
), 20);
145 /* Both have unique ones -- do they match? */
146 if (memcmp(packed_sha1
, unpacked_sha1
, 20))
147 return SHORT_NAME_AMBIGUOUS
;
148 memcpy(sha1
, packed_sha1
, 20);
152 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
157 unsigned char res
[20];
159 if (len
< MINIMUM_ABBREV
)
162 memset(canonical
, 'x', 40);
163 for (i
= 0; i
< len
;i
++) {
164 unsigned char c
= name
[i
];
166 if (c
>= '0' && c
<= '9')
168 else if (c
>= 'a' && c
<= 'f')
170 else if (c
>= 'A' && c
<='F') {
182 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
183 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
184 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
188 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
193 is_null
= !memcmp(sha1
, null_sha1
, 20);
194 memcpy(hex
, sha1_to_hex(sha1
), 40);
198 unsigned char sha1_ret
[20];
199 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
201 (is_null
&& status
!= SHORT_NAME_AMBIGUOUS
)) {
205 if (status
!= SHORT_NAME_AMBIGUOUS
)
212 static int ambiguous_path(const char *path
, int len
)
217 for (cnt
= 0; cnt
< len
; cnt
++) {
237 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
239 static const char *fmt
[] = {
245 "refs/remotes/%.*s/HEAD",
249 const char *warning
= "warning: refname '%.*s' is ambiguous.\n";
251 int already_found
= 0;
252 unsigned char *this_result
;
253 unsigned char sha1_from_ref
[20];
255 if (len
== 40 && !get_sha1_hex(str
, sha1
))
258 /* Accept only unambiguous ref paths. */
259 if (ambiguous_path(str
, len
))
262 for (p
= fmt
; *p
; p
++) {
263 this_result
= already_found
? sha1_from_ref
: sha1
;
264 pathname
= git_path(*p
, len
, str
);
265 if (!read_ref(pathname
, this_result
)) {
266 if (warn_ambiguous_refs
) {
268 fprintf(stderr
, warning
, len
, str
);
280 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
282 static int get_parent(const char *name
, int len
,
283 unsigned char *result
, int idx
)
285 unsigned char sha1
[20];
286 int ret
= get_sha1_1(name
, len
, sha1
);
287 struct commit
*commit
;
288 struct commit_list
*p
;
292 commit
= lookup_commit_reference(sha1
);
295 if (parse_commit(commit
))
298 memcpy(result
, commit
->object
.sha1
, 20);
304 memcpy(result
, p
->item
->object
.sha1
, 20);
312 static int get_nth_ancestor(const char *name
, int len
,
313 unsigned char *result
, int generation
)
315 unsigned char sha1
[20];
316 int ret
= get_sha1_1(name
, len
, sha1
);
320 while (generation
--) {
321 struct commit
*commit
= lookup_commit_reference(sha1
);
323 if (!commit
|| parse_commit(commit
) || !commit
->parents
)
325 memcpy(sha1
, commit
->parents
->item
->object
.sha1
, 20);
327 memcpy(result
, sha1
, 20);
331 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
333 unsigned char outer
[20];
335 const char *type_string
= NULL
;
339 * "ref^{type}" dereferences ref repeatedly until you cannot
340 * dereference anymore, or you get an object of given type,
341 * whichever comes first. "ref^{}" means just dereference
342 * tags until you get a non-tag. "ref^0" is a shorthand for
343 * "ref^{commit}". "commit^{tree}" could be used to find the
344 * top-level tree of the given commit.
346 if (len
< 4 || name
[len
-1] != '}')
349 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
351 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
357 sp
++; /* beginning of type name, or closing brace for empty */
358 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
359 type_string
= commit_type
;
360 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
361 type_string
= tree_type
;
362 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
363 type_string
= blob_type
;
364 else if (sp
[0] == '}')
369 if (get_sha1_1(name
, sp
- name
- 2, outer
))
372 o
= parse_object(outer
);
376 o
= deref_tag(o
, name
, sp
- name
- 2);
377 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
379 memcpy(sha1
, o
->sha1
, 20);
382 /* At this point, the syntax look correct, so
383 * if we do not get the needed object, we should
388 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
390 if (o
->type
== type_string
) {
391 memcpy(sha1
, o
->sha1
, 20);
394 if (o
->type
== tag_type
)
395 o
= ((struct tag
*) o
)->tagged
;
396 else if (o
->type
== commit_type
)
397 o
= &(((struct commit
*) o
)->tree
->object
);
399 return error("%.*s: expected %s type, but the object dereferences to %s type",
400 len
, name
, type_string
,
403 parse_object(o
->sha1
);
409 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
414 /* "name~3" is "name^^^",
415 * "name~" and "name~0" are name -- not "name^0"!
416 * "name^" is not "name^0"; it is "name^1".
419 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
421 if ('0' <= ch
&& ch
<= '9')
423 if (ch
== '~' || ch
== '^')
430 int len1
= cp
- name
;
432 while (cp
< name
+ len
)
433 num
= num
* 10 + *cp
++ - '0';
434 if (has_suffix
== '^') {
435 if (!num
&& len1
== len
- 1)
437 return get_parent(name
, len1
, sha1
, num
);
439 /* else if (has_suffix == '~') -- goes without saying */
440 return get_nth_ancestor(name
, len1
, sha1
, num
);
443 ret
= peel_onion(name
, len
, sha1
);
447 ret
= get_sha1_basic(name
, len
, sha1
);
450 return get_short_sha1(name
, len
, sha1
, 0);
453 static int get_tree_entry(const unsigned char *, const char *, unsigned char *, unsigned *);
455 static int find_tree_entry(struct tree_desc
*t
, const char *name
, unsigned char *result
, unsigned *mode
)
457 int namelen
= strlen(name
);
460 const unsigned char *sha1
;
463 sha1
= tree_entry_extract(t
, &entry
, mode
);
464 update_tree_entry(t
);
465 entrylen
= strlen(entry
);
466 if (entrylen
> namelen
)
468 cmp
= memcmp(name
, entry
, entrylen
);
473 if (entrylen
== namelen
) {
474 memcpy(result
, sha1
, 20);
477 if (name
[entrylen
] != '/')
481 if (++entrylen
== namelen
) {
482 memcpy(result
, sha1
, 20);
485 return get_tree_entry(sha1
, name
+ entrylen
, result
, mode
);
490 static int get_tree_entry(const unsigned char *tree_sha1
, const char *name
, unsigned char *sha1
, unsigned *mode
)
496 tree
= read_object_with_reference(tree_sha1
, tree_type
, &t
.size
, NULL
);
500 retval
= find_tree_entry(&t
, name
, sha1
, mode
);
506 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
507 * notably "xyz^" for "parent of xyz"
509 int get_sha1(const char *name
, unsigned char *sha1
)
515 ret
= get_sha1_1(name
, strlen(name
), sha1
);
517 const char *cp
= strchr(name
, ':');
519 unsigned char tree_sha1
[20];
520 if (!get_sha1_1(name
, cp
-name
, tree_sha1
))
521 return get_tree_entry(tree_sha1
, cp
+1, sha1
,