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
) {
219 static int ambiguous_path(const char *path
, int len
)
224 for (cnt
= 0; cnt
< len
; cnt
++) {
245 * *string and *len will only be substituted, and *string returned (for
246 * later free()ing) if the string passed in is a magic short-hand form
249 static char *substitute_branch_name(const char **string
, int *len
)
251 struct strbuf buf
= STRBUF_INIT
;
252 int ret
= interpret_branch_name(*string
, &buf
);
256 *string
= strbuf_detach(&buf
, &size
);
258 return (char *)*string
;
264 int dwim_ref(const char *str
, int len
, unsigned char *sha1
, char **ref
)
266 char *last_branch
= substitute_branch_name(&str
, &len
);
271 for (p
= ref_rev_parse_rules
; *p
; p
++) {
272 char fullref
[PATH_MAX
];
273 unsigned char sha1_from_ref
[20];
274 unsigned char *this_result
;
277 this_result
= refs_found
? sha1_from_ref
: sha1
;
278 mksnpath(fullref
, sizeof(fullref
), *p
, len
, str
);
279 r
= resolve_ref(fullref
, this_result
, 1, &flag
);
283 if (!warn_ambiguous_refs
)
285 } else if ((flag
& REF_ISSYMREF
) && strcmp(fullref
, "HEAD"))
286 warning("ignoring dangling symref %s.", fullref
);
292 int dwim_log(const char *str
, int len
, unsigned char *sha1
, char **log
)
294 char *last_branch
= substitute_branch_name(&str
, &len
);
299 for (p
= ref_rev_parse_rules
; *p
; p
++) {
301 unsigned char hash
[20];
303 const char *ref
, *it
;
305 mksnpath(path
, sizeof(path
), *p
, len
, str
);
306 ref
= resolve_ref(path
, hash
, 1, NULL
);
309 if (!stat(git_path("logs/%s", path
), &st
) &&
312 else if (strcmp(ref
, path
) &&
313 !stat(git_path("logs/%s", ref
), &st
) &&
322 if (!warn_ambiguous_refs
)
329 static inline int upstream_mark(const char *string
, int len
)
331 const char *suffix
[] = { "@{upstream}", "@{u}" };
334 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
335 int suffix_len
= strlen(suffix
[i
]);
336 if (suffix_len
<= len
337 && !memcmp(string
, suffix
[i
], suffix_len
))
343 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
345 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
347 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
348 char *real_ref
= NULL
;
352 if (len
== 40 && !get_sha1_hex(str
, sha1
))
355 /* basic@{time or number or -number} format to query ref-log */
357 if (len
&& str
[len
-1] == '}') {
358 for (at
= len
-2; at
>= 0; at
--) {
359 if (str
[at
] == '@' && str
[at
+1] == '{') {
360 if (!upstream_mark(str
+ at
, len
- at
)) {
361 reflog_len
= (len
-1) - (at
+2);
369 /* Accept only unambiguous ref paths. */
370 if (len
&& ambiguous_path(str
, len
))
373 if (!len
&& reflog_len
) {
374 struct strbuf buf
= STRBUF_INIT
;
376 /* try the @{-N} syntax for n-th checkout */
377 ret
= interpret_branch_name(str
+at
, &buf
);
379 /* substitute this branch name and restart */
380 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
381 } else if (ret
== 0) {
384 /* allow "@{...}" to mean the current branch reflog */
385 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
386 } else if (reflog_len
)
387 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
389 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
394 if (warn_ambiguous_refs
&& refs_found
> 1)
395 warning(warn_msg
, len
, str
);
399 unsigned long at_time
;
400 unsigned long co_time
;
403 /* a @{-N} placed anywhere except the start is an error */
404 if (str
[at
+2] == '-')
407 /* Is it asking for N-th entry, or approxidate? */
408 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
409 char ch
= str
[at
+2+i
];
410 if ('0' <= ch
&& ch
<= '9')
411 nth
= nth
* 10 + ch
- '0';
415 if (100000000 <= nth
) {
422 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
423 at_time
= approxidate_careful(tmp
, &errors
);
428 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
429 &co_time
, &co_tz
, &co_cnt
)) {
431 warning("Log for '%.*s' only goes "
432 "back to %s.", len
, str
,
433 show_date(co_time
, co_tz
, DATE_RFC2822
));
436 die("Log for '%.*s' only has %d entries.",
446 static int get_parent(const char *name
, int len
,
447 unsigned char *result
, int idx
)
449 unsigned char sha1
[20];
450 int ret
= get_sha1_1(name
, len
, sha1
);
451 struct commit
*commit
;
452 struct commit_list
*p
;
456 commit
= lookup_commit_reference(sha1
);
459 if (parse_commit(commit
))
462 hashcpy(result
, commit
->object
.sha1
);
468 hashcpy(result
, p
->item
->object
.sha1
);
476 static int get_nth_ancestor(const char *name
, int len
,
477 unsigned char *result
, int generation
)
479 unsigned char sha1
[20];
480 struct commit
*commit
;
483 ret
= get_sha1_1(name
, len
, sha1
);
486 commit
= lookup_commit_reference(sha1
);
490 while (generation
--) {
491 if (parse_commit(commit
) || !commit
->parents
)
493 commit
= commit
->parents
->item
;
495 hashcpy(result
, commit
->object
.sha1
);
499 struct object
*peel_to_type(const char *name
, int namelen
,
500 struct object
*o
, enum object_type expected_type
)
502 if (name
&& !namelen
)
503 namelen
= strlen(name
);
505 unsigned char sha1
[20];
506 if (get_sha1_1(name
, namelen
, sha1
))
508 o
= parse_object(sha1
);
511 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
513 if (o
->type
== expected_type
)
515 if (o
->type
== OBJ_TAG
)
516 o
= ((struct tag
*) o
)->tagged
;
517 else if (o
->type
== OBJ_COMMIT
)
518 o
= &(((struct commit
*) o
)->tree
->object
);
521 error("%.*s: expected %s type, but the object "
522 "dereferences to %s type",
523 namelen
, name
, typename(expected_type
),
530 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
532 unsigned char outer
[20];
534 unsigned int expected_type
= 0;
538 * "ref^{type}" dereferences ref repeatedly until you cannot
539 * dereference anymore, or you get an object of given type,
540 * whichever comes first. "ref^{}" means just dereference
541 * tags until you get a non-tag. "ref^0" is a shorthand for
542 * "ref^{commit}". "commit^{tree}" could be used to find the
543 * top-level tree of the given commit.
545 if (len
< 4 || name
[len
-1] != '}')
548 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
550 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
556 sp
++; /* beginning of type name, or closing brace for empty */
557 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
558 expected_type
= OBJ_COMMIT
;
559 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
560 expected_type
= OBJ_TREE
;
561 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
562 expected_type
= OBJ_BLOB
;
563 else if (sp
[0] == '}')
564 expected_type
= OBJ_NONE
;
565 else if (sp
[0] == '/')
566 expected_type
= OBJ_COMMIT
;
570 if (get_sha1_1(name
, sp
- name
- 2, outer
))
573 o
= parse_object(outer
);
576 if (!expected_type
) {
577 o
= deref_tag(o
, name
, sp
- name
- 2);
578 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
580 hashcpy(sha1
, o
->sha1
);
585 * At this point, the syntax look correct, so
586 * if we do not get the needed object, we should
589 o
= peel_to_type(name
, len
, o
, expected_type
);
593 hashcpy(sha1
, o
->sha1
);
595 /* "$commit^{/foo}" */
598 struct commit_list
*list
= NULL
;
601 * $commit^{/}. Some regex implementation may reject.
602 * We don't need regex anyway. '' pattern always matches.
607 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
608 commit_list_insert((struct commit
*)o
, &list
);
609 ret
= get_sha1_oneline(prefix
, sha1
, list
);
616 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
620 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
622 if (hexval(ch
) & ~0377) {
623 /* We must be looking at g in "SOMETHING-g"
624 * for it to be describe output.
626 if (ch
== 'g' && cp
[-1] == '-') {
629 return get_short_sha1(cp
, len
, sha1
, 1);
636 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
642 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
645 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
647 if ('0' <= ch
&& ch
<= '9')
649 if (ch
== '~' || ch
== '^')
656 int len1
= cp
- name
;
658 while (cp
< name
+ len
)
659 num
= num
* 10 + *cp
++ - '0';
660 if (!num
&& len1
== len
- 1)
662 if (has_suffix
== '^')
663 return get_parent(name
, len1
, sha1
, num
);
664 /* else if (has_suffix == '~') -- goes without saying */
665 return get_nth_ancestor(name
, len1
, sha1
, num
);
668 ret
= peel_onion(name
, len
, sha1
);
672 ret
= get_sha1_basic(name
, len
, sha1
);
676 /* It could be describe output that is "SOMETHING-gXXXX" */
677 ret
= get_describe_name(name
, len
, sha1
);
681 return get_short_sha1(name
, len
, sha1
, 0);
685 * This interprets names like ':/Initial revision of "git"' by searching
686 * through history and returning the first commit whose message starts
687 * the given regular expression.
689 * For future extension, ':/!' is reserved. If you want to match a message
690 * beginning with a '!', you have to repeat the exclamation mark.
692 #define ONELINE_SEEN (1u<<20)
694 static int handle_one_ref(const char *path
,
695 const unsigned char *sha1
, int flag
, void *cb_data
)
697 struct commit_list
**list
= cb_data
;
698 struct object
*object
= parse_object(sha1
);
701 if (object
->type
== OBJ_TAG
) {
702 object
= deref_tag(object
, path
, strlen(path
));
706 if (object
->type
!= OBJ_COMMIT
)
708 commit_list_insert_by_date((struct commit
*)object
, list
);
712 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
713 struct commit_list
*list
)
715 struct commit_list
*backup
= NULL
, *l
;
719 if (prefix
[0] == '!') {
720 if (prefix
[1] != '!')
721 die ("Invalid search pattern: %s", prefix
);
725 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
726 die("Invalid search pattern: %s", prefix
);
728 for (l
= list
; l
; l
= l
->next
) {
729 l
->item
->object
.flags
|= ONELINE_SEEN
;
730 commit_list_insert(l
->item
, &backup
);
733 char *p
, *to_free
= NULL
;
734 struct commit
*commit
;
735 enum object_type type
;
739 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
740 if (!parse_object(commit
->object
.sha1
))
745 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
751 p
= strstr(p
, "\n\n");
752 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
756 hashcpy(sha1
, commit
->object
.sha1
);
762 free_commit_list(list
);
763 for (l
= backup
; l
; l
= l
->next
)
764 clear_commit_marks(l
->item
, ONELINE_SEEN
);
765 free_commit_list(backup
);
766 return found
? 0 : -1;
769 struct grab_nth_branch_switch_cbdata
{
774 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
775 const char *email
, unsigned long timestamp
, int tz
,
776 const char *message
, void *cb_data
)
778 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
779 const char *match
= NULL
, *target
= NULL
;
783 if (!prefixcmp(message
, "checkout: moving from ")) {
784 match
= message
+ strlen("checkout: moving from ");
785 target
= strstr(match
, " to ");
788 if (!match
|| !target
)
791 len
= target
- match
;
792 nth
= cb
->cnt
++ % cb
->alloc
;
793 strbuf_reset(&cb
->buf
[nth
]);
794 strbuf_add(&cb
->buf
[nth
], match
, len
);
799 * Parse @{-N} syntax, return the number of characters parsed
800 * if successful; otherwise signal an error with negative value.
802 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
806 struct grab_nth_branch_switch_cbdata cb
;
810 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
812 brace
= strchr(name
, '}');
815 nth
= strtol(name
+3, &num_end
, 10);
816 if (num_end
!= brace
)
821 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
822 for (i
= 0; i
< nth
; i
++)
823 strbuf_init(&cb
.buf
[i
], 20);
826 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
829 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
835 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
836 retval
= brace
-name
+1;
839 for (i
= 0; i
< nth
; i
++)
840 strbuf_release(&cb
.buf
[i
]);
846 int get_sha1_mb(const char *name
, unsigned char *sha1
)
848 struct commit
*one
, *two
;
849 struct commit_list
*mbs
;
850 unsigned char sha1_tmp
[20];
854 dots
= strstr(name
, "...");
856 return get_sha1(name
, sha1
);
858 st
= get_sha1("HEAD", sha1_tmp
);
861 strbuf_init(&sb
, dots
- name
);
862 strbuf_add(&sb
, name
, dots
- name
);
863 st
= get_sha1(sb
.buf
, sha1_tmp
);
868 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
872 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
874 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
877 mbs
= get_merge_bases(one
, two
, 1);
878 if (!mbs
|| mbs
->next
)
882 hashcpy(sha1
, mbs
->item
->object
.sha1
);
884 free_commit_list(mbs
);
889 * This reads short-hand syntax that not only evaluates to a commit
890 * object name, but also can act as if the end user spelled the name
891 * of the branch from the command line.
893 * - "@{-N}" finds the name of the Nth previous branch we were on, and
894 * places the name of the branch in the given buf and returns the
895 * number of characters parsed if successful.
897 * - "<branch>@{upstream}" finds the name of the other ref that
898 * <branch> is configured to merge with (missing <branch> defaults
899 * to the current branch), and places the name of the branch in the
900 * given buf and returns the number of characters parsed if
903 * If the input is not of the accepted format, it returns a negative
904 * number to signal an error.
906 * If the input was ok but there are not N branch switches in the
907 * reflog, it returns 0.
909 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
912 struct branch
*upstream
;
913 int namelen
= strlen(name
);
914 int len
= interpret_nth_prior_checkout(name
, buf
);
918 return len
; /* syntax Ok, not enough switches */
919 if (0 < len
&& len
== namelen
)
920 return len
; /* consumed all */
922 /* we have extra data, which might need further processing */
923 struct strbuf tmp
= STRBUF_INIT
;
927 strbuf_add(buf
, name
+ len
, namelen
- len
);
928 ret
= interpret_branch_name(buf
->buf
, &tmp
);
929 /* that data was not interpreted, remove our cruft */
931 strbuf_setlen(buf
, used
);
935 strbuf_addbuf(buf
, &tmp
);
936 strbuf_release(&tmp
);
937 /* tweak for size of {-N} versus expanded ref name */
938 return ret
- used
+ len
;
941 cp
= strchr(name
, '@');
944 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
947 len
= cp
+ tmp_len
- name
;
948 cp
= xstrndup(name
, cp
- name
);
949 upstream
= branch_get(*cp
? cp
: NULL
);
952 || !upstream
->merge
[0]->dst
)
953 return error("No upstream branch found for '%s'", cp
);
955 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
957 strbuf_addstr(buf
, cp
);
962 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
964 int len
= strlen(name
);
965 if (interpret_branch_name(name
, sb
) == len
)
967 strbuf_add(sb
, name
, len
);
971 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
973 strbuf_branchname(sb
, name
);
975 return CHECK_REF_FORMAT_ERROR
;
976 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
977 return check_ref_format(sb
->buf
);
981 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
982 * notably "xyz^" for "parent of xyz"
984 int get_sha1(const char *name
, unsigned char *sha1
)
986 struct object_context unused
;
987 return get_sha1_with_context(name
, sha1
, &unused
);
990 /* Must be called only when object_name:filename doesn't exist. */
991 static void diagnose_invalid_sha1_path(const char *prefix
,
992 const char *filename
,
993 const unsigned char *tree_sha1
,
994 const char *object_name
)
997 unsigned char sha1
[20];
1003 if (!lstat(filename
, &st
))
1004 die("Path '%s' exists on disk, but not in '%s'.",
1005 filename
, object_name
);
1006 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
1007 char *fullname
= xmalloc(strlen(filename
)
1008 + strlen(prefix
) + 1);
1009 strcpy(fullname
, prefix
);
1010 strcat(fullname
, filename
);
1012 if (!get_tree_entry(tree_sha1
, fullname
,
1014 die("Path '%s' exists, but not '%s'.\n"
1015 "Did you mean '%s:%s'?",
1021 die("Path '%s' does not exist in '%s'",
1022 filename
, object_name
);
1026 /* Must be called only when :stage:filename doesn't exist. */
1027 static void diagnose_invalid_index_path(int stage
,
1029 const char *filename
)
1032 struct cache_entry
*ce
;
1034 unsigned namelen
= strlen(filename
);
1035 unsigned fullnamelen
;
1041 /* Wrong stage number? */
1042 pos
= cache_name_pos(filename
, namelen
);
1045 if (pos
< active_nr
) {
1046 ce
= active_cache
[pos
];
1047 if (ce_namelen(ce
) == namelen
&&
1048 !memcmp(ce
->name
, filename
, namelen
))
1049 die("Path '%s' is in the index, but not at stage %d.\n"
1050 "Did you mean ':%d:%s'?",
1052 ce_stage(ce
), filename
);
1055 /* Confusion between relative and absolute filenames? */
1056 fullnamelen
= namelen
+ strlen(prefix
);
1057 fullname
= xmalloc(fullnamelen
+ 1);
1058 strcpy(fullname
, prefix
);
1059 strcat(fullname
, filename
);
1060 pos
= cache_name_pos(fullname
, fullnamelen
);
1063 if (pos
< active_nr
) {
1064 ce
= active_cache
[pos
];
1065 if (ce_namelen(ce
) == fullnamelen
&&
1066 !memcmp(ce
->name
, fullname
, fullnamelen
))
1067 die("Path '%s' is in the index, but not '%s'.\n"
1068 "Did you mean ':%d:%s'?",
1070 ce_stage(ce
), fullname
);
1073 if (!lstat(filename
, &st
))
1074 die("Path '%s' exists on disk, but not in the index.", filename
);
1075 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1076 die("Path '%s' does not exist (neither on disk nor in the index).",
1083 int get_sha1_with_mode_1(const char *name
, unsigned char *sha1
, unsigned *mode
,
1084 int only_to_die
, const char *prefix
)
1086 struct object_context oc
;
1088 ret
= get_sha1_with_context_1(name
, sha1
, &oc
, only_to_die
, prefix
);
1093 static char *resolve_relative_path(const char *rel
)
1095 if (prefixcmp(rel
, "./") && prefixcmp(rel
, "../"))
1099 die("BUG: startup_info struct is not initialized.");
1101 if (!is_inside_work_tree())
1102 die("relative path syntax can't be used outside working tree.");
1104 /* die() inside prefix_path() if resolved path is outside worktree */
1105 return prefix_path(startup_info
->prefix
,
1106 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1110 int get_sha1_with_context_1(const char *name
, unsigned char *sha1
,
1111 struct object_context
*oc
,
1112 int only_to_die
, const char *prefix
)
1114 int ret
, bracket_depth
;
1115 int namelen
= strlen(name
);
1118 memset(oc
, 0, sizeof(*oc
));
1119 oc
->mode
= S_IFINVALID
;
1120 ret
= get_sha1_1(name
, namelen
, sha1
);
1123 /* sha1:path --> object name of path in ent sha1
1124 * :path -> object name of absolute path in index
1125 * :./path -> object name of path relative to cwd in index
1126 * :[0-3]:path -> object name of path in index at stage
1127 * :/foo -> recent commit matching foo
1129 if (name
[0] == ':') {
1131 struct cache_entry
*ce
;
1132 char *new_path
= NULL
;
1134 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1135 struct commit_list
*list
= NULL
;
1136 for_each_ref(handle_one_ref
, &list
);
1137 return get_sha1_oneline(name
+ 2, sha1
, list
);
1141 name
[1] < '0' || '3' < name
[1])
1144 stage
= name
[1] - '0';
1147 new_path
= resolve_relative_path(cp
);
1149 namelen
= namelen
- (cp
- name
);
1152 namelen
= strlen(cp
);
1155 strncpy(oc
->path
, cp
,
1157 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1161 pos
= cache_name_pos(cp
, namelen
);
1164 while (pos
< active_nr
) {
1165 ce
= active_cache
[pos
];
1166 if (ce_namelen(ce
) != namelen
||
1167 memcmp(ce
->name
, cp
, namelen
))
1169 if (ce_stage(ce
) == stage
) {
1170 hashcpy(sha1
, ce
->sha1
);
1171 oc
->mode
= ce
->ce_mode
;
1177 if (only_to_die
&& name
[1] && name
[1] != '/')
1178 diagnose_invalid_index_path(stage
, prefix
, cp
);
1182 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1185 else if (bracket_depth
&& *cp
== '}')
1187 else if (!bracket_depth
&& *cp
== ':')
1191 unsigned char tree_sha1
[20];
1192 char *object_name
= NULL
;
1194 object_name
= xmalloc(cp
-name
+1);
1195 strncpy(object_name
, name
, cp
-name
);
1196 object_name
[cp
-name
] = '\0';
1198 if (!get_sha1_1(name
, cp
-name
, tree_sha1
)) {
1199 const char *filename
= cp
+1;
1200 char *new_filename
= NULL
;
1202 new_filename
= resolve_relative_path(filename
);
1204 filename
= new_filename
;
1205 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1207 diagnose_invalid_sha1_path(prefix
, filename
,
1208 tree_sha1
, object_name
);
1211 hashcpy(oc
->tree
, tree_sha1
);
1212 strncpy(oc
->path
, filename
,
1214 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1220 die("Invalid object name '%s'.", object_name
);