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
++) {
244 static inline int upstream_mark(const char *string
, int len
)
246 const char *suffix
[] = { "@{upstream}", "@{u}" };
249 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
250 int suffix_len
= strlen(suffix
[i
]);
251 if (suffix_len
<= len
252 && !memcmp(string
, suffix
[i
], suffix_len
))
258 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
260 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
262 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
263 char *real_ref
= NULL
;
267 if (len
== 40 && !get_sha1_hex(str
, sha1
))
270 /* basic@{time or number or -number} format to query ref-log */
272 if (len
&& str
[len
-1] == '}') {
273 for (at
= len
-2; at
>= 0; at
--) {
274 if (str
[at
] == '@' && str
[at
+1] == '{') {
275 if (!upstream_mark(str
+ at
, len
- at
)) {
276 reflog_len
= (len
-1) - (at
+2);
284 /* Accept only unambiguous ref paths. */
285 if (len
&& ambiguous_path(str
, len
))
288 if (!len
&& reflog_len
) {
289 struct strbuf buf
= STRBUF_INIT
;
291 /* try the @{-N} syntax for n-th checkout */
292 ret
= interpret_branch_name(str
+at
, &buf
);
294 /* substitute this branch name and restart */
295 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
296 } else if (ret
== 0) {
299 /* allow "@{...}" to mean the current branch reflog */
300 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
301 } else if (reflog_len
)
302 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
304 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
309 if (warn_ambiguous_refs
&& refs_found
> 1)
310 warning(warn_msg
, len
, str
);
314 unsigned long at_time
;
315 unsigned long co_time
;
318 /* a @{-N} placed anywhere except the start is an error */
319 if (str
[at
+2] == '-')
322 /* Is it asking for N-th entry, or approxidate? */
323 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
324 char ch
= str
[at
+2+i
];
325 if ('0' <= ch
&& ch
<= '9')
326 nth
= nth
* 10 + ch
- '0';
330 if (100000000 <= nth
) {
337 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
338 at_time
= approxidate_careful(tmp
, &errors
);
343 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
344 &co_time
, &co_tz
, &co_cnt
)) {
346 warning("Log for '%.*s' only goes "
347 "back to %s.", len
, str
,
348 show_date(co_time
, co_tz
, DATE_RFC2822
));
351 die("Log for '%.*s' only has %d entries.",
361 static int get_parent(const char *name
, int len
,
362 unsigned char *result
, int idx
)
364 unsigned char sha1
[20];
365 int ret
= get_sha1_1(name
, len
, sha1
);
366 struct commit
*commit
;
367 struct commit_list
*p
;
371 commit
= lookup_commit_reference(sha1
);
374 if (parse_commit(commit
))
377 hashcpy(result
, commit
->object
.sha1
);
383 hashcpy(result
, p
->item
->object
.sha1
);
391 static int get_nth_ancestor(const char *name
, int len
,
392 unsigned char *result
, int generation
)
394 unsigned char sha1
[20];
395 struct commit
*commit
;
398 ret
= get_sha1_1(name
, len
, sha1
);
401 commit
= lookup_commit_reference(sha1
);
405 while (generation
--) {
406 if (parse_commit(commit
) || !commit
->parents
)
408 commit
= commit
->parents
->item
;
410 hashcpy(result
, commit
->object
.sha1
);
414 struct object
*peel_to_type(const char *name
, int namelen
,
415 struct object
*o
, enum object_type expected_type
)
417 if (name
&& !namelen
)
418 namelen
= strlen(name
);
420 unsigned char sha1
[20];
421 if (get_sha1_1(name
, namelen
, sha1
))
423 o
= parse_object(sha1
);
426 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
428 if (o
->type
== expected_type
)
430 if (o
->type
== OBJ_TAG
)
431 o
= ((struct tag
*) o
)->tagged
;
432 else if (o
->type
== OBJ_COMMIT
)
433 o
= &(((struct commit
*) o
)->tree
->object
);
436 error("%.*s: expected %s type, but the object "
437 "dereferences to %s type",
438 namelen
, name
, typename(expected_type
),
445 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
447 unsigned char outer
[20];
449 unsigned int expected_type
= 0;
453 * "ref^{type}" dereferences ref repeatedly until you cannot
454 * dereference anymore, or you get an object of given type,
455 * whichever comes first. "ref^{}" means just dereference
456 * tags until you get a non-tag. "ref^0" is a shorthand for
457 * "ref^{commit}". "commit^{tree}" could be used to find the
458 * top-level tree of the given commit.
460 if (len
< 4 || name
[len
-1] != '}')
463 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
465 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
471 sp
++; /* beginning of type name, or closing brace for empty */
472 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
473 expected_type
= OBJ_COMMIT
;
474 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
475 expected_type
= OBJ_TREE
;
476 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
477 expected_type
= OBJ_BLOB
;
478 else if (sp
[0] == '}')
479 expected_type
= OBJ_NONE
;
480 else if (sp
[0] == '/')
481 expected_type
= OBJ_COMMIT
;
485 if (get_sha1_1(name
, sp
- name
- 2, outer
))
488 o
= parse_object(outer
);
491 if (!expected_type
) {
492 o
= deref_tag(o
, name
, sp
- name
- 2);
493 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
495 hashcpy(sha1
, o
->sha1
);
500 * At this point, the syntax look correct, so
501 * if we do not get the needed object, we should
504 o
= peel_to_type(name
, len
, o
, expected_type
);
508 hashcpy(sha1
, o
->sha1
);
510 /* "$commit^{/foo}" */
513 struct commit_list
*list
= NULL
;
516 * $commit^{/}. Some regex implementation may reject.
517 * We don't need regex anyway. '' pattern always matches.
522 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
523 commit_list_insert((struct commit
*)o
, &list
);
524 ret
= get_sha1_oneline(prefix
, sha1
, list
);
531 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
535 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
537 if (hexval(ch
) & ~0377) {
538 /* We must be looking at g in "SOMETHING-g"
539 * for it to be describe output.
541 if (ch
== 'g' && cp
[-1] == '-') {
544 return get_short_sha1(cp
, len
, sha1
, 1);
551 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
557 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
560 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
562 if ('0' <= ch
&& ch
<= '9')
564 if (ch
== '~' || ch
== '^')
571 int len1
= cp
- name
;
573 while (cp
< name
+ len
)
574 num
= num
* 10 + *cp
++ - '0';
575 if (!num
&& len1
== len
- 1)
577 if (has_suffix
== '^')
578 return get_parent(name
, len1
, sha1
, num
);
579 /* else if (has_suffix == '~') -- goes without saying */
580 return get_nth_ancestor(name
, len1
, sha1
, num
);
583 ret
= peel_onion(name
, len
, sha1
);
587 ret
= get_sha1_basic(name
, len
, sha1
);
591 /* It could be describe output that is "SOMETHING-gXXXX" */
592 ret
= get_describe_name(name
, len
, sha1
);
596 return get_short_sha1(name
, len
, sha1
, 0);
600 * This interprets names like ':/Initial revision of "git"' by searching
601 * through history and returning the first commit whose message starts
602 * the given regular expression.
604 * For future extension, ':/!' is reserved. If you want to match a message
605 * beginning with a '!', you have to repeat the exclamation mark.
607 #define ONELINE_SEEN (1u<<20)
609 static int handle_one_ref(const char *path
,
610 const unsigned char *sha1
, int flag
, void *cb_data
)
612 struct commit_list
**list
= cb_data
;
613 struct object
*object
= parse_object(sha1
);
616 if (object
->type
== OBJ_TAG
) {
617 object
= deref_tag(object
, path
, strlen(path
));
621 if (object
->type
!= OBJ_COMMIT
)
623 commit_list_insert_by_date((struct commit
*)object
, list
);
627 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
628 struct commit_list
*list
)
630 struct commit_list
*backup
= NULL
, *l
;
634 if (prefix
[0] == '!') {
635 if (prefix
[1] != '!')
636 die ("Invalid search pattern: %s", prefix
);
640 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
641 die("Invalid search pattern: %s", prefix
);
643 for (l
= list
; l
; l
= l
->next
) {
644 l
->item
->object
.flags
|= ONELINE_SEEN
;
645 commit_list_insert(l
->item
, &backup
);
648 char *p
, *to_free
= NULL
;
649 struct commit
*commit
;
650 enum object_type type
;
654 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
655 if (!parse_object(commit
->object
.sha1
))
660 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
666 p
= strstr(p
, "\n\n");
667 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
671 hashcpy(sha1
, commit
->object
.sha1
);
677 free_commit_list(list
);
678 for (l
= backup
; l
; l
= l
->next
)
679 clear_commit_marks(l
->item
, ONELINE_SEEN
);
680 free_commit_list(backup
);
681 return found
? 0 : -1;
684 struct grab_nth_branch_switch_cbdata
{
689 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
690 const char *email
, unsigned long timestamp
, int tz
,
691 const char *message
, void *cb_data
)
693 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
694 const char *match
= NULL
, *target
= NULL
;
698 if (!prefixcmp(message
, "checkout: moving from ")) {
699 match
= message
+ strlen("checkout: moving from ");
700 target
= strstr(match
, " to ");
703 if (!match
|| !target
)
706 len
= target
- match
;
707 nth
= cb
->cnt
++ % cb
->alloc
;
708 strbuf_reset(&cb
->buf
[nth
]);
709 strbuf_add(&cb
->buf
[nth
], match
, len
);
714 * Parse @{-N} syntax, return the number of characters parsed
715 * if successful; otherwise signal an error with negative value.
717 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
721 struct grab_nth_branch_switch_cbdata cb
;
725 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
727 brace
= strchr(name
, '}');
730 nth
= strtol(name
+3, &num_end
, 10);
731 if (num_end
!= brace
)
736 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
737 for (i
= 0; i
< nth
; i
++)
738 strbuf_init(&cb
.buf
[i
], 20);
741 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
744 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
750 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
751 retval
= brace
-name
+1;
754 for (i
= 0; i
< nth
; i
++)
755 strbuf_release(&cb
.buf
[i
]);
761 int get_sha1_mb(const char *name
, unsigned char *sha1
)
763 struct commit
*one
, *two
;
764 struct commit_list
*mbs
;
765 unsigned char sha1_tmp
[20];
769 dots
= strstr(name
, "...");
771 return get_sha1(name
, sha1
);
773 st
= get_sha1("HEAD", sha1_tmp
);
776 strbuf_init(&sb
, dots
- name
);
777 strbuf_add(&sb
, name
, dots
- name
);
778 st
= get_sha1(sb
.buf
, sha1_tmp
);
783 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
787 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
789 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
792 mbs
= get_merge_bases(one
, two
, 1);
793 if (!mbs
|| mbs
->next
)
797 hashcpy(sha1
, mbs
->item
->object
.sha1
);
799 free_commit_list(mbs
);
804 * This reads short-hand syntax that not only evaluates to a commit
805 * object name, but also can act as if the end user spelled the name
806 * of the branch from the command line.
808 * - "@{-N}" finds the name of the Nth previous branch we were on, and
809 * places the name of the branch in the given buf and returns the
810 * number of characters parsed if successful.
812 * - "<branch>@{upstream}" finds the name of the other ref that
813 * <branch> is configured to merge with (missing <branch> defaults
814 * to the current branch), and places the name of the branch in the
815 * given buf and returns the number of characters parsed if
818 * If the input is not of the accepted format, it returns a negative
819 * number to signal an error.
821 * If the input was ok but there are not N branch switches in the
822 * reflog, it returns 0.
824 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
827 struct branch
*upstream
;
828 int namelen
= strlen(name
);
829 int len
= interpret_nth_prior_checkout(name
, buf
);
833 return len
; /* syntax Ok, not enough switches */
834 if (0 < len
&& len
== namelen
)
835 return len
; /* consumed all */
837 /* we have extra data, which might need further processing */
838 struct strbuf tmp
= STRBUF_INIT
;
842 strbuf_add(buf
, name
+ len
, namelen
- len
);
843 ret
= interpret_branch_name(buf
->buf
, &tmp
);
844 /* that data was not interpreted, remove our cruft */
846 strbuf_setlen(buf
, used
);
850 strbuf_addbuf(buf
, &tmp
);
851 strbuf_release(&tmp
);
852 /* tweak for size of {-N} versus expanded ref name */
853 return ret
- used
+ len
;
856 cp
= strchr(name
, '@');
859 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
862 len
= cp
+ tmp_len
- name
;
863 cp
= xstrndup(name
, cp
- name
);
864 upstream
= branch_get(*cp
? cp
: NULL
);
867 || !upstream
->merge
[0]->dst
)
868 return error("No upstream branch found for '%s'", cp
);
870 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
872 strbuf_addstr(buf
, cp
);
877 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
879 int len
= strlen(name
);
880 if (interpret_branch_name(name
, sb
) == len
)
882 strbuf_add(sb
, name
, len
);
886 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
888 strbuf_branchname(sb
, name
);
891 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
892 return check_refname_format(sb
->buf
, 0);
896 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
897 * notably "xyz^" for "parent of xyz"
899 int get_sha1(const char *name
, unsigned char *sha1
)
901 struct object_context unused
;
902 return get_sha1_with_context(name
, sha1
, &unused
);
905 /* Must be called only when object_name:filename doesn't exist. */
906 static void diagnose_invalid_sha1_path(const char *prefix
,
907 const char *filename
,
908 const unsigned char *tree_sha1
,
909 const char *object_name
)
912 unsigned char sha1
[20];
918 if (!lstat(filename
, &st
))
919 die("Path '%s' exists on disk, but not in '%s'.",
920 filename
, object_name
);
921 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
922 char *fullname
= xmalloc(strlen(filename
)
923 + strlen(prefix
) + 1);
924 strcpy(fullname
, prefix
);
925 strcat(fullname
, filename
);
927 if (!get_tree_entry(tree_sha1
, fullname
,
929 die("Path '%s' exists, but not '%s'.\n"
930 "Did you mean '%s:%s' aka '%s:./%s'?",
938 die("Path '%s' does not exist in '%s'",
939 filename
, object_name
);
943 /* Must be called only when :stage:filename doesn't exist. */
944 static void diagnose_invalid_index_path(int stage
,
946 const char *filename
)
949 struct cache_entry
*ce
;
951 unsigned namelen
= strlen(filename
);
952 unsigned fullnamelen
;
958 /* Wrong stage number? */
959 pos
= cache_name_pos(filename
, namelen
);
962 if (pos
< active_nr
) {
963 ce
= active_cache
[pos
];
964 if (ce_namelen(ce
) == namelen
&&
965 !memcmp(ce
->name
, filename
, namelen
))
966 die("Path '%s' is in the index, but not at stage %d.\n"
967 "Did you mean ':%d:%s'?",
969 ce_stage(ce
), filename
);
972 /* Confusion between relative and absolute filenames? */
973 fullnamelen
= namelen
+ strlen(prefix
);
974 fullname
= xmalloc(fullnamelen
+ 1);
975 strcpy(fullname
, prefix
);
976 strcat(fullname
, filename
);
977 pos
= cache_name_pos(fullname
, fullnamelen
);
980 if (pos
< active_nr
) {
981 ce
= active_cache
[pos
];
982 if (ce_namelen(ce
) == fullnamelen
&&
983 !memcmp(ce
->name
, fullname
, fullnamelen
))
984 die("Path '%s' is in the index, but not '%s'.\n"
985 "Did you mean ':%d:%s' aka ':%d:./%s'?",
987 ce_stage(ce
), fullname
,
988 ce_stage(ce
), filename
);
991 if (!lstat(filename
, &st
))
992 die("Path '%s' exists on disk, but not in the index.", filename
);
993 if (errno
== ENOENT
|| errno
== ENOTDIR
)
994 die("Path '%s' does not exist (neither on disk nor in the index).",
1001 int get_sha1_with_mode_1(const char *name
, unsigned char *sha1
, unsigned *mode
,
1002 int only_to_die
, const char *prefix
)
1004 struct object_context oc
;
1006 ret
= get_sha1_with_context_1(name
, sha1
, &oc
, only_to_die
, prefix
);
1011 static char *resolve_relative_path(const char *rel
)
1013 if (prefixcmp(rel
, "./") && prefixcmp(rel
, "../"))
1017 die("BUG: startup_info struct is not initialized.");
1019 if (!is_inside_work_tree())
1020 die("relative path syntax can't be used outside working tree.");
1022 /* die() inside prefix_path() if resolved path is outside worktree */
1023 return prefix_path(startup_info
->prefix
,
1024 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1028 int get_sha1_with_context_1(const char *name
, unsigned char *sha1
,
1029 struct object_context
*oc
,
1030 int only_to_die
, const char *prefix
)
1032 int ret
, bracket_depth
;
1033 int namelen
= strlen(name
);
1036 memset(oc
, 0, sizeof(*oc
));
1037 oc
->mode
= S_IFINVALID
;
1038 ret
= get_sha1_1(name
, namelen
, sha1
);
1041 /* sha1:path --> object name of path in ent sha1
1042 * :path -> object name of absolute path in index
1043 * :./path -> object name of path relative to cwd in index
1044 * :[0-3]:path -> object name of path in index at stage
1045 * :/foo -> recent commit matching foo
1047 if (name
[0] == ':') {
1049 struct cache_entry
*ce
;
1050 char *new_path
= NULL
;
1052 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1053 struct commit_list
*list
= NULL
;
1054 for_each_ref(handle_one_ref
, &list
);
1055 return get_sha1_oneline(name
+ 2, sha1
, list
);
1059 name
[1] < '0' || '3' < name
[1])
1062 stage
= name
[1] - '0';
1065 new_path
= resolve_relative_path(cp
);
1067 namelen
= namelen
- (cp
- name
);
1070 namelen
= strlen(cp
);
1073 strncpy(oc
->path
, cp
,
1075 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1079 pos
= cache_name_pos(cp
, namelen
);
1082 while (pos
< active_nr
) {
1083 ce
= active_cache
[pos
];
1084 if (ce_namelen(ce
) != namelen
||
1085 memcmp(ce
->name
, cp
, namelen
))
1087 if (ce_stage(ce
) == stage
) {
1088 hashcpy(sha1
, ce
->sha1
);
1089 oc
->mode
= ce
->ce_mode
;
1095 if (only_to_die
&& name
[1] && name
[1] != '/')
1096 diagnose_invalid_index_path(stage
, prefix
, cp
);
1100 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1103 else if (bracket_depth
&& *cp
== '}')
1105 else if (!bracket_depth
&& *cp
== ':')
1109 unsigned char tree_sha1
[20];
1110 char *object_name
= NULL
;
1112 object_name
= xmalloc(cp
-name
+1);
1113 strncpy(object_name
, name
, cp
-name
);
1114 object_name
[cp
-name
] = '\0';
1116 if (!get_sha1_1(name
, cp
-name
, tree_sha1
)) {
1117 const char *filename
= cp
+1;
1118 char *new_filename
= NULL
;
1120 new_filename
= resolve_relative_path(filename
);
1122 filename
= new_filename
;
1123 ret
= get_tree_entry(tree_sha1
, filename
, sha1
, &oc
->mode
);
1125 diagnose_invalid_sha1_path(prefix
, filename
,
1126 tree_sha1
, object_name
);
1129 hashcpy(oc
->tree
, tree_sha1
);
1130 strncpy(oc
->path
, filename
,
1132 oc
->path
[sizeof(oc
->path
)-1] = '\0';
1138 die("Invalid object name '%s'.", object_name
);