10 static int get_sha1_oneline(const char *, unsigned char *, struct commit_list
*);
12 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
14 struct alternate_object_database
*alt
;
17 static struct alternate_object_database
*fakeent
;
20 const char *objdir
= get_object_directory();
21 int objdir_len
= strlen(objdir
);
22 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 fakeent
->next
= alt_odb_list
;
30 sprintf(hex
, "%.2s", name
);
31 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
34 sprintf(alt
->name
, "%.2s/", name
);
35 dir
= opendir(alt
->base
);
38 while ((de
= readdir(dir
)) != NULL
) {
39 if (strlen(de
->d_name
) != 38)
41 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
44 memcpy(hex
+ 2, de
->d_name
, 38);
47 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
55 return get_sha1_hex(hex
, sha1
) == 0;
59 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
74 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
77 const unsigned char *found_sha1
= NULL
;
81 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
) {
87 while (first
< last
) {
88 uint32_t mid
= (first
+ last
) / 2;
89 const unsigned char *now
;
92 now
= nth_packed_object_sha1(p
, mid
);
93 cmp
= hashcmp(match
, now
);
105 const unsigned char *now
, *next
;
106 now
= nth_packed_object_sha1(p
, first
);
107 if (match_sha(len
, match
, now
)) {
108 next
= nth_packed_object_sha1(p
, first
+1);
109 if (!next
|| !match_sha(len
, match
, next
)) {
110 /* unique within this pack */
115 else if (hashcmp(found_sha1
, now
)) {
121 /* not even unique within this pack */
129 hashcpy(sha1
, found_sha1
);
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];
143 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
144 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
145 if (!has_unpacked
&& !has_packed
)
146 return SHORT_NAME_NOT_FOUND
;
147 if (1 < has_unpacked
|| 1 < has_packed
)
148 return SHORT_NAME_AMBIGUOUS
;
149 if (has_unpacked
!= has_packed
) {
150 hashcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
));
153 /* Both have unique ones -- do they match? */
154 if (hashcmp(packed_sha1
, unpacked_sha1
))
155 return SHORT_NAME_AMBIGUOUS
;
156 hashcpy(sha1
, packed_sha1
);
160 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
165 unsigned char res
[20];
167 if (len
< MINIMUM_ABBREV
|| len
> 40)
170 memset(canonical
, 'x', 40);
171 for (i
= 0; i
< len
;i
++) {
172 unsigned char c
= name
[i
];
174 if (c
>= '0' && c
<= '9')
176 else if (c
>= 'a' && c
<= 'f')
178 else if (c
>= 'A' && c
<='F') {
190 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
191 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
192 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
196 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
201 exists
= has_sha1_file(sha1
);
202 memcpy(hex
, sha1_to_hex(sha1
), 40);
203 if (len
== 40 || !len
)
206 unsigned char sha1_ret
[20];
207 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
210 : status
== SHORT_NAME_NOT_FOUND
) {
211 int cut_at
= len
+ unique_abbrev_extra_length
;
212 cut_at
= (cut_at
< 40) ? cut_at
: 40;
221 static int ambiguous_path(const char *path
, int len
)
226 for (cnt
= 0; cnt
< len
; cnt
++) {
247 * *string and *len will only be substituted, and *string returned (for
248 * later free()ing) if the string passed in is a magic short-hand form
251 static char *substitute_branch_name(const char **string
, int *len
)
253 struct strbuf buf
= STRBUF_INIT
;
254 int ret
= interpret_branch_name(*string
, &buf
);
258 *string
= strbuf_detach(&buf
, &size
);
260 return (char *)*string
;
266 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
268 char *last_branch
= substitute_branch_name(&str
, &len
);
273 for (p
= ref_rev_parse_rules
; *p
; p
++) {
274 char fullref
[PATH_MAX
];
275 unsigned char sha1_from_ref
[20];
276 unsigned char *this_result
;
279 this_result
= refs_found
? sha1_from_ref
: sha1
;
280 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
281 r
= resolve_ref(fullref
, this_result
, 1, &flag
);
285 if (!warn_ambiguous_refs
)
287 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD"))
288 warning("ignoring dangling symref %s.", fullref
);
294 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
296 char *last_branch
= substitute_branch_name(&str
, &len
);
301 for (p
= ref_rev_parse_rules
; *p
; p
++) {
303 unsigned char hash
[20];
305 const char *ref
, *it
;
307 mksnpath(path
, sizeof(path
), *p
, len
, str
);
308 ref
= resolve_ref(path
, hash
, 1, NULL
);
311 if (!stat(git_path("logs/%s", path
), &st
) &&
314 else if (strcmp(ref
, path
) &&
315 !stat(git_path("logs/%s", ref
), &st
) &&
324 if (!warn_ambiguous_refs
)
331 static inline int upstream_mark(const char *string
, int len
)
333 const char *suffix
[] = { "@{upstream}", "@{u}" };
336 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
337 int suffix_len
= strlen(suffix
[i
]);
338 if (suffix_len
<= len
339 && !memcmp(string
, suffix
[i
], suffix_len
))
345 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
347 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
349 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
350 char *real_ref
= NULL
;
354 if (len
== 40 && !get_sha1_hex(str
, sha1
))
357 /* basic@{time or number or -number} format to query ref-log */
359 if (len
&& str
[len
-1] == '}') {
360 for (at
= len
-2; at
>= 0; at
--) {
361 if (str
[at
] == '@' && str
[at
+1] == '{') {
362 if (!upstream_mark(str
+ at
, len
- at
)) {
363 reflog_len
= (len
-1) - (at
+2);
371 /* Accept only unambiguous ref paths. */
372 if (len
&& ambiguous_path(str
, len
))
375 if (!len
&& reflog_len
) {
376 struct strbuf buf
= STRBUF_INIT
;
378 /* try the @{-N} syntax for n-th checkout */
379 ret
= interpret_branch_name(str
+at
, &buf
);
381 /* substitute this branch name and restart */
382 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
383 } else if (ret
== 0) {
386 /* allow "@{...}" to mean the current branch reflog */
387 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
388 } else if (reflog_len
)
389 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
391 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
396 if (warn_ambiguous_refs
&& refs_found
> 1)
397 warning(warn_msg
, len
, str
);
401 unsigned long at_time
;
402 unsigned long co_time
;
405 /* a @{-N} placed anywhere except the start is an error */
406 if (str
[at
+2] == '-')
409 /* Is it asking for N-th entry, or approxidate? */
410 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
411 char ch
= str
[at
+2+i
];
412 if ('0' <= ch
&& ch
<= '9')
413 nth
= nth
* 10 + ch
- '0';
417 if (100000000 <= nth
) {
424 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
425 at_time
= approxidate_careful(tmp
, &errors
);
430 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
431 &co_time
, &co_tz
, &co_cnt
)) {
433 warning("Log for '%.*s' only goes "
434 "back to %s.", len
, str
,
435 show_date(co_time
, co_tz
, DATE_RFC2822
));
438 die("Log for '%.*s' only has %d entries.",
448 static int get_parent(const char *name
, int len
,
449 unsigned char *result
, int idx
)
451 unsigned char sha1
[20];
452 int ret
= get_sha1_1(name
, len
, sha1
);
453 struct commit
*commit
;
454 struct commit_list
*p
;
458 commit
= lookup_commit_reference(sha1
);
461 if (parse_commit(commit
))
464 hashcpy(result
, commit
->object
.sha1
);
470 hashcpy(result
, p
->item
->object
.sha1
);
478 static int get_nth_ancestor(const char *name
, int len
,
479 unsigned char *result
, int generation
)
481 unsigned char sha1
[20];
482 struct commit
*commit
;
485 ret
= get_sha1_1(name
, len
, sha1
);
488 commit
= lookup_commit_reference(sha1
);
492 while (generation
--) {
493 if (parse_commit(commit
) || !commit
->parents
)
495 commit
= commit
->parents
->item
;
497 hashcpy(result
, commit
->object
.sha1
);
501 struct object
*peel_to_type(const char *name
, int namelen
,
502 struct object
*o
, enum object_type expected_type
)
504 if (name
&& !namelen
)
505 namelen
= strlen(name
);
507 unsigned char sha1
[20];
508 if (get_sha1_1(name
, namelen
, sha1
))
510 o
= parse_object(sha1
);
513 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
515 if (o
->type
== expected_type
)
517 if (o
->type
== OBJ_TAG
)
518 o
= ((struct tag
*) o
)->tagged
;
519 else if (o
->type
== OBJ_COMMIT
)
520 o
= &(((struct commit
*) o
)->tree
->object
);
523 error("%.*s: expected %s type, but the object "
524 "dereferences to %s type",
525 namelen
, name
, typename(expected_type
),
532 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
534 unsigned char outer
[20];
536 unsigned int expected_type
= 0;
540 * "ref^{type}" dereferences ref repeatedly until you cannot
541 * dereference anymore, or you get an object of given type,
542 * whichever comes first. "ref^{}" means just dereference
543 * tags until you get a non-tag. "ref^0" is a shorthand for
544 * "ref^{commit}". "commit^{tree}" could be used to find the
545 * top-level tree of the given commit.
547 if (len
< 4 || name
[len
-1] != '}')
550 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
552 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
558 sp
++; /* beginning of type name, or closing brace for empty */
559 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
560 expected_type
= OBJ_COMMIT
;
561 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
562 expected_type
= OBJ_TREE
;
563 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
564 expected_type
= OBJ_BLOB
;
565 else if (sp
[0] == '}')
566 expected_type
= OBJ_NONE
;
567 else if (sp
[0] == '/')
568 expected_type
= OBJ_COMMIT
;
572 if (get_sha1_1(name
, sp
- name
- 2, outer
))
575 o
= parse_object(outer
);
578 if (!expected_type
) {
579 o
= deref_tag(o
, name
, sp
- name
- 2);
580 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
582 hashcpy(sha1
, o
->sha1
);
587 * At this point, the syntax look correct, so
588 * if we do not get the needed object, we should
591 o
= peel_to_type(name
, len
, o
, expected_type
);
595 hashcpy(sha1
, o
->sha1
);
597 /* "$commit^{/foo}" */
600 struct commit_list
*list
= NULL
;
603 * $commit^{/}. Some regex implementation may reject.
604 * We don't need regex anyway. '' pattern always matches.
609 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
610 commit_list_insert((struct commit
*)o
, &list
);
611 ret
= get_sha1_oneline(prefix
, sha1
, list
);
618 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
622 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
624 if (hexval(ch
) & ~0377) {
625 /* We must be looking at g in "SOMETHING-g"
626 * for it to be describe output.
628 if (ch
== 'g' && cp
[-1] == '-') {
631 return get_short_sha1(cp
, len
, sha1
, 1);
638 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
644 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
647 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
649 if ('0' <= ch
&& ch
<= '9')
651 if (ch
== '~' || ch
== '^')
658 int len1
= cp
- name
;
660 while (cp
< name
+ len
)
661 num
= num
* 10 + *cp
++ - '0';
662 if (!num
&& len1
== len
- 1)
664 if (has_suffix
== '^')
665 return get_parent(name
, len1
, sha1
, num
);
666 /* else if (has_suffix == '~') -- goes without saying */
667 return get_nth_ancestor(name
, len1
, sha1
, num
);
670 ret
= peel_onion(name
, len
, sha1
);
674 ret
= get_sha1_basic(name
, len
, sha1
);
678 /* It could be describe output that is "SOMETHING-gXXXX" */
679 ret
= get_describe_name(name
, len
, sha1
);
683 return get_short_sha1(name
, len
, sha1
, 0);
687 * This interprets names like ':/Initial revision of "git"' by searching
688 * through history and returning the first commit whose message starts
689 * the given regular expression.
691 * For future extension, ':/!' is reserved. If you want to match a message
692 * beginning with a '!', you have to repeat the exclamation mark.
694 #define ONELINE_SEEN (1u<<20)
696 static int handle_one_ref(const char *path
,
697 const unsigned char *sha1
, int flag
, void *cb_data
)
699 struct commit_list
**list
= cb_data
;
700 struct object
*object
= parse_object(sha1
);
703 if (object
->type
== OBJ_TAG
) {
704 object
= deref_tag(object
, path
, strlen(path
));
708 if (object
->type
!= OBJ_COMMIT
)
710 insert_by_date((struct commit
*)object
, list
);
714 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
715 struct commit_list
*list
)
717 struct commit_list
*backup
= NULL
, *l
;
721 if (prefix
[0] == '!') {
722 if (prefix
[1] != '!')
723 die ("Invalid search pattern: %s", prefix
);
727 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
728 die("Invalid search pattern: %s", prefix
);
730 for (l
= list
; l
; l
= l
->next
) {
731 l
->item
->object
.flags
|= ONELINE_SEEN
;
732 commit_list_insert(l
->item
, &backup
);
735 char *p
, *to_free
= NULL
;
736 struct commit
*commit
;
737 enum object_type type
;
741 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
742 if (!parse_object(commit
->object
.sha1
))
747 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
753 p
= strstr(p
, "\n\n");
754 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
758 hashcpy(sha1
, commit
->object
.sha1
);
764 free_commit_list(list
);
765 for (l
= backup
; l
; l
= l
->next
)
766 clear_commit_marks(l
->item
, ONELINE_SEEN
);
767 free_commit_list(backup
);
768 return found
? 0 : -1;
771 struct grab_nth_branch_switch_cbdata
{
776 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
777 const char *email
, unsigned long timestamp
, int tz
,
778 const char *message
, void *cb_data
)
780 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
781 const char *match
= NULL
, *target
= NULL
;
785 if (!prefixcmp(message
, "checkout: moving from ")) {
786 match
= message
+ strlen("checkout: moving from ");
787 target
= strstr(match
, " to ");
790 if (!match
|| !target
)
793 len
= target
- match
;
794 nth
= cb
->cnt
++ % cb
->alloc
;
795 strbuf_reset(&cb
->buf
[nth
]);
796 strbuf_add(&cb
->buf
[nth
], match
, len
);
801 * Parse @{-N} syntax, return the number of characters parsed
802 * if successful; otherwise signal an error with negative value.
804 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
808 struct grab_nth_branch_switch_cbdata cb
;
812 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
814 brace
= strchr(name
, '}');
817 nth
= strtol(name
+3, &num_end
, 10);
818 if (num_end
!= brace
)
823 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
824 for (i
= 0; i
< nth
; i
++)
825 strbuf_init(&cb
.buf
[i
], 20);
828 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
831 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
837 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
838 retval
= brace
-name
+1;
841 for (i
= 0; i
< nth
; i
++)
842 strbuf_release(&cb
.buf
[i
]);
848 int get_sha1_mb(const char *name
, unsigned char *sha1
)
850 struct commit
*one
, *two
;
851 struct commit_list
*mbs
;
852 unsigned char sha1_tmp
[20];
856 dots
= strstr(name
, "...");
858 return get_sha1(name
, sha1
);
860 st
= get_sha1("HEAD", sha1_tmp
);
863 strbuf_init(&sb
, dots
- name
);
864 strbuf_add(&sb
, name
, dots
- name
);
865 st
= get_sha1(sb
.buf
, sha1_tmp
);
870 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
874 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
876 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
879 mbs
= get_merge_bases(one
, two
, 1);
880 if (!mbs
|| mbs
->next
)
884 hashcpy(sha1
, mbs
->item
->object
.sha1
);
886 free_commit_list(mbs
);
891 * This reads short-hand syntax that not only evaluates to a commit
892 * object name, but also can act as if the end user spelled the name
893 * of the branch from the command line.
895 * - "@{-N}" finds the name of the Nth previous branch we were on, and
896 * places the name of the branch in the given buf and returns the
897 * number of characters parsed if successful.
899 * - "<branch>@{upstream}" finds the name of the other ref that
900 * <branch> is configured to merge with (missing <branch> defaults
901 * to the current branch), and places the name of the branch in the
902 * given buf and returns the number of characters parsed if
905 * If the input is not of the accepted format, it returns a negative
906 * number to signal an error.
908 * If the input was ok but there are not N branch switches in the
909 * reflog, it returns 0.
911 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
914 struct branch
*upstream
;
915 int namelen
= strlen(name
);
916 int len
= interpret_nth_prior_checkout(name
, buf
);
920 return len
; /* syntax Ok, not enough switches */
921 if (0 < len
&& len
== namelen
)
922 return len
; /* consumed all */
924 /* we have extra data, which might need further processing */
925 struct strbuf tmp
= STRBUF_INIT
;
929 strbuf_add(buf
, name
+ len
, namelen
- len
);
930 ret
= interpret_branch_name(buf
->buf
, &tmp
);
931 /* that data was not interpreted, remove our cruft */
933 strbuf_setlen(buf
, used
);
937 strbuf_addbuf(buf
, &tmp
);
938 strbuf_release(&tmp
);
939 /* tweak for size of {-N} versus expanded ref name */
940 return ret
- used
+ len
;
943 cp
= strchr(name
, '@');
946 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
949 len
= cp
+ tmp_len
- name
;
950 cp
= xstrndup(name
, cp
- name
);
951 upstream
= branch_get(*cp
? cp
: NULL
);
954 || !upstream
->merge
[0]->dst
)
955 return error("No upstream branch found for '%s'", cp
);
957 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
959 strbuf_addstr(buf
, cp
);
964 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
966 int len
= strlen(name
);
967 if (interpret_branch_name(name
, sb
) == len
)
969 strbuf_add(sb
, name
, len
);
973 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
975 strbuf_branchname(sb
, name
);
977 return CHECK_REF_FORMAT_ERROR
;
978 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
979 return check_ref_format(sb
->buf
);
983 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
984 * notably "xyz^" for "parent of xyz"
986 int get_sha1(const char *name
, unsigned char *sha1
)
988 struct object_context unused
;
989 return get_sha1_with_context(name
, sha1
, &unused
);
992 /* Must be called only when object_name:filename doesn't exist. */
993 static void diagnose_invalid_sha1_path(const char *prefix
,
994 const char *filename
,
995 const unsigned char *tree_sha1
,
996 const char *object_name
)
999 unsigned char sha1
[20];
1005 if (!lstat(filename
, &st
))
1006 die("Path '%s' exists on disk, but not in '%s'.",
1007 filename
, object_name
);
1008 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
1009 char *fullname
= xmalloc(strlen(filename
)
1010 + strlen(prefix
) + 1);
1011 strcpy(fullname
, prefix
);
1012 strcat(fullname
, filename
);
1014 if (!get_tree_entry(tree_sha1
, fullname
,
1016 die("Path '%s' exists, but not '%s'.\n"
1017 "Did you mean '%s:%s'?",
1023 die("Path '%s' does not exist in '%s'",
1024 filename
, object_name
);
1028 /* Must be called only when :stage:filename doesn't exist. */
1029 static void diagnose_invalid_index_path(int stage
,
1031 const char *filename
)
1034 struct cache_entry
*ce
;
1036 unsigned namelen
= strlen(filename
);
1037 unsigned fullnamelen
;
1043 /* Wrong stage number? */
1044 pos
= cache_name_pos(filename
, namelen
);
1047 if (pos
< active_nr
) {
1048 ce
= active_cache
[pos
];
1049 if (ce_namelen(ce
) == namelen
&&
1050 !memcmp(ce
->name
, filename
, namelen
))
1051 die("Path '%s' is in the index, but not at stage %d.\n"
1052 "Did you mean ':%d:%s'?",
1054 ce_stage(ce
), filename
);
1057 /* Confusion between relative and absolute filenames? */
1058 fullnamelen
= namelen
+ strlen(prefix
);
1059 fullname
= xmalloc(fullnamelen
+ 1);
1060 strcpy(fullname
, prefix
);
1061 strcat(fullname
, filename
);
1062 pos
= cache_name_pos(fullname
, fullnamelen
);
1065 if (pos
< active_nr
) {
1066 ce
= active_cache
[pos
];
1067 if (ce_namelen(ce
) == fullnamelen
&&
1068 !memcmp(ce
->name
, fullname
, fullnamelen
))
1069 die("Path '%s' is in the index, but not '%s'.\n"
1070 "Did you mean ':%d:%s'?",
1072 ce_stage(ce
), fullname
);
1075 if (!lstat(filename
, &st
))
1076 die("Path '%s' exists on disk, but not in the index.", filename
);
1077 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1078 die("Path '%s' does not exist (neither on disk nor in the index).",
1085 int get_sha1_with_mode_1(const char *name
, unsigned char *sha1
, unsigned *mode
, int gently
, const char *prefix
)
1087 struct object_context oc
;
1089 ret
= get_sha1_with_context_1(name
, sha1
, &oc
, gently
, prefix
);
1094 static char *resolve_relative_path(const char *rel
)
1096 if (prefixcmp(rel
, "./") && prefixcmp(rel
, "../"))
1100 die("BUG: startup_info struct is not initialized.");
1102 if (!is_inside_work_tree())
1103 die("relative path syntax can't be used outside working tree.");
1105 /* die() inside prefix_path() if resolved path is outside worktree */
1106 return prefix_path(startup_info
->prefix
,
1107 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1111 int get_sha1_with_context_1(const char *name
, unsigned char *sha1
,
1112 struct object_context
*oc
,
1113 int gently
, const char *prefix
)
1115 int ret
, bracket_depth
;
1116 int namelen
= strlen(name
);
1119 memset(oc
, 0, sizeof(*oc
));
1120 oc
->mode
= S_IFINVALID
;
1121 ret
= get_sha1_1(name
, namelen
, sha1
);
1124 /* sha1:path --> object name of path in ent sha1
1125 * :path -> object name of absolute path in index
1126 * :./path -> object name of path relative to cwd in index
1127 * :[0-3]:path -> object name of path in index at stage
1128 * :/foo -> recent commit matching foo
1130 if (name
[0] == ':') {
1132 struct cache_entry
*ce
;
1133 char *new_path
= NULL
;
1135 if (namelen
> 2 && name
[1] == '/') {
1136 struct commit_list
*list
= NULL
;
1137 for_each_ref(handle_one_ref
, &list
);
1138 return get_sha1_oneline(name
+ 2, sha1
, list
);
1142 name
[1] < '0' || '3' < name
[1])
1145 stage
= name
[1] - '0';
1148 new_path
= resolve_relative_path(cp
);
1150 namelen
= namelen
- (cp
- name
);
1153 namelen
= strlen(cp
);
1156 strncpy(oc
->path
, cp
,
1158 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1162 pos
= cache_name_pos(cp
, namelen
);
1165 while (pos
< active_nr
) {
1166 ce
= active_cache
[pos
];
1167 if (ce_namelen(ce
) != namelen
||
1168 memcmp(ce
->name
, cp
, namelen
))
1170 if (ce_stage(ce
) == stage
) {
1171 hashcpy(sha1
, ce
->sha1
);
1172 oc
->mode
= ce
->ce_mode
;
1179 diagnose_invalid_index_path(stage
, prefix
, cp
);
1183 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1186 else if (bracket_depth
&& *cp
== '}')
1188 else if (!bracket_depth
&& *cp
== ':')
1192 unsigned char tree_sha1
[20];
1193 char *object_name
= NULL
;
1195 object_name
= xmalloc(cp
-name
+1);
1196 strncpy(object_name
, name
, cp
-name
);
1197 object_name
[cp
-name
] = '\0';
1199 if (!get_sha1_1(name
, cp
-name
, tree_sha1
)) {
1200 const char *filename
= cp
+1;
1201 char *new_filename
= NULL
;
1203 new_filename
= resolve_relative_path(filename
);
1205 filename
= new_filename
;
1206 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1208 diagnose_invalid_sha1_path(prefix
, filename
,
1209 tree_sha1
, object_name
);
1212 hashcpy(oc
->tree
, tree_sha1
);
1213 strncpy(oc
->path
, filename
,
1215 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1221 die("Invalid object name '%s'.", object_name
);