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
;
21 * Create a "fake" alternate object database that
22 * points to our own object database, to make it
23 * easier to get a temporary working space in
24 * alt->name/alt->base while iterating over the
25 * object databases including our own.
27 const char *objdir
= get_object_directory();
28 int objdir_len
= strlen(objdir
);
29 int entlen
= objdir_len
+ 43;
30 fakeent
= xmalloc(sizeof(*fakeent
) + entlen
);
31 memcpy(fakeent
->base
, objdir
, objdir_len
);
32 fakeent
->name
= fakeent
->base
+ objdir_len
+ 1;
33 fakeent
->name
[-1] = '/';
35 fakeent
->next
= alt_odb_list
;
37 sprintf(hex
, "%.2s", name
);
38 for (alt
= fakeent
; alt
&& found
< 2; alt
= alt
->next
) {
41 sprintf(alt
->name
, "%.2s/", name
);
42 dir
= opendir(alt
->base
);
45 while ((de
= readdir(dir
)) != NULL
) {
46 if (strlen(de
->d_name
) != 38)
48 if (memcmp(de
->d_name
, name
+ 2, len
- 2))
51 memcpy(hex
+ 2, de
->d_name
, 38);
54 else if (memcmp(hex
+ 2, de
->d_name
, 38)) {
62 return get_sha1_hex(hex
, sha1
) == 0;
66 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
81 static int unique_in_pack(int len
,
82 const unsigned char *match
,
84 const unsigned char **found_sha1
,
87 uint32_t num
, last
, i
, first
= 0;
88 const unsigned char *current
= NULL
;
93 while (first
< last
) {
94 uint32_t mid
= (first
+ last
) / 2;
95 const unsigned char *current
;
98 current
= nth_packed_object_sha1(p
, mid
);
99 cmp
= hashcmp(match
, current
);
112 * At this point, "first" is the location of the lowest object
113 * with an object name that could match "match". See if we have
114 * 0, 1 or more objects that actually match(es).
116 for (i
= first
; i
< num
; i
++) {
117 current
= nth_packed_object_sha1(p
, first
);
118 if (!match_sha(len
, match
, current
))
121 /* current matches */
123 *found_sha1
= current
;
125 } else if (seen_so_far
) {
126 /* is it the same as the one previously found elsewhere? */
127 if (hashcmp(*found_sha1
, current
))
128 return 2; /* definitely not unique */
134 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
136 struct packed_git
*p
;
137 const unsigned char *found_sha1
= NULL
;
140 prepare_packed_git();
141 for (p
= packed_git
; p
&& found
< 2; p
= p
->next
)
142 found
= unique_in_pack(len
, match
, p
, &found_sha1
, found
);
145 hashcpy(sha1
, found_sha1
);
149 #define SHORT_NAME_NOT_FOUND (-1)
150 #define SHORT_NAME_AMBIGUOUS (-2)
152 static int find_unique_short_object(int len
, char *canonical
,
153 unsigned char *res
, unsigned char *sha1
)
155 int has_unpacked
, has_packed
;
156 unsigned char unpacked_sha1
[20], packed_sha1
[20];
159 has_unpacked
= find_short_object_filename(len
, canonical
, unpacked_sha1
);
160 has_packed
= find_short_packed_object(len
, res
, packed_sha1
);
161 if (!has_unpacked
&& !has_packed
)
162 return SHORT_NAME_NOT_FOUND
;
163 if (1 < has_unpacked
|| 1 < has_packed
)
164 return SHORT_NAME_AMBIGUOUS
;
165 if (has_unpacked
!= has_packed
) {
166 hashcpy(sha1
, (has_packed
? packed_sha1
: unpacked_sha1
));
169 /* Both have unique ones -- do they match? */
170 if (hashcmp(packed_sha1
, unpacked_sha1
))
171 return SHORT_NAME_AMBIGUOUS
;
172 hashcpy(sha1
, packed_sha1
);
176 static int get_short_sha1(const char *name
, int len
, unsigned char *sha1
,
181 unsigned char res
[20];
183 if (len
< MINIMUM_ABBREV
|| len
> 40)
186 memset(canonical
, 'x', 40);
187 for (i
= 0; i
< len
;i
++) {
188 unsigned char c
= name
[i
];
190 if (c
>= '0' && c
<= '9')
192 else if (c
>= 'a' && c
<= 'f')
194 else if (c
>= 'A' && c
<='F') {
206 status
= find_unique_short_object(i
, canonical
, res
, sha1
);
207 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
))
208 return error("short SHA1 %.*s is ambiguous.", len
, canonical
);
212 const char *find_unique_abbrev(const unsigned char *sha1
, int len
)
217 exists
= has_sha1_file(sha1
);
218 memcpy(hex
, sha1_to_hex(sha1
), 40);
219 if (len
== 40 || !len
)
222 unsigned char sha1_ret
[20];
223 status
= get_short_sha1(hex
, len
, sha1_ret
, 1);
226 : status
== SHORT_NAME_NOT_FOUND
) {
235 static int ambiguous_path(const char *path
, int len
)
240 for (cnt
= 0; cnt
< len
; cnt
++) {
260 static inline int upstream_mark(const char *string
, int len
)
262 const char *suffix
[] = { "@{upstream}", "@{u}" };
265 for (i
= 0; i
< ARRAY_SIZE(suffix
); i
++) {
266 int suffix_len
= strlen(suffix
[i
]);
267 if (suffix_len
<= len
268 && !memcmp(string
, suffix
[i
], suffix_len
))
274 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
);
276 static int get_sha1_basic(const char *str
, int len
, unsigned char *sha1
)
278 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
279 char *real_ref
= NULL
;
283 if (len
== 40 && !get_sha1_hex(str
, sha1
))
286 /* basic@{time or number or -number} format to query ref-log */
288 if (len
&& str
[len
-1] == '}') {
289 for (at
= len
-2; at
>= 0; at
--) {
290 if (str
[at
] == '@' && str
[at
+1] == '{') {
291 if (!upstream_mark(str
+ at
, len
- at
)) {
292 reflog_len
= (len
-1) - (at
+2);
300 /* Accept only unambiguous ref paths. */
301 if (len
&& ambiguous_path(str
, len
))
304 if (!len
&& reflog_len
) {
305 struct strbuf buf
= STRBUF_INIT
;
307 /* try the @{-N} syntax for n-th checkout */
308 ret
= interpret_branch_name(str
+at
, &buf
);
310 /* substitute this branch name and restart */
311 return get_sha1_1(buf
.buf
, buf
.len
, sha1
);
312 } else if (ret
== 0) {
315 /* allow "@{...}" to mean the current branch reflog */
316 refs_found
= dwim_ref("HEAD", 4, sha1
, &real_ref
);
317 } else if (reflog_len
)
318 refs_found
= dwim_log(str
, len
, sha1
, &real_ref
);
320 refs_found
= dwim_ref(str
, len
, sha1
, &real_ref
);
325 if (warn_ambiguous_refs
&& refs_found
> 1)
326 warning(warn_msg
, len
, str
);
330 unsigned long at_time
;
331 unsigned long co_time
;
334 /* a @{-N} placed anywhere except the start is an error */
335 if (str
[at
+2] == '-')
338 /* Is it asking for N-th entry, or approxidate? */
339 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
340 char ch
= str
[at
+2+i
];
341 if ('0' <= ch
&& ch
<= '9')
342 nth
= nth
* 10 + ch
- '0';
346 if (100000000 <= nth
) {
353 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
354 at_time
= approxidate_careful(tmp
, &errors
);
359 if (read_ref_at(real_ref
, at_time
, nth
, sha1
, NULL
,
360 &co_time
, &co_tz
, &co_cnt
)) {
362 warning("Log for '%.*s' only goes "
363 "back to %s.", len
, str
,
364 show_date(co_time
, co_tz
, DATE_RFC2822
));
367 die("Log for '%.*s' only has %d entries.",
377 static int get_parent(const char *name
, int len
,
378 unsigned char *result
, int idx
)
380 unsigned char sha1
[20];
381 int ret
= get_sha1_1(name
, len
, sha1
);
382 struct commit
*commit
;
383 struct commit_list
*p
;
387 commit
= lookup_commit_reference(sha1
);
390 if (parse_commit(commit
))
393 hashcpy(result
, commit
->object
.sha1
);
399 hashcpy(result
, p
->item
->object
.sha1
);
407 static int get_nth_ancestor(const char *name
, int len
,
408 unsigned char *result
, int generation
)
410 unsigned char sha1
[20];
411 struct commit
*commit
;
414 ret
= get_sha1_1(name
, len
, sha1
);
417 commit
= lookup_commit_reference(sha1
);
421 while (generation
--) {
422 if (parse_commit(commit
) || !commit
->parents
)
424 commit
= commit
->parents
->item
;
426 hashcpy(result
, commit
->object
.sha1
);
430 struct object
*peel_to_type(const char *name
, int namelen
,
431 struct object
*o
, enum object_type expected_type
)
433 if (name
&& !namelen
)
434 namelen
= strlen(name
);
436 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
438 if (o
->type
== expected_type
)
440 if (o
->type
== OBJ_TAG
)
441 o
= ((struct tag
*) o
)->tagged
;
442 else if (o
->type
== OBJ_COMMIT
)
443 o
= &(((struct commit
*) o
)->tree
->object
);
446 error("%.*s: expected %s type, but the object "
447 "dereferences to %s type",
448 namelen
, name
, typename(expected_type
),
455 static int peel_onion(const char *name
, int len
, unsigned char *sha1
)
457 unsigned char outer
[20];
459 unsigned int expected_type
= 0;
463 * "ref^{type}" dereferences ref repeatedly until you cannot
464 * dereference anymore, or you get an object of given type,
465 * whichever comes first. "ref^{}" means just dereference
466 * tags until you get a non-tag. "ref^0" is a shorthand for
467 * "ref^{commit}". "commit^{tree}" could be used to find the
468 * top-level tree of the given commit.
470 if (len
< 4 || name
[len
-1] != '}')
473 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
475 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
481 sp
++; /* beginning of type name, or closing brace for empty */
482 if (!strncmp(commit_type
, sp
, 6) && sp
[6] == '}')
483 expected_type
= OBJ_COMMIT
;
484 else if (!strncmp(tree_type
, sp
, 4) && sp
[4] == '}')
485 expected_type
= OBJ_TREE
;
486 else if (!strncmp(blob_type
, sp
, 4) && sp
[4] == '}')
487 expected_type
= OBJ_BLOB
;
488 else if (sp
[0] == '}')
489 expected_type
= OBJ_NONE
;
490 else if (sp
[0] == '/')
491 expected_type
= OBJ_COMMIT
;
495 if (get_sha1_1(name
, sp
- name
- 2, outer
))
498 o
= parse_object(outer
);
501 if (!expected_type
) {
502 o
= deref_tag(o
, name
, sp
- name
- 2);
503 if (!o
|| (!o
->parsed
&& !parse_object(o
->sha1
)))
505 hashcpy(sha1
, o
->sha1
);
510 * At this point, the syntax look correct, so
511 * if we do not get the needed object, we should
514 o
= peel_to_type(name
, len
, o
, expected_type
);
518 hashcpy(sha1
, o
->sha1
);
520 /* "$commit^{/foo}" */
523 struct commit_list
*list
= NULL
;
526 * $commit^{/}. Some regex implementation may reject.
527 * We don't need regex anyway. '' pattern always matches.
532 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
533 commit_list_insert((struct commit
*)o
, &list
);
534 ret
= get_sha1_oneline(prefix
, sha1
, list
);
541 static int get_describe_name(const char *name
, int len
, unsigned char *sha1
)
545 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
547 if (hexval(ch
) & ~0377) {
548 /* We must be looking at g in "SOMETHING-g"
549 * for it to be describe output.
551 if (ch
== 'g' && cp
[-1] == '-') {
554 return get_short_sha1(cp
, len
, sha1
, 1);
561 static int get_sha1_1(const char *name
, int len
, unsigned char *sha1
)
567 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
570 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
572 if ('0' <= ch
&& ch
<= '9')
574 if (ch
== '~' || ch
== '^')
581 int len1
= cp
- name
;
583 while (cp
< name
+ len
)
584 num
= num
* 10 + *cp
++ - '0';
585 if (!num
&& len1
== len
- 1)
587 if (has_suffix
== '^')
588 return get_parent(name
, len1
, sha1
, num
);
589 /* else if (has_suffix == '~') -- goes without saying */
590 return get_nth_ancestor(name
, len1
, sha1
, num
);
593 ret
= peel_onion(name
, len
, sha1
);
597 ret
= get_sha1_basic(name
, len
, sha1
);
601 /* It could be describe output that is "SOMETHING-gXXXX" */
602 ret
= get_describe_name(name
, len
, sha1
);
606 return get_short_sha1(name
, len
, sha1
, 0);
610 * This interprets names like ':/Initial revision of "git"' by searching
611 * through history and returning the first commit whose message starts
612 * the given regular expression.
614 * For future extension, ':/!' is reserved. If you want to match a message
615 * beginning with a '!', you have to repeat the exclamation mark.
617 #define ONELINE_SEEN (1u<<20)
619 static int handle_one_ref(const char *path
,
620 const unsigned char *sha1
, int flag
, void *cb_data
)
622 struct commit_list
**list
= cb_data
;
623 struct object
*object
= parse_object(sha1
);
626 if (object
->type
== OBJ_TAG
) {
627 object
= deref_tag(object
, path
, strlen(path
));
631 if (object
->type
!= OBJ_COMMIT
)
633 commit_list_insert_by_date((struct commit
*)object
, list
);
637 static int get_sha1_oneline(const char *prefix
, unsigned char *sha1
,
638 struct commit_list
*list
)
640 struct commit_list
*backup
= NULL
, *l
;
644 if (prefix
[0] == '!') {
645 if (prefix
[1] != '!')
646 die ("Invalid search pattern: %s", prefix
);
650 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
651 die("Invalid search pattern: %s", prefix
);
653 for (l
= list
; l
; l
= l
->next
) {
654 l
->item
->object
.flags
|= ONELINE_SEEN
;
655 commit_list_insert(l
->item
, &backup
);
658 char *p
, *to_free
= NULL
;
659 struct commit
*commit
;
660 enum object_type type
;
664 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
665 if (!parse_object(commit
->object
.sha1
))
670 p
= read_sha1_file(commit
->object
.sha1
, &type
, &size
);
676 p
= strstr(p
, "\n\n");
677 matches
= p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0);
681 hashcpy(sha1
, commit
->object
.sha1
);
687 free_commit_list(list
);
688 for (l
= backup
; l
; l
= l
->next
)
689 clear_commit_marks(l
->item
, ONELINE_SEEN
);
690 free_commit_list(backup
);
691 return found
? 0 : -1;
694 struct grab_nth_branch_switch_cbdata
{
699 static int grab_nth_branch_switch(unsigned char *osha1
, unsigned char *nsha1
,
700 const char *email
, unsigned long timestamp
, int tz
,
701 const char *message
, void *cb_data
)
703 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
704 const char *match
= NULL
, *target
= NULL
;
708 if (!prefixcmp(message
, "checkout: moving from ")) {
709 match
= message
+ strlen("checkout: moving from ");
710 target
= strstr(match
, " to ");
713 if (!match
|| !target
)
716 len
= target
- match
;
717 nth
= cb
->cnt
++ % cb
->alloc
;
718 strbuf_reset(&cb
->buf
[nth
]);
719 strbuf_add(&cb
->buf
[nth
], match
, len
);
724 * Parse @{-N} syntax, return the number of characters parsed
725 * if successful; otherwise signal an error with negative value.
727 static int interpret_nth_prior_checkout(const char *name
, struct strbuf
*buf
)
731 struct grab_nth_branch_switch_cbdata cb
;
735 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
737 brace
= strchr(name
, '}');
740 nth
= strtol(name
+3, &num_end
, 10);
741 if (num_end
!= brace
)
746 cb
.buf
= xmalloc(nth
* sizeof(struct strbuf
));
747 for (i
= 0; i
< nth
; i
++)
748 strbuf_init(&cb
.buf
[i
], 20);
751 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch
, 40960, &cb
);
754 for_each_reflog_ent("HEAD", grab_nth_branch_switch
, &cb
);
760 strbuf_add(buf
, cb
.buf
[i
].buf
, cb
.buf
[i
].len
);
761 retval
= brace
-name
+1;
764 for (i
= 0; i
< nth
; i
++)
765 strbuf_release(&cb
.buf
[i
]);
771 int get_sha1_mb(const char *name
, unsigned char *sha1
)
773 struct commit
*one
, *two
;
774 struct commit_list
*mbs
;
775 unsigned char sha1_tmp
[20];
779 dots
= strstr(name
, "...");
781 return get_sha1(name
, sha1
);
783 st
= get_sha1("HEAD", sha1_tmp
);
786 strbuf_init(&sb
, dots
- name
);
787 strbuf_add(&sb
, name
, dots
- name
);
788 st
= get_sha1(sb
.buf
, sha1_tmp
);
793 one
= lookup_commit_reference_gently(sha1_tmp
, 0);
797 if (get_sha1(dots
[3] ? (dots
+ 3) : "HEAD", sha1_tmp
))
799 two
= lookup_commit_reference_gently(sha1_tmp
, 0);
802 mbs
= get_merge_bases(one
, two
, 1);
803 if (!mbs
|| mbs
->next
)
807 hashcpy(sha1
, mbs
->item
->object
.sha1
);
809 free_commit_list(mbs
);
814 * This reads short-hand syntax that not only evaluates to a commit
815 * object name, but also can act as if the end user spelled the name
816 * of the branch from the command line.
818 * - "@{-N}" finds the name of the Nth previous branch we were on, and
819 * places the name of the branch in the given buf and returns the
820 * number of characters parsed if successful.
822 * - "<branch>@{upstream}" finds the name of the other ref that
823 * <branch> is configured to merge with (missing <branch> defaults
824 * to the current branch), and places the name of the branch in the
825 * given buf and returns the number of characters parsed if
828 * If the input is not of the accepted format, it returns a negative
829 * number to signal an error.
831 * If the input was ok but there are not N branch switches in the
832 * reflog, it returns 0.
834 int interpret_branch_name(const char *name
, struct strbuf
*buf
)
837 struct branch
*upstream
;
838 int namelen
= strlen(name
);
839 int len
= interpret_nth_prior_checkout(name
, buf
);
843 return len
; /* syntax Ok, not enough switches */
844 if (0 < len
&& len
== namelen
)
845 return len
; /* consumed all */
847 /* we have extra data, which might need further processing */
848 struct strbuf tmp
= STRBUF_INIT
;
852 strbuf_add(buf
, name
+ len
, namelen
- len
);
853 ret
= interpret_branch_name(buf
->buf
, &tmp
);
854 /* that data was not interpreted, remove our cruft */
856 strbuf_setlen(buf
, used
);
860 strbuf_addbuf(buf
, &tmp
);
861 strbuf_release(&tmp
);
862 /* tweak for size of {-N} versus expanded ref name */
863 return ret
- used
+ len
;
866 cp
= strchr(name
, '@');
869 tmp_len
= upstream_mark(cp
, namelen
- (cp
- name
));
872 len
= cp
+ tmp_len
- name
;
873 cp
= xstrndup(name
, cp
- name
);
874 upstream
= branch_get(*cp
? cp
: NULL
);
877 || !upstream
->merge
[0]->dst
)
878 return error("No upstream branch found for '%s'", cp
);
880 cp
= shorten_unambiguous_ref(upstream
->merge
[0]->dst
, 0);
882 strbuf_addstr(buf
, cp
);
887 int strbuf_branchname(struct strbuf
*sb
, const char *name
)
889 int len
= strlen(name
);
890 if (interpret_branch_name(name
, sb
) == len
)
892 strbuf_add(sb
, name
, len
);
896 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
898 strbuf_branchname(sb
, name
);
901 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
902 return check_refname_format(sb
->buf
, 0);
906 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
907 * notably "xyz^" for "parent of xyz"
909 int get_sha1(const char *name
, unsigned char *sha1
)
911 struct object_context unused
;
912 return get_sha1_with_context(name
, sha1
, &unused
);
915 /* Must be called only when object_name:filename doesn't exist. */
916 static void diagnose_invalid_sha1_path(const char *prefix
,
917 const char *filename
,
918 const unsigned char *tree_sha1
,
919 const char *object_name
)
922 unsigned char sha1
[20];
928 if (!lstat(filename
, &st
))
929 die("Path '%s' exists on disk, but not in '%s'.",
930 filename
, object_name
);
931 if (errno
== ENOENT
|| errno
== ENOTDIR
) {
932 char *fullname
= xmalloc(strlen(filename
)
933 + strlen(prefix
) + 1);
934 strcpy(fullname
, prefix
);
935 strcat(fullname
, filename
);
937 if (!get_tree_entry(tree_sha1
, fullname
,
939 die("Path '%s' exists, but not '%s'.\n"
940 "Did you mean '%s:%s' aka '%s:./%s'?",
948 die("Path '%s' does not exist in '%s'",
949 filename
, object_name
);
953 /* Must be called only when :stage:filename doesn't exist. */
954 static void diagnose_invalid_index_path(int stage
,
956 const char *filename
)
959 struct cache_entry
*ce
;
961 unsigned namelen
= strlen(filename
);
962 unsigned fullnamelen
;
968 /* Wrong stage number? */
969 pos
= cache_name_pos(filename
, namelen
);
972 if (pos
< active_nr
) {
973 ce
= active_cache
[pos
];
974 if (ce_namelen(ce
) == namelen
&&
975 !memcmp(ce
->name
, filename
, namelen
))
976 die("Path '%s' is in the index, but not at stage %d.\n"
977 "Did you mean ':%d:%s'?",
979 ce_stage(ce
), filename
);
982 /* Confusion between relative and absolute filenames? */
983 fullnamelen
= namelen
+ strlen(prefix
);
984 fullname
= xmalloc(fullnamelen
+ 1);
985 strcpy(fullname
, prefix
);
986 strcat(fullname
, filename
);
987 pos
= cache_name_pos(fullname
, fullnamelen
);
990 if (pos
< active_nr
) {
991 ce
= active_cache
[pos
];
992 if (ce_namelen(ce
) == fullnamelen
&&
993 !memcmp(ce
->name
, fullname
, fullnamelen
))
994 die("Path '%s' is in the index, but not '%s'.\n"
995 "Did you mean ':%d:%s' aka ':%d:./%s'?",
997 ce_stage(ce
), fullname
,
998 ce_stage(ce
), filename
);
1001 if (!lstat(filename
, &st
))
1002 die("Path '%s' exists on disk, but not in the index.", filename
);
1003 if (errno
== ENOENT
|| errno
== ENOTDIR
)
1004 die("Path '%s' does not exist (neither on disk nor in the index).",
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 static 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
);
1145 * Call this function when you know "name" given by the end user must
1146 * name an object but it doesn't; the function _may_ die with a better
1147 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1148 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1149 * you have a chance to diagnose the error further.
1151 void maybe_die_on_misspelt_object_name(const char *name
, const char *prefix
)
1153 struct object_context oc
;
1154 unsigned char sha1
[20];
1155 get_sha1_with_context_1(name
, sha1
, &oc
, 1, prefix
);
1158 int get_sha1_with_context(const char *str
, unsigned char *sha1
, struct object_context
*orc
)
1160 return get_sha1_with_context_1(str
, sha1
, orc
, 0, NULL
);