9 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
11 struct alternate_object_database
*alt
;
14 static struct alternate_object_database
*fakeent
;
15 static const char *last_objdir
;
16 const char *objdir
= get_object_directory();
18 if (!last_objdir
|| strcmp(last_objdir
, objdir
)) {
19 int objdir_len
= strlen(objdir
);
20 int entlen
= objdir_len
+ 43;
23 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
24 memcpy(fakeent
->base
, objdir
, objdir_len
);
25 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
26 fakeent
->name
[-1] = '/';
28 free((char *) last_objdir
);
29 last_objdir
= strdup(objdir
);
31 fakeent
->next
= alt_odb_list
;
33 sprintf(hex
, "%.2s", name
);
34 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
37 sprintf(alt
->name
, "%.2s/", name
);
38 dir
= opendir(alt
->base
);
41 while ((de
= readdir(dir
)) != NULL
) {
42 if (strlen(de
->d_name
) != 38)
44 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
47 memcpy(hex
+ 2, de
->d_name
, 38);
50 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
58 return get_sha1_hex(hex
, sha1
) == 0;
62 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
77 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
80 unsigned char found_sha1
[20];
84 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
) {
85 unsigned num
= num_packed_objects(p
);
86 unsigned first
= 0, last
= num
;
87 while (first
< last
) {
88 unsigned mid
= (first
+ last
) / 2;
89 unsigned char now
[20];
92 nth_packed_object_sha1(p
, mid
, now
);
93 cmp
= memcmp(match
, now
, 20);
105 unsigned char now
[20], next
[20];
106 nth_packed_object_sha1(p
, first
, now
);
107 if (match_sha(len
, match
, now
)) {
108 if (nth_packed_object_sha1(p
, first
+1, next
) ||
109 !match_sha(len
, match
, next
)) {
110 /* unique within this pack */
112 memcpy(found_sha1
, now
, 20);
115 else if (memcmp(found_sha1
, now
, 20)) {
121 /* not even unique within this pack */
129 memcpy(sha1
, found_sha1
, 20);
133 #define SHORT_NAME_NOT_FOUND (-1)
134 #define SHORT_NAME_AMBIGUOUS (-2)
136 static int find_unique_short_object(int len
, char *canonical
,
137 unsigned char *res
, unsigned char *sha1
)
139 int has_unpacked
, has_packed
;
140 unsigned char unpacked_sha1
[20], packed_sha1
[20];
142 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
143 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
144 if (!has_unpacked
&& !has_packed
)
145 return SHORT_NAME_NOT_FOUND
;
146 if (1 < has_unpacked
|| 1 < has_packed
)
147 return SHORT_NAME_AMBIGUOUS
;
148 if (has_unpacked
!= has_packed
) {
149 memcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
), 20);
152 /* Both have unique ones -- do they match? */
153 if (memcmp(packed_sha1
, unpacked_sha1
, 20))
154 return SHORT_NAME_AMBIGUOUS
;
155 memcpy(sha1
, packed_sha1
, 20);
159 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
164 unsigned char res
[20];
166 if (len
< MINIMUM_ABBREV
)
169 memset(canonical
, 'x', 40);
170 for (i
= 0; i
< len
;i
++) {
171 unsigned char c
= name
[i
];
173 if (c
>= '0' && c
<= '9')
175 else if (c
>= 'a' && c
<= 'f')
177 else if (c
>= 'A' && c
<='F') {
189 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
190 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
191 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
195 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
200 is_null
= !memcmp(sha1
, null_sha1
, 20);
201 memcpy(hex
, sha1_to_hex(sha1
), 40);
205 unsigned char sha1_ret
[20];
206 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
208 (is_null
&& status
!= SHORT_NAME_AMBIGUOUS
)) {
212 if (status
!= SHORT_NAME_AMBIGUOUS
)
219 static int ambiguous_path(const char *path
, int len
)
224 for (cnt
= 0; cnt
< len
; cnt
++) {
244 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
246 static const char *fmt
[] = {
252 "refs/remotes/%.*s/HEAD",
255 static const char *warning
= "warning: refname '%.*s' is ambiguous.\n";
256 const char **p
, *pathname
;
257 char *real_path
= NULL
;
258 int refs_found
= 0, am
;
259 unsigned long at_time
= (unsigned long)-1;
260 unsigned char *this_result
;
261 unsigned char sha1_from_ref
[20];
263 if (len
== 40 && !get_sha1_hex(str
, sha1
))
266 /* At a given period of time? "@{2 hours ago}" */
267 for (am
= 1; am
< len
- 1; am
++) {
268 if (str
[am
] == '@' && str
[am
+1] == '{' && str
[len
-1] == '}') {
269 int date_len
= len
- am
- 3;
270 char *date_spec
= xmalloc(date_len
+ 1);
271 strlcpy(date_spec
, str
+ am
+ 2, date_len
+ 1);
272 at_time
= approxidate(date_spec
);
279 /* Accept only unambiguous ref paths. */
280 if (ambiguous_path(str
, len
))
283 for (p
= fmt
; *p
; p
++) {
284 this_result
= refs_found
? sha1_from_ref
: sha1
;
285 pathname
= resolve_ref(git_path(*p
, len
, str
), this_result
, 1);
288 real_path
= strdup(pathname
);
289 if (!warn_ambiguous_refs
)
297 if (warn_ambiguous_refs
&& refs_found
> 1)
298 fprintf(stderr
, warning
, len
, str
);
300 if (at_time
!= (unsigned long)-1) {
302 real_path
+ strlen(git_path(".")) - 1,
311 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
313 static int get_parent(const char *name
, int len
,
314 unsigned char *result
, int idx
)
316 unsigned char sha1
[20];
317 int ret
= get_sha1_1(name
, len
, sha1
);
318 struct commit
*commit
;
319 struct commit_list
*p
;
323 commit
= lookup_commit_reference(sha1
);
326 if (parse_commit(commit
))
329 memcpy(result
, commit
->object
.sha1
, 20);
335 memcpy(result
, p
->item
->object
.sha1
, 20);
343 static int get_nth_ancestor(const char *name
, int len
,
344 unsigned char *result
, int generation
)
346 unsigned char sha1
[20];
347 int ret
= get_sha1_1(name
, len
, sha1
);
351 while (generation
--) {
352 struct commit
*commit
= lookup_commit_reference(sha1
);
354 if (!commit
|| parse_commit(commit
) || !commit
->parents
)
356 memcpy(sha1
, commit
->parents
->item
->object
.sha1
, 20);
358 memcpy(result
, sha1
, 20);
362 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
364 unsigned char outer
[20];
366 unsigned int expected_type
= 0;
370 * "ref^{type}" dereferences ref repeatedly until you cannot
371 * dereference anymore, or you get an object of given type,
372 * whichever comes first. "ref^{}" means just dereference
373 * tags until you get a non-tag. "ref^0" is a shorthand for
374 * "ref^{commit}". "commit^{tree}" could be used to find the
375 * top-level tree of the given commit.
377 if (len
< 4 || name
[len
-1] != '}')
380 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
382 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
388 sp
++; /* beginning of type name, or closing brace for empty */
389 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
390 expected_type
= OBJ_COMMIT
;
391 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
392 expected_type
= OBJ_TREE
;
393 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
394 expected_type
= OBJ_BLOB
;
395 else if (sp
[0] == '}')
396 expected_type
= OBJ_NONE
;
400 if (get_sha1_1(name
, sp
- name
- 2, outer
))
403 o
= parse_object(outer
);
406 if (!expected_type
) {
407 o
= deref_tag(o
, name
, sp
- name
- 2);
408 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
410 memcpy(sha1
, o
->sha1
, 20);
413 /* At this point, the syntax look correct, so
414 * if we do not get the needed object, we should
419 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
421 if (o
->type
== expected_type
) {
422 memcpy(sha1
, o
->sha1
, 20);
425 if (o
->type
== OBJ_TAG
)
426 o
= ((struct tag
*) o
)->tagged
;
427 else if (o
->type
== OBJ_COMMIT
)
428 o
= &(((struct commit
*) o
)->tree
->object
);
430 return error("%.*s: expected %s type, but the object dereferences to %s type",
431 len
, name
, typename(expected_type
),
434 parse_object(o
->sha1
);
440 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
445 /* "name~3" is "name^^^",
446 * "name~" and "name~0" are name -- not "name^0"!
447 * "name^" is not "name^0"; it is "name^1".
450 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
452 if ('0' <= ch
&& ch
<= '9')
454 if (ch
== '~' || ch
== '^')
461 int len1
= cp
- name
;
463 while (cp
< name
+ len
)
464 num
= num
* 10 + *cp
++ - '0';
465 if (has_suffix
== '^') {
466 if (!num
&& len1
== len
- 1)
468 return get_parent(name
, len1
, sha1
, num
);
470 /* else if (has_suffix == '~') -- goes without saying */
471 return get_nth_ancestor(name
, len1
, sha1
, num
);
474 ret
= peel_onion(name
, len
, sha1
);
478 ret
= get_sha1_basic(name
, len
, sha1
);
481 return get_short_sha1(name
, len
, sha1
, 0);
485 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
486 * notably "xyz^" for "parent of xyz"
488 int get_sha1(const char *name
, unsigned char *sha1
)
490 int ret
, bracket_depth
;
492 int namelen
= strlen(name
);
496 ret
= get_sha1_1(name
, namelen
, sha1
);
499 /* sha1:path --> object name of path in ent sha1
500 * :path -> object name of path in index
501 * :[0-3]:path -> object name of path in index at stage
503 if (name
[0] == ':') {
505 struct cache_entry
*ce
;
509 name
[1] < '0' || '3' < name
[1])
512 stage
= name
[1] - '0';
515 namelen
= namelen
- (cp
- name
);
520 pos
= cache_name_pos(cp
, namelen
);
523 while (pos
< active_nr
) {
524 ce
= active_cache
[pos
];
525 if (ce_namelen(ce
) != namelen
||
526 memcmp(ce
->name
, cp
, namelen
))
528 if (ce_stage(ce
) == stage
) {
529 memcpy(sha1
, ce
->sha1
, 20);
536 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
539 else if (bracket_depth
&& *cp
== '}')
541 else if (!bracket_depth
&& *cp
== ':')
545 unsigned char tree_sha1
[20];
546 if (!get_sha1_1(name
, cp
-name
, tree_sha1
))
547 return get_tree_entry(tree_sha1
, cp
+1, sha1
,