10 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
12 struct alternate_object_database
*alt
;
15 static struct alternate_object_database
*fakeent
;
18 const char *objdir
= get_object_directory();
19 int objdir_len
= strlen(objdir
);
20 int entlen
= objdir_len
+ 43;
21 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
22 memcpy(fakeent
->base
, objdir
, objdir_len
);
23 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
24 fakeent
->name
[-1] = '/';
26 fakeent
->next
= alt_odb_list
;
28 sprintf(hex
, "%.2s", name
);
29 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
32 sprintf(alt
->name
, "%.2s/", name
);
33 dir
= opendir(alt
->base
);
36 while ((de
= readdir(dir
)) != NULL
) {
37 if (strlen(de
->d_name
) != 38)
39 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
42 memcpy(hex
+ 2, de
->d_name
, 38);
45 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
53 return get_sha1_hex(hex
, sha1
) == 0;
57 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
72 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
75 const unsigned char *found_sha1
= NULL
;
79 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
) {
85 while (first
< last
) {
86 uint32_t mid
= (first
+ last
) / 2;
87 const unsigned char *now
;
90 now
= nth_packed_object_sha1(p
, mid
);
91 cmp
= hashcmp(match
, now
);
103 const unsigned char *now
, *next
;
104 now
= nth_packed_object_sha1(p
, first
);
105 if (match_sha(len
, match
, now
)) {
106 next
= nth_packed_object_sha1(p
, first
+1);
107 if (!next
|| !match_sha(len
, match
, next
)) {
108 /* unique within this pack */
113 else if (hashcmp(found_sha1
, now
)) {
119 /* not even unique within this pack */
127 hashcpy(sha1
, found_sha1
);
131 #define SHORT_NAME_NOT_FOUND (-1)
132 #define SHORT_NAME_AMBIGUOUS (-2)
134 static int find_unique_short_object(int len
, char *canonical
,
135 unsigned char *res
, unsigned char *sha1
)
137 int has_unpacked
, has_packed
;
138 unsigned char unpacked_sha1
[20], packed_sha1
[20];
141 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
142 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
143 if (!has_unpacked
&& !has_packed
)
144 return SHORT_NAME_NOT_FOUND
;
145 if (1 < has_unpacked
|| 1 < has_packed
)
146 return SHORT_NAME_AMBIGUOUS
;
147 if (has_unpacked
!= has_packed
) {
148 hashcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
));
151 /* Both have unique ones -- do they match? */
152 if (hashcmp(packed_sha1
, unpacked_sha1
))
153 return SHORT_NAME_AMBIGUOUS
;
154 hashcpy(sha1
, packed_sha1
);
158 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
163 unsigned char res
[20];
165 if (len
< MINIMUM_ABBREV
|| len
> 40)
168 memset(canonical
, 'x', 40);
169 for (i
= 0; i
< len
;i
++) {
170 unsigned char c
= name
[i
];
172 if (c
>= '0' && c
<= '9')
174 else if (c
>= 'a' && c
<= 'f')
176 else if (c
>= 'A' && c
<='F') {
188 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
189 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
190 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
194 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
199 exists
= has_sha1_file(sha1
);
200 memcpy(hex
, sha1_to_hex(sha1
), 40);
201 if (len
== 40 || !len
)
204 unsigned char sha1_ret
[20];
205 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
208 : status
== SHORT_NAME_NOT_FOUND
) {
209 int cut_at
= len
+ unique_abbrev_extra_length
;
210 cut_at
= (cut_at
< 40) ? cut_at
: 40;
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
;
568 if (get_sha1_1(name
, sp
- name
- 2, outer
))
571 o
= parse_object(outer
);
574 if (!expected_type
) {
575 o
= deref_tag(o
, name
, sp
- name
- 2);
576 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
578 hashcpy(sha1
, o
->sha1
);
582 * At this point, the syntax look correct, so
583 * if we do not get the needed object, we should
586 o
= peel_to_type(name
, len
, o
, expected_type
);
588 hashcpy(sha1
, o
->sha1
);
596 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
600 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
602 if (hexval(ch
) & ~0377) {
603 /* We must be looking at g in "SOMETHING-g"
604 * for it to be describe output.
606 if (ch
== 'g' && cp
[-1] == '-') {
609 return get_short_sha1(cp
, len
, sha1
, 1);
616 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
622 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
625 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
627 if ('0' <= ch
&& ch
<= '9')
629 if (ch
== '~' || ch
== '^')
636 int len1
= cp
- name
;
638 while (cp
< name
+ len
)
639 num
= num
* 10 + *cp
++ - '0';
640 if (!num
&& len1
== len
- 1)
642 if (has_suffix
== '^')
643 return get_parent(name
, len1
, sha1
, num
);
644 /* else if (has_suffix == '~') -- goes without saying */
645 return get_nth_ancestor(name
, len1
, sha1
, num
);
648 ret
= peel_onion(name
, len
, sha1
);
652 ret
= get_sha1_basic(name
, len
, sha1
);
656 /* It could be describe output that is "SOMETHING-gXXXX" */
657 ret
= get_describe_name(name
, len
, sha1
);
661 return get_short_sha1(name
, len
, sha1
, 0);
665 * This interprets names like ':/Initial revision of "git"' by searching
666 * through history and returning the first commit whose message starts
667 * the given regular expression.
669 * For future extension, ':/!' is reserved. If you want to match a message
670 * beginning with a '!', you have to repeat the exclamation mark.
672 #define ONELINE_SEEN (1u<<20)
674 static int handle_one_ref(const char *path
,
675 const unsigned char *sha1
, int flag
, void *cb_data
)
677 struct commit_list
**list
= cb_data
;
678 struct object
*object
= parse_object(sha1
);
681 if (object
->type
== OBJ_TAG
) {
682 object
= deref_tag(object
, path
, strlen(path
));
686 if (object
->type
!= OBJ_COMMIT
)
688 insert_by_date((struct commit
*)object
, list
);
689 object
->flags
|= ONELINE_SEEN
;
693 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
)
695 struct commit_list
*list
= NULL
, *backup
= NULL
, *l
;
697 char *temp_commit_buffer
= NULL
;
700 if (prefix
[0] == '!') {
701 if (prefix
[1] != '!')
702 die ("Invalid search pattern: %s", prefix
);
706 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
707 die("Invalid search pattern: %s", prefix
);
709 for_each_ref(handle_one_ref
, &list
);
710 for (l
= list
; l
; l
= l
->next
)
711 commit_list_insert(l
->item
, &backup
);
714 struct commit
*commit
;
715 enum object_type type
;
718 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
719 if (!parse_object(commit
->object
.sha1
))
721 free(temp_commit_buffer
);
725 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
728 temp_commit_buffer
= p
;
730 if (!(p
= strstr(p
, "\n\n")))
732 if (!regexec(®ex
, p
+ 2, 0, NULL
, 0)) {
733 hashcpy(sha1
, commit
->object
.sha1
);
739 free(temp_commit_buffer
);
740 free_commit_list(list
);
741 for (l
= backup
; l
; l
= l
->next
)
742 clear_commit_marks(l
->item
, ONELINE_SEEN
);
746 struct grab_nth_branch_switch_cbdata
{
751 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
752 const char *email
, unsigned long timestamp
, int tz
,
753 const char *message
, void *cb_data
)
755 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
756 const char *match
= NULL
, *target
= NULL
;
760 if (!prefixcmp(message
, "checkout: moving from ")) {
761 match
= message
+ strlen("checkout: moving from ");
762 target
= strstr(match
, " to ");
765 if (!match
|| !target
)
768 len
= target
- match
;
769 nth
= cb
->cnt
++ % cb
->alloc
;
770 strbuf_reset(&cb
->buf
[nth
]);
771 strbuf_add(&cb
->buf
[nth
], match
, len
);
776 * 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
);
940 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
941 * notably "xyz^" for "parent of xyz"
943 int get_sha1(const char *name
, unsigned char *sha1
)
945 struct object_context unused
;
946 return get_sha1_with_context(name
, sha1
, &unused
);
949 /* Must be called only when object_name:filename doesn't exist. */
950 static void diagnose_invalid_sha1_path(const char *prefix
,
951 const char *filename
,
952 const unsigned char *tree_sha1
,
953 const char *object_name
)
956 unsigned char sha1
[20];
962 if (!lstat(filename
, &st
))
963 die("Path '%s' exists on disk, but not in '%s'.",
964 filename
, object_name
);
965 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
966 char *fullname
= xmalloc(strlen(filename
)
967 + strlen(prefix
) + 1);
968 strcpy(fullname
, prefix
);
969 strcat(fullname
, filename
);
971 if (!get_tree_entry(tree_sha1
, fullname
,
973 die("Path '%s' exists, but not '%s'.\n"
974 "Did you mean '%s:%s'?",
980 die("Path '%s' does not exist in '%s'",
981 filename
, object_name
);
985 /* Must be called only when :stage:filename doesn't exist. */
986 static void diagnose_invalid_index_path(int stage
,
988 const char *filename
)
991 struct cache_entry
*ce
;
993 unsigned namelen
= strlen(filename
);
994 unsigned fullnamelen
;
1000 /* Wrong stage number? */
1001 pos
= cache_name_pos(filename
, namelen
);
1004 if (pos
< active_nr
) {
1005 ce
= active_cache
[pos
];
1006 if (ce_namelen(ce
) == namelen
&&
1007 !memcmp(ce
->name
, filename
, namelen
))
1008 die("Path '%s' is in the index, but not at stage %d.\n"
1009 "Did you mean ':%d:%s'?",
1011 ce_stage(ce
), filename
);
1014 /* Confusion between relative and absolute filenames? */
1015 fullnamelen
= namelen
+ strlen(prefix
);
1016 fullname
= xmalloc(fullnamelen
+ 1);
1017 strcpy(fullname
, prefix
);
1018 strcat(fullname
, filename
);
1019 pos
= cache_name_pos(fullname
, fullnamelen
);
1022 if (pos
< active_nr
) {
1023 ce
= active_cache
[pos
];
1024 if (ce_namelen(ce
) == fullnamelen
&&
1025 !memcmp(ce
->name
, fullname
, fullnamelen
))
1026 die("Path '%s' is in the index, but not '%s'.\n"
1027 "Did you mean ':%d:%s'?",
1029 ce_stage(ce
), fullname
);
1032 if (!lstat(filename
, &st
))
1033 die("Path '%s' exists on disk, but not in the index.", filename
);
1034 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1035 die("Path '%s' does not exist (neither on disk nor in the index).",
1042 int get_sha1_with_mode_1(const char *name
, unsigned char *sha1
, unsigned *mode
, int gently
, const char *prefix
)
1044 struct object_context oc
;
1046 ret
= get_sha1_with_context_1(name
, sha1
, &oc
, gently
, prefix
);
1051 int get_sha1_with_context_1(const char *name
, unsigned char *sha1
,
1052 struct object_context
*oc
,
1053 int gently
, const char *prefix
)
1055 int ret
, bracket_depth
;
1056 int namelen
= strlen(name
);
1059 memset(oc
, 0, sizeof(*oc
));
1060 oc
->mode
= S_IFINVALID
;
1061 ret
= get_sha1_1(name
, namelen
, sha1
);
1064 /* sha1:path --> object name of path in ent sha1
1065 * :path -> object name of path in index
1066 * :[0-3]:path -> object name of path in index at stage
1067 * :/foo -> recent commit matching foo
1069 if (name
[0] == ':') {
1071 struct cache_entry
*ce
;
1073 if (namelen
> 2 && name
[1] == '/')
1074 /* don't need mode for commit */
1075 return get_sha1_oneline(name
+ 2, sha1
);
1078 name
[1] < '0' || '3' < name
[1])
1081 stage
= name
[1] - '0';
1084 namelen
= namelen
- (cp
- name
);
1086 strncpy(oc
->path
, cp
,
1088 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1092 pos
= cache_name_pos(cp
, namelen
);
1095 while (pos
< active_nr
) {
1096 ce
= active_cache
[pos
];
1097 if (ce_namelen(ce
) != namelen
||
1098 memcmp(ce
->name
, cp
, namelen
))
1100 if (ce_stage(ce
) == stage
) {
1101 hashcpy(sha1
, ce
->sha1
);
1102 oc
->mode
= ce
->ce_mode
;
1108 diagnose_invalid_index_path(stage
, prefix
, cp
);
1111 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1114 else if (bracket_depth
&& *cp
== '}')
1116 else if (!bracket_depth
&& *cp
== ':')
1120 unsigned char tree_sha1
[20];
1121 char *object_name
= NULL
;
1123 object_name
= xmalloc(cp
-name
+1);
1124 strncpy(object_name
, name
, cp
-name
);
1125 object_name
[cp
-name
] = '\0';
1127 if (!get_sha1_1(name
, cp
-name
, tree_sha1
)) {
1128 const char *filename
= cp
+1;
1129 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1131 diagnose_invalid_sha1_path(prefix
, filename
,
1132 tree_sha1
, object_name
);
1135 hashcpy(oc
->tree
, tree_sha1
);
1136 strncpy(oc
->path
, filename
,
1138 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1143 die("Invalid object name '%s'.", object_name
);