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 get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
226 unsigned char bin_pfx
[20];
227 struct disambiguate_state ds
;
229 if (len
< MINIMUM_ABBREV
|| len
> 40)
232 memset(hex_pfx
, 'x', 40);
233 for (i
= 0; i
< len
;i
++) {
234 unsigned char c
= name
[i
];
236 if (c
>= '0' && c
<= '9')
238 else if (c
>= 'a' && c
<= 'f')
240 else if (c
>= 'A' && c
<='F') {
249 bin_pfx
[i
>> 1] |= val
;
254 memset(&ds
, 0, sizeof(ds
));
255 find_short_object_filename(len
, hex_pfx
, &ds
);
256 find_short_packed_object(len
, bin_pfx
, &ds
);
257 status
= finish_object_disambiguation(&ds
, sha1
);
259 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
260 return error("short SHA1 %.*s is ambiguous.", len
, hex_pfx
);
264 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
269 exists
= has_sha1_file(sha1
);
270 memcpy(hex
, sha1_to_hex(sha1
), 40);
271 if (len
== 40 || !len
)
274 unsigned char sha1_ret
[20];
275 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
278 : status
== SHORT_NAME_NOT_FOUND
) {
287 static int ambiguous_path(const char *path
, int len
)
292 for (cnt
= 0; cnt
< len
; cnt
++) {
312 static inline int upstream_mark(const char *string
, int len
)
314 const char *suffix
[] = { "@{upstream}", "@{u}" };
317 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
318 int suffix_len
= strlen(suffix
[i
]);
319 if (suffix_len
<= len
320 && !memcmp(string
, suffix
[i
], suffix_len
))
326 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
328 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
330 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
331 char *real_ref
= NULL
;
335 if (len
== 40 && !get_sha1_hex(str
, sha1
))
338 /* basic@{time or number or -number} format to query ref-log */
340 if (len
&& str
[len
-1] == '}') {
341 for (at
= len
-2; at
>= 0; at
--) {
342 if (str
[at
] == '@' && str
[at
+1] == '{') {
343 if (!upstream_mark(str
+ at
, len
- at
)) {
344 reflog_len
= (len
-1) - (at
+2);
352 /* Accept only unambiguous ref paths. */
353 if (len
&& ambiguous_path(str
, len
))
356 if (!len
&& reflog_len
) {
357 struct strbuf buf
= STRBUF_INIT
;
359 /* try the @{-N} syntax for n-th checkout */
360 ret
= interpret_branch_name(str
+at
, &buf
);
362 /* substitute this branch name and restart */
363 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
364 } else if (ret
== 0) {
367 /* allow "@{...}" to mean the current branch reflog */
368 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
369 } else if (reflog_len
)
370 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
372 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
377 if (warn_ambiguous_refs
&& refs_found
> 1)
378 warning(warn_msg
, len
, str
);
382 unsigned long at_time
;
383 unsigned long co_time
;
386 /* a @{-N} placed anywhere except the start is an error */
387 if (str
[at
+2] == '-')
390 /* Is it asking for N-th entry, or approxidate? */
391 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
392 char ch
= str
[at
+2+i
];
393 if ('0' <= ch
&& ch
<= '9')
394 nth
= nth
* 10 + ch
- '0';
398 if (100000000 <= nth
) {
405 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
406 at_time
= approxidate_careful(tmp
, &errors
);
411 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
412 &co_time
, &co_tz
, &co_cnt
)) {
414 warning("Log for '%.*s' only goes "
415 "back to %s.", len
, str
,
416 show_date(co_time
, co_tz
, DATE_RFC2822
));
419 die("Log for '%.*s' only has %d entries.",
429 static int get_parent(const char *name
, int len
,
430 unsigned char *result
, int idx
)
432 unsigned char sha1
[20];
433 int ret
= get_sha1_1(name
, len
, sha1
);
434 struct commit
*commit
;
435 struct commit_list
*p
;
439 commit
= lookup_commit_reference(sha1
);
442 if (parse_commit(commit
))
445 hashcpy(result
, commit
->object
.sha1
);
451 hashcpy(result
, p
->item
->object
.sha1
);
459 static int get_nth_ancestor(const char *name
, int len
,
460 unsigned char *result
, int generation
)
462 unsigned char sha1
[20];
463 struct commit
*commit
;
466 ret
= get_sha1_1(name
, len
, sha1
);
469 commit
= lookup_commit_reference(sha1
);
473 while (generation
--) {
474 if (parse_commit(commit
) || !commit
->parents
)
476 commit
= commit
->parents
->item
;
478 hashcpy(result
, commit
->object
.sha1
);
482 struct object
*peel_to_type(const char *name
, int namelen
,
483 struct object
*o
, enum object_type expected_type
)
485 if (name
&& !namelen
)
486 namelen
= strlen(name
);
488 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
490 if (o
->type
== expected_type
)
492 if (o
->type
== OBJ_TAG
)
493 o
= ((struct tag
*) o
)->tagged
;
494 else if (o
->type
== OBJ_COMMIT
)
495 o
= &(((struct commit
*) o
)->tree
->object
);
498 error("%.*s: expected %s type, but the object "
499 "dereferences to %s type",
500 namelen
, name
, typename(expected_type
),
507 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
509 unsigned char outer
[20];
511 unsigned int expected_type
= 0;
515 * "ref^{type}" dereferences ref repeatedly until you cannot
516 * dereference anymore, or you get an object of given type,
517 * whichever comes first. "ref^{}" means just dereference
518 * tags until you get a non-tag. "ref^0" is a shorthand for
519 * "ref^{commit}". "commit^{tree}" could be used to find the
520 * top-level tree of the given commit.
522 if (len
< 4 || name
[len
-1] != '}')
525 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
527 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
533 sp
++; /* beginning of type name, or closing brace for empty */
534 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
535 expected_type
= OBJ_COMMIT
;
536 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
537 expected_type
= OBJ_TREE
;
538 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
539 expected_type
= OBJ_BLOB
;
540 else if (sp
[0] == '}')
541 expected_type
= OBJ_NONE
;
542 else if (sp
[0] == '/')
543 expected_type
= OBJ_COMMIT
;
547 if (get_sha1_1(name
, sp
- name
- 2, outer
))
550 o
= parse_object(outer
);
553 if (!expected_type
) {
554 o
= deref_tag(o
, name
, sp
- name
- 2);
555 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
557 hashcpy(sha1
, o
->sha1
);
562 * At this point, the syntax look correct, so
563 * if we do not get the needed object, we should
566 o
= peel_to_type(name
, len
, o
, expected_type
);
570 hashcpy(sha1
, o
->sha1
);
572 /* "$commit^{/foo}" */
575 struct commit_list
*list
= NULL
;
578 * $commit^{/}. Some regex implementation may reject.
579 * We don't need regex anyway. '' pattern always matches.
584 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
585 commit_list_insert((struct commit
*)o
, &list
);
586 ret
= get_sha1_oneline(prefix
, sha1
, list
);
593 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
597 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
599 if (hexval(ch
) & ~0377) {
600 /* We must be looking at g in "SOMETHING-g"
601 * for it to be describe output.
603 if (ch
== 'g' && cp
[-1] == '-') {
606 return get_short_sha1(cp
, len
, sha1
, 1);
613 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
619 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
622 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
624 if ('0' <= ch
&& ch
<= '9')
626 if (ch
== '~' || ch
== '^')
633 int len1
= cp
- name
;
635 while (cp
< name
+ len
)
636 num
= num
* 10 + *cp
++ - '0';
637 if (!num
&& len1
== len
- 1)
639 if (has_suffix
== '^')
640 return get_parent(name
, len1
, sha1
, num
);
641 /* else if (has_suffix == '~') -- goes without saying */
642 return get_nth_ancestor(name
, len1
, sha1
, num
);
645 ret
= peel_onion(name
, len
, sha1
);
649 ret
= get_sha1_basic(name
, len
, sha1
);
653 /* It could be describe output that is "SOMETHING-gXXXX" */
654 ret
= get_describe_name(name
, len
, sha1
);
658 return get_short_sha1(name
, len
, sha1
, 0);
662 * This interprets names like ':/Initial revision of "git"' by searching
663 * through history and returning the first commit whose message starts
664 * the given regular expression.
666 * For future extension, ':/!' is reserved. If you want to match a message
667 * beginning with a '!', you have to repeat the exclamation mark.
669 #define ONELINE_SEEN (1u<<20)
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 commit_list_insert_by_date((struct commit
*)object
, list
);
689 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
690 struct commit_list
*list
)
692 struct commit_list
*backup
= NULL
, *l
;
696 if (prefix
[0] == '!') {
697 if (prefix
[1] != '!')
698 die ("Invalid search pattern: %s", prefix
);
702 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
703 die("Invalid search pattern: %s", prefix
);
705 for (l
= list
; l
; l
= l
->next
) {
706 l
->item
->object
.flags
|= ONELINE_SEEN
;
707 commit_list_insert(l
->item
, &backup
);
710 char *p
, *to_free
= NULL
;
711 struct commit
*commit
;
712 enum object_type type
;
716 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
717 if (!parse_object(commit
->object
.sha1
))
722 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
728 p
= strstr(p
, "\n\n");
729 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
733 hashcpy(sha1
, commit
->object
.sha1
);
739 free_commit_list(list
);
740 for (l
= backup
; l
; l
= l
->next
)
741 clear_commit_marks(l
->item
, ONELINE_SEEN
);
742 free_commit_list(backup
);
743 return found
? 0 : -1;
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 * Parse @{-N} syntax, return the number of characters parsed
777 * if successful; otherwise signal an error with negative value.
779 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
783 struct grab_nth_branch_switch_cbdata cb
;
787 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
789 brace
= strchr(name
, '}');
792 nth
= strtol(name
+3, &num_end
, 10);
793 if (num_end
!= brace
)
798 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
799 for (i
= 0; i
< nth
; i
++)
800 strbuf_init(&cb
.buf
[i
], 20);
803 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
806 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
812 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
813 retval
= brace
-name
+1;
816 for (i
= 0; i
< nth
; i
++)
817 strbuf_release(&cb
.buf
[i
]);
823 int get_sha1_mb(const char *name
, unsigned char *sha1
)
825 struct commit
*one
, *two
;
826 struct commit_list
*mbs
;
827 unsigned char sha1_tmp
[20];
831 dots
= strstr(name
, "...");
833 return get_sha1(name
, sha1
);
835 st
= get_sha1("HEAD", sha1_tmp
);
838 strbuf_init(&sb
, dots
- name
);
839 strbuf_add(&sb
, name
, dots
- name
);
840 st
= get_sha1(sb
.buf
, sha1_tmp
);
845 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
849 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
851 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
854 mbs
= get_merge_bases(one
, two
, 1);
855 if (!mbs
|| mbs
->next
)
859 hashcpy(sha1
, mbs
->item
->object
.sha1
);
861 free_commit_list(mbs
);
866 * This reads short-hand syntax that not only evaluates to a commit
867 * object name, but also can act as if the end user spelled the name
868 * of the branch from the command line.
870 * - "@{-N}" finds the name of the Nth previous branch we were on, and
871 * places the name of the branch in the given buf and returns the
872 * number of characters parsed if successful.
874 * - "<branch>@{upstream}" finds the name of the other ref that
875 * <branch> is configured to merge with (missing <branch> defaults
876 * to the current branch), and places the name of the branch in the
877 * given buf and returns the number of characters parsed if
880 * If the input is not of the accepted format, it returns a negative
881 * number to signal an error.
883 * If the input was ok but there are not N branch switches in the
884 * reflog, it returns 0.
886 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
889 struct branch
*upstream
;
890 int namelen
= strlen(name
);
891 int len
= interpret_nth_prior_checkout(name
, buf
);
895 return len
; /* syntax Ok, not enough switches */
896 if (0 < len
&& len
== namelen
)
897 return len
; /* consumed all */
899 /* we have extra data, which might need further processing */
900 struct strbuf tmp
= STRBUF_INIT
;
904 strbuf_add(buf
, name
+ len
, namelen
- len
);
905 ret
= interpret_branch_name(buf
->buf
, &tmp
);
906 /* that data was not interpreted, remove our cruft */
908 strbuf_setlen(buf
, used
);
912 strbuf_addbuf(buf
, &tmp
);
913 strbuf_release(&tmp
);
914 /* tweak for size of {-N} versus expanded ref name */
915 return ret
- used
+ len
;
918 cp
= strchr(name
, '@');
921 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
924 len
= cp
+ tmp_len
- name
;
925 cp
= xstrndup(name
, cp
- name
);
926 upstream
= branch_get(*cp
? cp
: NULL
);
929 || !upstream
->merge
[0]->dst
)
930 return error("No upstream branch found for '%s'", cp
);
932 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
934 strbuf_addstr(buf
, cp
);
939 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
941 int len
= strlen(name
);
942 if (interpret_branch_name(name
, sb
) == len
)
944 strbuf_add(sb
, name
, len
);
948 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
950 strbuf_branchname(sb
, name
);
953 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
954 return check_refname_format(sb
->buf
, 0);
958 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
959 * notably "xyz^" for "parent of xyz"
961 int get_sha1(const char *name
, unsigned char *sha1
)
963 struct object_context unused
;
964 return get_sha1_with_context(name
, sha1
, &unused
);
967 /* Must be called only when object_name:filename doesn't exist. */
968 static void diagnose_invalid_sha1_path(const char *prefix
,
969 const char *filename
,
970 const unsigned char *tree_sha1
,
971 const char *object_name
)
974 unsigned char sha1
[20];
980 if (!lstat(filename
, &st
))
981 die("Path '%s' exists on disk, but not in '%s'.",
982 filename
, object_name
);
983 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
984 char *fullname
= xmalloc(strlen(filename
)
985 + strlen(prefix
) + 1);
986 strcpy(fullname
, prefix
);
987 strcat(fullname
, filename
);
989 if (!get_tree_entry(tree_sha1
, fullname
,
991 die("Path '%s' exists, but not '%s'.\n"
992 "Did you mean '%s:%s' aka '%s:./%s'?",
1000 die("Path '%s' does not exist in '%s'",
1001 filename
, object_name
);
1005 /* Must be called only when :stage:filename doesn't exist. */
1006 static void diagnose_invalid_index_path(int stage
,
1008 const char *filename
)
1011 struct cache_entry
*ce
;
1013 unsigned namelen
= strlen(filename
);
1014 unsigned fullnamelen
;
1020 /* Wrong stage number? */
1021 pos
= cache_name_pos(filename
, namelen
);
1024 if (pos
< active_nr
) {
1025 ce
= active_cache
[pos
];
1026 if (ce_namelen(ce
) == namelen
&&
1027 !memcmp(ce
->name
, filename
, namelen
))
1028 die("Path '%s' is in the index, but not at stage %d.\n"
1029 "Did you mean ':%d:%s'?",
1031 ce_stage(ce
), filename
);
1034 /* Confusion between relative and absolute filenames? */
1035 fullnamelen
= namelen
+ strlen(prefix
);
1036 fullname
= xmalloc(fullnamelen
+ 1);
1037 strcpy(fullname
, prefix
);
1038 strcat(fullname
, filename
);
1039 pos
= cache_name_pos(fullname
, fullnamelen
);
1042 if (pos
< active_nr
) {
1043 ce
= active_cache
[pos
];
1044 if (ce_namelen(ce
) == fullnamelen
&&
1045 !memcmp(ce
->name
, fullname
, fullnamelen
))
1046 die("Path '%s' is in the index, but not '%s'.\n"
1047 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1049 ce_stage(ce
), fullname
,
1050 ce_stage(ce
), filename
);
1053 if (!lstat(filename
, &st
))
1054 die("Path '%s' exists on disk, but not in the index.", filename
);
1055 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1056 die("Path '%s' does not exist (neither on disk nor in the index).",
1063 static char *resolve_relative_path(const char *rel
)
1065 if (prefixcmp(rel
, "./") && prefixcmp(rel
, "../"))
1069 die("BUG: startup_info struct is not initialized.");
1071 if (!is_inside_work_tree())
1072 die("relative path syntax can't be used outside working tree.");
1074 /* die() inside prefix_path() if resolved path is outside worktree */
1075 return prefix_path(startup_info
->prefix
,
1076 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1080 static int get_sha1_with_context_1(const char *name
, unsigned char *sha1
,
1081 struct object_context
*oc
,
1082 int only_to_die
, const char *prefix
)
1084 int ret
, bracket_depth
;
1085 int namelen
= strlen(name
);
1088 memset(oc
, 0, sizeof(*oc
));
1089 oc
->mode
= S_IFINVALID
;
1090 ret
= get_sha1_1(name
, namelen
, sha1
);
1093 /* sha1:path --> object name of path in ent sha1
1094 * :path -> object name of absolute path in index
1095 * :./path -> object name of path relative to cwd in index
1096 * :[0-3]:path -> object name of path in index at stage
1097 * :/foo -> recent commit matching foo
1099 if (name
[0] == ':') {
1101 struct cache_entry
*ce
;
1102 char *new_path
= NULL
;
1104 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1105 struct commit_list
*list
= NULL
;
1106 for_each_ref(handle_one_ref
, &list
);
1107 return get_sha1_oneline(name
+ 2, sha1
, list
);
1111 name
[1] < '0' || '3' < name
[1])
1114 stage
= name
[1] - '0';
1117 new_path
= resolve_relative_path(cp
);
1119 namelen
= namelen
- (cp
- name
);
1122 namelen
= strlen(cp
);
1125 strncpy(oc
->path
, cp
,
1127 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1131 pos
= cache_name_pos(cp
, namelen
);
1134 while (pos
< active_nr
) {
1135 ce
= active_cache
[pos
];
1136 if (ce_namelen(ce
) != namelen
||
1137 memcmp(ce
->name
, cp
, namelen
))
1139 if (ce_stage(ce
) == stage
) {
1140 hashcpy(sha1
, ce
->sha1
);
1141 oc
->mode
= ce
->ce_mode
;
1147 if (only_to_die
&& name
[1] && name
[1] != '/')
1148 diagnose_invalid_index_path(stage
, prefix
, cp
);
1152 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1155 else if (bracket_depth
&& *cp
== '}')
1157 else if (!bracket_depth
&& *cp
== ':')
1161 unsigned char tree_sha1
[20];
1162 char *object_name
= NULL
;
1164 object_name
= xmalloc(cp
-name
+1);
1165 strncpy(object_name
, name
, cp
-name
);
1166 object_name
[cp
-name
] = '\0';
1168 if (!get_sha1_1(name
, cp
-name
, tree_sha1
)) {
1169 const char *filename
= cp
+1;
1170 char *new_filename
= NULL
;
1172 new_filename
= resolve_relative_path(filename
);
1174 filename
= new_filename
;
1175 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1177 diagnose_invalid_sha1_path(prefix
, filename
,
1178 tree_sha1
, object_name
);
1181 hashcpy(oc
->tree
, tree_sha1
);
1182 strncpy(oc
->path
, filename
,
1184 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1190 die("Invalid object name '%s'.", object_name
);
1197 * Call this function when you know "name" given by the end user must
1198 * name an object but it doesn't; the function _may_ die with a better
1199 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1200 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1201 * you have a chance to diagnose the error further.
1203 void maybe_die_on_misspelt_object_name(const char *name
, const char *prefix
)
1205 struct object_context oc
;
1206 unsigned char sha1
[20];
1207 get_sha1_with_context_1(name
, sha1
, &oc
, 1, prefix
);
1210 int get_sha1_with_context(const char *str
, unsigned char *sha1
, struct object_context
*orc
)
1212 return get_sha1_with_context_1(str
, sha1
, orc
, 0, NULL
);