10 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
12 struct alternate_object_database
*alt
;
15 static struct alternate_object_database
*fakeent
;
18 const char *objdir
= get_object_directory();
19 int objdir_len
= strlen(objdir
);
20 int entlen
= objdir_len
+ 43;
21 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
22 memcpy(fakeent
->base
, objdir
, objdir_len
);
23 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
24 fakeent
->name
[-1] = '/';
26 fakeent
->next
= alt_odb_list
;
28 sprintf(hex
, "%.2s", name
);
29 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
32 sprintf(alt
->name
, "%.2s/", name
);
33 dir
= opendir(alt
->base
);
36 while ((de
= readdir(dir
)) != NULL
) {
37 if (strlen(de
->d_name
) != 38)
39 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
42 memcpy(hex
+ 2, de
->d_name
, 38);
45 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
53 return get_sha1_hex(hex
, sha1
) == 0;
57 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
72 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
75 const unsigned char *found_sha1
= NULL
;
79 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
) {
85 while (first
< last
) {
86 uint32_t mid
= (first
+ last
) / 2;
87 const unsigned char *now
;
90 now
= nth_packed_object_sha1(p
, mid
);
91 cmp
= hashcmp(match
, now
);
103 const unsigned char *now
, *next
;
104 now
= nth_packed_object_sha1(p
, first
);
105 if (match_sha(len
, match
, now
)) {
106 next
= nth_packed_object_sha1(p
, first
+1);
107 if (!next
|| !match_sha(len
, match
, next
)) {
108 /* unique within this pack */
113 else if (hashcmp(found_sha1
, now
)) {
119 /* not even unique within this pack */
127 hashcpy(sha1
, found_sha1
);
131 #define SHORT_NAME_NOT_FOUND (-1)
132 #define SHORT_NAME_AMBIGUOUS (-2)
134 static int find_unique_short_object(int len
, char *canonical
,
135 unsigned char *res
, unsigned char *sha1
)
137 int has_unpacked
, has_packed
;
138 unsigned char unpacked_sha1
[20], packed_sha1
[20];
141 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
142 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
143 if (!has_unpacked
&& !has_packed
)
144 return SHORT_NAME_NOT_FOUND
;
145 if (1 < has_unpacked
|| 1 < has_packed
)
146 return SHORT_NAME_AMBIGUOUS
;
147 if (has_unpacked
!= has_packed
) {
148 hashcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
));
151 /* Both have unique ones -- do they match? */
152 if (hashcmp(packed_sha1
, unpacked_sha1
))
153 return SHORT_NAME_AMBIGUOUS
;
154 hashcpy(sha1
, packed_sha1
);
158 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
163 unsigned char res
[20];
165 if (len
< MINIMUM_ABBREV
|| len
> 40)
168 memset(canonical
, 'x', 40);
169 for (i
= 0; i
< len
;i
++) {
170 unsigned char c
= name
[i
];
172 if (c
>= '0' && c
<= '9')
174 else if (c
>= 'a' && c
<= 'f')
176 else if (c
>= 'A' && c
<='F') {
188 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
189 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
190 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
194 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
199 exists
= has_sha1_file(sha1
);
200 memcpy(hex
, sha1_to_hex(sha1
), 40);
201 if (len
== 40 || !len
)
204 unsigned char sha1_ret
[20];
205 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
208 : status
== SHORT_NAME_NOT_FOUND
) {
217 static int ambiguous_path(const char *path
, int len
)
222 for (cnt
= 0; cnt
< len
; cnt
++) {
242 static inline int tracked_suffix(const char *string
, int len
)
244 const char *suffix
[] = { "@{upstream}", "@{u}" };
247 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
248 int suffix_len
= strlen(suffix
[i
]);
249 if (len
>= suffix_len
&& !memcmp(string
+ len
- suffix_len
,
250 suffix
[i
], suffix_len
))
257 * *string and *len will only be substituted, and *string returned (for
258 * later free()ing) if the string passed in is of the form @{-<n>} or
259 * of the form <branch>@{upstream}.
261 static char *substitute_branch_name(const char **string
, int *len
)
263 struct strbuf buf
= STRBUF_INIT
;
264 int ret
= interpret_branch_name(*string
, &buf
);
268 *string
= strbuf_detach(&buf
, &size
);
270 return (char *)*string
;
273 ret
= tracked_suffix(*string
, *len
);
275 char *ref
= xstrndup(*string
, *len
- ret
);
276 struct branch
*tracking
= branch_get(*ref
? ref
: NULL
);
279 die ("No tracking branch found for '%s'", ref
);
281 if (tracking
->merge
&& tracking
->merge
[0]->dst
) {
282 *string
= xstrdup(tracking
->merge
[0]->dst
);
283 *len
= strlen(*string
);
284 return (char *)*string
;
291 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
293 char *last_branch
= substitute_branch_name(&str
, &len
);
298 for (p
= ref_rev_parse_rules
; *p
; p
++) {
299 char fullref
[PATH_MAX
];
300 unsigned char sha1_from_ref
[20];
301 unsigned char *this_result
;
304 this_result
= refs_found
? sha1_from_ref
: sha1
;
305 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
306 r
= resolve_ref(fullref
, this_result
, 1, &flag
);
310 if (!warn_ambiguous_refs
)
312 } else if ((flag
& REF_ISSYMREF
) &&
313 (len
!= 4 || strcmp(str
, "HEAD")))
314 warning("ignoring dangling symref %s.", fullref
);
320 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
322 char *last_branch
= substitute_branch_name(&str
, &len
);
327 for (p
= ref_rev_parse_rules
; *p
; p
++) {
329 unsigned char hash
[20];
331 const char *ref
, *it
;
333 mksnpath(path
, sizeof(path
), *p
, len
, str
);
334 ref
= resolve_ref(path
, hash
, 1, NULL
);
337 if (!stat(git_path("logs/%s", path
), &st
) &&
340 else if (strcmp(ref
, path
) &&
341 !stat(git_path("logs/%s", ref
), &st
) &&
350 if (!warn_ambiguous_refs
)
357 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
359 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
361 static const char *warning
= "warning: refname '%.*s' is ambiguous.\n";
362 char *real_ref
= NULL
;
366 if (len
== 40 && !get_sha1_hex(str
, sha1
))
369 /* basic@{time or number or -number} format to query ref-log */
371 if (len
&& str
[len
-1] == '}') {
372 for (at
= len
-2; at
>= 0; at
--) {
373 if (str
[at
] == '@' && str
[at
+1] == '{') {
374 if (!tracked_suffix(str
+ at
, len
- at
)) {
375 reflog_len
= (len
-1) - (at
+2);
383 /* Accept only unambiguous ref paths. */
384 if (len
&& ambiguous_path(str
, len
))
387 if (!len
&& reflog_len
) {
388 struct strbuf buf
= STRBUF_INIT
;
390 /* try the @{-N} syntax for n-th checkout */
391 ret
= interpret_branch_name(str
+at
, &buf
);
393 /* substitute this branch name and restart */
394 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
395 } else if (ret
== 0) {
398 /* allow "@{...}" to mean the current branch reflog */
399 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
400 } else if (reflog_len
)
401 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
403 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
408 if (warn_ambiguous_refs
&& refs_found
> 1)
409 fprintf(stderr
, warning
, len
, str
);
413 unsigned long at_time
;
414 unsigned long co_time
;
417 /* Is it asking for N-th entry, or approxidate? */
418 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
419 char ch
= str
[at
+2+i
];
420 if ('0' <= ch
&& ch
<= '9')
421 nth
= nth
* 10 + ch
- '0';
425 if (100000000 <= nth
) {
431 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
432 at_time
= approxidate(tmp
);
435 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
436 &co_time
, &co_tz
, &co_cnt
)) {
439 "warning: Log for '%.*s' only goes "
440 "back to %s.\n", len
, str
,
441 show_date(co_time
, co_tz
, DATE_RFC2822
));
444 "warning: Log for '%.*s' only has "
445 "%d entries.\n", len
, str
, co_cnt
);
453 static int get_parent(const char *name
, int len
,
454 unsigned char *result
, int idx
)
456 unsigned char sha1
[20];
457 int ret
= get_sha1_1(name
, len
, sha1
);
458 struct commit
*commit
;
459 struct commit_list
*p
;
463 commit
= lookup_commit_reference(sha1
);
466 if (parse_commit(commit
))
469 hashcpy(result
, commit
->object
.sha1
);
475 hashcpy(result
, p
->item
->object
.sha1
);
483 static int get_nth_ancestor(const char *name
, int len
,
484 unsigned char *result
, int generation
)
486 unsigned char sha1
[20];
487 struct commit
*commit
;
490 ret
= get_sha1_1(name
, len
, sha1
);
493 commit
= lookup_commit_reference(sha1
);
497 while (generation
--) {
498 if (parse_commit(commit
) || !commit
->parents
)
500 commit
= commit
->parents
->item
;
502 hashcpy(result
, commit
->object
.sha1
);
506 struct object
*peel_to_type(const char *name
, int namelen
,
507 struct object
*o
, enum object_type expected_type
)
509 if (name
&& !namelen
)
510 namelen
= strlen(name
);
512 unsigned char sha1
[20];
513 if (get_sha1_1(name
, namelen
, sha1
))
515 o
= parse_object(sha1
);
518 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
520 if (o
->type
== expected_type
)
522 if (o
->type
== OBJ_TAG
)
523 o
= ((struct tag
*) o
)->tagged
;
524 else if (o
->type
== OBJ_COMMIT
)
525 o
= &(((struct commit
*) o
)->tree
->object
);
528 error("%.*s: expected %s type, but the object "
529 "dereferences to %s type",
530 namelen
, name
, typename(expected_type
),
537 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
539 unsigned char outer
[20];
541 unsigned int expected_type
= 0;
545 * "ref^{type}" dereferences ref repeatedly until you cannot
546 * dereference anymore, or you get an object of given type,
547 * whichever comes first. "ref^{}" means just dereference
548 * tags until you get a non-tag. "ref^0" is a shorthand for
549 * "ref^{commit}". "commit^{tree}" could be used to find the
550 * top-level tree of the given commit.
552 if (len
< 4 || name
[len
-1] != '}')
555 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
557 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
563 sp
++; /* beginning of type name, or closing brace for empty */
564 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
565 expected_type
= OBJ_COMMIT
;
566 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
567 expected_type
= OBJ_TREE
;
568 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
569 expected_type
= OBJ_BLOB
;
570 else if (sp
[0] == '}')
571 expected_type
= OBJ_NONE
;
575 if (get_sha1_1(name
, sp
- name
- 2, outer
))
578 o
= parse_object(outer
);
581 if (!expected_type
) {
582 o
= deref_tag(o
, name
, sp
- name
- 2);
583 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
585 hashcpy(sha1
, o
->sha1
);
589 * At this point, the syntax look correct, so
590 * if we do not get the needed object, we should
593 o
= peel_to_type(name
, len
, o
, expected_type
);
595 hashcpy(sha1
, o
->sha1
);
603 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
607 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
609 if (hexval(ch
) & ~0377) {
610 /* We must be looking at g in "SOMETHING-g"
611 * for it to be describe output.
613 if (ch
== 'g' && cp
[-1] == '-') {
616 return get_short_sha1(cp
, len
, sha1
, 1);
623 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
629 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
632 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
634 if ('0' <= ch
&& ch
<= '9')
636 if (ch
== '~' || ch
== '^')
643 int len1
= cp
- name
;
645 while (cp
< name
+ len
)
646 num
= num
* 10 + *cp
++ - '0';
647 if (!num
&& len1
== len
- 1)
649 if (has_suffix
== '^')
650 return get_parent(name
, len1
, sha1
, num
);
651 /* else if (has_suffix == '~') -- goes without saying */
652 return get_nth_ancestor(name
, len1
, sha1
, num
);
655 ret
= peel_onion(name
, len
, sha1
);
659 ret
= get_sha1_basic(name
, len
, sha1
);
663 /* It could be describe output that is "SOMETHING-gXXXX" */
664 ret
= get_describe_name(name
, len
, sha1
);
668 return get_short_sha1(name
, len
, sha1
, 0);
671 static int handle_one_ref(const char *path
,
672 const unsigned char *sha1
, int flag
, void *cb_data
)
674 struct commit_list
**list
= cb_data
;
675 struct object
*object
= parse_object(sha1
);
678 if (object
->type
== OBJ_TAG
) {
679 object
= deref_tag(object
, path
, strlen(path
));
683 if (object
->type
!= OBJ_COMMIT
)
685 insert_by_date((struct commit
*)object
, list
);
690 * This interprets names like ':/Initial revision of "git"' by searching
691 * through history and returning the first commit whose message starts
692 * with the given string.
694 * For future extension, ':/!' is reserved. If you want to match a message
695 * beginning with a '!', you have to repeat the exclamation mark.
698 #define ONELINE_SEEN (1u<<20)
699 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
)
701 struct commit_list
*list
= NULL
, *backup
= NULL
, *l
;
703 char *temp_commit_buffer
= NULL
;
705 if (prefix
[0] == '!') {
706 if (prefix
[1] != '!')
707 die ("Invalid search pattern: %s", prefix
);
710 for_each_ref(handle_one_ref
, &list
);
711 for (l
= list
; l
; l
= l
->next
)
712 commit_list_insert(l
->item
, &backup
);
715 struct commit
*commit
;
716 enum object_type type
;
719 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
720 if (!parse_object(commit
->object
.sha1
))
722 free(temp_commit_buffer
);
726 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
729 temp_commit_buffer
= p
;
731 if (!(p
= strstr(p
, "\n\n")))
733 if (!prefixcmp(p
+ 2, prefix
)) {
734 hashcpy(sha1
, commit
->object
.sha1
);
739 free(temp_commit_buffer
);
740 free_commit_list(list
);
741 for (l
= backup
; l
; l
= l
->next
)
742 clear_commit_marks(l
->item
, ONELINE_SEEN
);
746 struct grab_nth_branch_switch_cbdata
{
751 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
752 const char *email
, unsigned long timestamp
, int tz
,
753 const char *message
, void *cb_data
)
755 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
756 const char *match
= NULL
, *target
= NULL
;
760 if (!prefixcmp(message
, "checkout: moving from ")) {
761 match
= message
+ strlen("checkout: moving from ");
762 target
= strstr(match
, " to ");
765 if (!match
|| !target
)
768 len
= target
- match
;
769 nth
= cb
->cnt
++ % cb
->alloc
;
770 strbuf_reset(&cb
->buf
[nth
]);
771 strbuf_add(&cb
->buf
[nth
], match
, len
);
776 * This reads "@{-N}" syntax, finds the name of the Nth previous
777 * branch we were on, and places the name of the branch in the given
778 * buf and returns the number of characters parsed if successful.
780 * If the input is not of the accepted format, it returns a negative
781 * number to signal an error.
783 * If the input was ok but there are not N branch switches in the
784 * reflog, it returns 0.
786 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
790 struct grab_nth_branch_switch_cbdata cb
;
794 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
796 brace
= strchr(name
, '}');
799 nth
= strtol(name
+3, &num_end
, 10);
800 if (num_end
!= brace
)
805 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
806 for (i
= 0; i
< nth
; i
++)
807 strbuf_init(&cb
.buf
[i
], 20);
810 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
813 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
819 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
820 retval
= brace
-name
+1;
823 for (i
= 0; i
< nth
; i
++)
824 strbuf_release(&cb
.buf
[i
]);
831 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
832 * notably "xyz^" for "parent of xyz"
834 int get_sha1(const char *name
, unsigned char *sha1
)
837 return get_sha1_with_mode(name
, sha1
, &unused
);
840 int get_sha1_with_mode(const char *name
, unsigned char *sha1
, unsigned *mode
)
842 int ret
, bracket_depth
;
843 int namelen
= strlen(name
);
847 ret
= get_sha1_1(name
, namelen
, sha1
);
850 /* sha1:path --> object name of path in ent sha1
851 * :path -> object name of path in index
852 * :[0-3]:path -> object name of path in index at stage
854 if (name
[0] == ':') {
856 struct cache_entry
*ce
;
858 if (namelen
> 2 && name
[1] == '/')
859 return get_sha1_oneline(name
+ 2, sha1
);
862 name
[1] < '0' || '3' < name
[1])
865 stage
= name
[1] - '0';
868 namelen
= namelen
- (cp
- name
);
871 pos
= cache_name_pos(cp
, namelen
);
874 while (pos
< active_nr
) {
875 ce
= active_cache
[pos
];
876 if (ce_namelen(ce
) != namelen
||
877 memcmp(ce
->name
, cp
, namelen
))
879 if (ce_stage(ce
) == stage
) {
880 hashcpy(sha1
, ce
->sha1
);
888 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
891 else if (bracket_depth
&& *cp
== '}')
893 else if (!bracket_depth
&& *cp
== ':')
897 unsigned char tree_sha1
[20];
898 if (!get_sha1_1(name
, cp
-name
, tree_sha1
))
899 return get_tree_entry(tree_sha1
, cp
+1, sha1
,