10 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list
*);
12 typedef int (*disambiguate_hint_fn
)(const unsigned char *, void *);
14 struct disambiguate_state
{
15 disambiguate_hint_fn fn
;
17 unsigned char candidate
[20];
18 unsigned candidate_exists
:1;
19 unsigned candidate_checked
:1;
20 unsigned candidate_ok
:1;
21 unsigned disambiguate_fn_used
:1;
25 static void update_candidates(struct disambiguate_state
*ds
, const unsigned char *current
)
27 if (!ds
->candidate_exists
) {
28 /* this is the first candidate */
29 hashcpy(ds
->candidate
, current
);
30 ds
->candidate_exists
= 1;
32 } else if (!hashcmp(ds
->candidate
, current
)) {
33 /* the same as what we already have seen */
38 /* cannot disambiguate between ds->candidate and current */
43 if (!ds
->candidate_checked
) {
44 ds
->candidate_ok
= ds
->fn(ds
->candidate
, ds
->cb_data
);
45 ds
->disambiguate_fn_used
= 1;
46 ds
->candidate_checked
= 1;
49 if (!ds
->candidate_ok
) {
50 /* discard the candidate; we know it does not satisify fn */
51 hashcpy(ds
->candidate
, current
);
52 ds
->candidate_checked
= 0;
56 /* if we reach this point, we know ds->candidate satisfies fn */
57 if (ds
->fn(current
, ds
->cb_data
)) {
59 * if both current and candidate satisfy fn, we cannot
66 /* otherwise, current can be discarded and candidate is still good */
69 static void find_short_object_filename(int len
, const char *hex_pfx
, struct disambiguate_state
*ds
)
71 struct alternate_object_database
*alt
;
73 static struct alternate_object_database
*fakeent
;
77 * Create a "fake" alternate object database that
78 * points to our own object database, to make it
79 * easier to get a temporary working space in
80 * alt->name/alt->base while iterating over the
81 * object databases including our own.
83 const char *objdir
= get_object_directory();
84 int objdir_len
= strlen(objdir
);
85 int entlen
= objdir_len
+ 43;
86 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
87 memcpy(fakeent
->base
, objdir
, objdir_len
);
88 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
89 fakeent
->name
[-1] = '/';
91 fakeent
->next
= alt_odb_list
;
93 sprintf(hex
, "%.2s", hex_pfx
);
94 for (alt
= fakeent
; alt
&& !ds
->ambiguous
; alt
= alt
->next
) {
97 sprintf(alt
->name
, "%.2s/", hex_pfx
);
98 dir
= opendir(alt
->base
);
102 while (!ds
->ambiguous
&& (de
= readdir(dir
)) != NULL
) {
103 unsigned char sha1
[20];
105 if (strlen(de
->d_name
) != 38)
107 if (memcmp(de
->d_name
, hex_pfx
+ 2, len
- 2))
109 memcpy(hex
+ 2, de
->d_name
, 38);
110 if (!get_sha1_hex(hex
, sha1
))
111 update_candidates(ds
, sha1
);
117 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
127 if ((*a
^ *b
) & 0xf0)
132 static void unique_in_pack(int len
,
133 const unsigned char *bin_pfx
,
134 struct packed_git
*p
,
135 struct disambiguate_state
*ds
)
137 uint32_t num
, last
, i
, first
= 0;
138 const unsigned char *current
= NULL
;
141 num
= p
->num_objects
;
143 while (first
< last
) {
144 uint32_t mid
= (first
+ last
) / 2;
145 const unsigned char *current
;
148 current
= nth_packed_object_sha1(p
, mid
);
149 cmp
= hashcmp(bin_pfx
, current
);
162 * At this point, "first" is the location of the lowest object
163 * with an object name that could match "bin_pfx". See if we have
164 * 0, 1 or more objects that actually match(es).
166 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
167 current
= nth_packed_object_sha1(p
, i
);
168 if (!match_sha(len
, bin_pfx
, current
))
170 update_candidates(ds
, current
);
174 static void find_short_packed_object(int len
, const unsigned char *bin_pfx
,
175 struct disambiguate_state
*ds
)
177 struct packed_git
*p
;
179 prepare_packed_git();
180 for (p
= packed_git
; p
&& !ds
->ambiguous
; p
= p
->next
)
181 unique_in_pack(len
, bin_pfx
, p
, ds
);
184 #define SHORT_NAME_NOT_FOUND (-1)
185 #define SHORT_NAME_AMBIGUOUS (-2)
187 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
191 return SHORT_NAME_AMBIGUOUS
;
193 if (!ds
->candidate_exists
)
194 return SHORT_NAME_NOT_FOUND
;
196 if (!ds
->candidate_checked
)
198 * If this is the only candidate, there is no point
199 * calling the disambiguation hint callback.
201 * On the other hand, if the current candidate
202 * replaced an earlier candidate that did _not_ pass
203 * the disambiguation hint callback, then we do have
204 * more than one objects that match the short name
205 * given, so we should make sure this one matches;
206 * otherwise, if we discovered this one and the one
207 * that we previously discarded in the reverse order,
208 * we would end up showing different results in the
211 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
212 ds
->fn(ds
->candidate
, ds
->cb_data
));
214 if (!ds
->candidate_ok
)
215 return SHORT_NAME_AMBIGUOUS
;
217 hashcpy(sha1
, ds
->candidate
);
221 static int disambiguate_commit_only(const unsigned char *sha1
, void *cb_data_unused
)
223 int kind
= sha1_object_info(sha1
, NULL
);
224 return kind
== OBJ_COMMIT
;
227 static int disambiguate_committish_only(const unsigned char *sha1
, void *cb_data_unused
)
232 kind
= sha1_object_info(sha1
, NULL
);
233 if (kind
== OBJ_COMMIT
)
238 /* We need to do this the hard way... */
239 obj
= deref_tag(lookup_object(sha1
), NULL
, 0);
240 if (obj
&& obj
->type
== OBJ_COMMIT
)
245 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
250 unsigned char bin_pfx
[20];
251 struct disambiguate_state ds
;
252 int quietly
= !!(flags
& GET_SHA1_QUIETLY
);
254 if (len
< MINIMUM_ABBREV
|| len
> 40)
257 memset(hex_pfx
, 'x', 40);
258 for (i
= 0; i
< len
;i
++) {
259 unsigned char c
= name
[i
];
261 if (c
>= '0' && c
<= '9')
263 else if (c
>= 'a' && c
<= 'f')
265 else if (c
>= 'A' && c
<='F') {
274 bin_pfx
[i
>> 1] |= val
;
279 memset(&ds
, 0, sizeof(ds
));
280 if (flags
& GET_SHA1_COMMIT
)
281 ds
.fn
= disambiguate_commit_only
;
282 else if (flags
& GET_SHA1_COMMITTISH
)
283 ds
.fn
= disambiguate_committish_only
;
285 find_short_object_filename(len
, hex_pfx
, &ds
);
286 find_short_packed_object(len
, bin_pfx
, &ds
);
287 status
= finish_object_disambiguation(&ds
, sha1
);
289 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
290 return error("short SHA1 %.*s is ambiguous.", len
, hex_pfx
);
294 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
299 exists
= has_sha1_file(sha1
);
300 memcpy(hex
, sha1_to_hex(sha1
), 40);
301 if (len
== 40 || !len
)
304 unsigned char sha1_ret
[20];
305 status
= get_short_sha1(hex
, len
, sha1_ret
, GET_SHA1_QUIETLY
);
308 : status
== SHORT_NAME_NOT_FOUND
) {
317 static int ambiguous_path(const char *path
, int len
)
322 for (cnt
= 0; cnt
< len
; cnt
++) {
342 static inline int upstream_mark(const char *string
, int len
)
344 const char *suffix
[] = { "@{upstream}", "@{u}" };
347 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
348 int suffix_len
= strlen(suffix
[i
]);
349 if (suffix_len
<= len
350 && !memcmp(string
, suffix
[i
], suffix_len
))
356 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
, unsigned lookup_flags
);
358 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
360 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
361 char *real_ref
= NULL
;
365 if (len
== 40 && !get_sha1_hex(str
, sha1
))
368 /* basic@{time or number or -number} format to query ref-log */
370 if (len
&& str
[len
-1] == '}') {
371 for (at
= len
-2; at
>= 0; at
--) {
372 if (str
[at
] == '@' && str
[at
+1] == '{') {
373 if (!upstream_mark(str
+ at
, len
- at
)) {
374 reflog_len
= (len
-1) - (at
+2);
382 /* Accept only unambiguous ref paths. */
383 if (len
&& ambiguous_path(str
, len
))
386 if (!len
&& reflog_len
) {
387 struct strbuf buf
= STRBUF_INIT
;
389 /* try the @{-N} syntax for n-th checkout */
390 ret
= interpret_branch_name(str
+at
, &buf
);
392 /* substitute this branch name and restart */
393 return get_sha1_1(buf
.buf
, buf
.len
, sha1
, 0);
394 } else if (ret
== 0) {
397 /* allow "@{...}" to mean the current branch reflog */
398 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
399 } else if (reflog_len
)
400 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
402 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
407 if (warn_ambiguous_refs
&& refs_found
> 1)
408 warning(warn_msg
, len
, str
);
412 unsigned long at_time
;
413 unsigned long co_time
;
416 /* a @{-N} placed anywhere except the start is an error */
417 if (str
[at
+2] == '-')
420 /* Is it asking for N-th entry, or approxidate? */
421 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
422 char ch
= str
[at
+2+i
];
423 if ('0' <= ch
&& ch
<= '9')
424 nth
= nth
* 10 + ch
- '0';
428 if (100000000 <= nth
) {
435 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
436 at_time
= approxidate_careful(tmp
, &errors
);
441 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
442 &co_time
, &co_tz
, &co_cnt
)) {
444 warning("Log for '%.*s' only goes "
445 "back to %s.", len
, str
,
446 show_date(co_time
, co_tz
, DATE_RFC2822
));
449 die("Log for '%.*s' only has %d entries.",
459 static int get_parent(const char *name
, int len
,
460 unsigned char *result
, int idx
)
462 unsigned char sha1
[20];
463 int ret
= get_sha1_1(name
, len
, sha1
, GET_SHA1_COMMITTISH
);
464 struct commit
*commit
;
465 struct commit_list
*p
;
469 commit
= lookup_commit_reference(sha1
);
472 if (parse_commit(commit
))
475 hashcpy(result
, commit
->object
.sha1
);
481 hashcpy(result
, p
->item
->object
.sha1
);
489 static int get_nth_ancestor(const char *name
, int len
,
490 unsigned char *result
, int generation
)
492 unsigned char sha1
[20];
493 struct commit
*commit
;
496 ret
= get_sha1_1(name
, len
, sha1
, GET_SHA1_COMMITTISH
);
499 commit
= lookup_commit_reference(sha1
);
503 while (generation
--) {
504 if (parse_commit(commit
) || !commit
->parents
)
506 commit
= commit
->parents
->item
;
508 hashcpy(result
, commit
->object
.sha1
);
512 struct object
*peel_to_type(const char *name
, int namelen
,
513 struct object
*o
, enum object_type expected_type
)
515 if (name
&& !namelen
)
516 namelen
= strlen(name
);
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;
542 unsigned lookup_flags
= 0;
546 * "ref^{type}" dereferences ref repeatedly until you cannot
547 * dereference anymore, or you get an object of given type,
548 * whichever comes first. "ref^{}" means just dereference
549 * tags until you get a non-tag. "ref^0" is a shorthand for
550 * "ref^{commit}". "commit^{tree}" could be used to find the
551 * top-level tree of the given commit.
553 if (len
< 4 || name
[len
-1] != '}')
556 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
558 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
564 sp
++; /* beginning of type name, or closing brace for empty */
565 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
566 expected_type
= OBJ_COMMIT
;
567 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
568 expected_type
= OBJ_TREE
;
569 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
570 expected_type
= OBJ_BLOB
;
571 else if (sp
[0] == '}')
572 expected_type
= OBJ_NONE
;
573 else if (sp
[0] == '/')
574 expected_type
= OBJ_COMMIT
;
578 if (expected_type
== OBJ_COMMIT
)
579 lookup_flags
= GET_SHA1_COMMITTISH
;
581 if (get_sha1_1(name
, sp
- name
- 2, outer
, lookup_flags
))
584 o
= parse_object(outer
);
587 if (!expected_type
) {
588 o
= deref_tag(o
, name
, sp
- name
- 2);
589 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
591 hashcpy(sha1
, o
->sha1
);
596 * At this point, the syntax look correct, so
597 * if we do not get the needed object, we should
600 o
= peel_to_type(name
, len
, o
, expected_type
);
604 hashcpy(sha1
, o
->sha1
);
606 /* "$commit^{/foo}" */
609 struct commit_list
*list
= NULL
;
612 * $commit^{/}. Some regex implementation may reject.
613 * We don't need regex anyway. '' pattern always matches.
618 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
619 commit_list_insert((struct commit
*)o
, &list
);
620 ret
= get_sha1_oneline(prefix
, sha1
, list
);
627 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
630 unsigned flags
= GET_SHA1_QUIETLY
| GET_SHA1_COMMIT
;
632 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
634 if (hexval(ch
) & ~0377) {
635 /* We must be looking at g in "SOMETHING-g"
636 * for it to be describe output.
638 if (ch
== 'g' && cp
[-1] == '-') {
641 return get_short_sha1(cp
, len
, sha1
, flags
);
648 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
, unsigned lookup_flags
)
654 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
657 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
659 if ('0' <= ch
&& ch
<= '9')
661 if (ch
== '~' || ch
== '^')
668 int len1
= cp
- name
;
670 while (cp
< name
+ len
)
671 num
= num
* 10 + *cp
++ - '0';
672 if (!num
&& len1
== len
- 1)
674 if (has_suffix
== '^')
675 return get_parent(name
, len1
, sha1
, num
);
676 /* else if (has_suffix == '~') -- goes without saying */
677 return get_nth_ancestor(name
, len1
, sha1
, num
);
680 ret
= peel_onion(name
, len
, sha1
);
684 ret
= get_sha1_basic(name
, len
, sha1
);
688 /* It could be describe output that is "SOMETHING-gXXXX" */
689 ret
= get_describe_name(name
, len
, sha1
);
693 return get_short_sha1(name
, len
, sha1
, lookup_flags
);
697 * This interprets names like ':/Initial revision of "git"' by searching
698 * through history and returning the first commit whose message starts
699 * the given regular expression.
701 * For future extension, ':/!' is reserved. If you want to match a message
702 * beginning with a '!', you have to repeat the exclamation mark.
704 #define ONELINE_SEEN (1u<<20)
706 static int handle_one_ref(const char *path
,
707 const unsigned char *sha1
, int flag
, void *cb_data
)
709 struct commit_list
**list
= cb_data
;
710 struct object
*object
= parse_object(sha1
);
713 if (object
->type
== OBJ_TAG
) {
714 object
= deref_tag(object
, path
, strlen(path
));
718 if (object
->type
!= OBJ_COMMIT
)
720 commit_list_insert_by_date((struct commit
*)object
, list
);
724 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
725 struct commit_list
*list
)
727 struct commit_list
*backup
= NULL
, *l
;
731 if (prefix
[0] == '!') {
732 if (prefix
[1] != '!')
733 die ("Invalid search pattern: %s", prefix
);
737 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
738 die("Invalid search pattern: %s", prefix
);
740 for (l
= list
; l
; l
= l
->next
) {
741 l
->item
->object
.flags
|= ONELINE_SEEN
;
742 commit_list_insert(l
->item
, &backup
);
745 char *p
, *to_free
= NULL
;
746 struct commit
*commit
;
747 enum object_type type
;
751 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
752 if (!parse_object(commit
->object
.sha1
))
757 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
763 p
= strstr(p
, "\n\n");
764 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
768 hashcpy(sha1
, commit
->object
.sha1
);
774 free_commit_list(list
);
775 for (l
= backup
; l
; l
= l
->next
)
776 clear_commit_marks(l
->item
, ONELINE_SEEN
);
777 free_commit_list(backup
);
778 return found
? 0 : -1;
781 struct grab_nth_branch_switch_cbdata
{
786 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
787 const char *email
, unsigned long timestamp
, int tz
,
788 const char *message
, void *cb_data
)
790 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
791 const char *match
= NULL
, *target
= NULL
;
795 if (!prefixcmp(message
, "checkout: moving from ")) {
796 match
= message
+ strlen("checkout: moving from ");
797 target
= strstr(match
, " to ");
800 if (!match
|| !target
)
803 len
= target
- match
;
804 nth
= cb
->cnt
++ % cb
->alloc
;
805 strbuf_reset(&cb
->buf
[nth
]);
806 strbuf_add(&cb
->buf
[nth
], match
, len
);
811 * Parse @{-N} syntax, return the number of characters parsed
812 * if successful; otherwise signal an error with negative value.
814 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
818 struct grab_nth_branch_switch_cbdata cb
;
822 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
824 brace
= strchr(name
, '}');
827 nth
= strtol(name
+3, &num_end
, 10);
828 if (num_end
!= brace
)
833 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
834 for (i
= 0; i
< nth
; i
++)
835 strbuf_init(&cb
.buf
[i
], 20);
838 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
841 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
847 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
848 retval
= brace
-name
+1;
851 for (i
= 0; i
< nth
; i
++)
852 strbuf_release(&cb
.buf
[i
]);
858 int get_sha1_mb(const char *name
, unsigned char *sha1
)
860 struct commit
*one
, *two
;
861 struct commit_list
*mbs
;
862 unsigned char sha1_tmp
[20];
866 dots
= strstr(name
, "...");
868 return get_sha1(name
, sha1
);
870 st
= get_sha1("HEAD", sha1_tmp
);
873 strbuf_init(&sb
, dots
- name
);
874 strbuf_add(&sb
, name
, dots
- name
);
875 st
= get_sha1(sb
.buf
, sha1_tmp
);
880 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
884 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
886 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
889 mbs
= get_merge_bases(one
, two
, 1);
890 if (!mbs
|| mbs
->next
)
894 hashcpy(sha1
, mbs
->item
->object
.sha1
);
896 free_commit_list(mbs
);
901 * This reads short-hand syntax that not only evaluates to a commit
902 * object name, but also can act as if the end user spelled the name
903 * of the branch from the command line.
905 * - "@{-N}" finds the name of the Nth previous branch we were on, and
906 * places the name of the branch in the given buf and returns the
907 * number of characters parsed if successful.
909 * - "<branch>@{upstream}" finds the name of the other ref that
910 * <branch> is configured to merge with (missing <branch> defaults
911 * to the current branch), and places the name of the branch in the
912 * given buf and returns the number of characters parsed if
915 * If the input is not of the accepted format, it returns a negative
916 * number to signal an error.
918 * If the input was ok but there are not N branch switches in the
919 * reflog, it returns 0.
921 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
924 struct branch
*upstream
;
925 int namelen
= strlen(name
);
926 int len
= interpret_nth_prior_checkout(name
, buf
);
930 return len
; /* syntax Ok, not enough switches */
931 if (0 < len
&& len
== namelen
)
932 return len
; /* consumed all */
934 /* we have extra data, which might need further processing */
935 struct strbuf tmp
= STRBUF_INIT
;
939 strbuf_add(buf
, name
+ len
, namelen
- len
);
940 ret
= interpret_branch_name(buf
->buf
, &tmp
);
941 /* that data was not interpreted, remove our cruft */
943 strbuf_setlen(buf
, used
);
947 strbuf_addbuf(buf
, &tmp
);
948 strbuf_release(&tmp
);
949 /* tweak for size of {-N} versus expanded ref name */
950 return ret
- used
+ len
;
953 cp
= strchr(name
, '@');
956 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
959 len
= cp
+ tmp_len
- name
;
960 cp
= xstrndup(name
, cp
- name
);
961 upstream
= branch_get(*cp
? cp
: NULL
);
964 || !upstream
->merge
[0]->dst
)
965 return error("No upstream branch found for '%s'", cp
);
967 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
969 strbuf_addstr(buf
, cp
);
974 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
976 int len
= strlen(name
);
977 if (interpret_branch_name(name
, sb
) == len
)
979 strbuf_add(sb
, name
, len
);
983 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
985 strbuf_branchname(sb
, name
);
988 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
989 return check_refname_format(sb
->buf
, 0);
993 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
994 * notably "xyz^" for "parent of xyz"
996 int get_sha1(const char *name
, unsigned char *sha1
)
998 struct object_context unused
;
999 return get_sha1_with_context(name
, 0, sha1
, &unused
);
1002 /* Must be called only when object_name:filename doesn't exist. */
1003 static void diagnose_invalid_sha1_path(const char *prefix
,
1004 const char *filename
,
1005 const unsigned char *tree_sha1
,
1006 const char *object_name
)
1009 unsigned char sha1
[20];
1015 if (!lstat(filename
, &st
))
1016 die("Path '%s' exists on disk, but not in '%s'.",
1017 filename
, object_name
);
1018 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
1019 char *fullname
= xmalloc(strlen(filename
)
1020 + strlen(prefix
) + 1);
1021 strcpy(fullname
, prefix
);
1022 strcat(fullname
, filename
);
1024 if (!get_tree_entry(tree_sha1
, fullname
,
1026 die("Path '%s' exists, but not '%s'.\n"
1027 "Did you mean '%s:%s' aka '%s:./%s'?",
1035 die("Path '%s' does not exist in '%s'",
1036 filename
, object_name
);
1040 /* Must be called only when :stage:filename doesn't exist. */
1041 static void diagnose_invalid_index_path(int stage
,
1043 const char *filename
)
1046 struct cache_entry
*ce
;
1048 unsigned namelen
= strlen(filename
);
1049 unsigned fullnamelen
;
1055 /* Wrong stage number? */
1056 pos
= cache_name_pos(filename
, namelen
);
1059 if (pos
< active_nr
) {
1060 ce
= active_cache
[pos
];
1061 if (ce_namelen(ce
) == namelen
&&
1062 !memcmp(ce
->name
, filename
, namelen
))
1063 die("Path '%s' is in the index, but not at stage %d.\n"
1064 "Did you mean ':%d:%s'?",
1066 ce_stage(ce
), filename
);
1069 /* Confusion between relative and absolute filenames? */
1070 fullnamelen
= namelen
+ strlen(prefix
);
1071 fullname
= xmalloc(fullnamelen
+ 1);
1072 strcpy(fullname
, prefix
);
1073 strcat(fullname
, filename
);
1074 pos
= cache_name_pos(fullname
, fullnamelen
);
1077 if (pos
< active_nr
) {
1078 ce
= active_cache
[pos
];
1079 if (ce_namelen(ce
) == fullnamelen
&&
1080 !memcmp(ce
->name
, fullname
, fullnamelen
))
1081 die("Path '%s' is in the index, but not '%s'.\n"
1082 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1084 ce_stage(ce
), fullname
,
1085 ce_stage(ce
), filename
);
1088 if (!lstat(filename
, &st
))
1089 die("Path '%s' exists on disk, but not in the index.", filename
);
1090 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1091 die("Path '%s' does not exist (neither on disk nor in the index).",
1098 static char *resolve_relative_path(const char *rel
)
1100 if (prefixcmp(rel
, "./") && prefixcmp(rel
, "../"))
1104 die("BUG: startup_info struct is not initialized.");
1106 if (!is_inside_work_tree())
1107 die("relative path syntax can't be used outside working tree.");
1109 /* die() inside prefix_path() if resolved path is outside worktree */
1110 return prefix_path(startup_info
->prefix
,
1111 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1115 static int get_sha1_with_context_1(const char *name
,
1118 unsigned char *sha1
,
1119 struct object_context
*oc
)
1121 int ret
, bracket_depth
;
1122 int namelen
= strlen(name
);
1124 int only_to_die
= flags
& GET_SHA1_ONLY_TO_DIE
;
1126 memset(oc
, 0, sizeof(*oc
));
1127 oc
->mode
= S_IFINVALID
;
1128 ret
= get_sha1_1(name
, namelen
, sha1
, flags
);
1132 * sha1:path --> object name of path in ent sha1
1133 * :path -> object name of absolute path in index
1134 * :./path -> object name of path relative to cwd in index
1135 * :[0-3]:path -> object name of path in index at stage
1136 * :/foo -> recent commit matching foo
1138 if (name
[0] == ':') {
1140 struct cache_entry
*ce
;
1141 char *new_path
= NULL
;
1143 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1144 struct commit_list
*list
= NULL
;
1145 for_each_ref(handle_one_ref
, &list
);
1146 return get_sha1_oneline(name
+ 2, sha1
, list
);
1150 name
[1] < '0' || '3' < name
[1])
1153 stage
= name
[1] - '0';
1156 new_path
= resolve_relative_path(cp
);
1158 namelen
= namelen
- (cp
- name
);
1161 namelen
= strlen(cp
);
1164 strncpy(oc
->path
, cp
,
1166 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1170 pos
= cache_name_pos(cp
, namelen
);
1173 while (pos
< active_nr
) {
1174 ce
= active_cache
[pos
];
1175 if (ce_namelen(ce
) != namelen
||
1176 memcmp(ce
->name
, cp
, namelen
))
1178 if (ce_stage(ce
) == stage
) {
1179 hashcpy(sha1
, ce
->sha1
);
1180 oc
->mode
= ce
->ce_mode
;
1186 if (only_to_die
&& name
[1] && name
[1] != '/')
1187 diagnose_invalid_index_path(stage
, prefix
, cp
);
1191 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1194 else if (bracket_depth
&& *cp
== '}')
1196 else if (!bracket_depth
&& *cp
== ':')
1200 unsigned char tree_sha1
[20];
1201 char *object_name
= NULL
;
1203 object_name
= xmalloc(cp
-name
+1);
1204 strncpy(object_name
, name
, cp
-name
);
1205 object_name
[cp
-name
] = '\0';
1207 if (!get_sha1_1(name
, cp
-name
, tree_sha1
, 0)) {
1208 const char *filename
= cp
+1;
1209 char *new_filename
= NULL
;
1211 new_filename
= resolve_relative_path(filename
);
1213 filename
= new_filename
;
1214 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1216 diagnose_invalid_sha1_path(prefix
, filename
,
1217 tree_sha1
, object_name
);
1220 hashcpy(oc
->tree
, tree_sha1
);
1221 strncpy(oc
->path
, filename
,
1223 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1229 die("Invalid object name '%s'.", object_name
);
1236 * Call this function when you know "name" given by the end user must
1237 * name an object but it doesn't; the function _may_ die with a better
1238 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1239 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1240 * you have a chance to diagnose the error further.
1242 void maybe_die_on_misspelt_object_name(const char *name
, const char *prefix
)
1244 struct object_context oc
;
1245 unsigned char sha1
[20];
1246 get_sha1_with_context_1(name
, GET_SHA1_ONLY_TO_DIE
, prefix
, sha1
, &oc
);
1249 int get_sha1_with_context(const char *str
, unsigned flags
, unsigned char *sha1
, struct object_context
*orc
)
1251 return get_sha1_with_context_1(str
, flags
, NULL
, sha1
, orc
);