@@ -884,12 +884,15 @@ static int index_entry_create(
884884 git_index_entry * * out ,
885885 git_repository * repo ,
886886 const char * path ,
887+ struct stat * st ,
887888 bool from_workdir )
888889{
889890 size_t pathlen = strlen (path ), alloclen ;
890891 struct entry_internal * entry ;
891892 unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS ;
892893
894+ GIT_UNUSED (st );
895+
893896 /* always reject placing `.git` in the index and directory traversal.
894897 * when requested, disallow platform-specific filenames and upgrade to
895898 * the platform-specific `.git` tests (eg, `git~1`, etc).
@@ -922,15 +925,35 @@ static int index_entry_init(
922925{
923926 int error = 0 ;
924927 git_index_entry * entry = NULL ;
928+ git_buf path ;
925929 struct stat st ;
926930 git_oid oid ;
931+ git_repository * repo ;
927932
928933 if (INDEX_OWNER (index ) == NULL )
929934 return create_index_error (-1 ,
930935 "could not initialize index entry. "
931936 "Index is not backed up by an existing repository." );
932937
933- if (index_entry_create (& entry , INDEX_OWNER (index ), rel_path , true) < 0 )
938+ /*
939+ * FIXME: this is duplicated with the work in
940+ * git_blob__create_from_paths. It should accept an optional stat
941+ * structure so we can pass in the one we have to do here.
942+ */
943+ repo = INDEX_OWNER (index );
944+ if (git_repository__ensure_not_bare (repo , "create blob from file" ) < 0 )
945+ return GIT_EBAREREPO ;
946+
947+ if (git_buf_joinpath (& path , git_repository_workdir (repo ), rel_path ) < 0 )
948+ return -1 ;
949+
950+ error = git_path_lstat (path .ptr , & st );
951+ git_buf_free (& path );
952+
953+ if (error < 0 )
954+ return error ;
955+
956+ if (index_entry_create (& entry , INDEX_OWNER (index ), rel_path , & st , true) < 0 )
934957 return -1 ;
935958
936959 /* write the blob to disk and get the oid and stat info */
@@ -1016,7 +1039,7 @@ static int index_entry_dup(
10161039 git_index * index ,
10171040 const git_index_entry * src )
10181041{
1019- if (index_entry_create (out , INDEX_OWNER (index ), src -> path , false) < 0 )
1042+ if (index_entry_create (out , INDEX_OWNER (index ), src -> path , NULL , false) < 0 )
10201043 return -1 ;
10211044
10221045 index_entry_cpy (* out , src );
@@ -1038,7 +1061,7 @@ static int index_entry_dup_nocache(
10381061 git_index * index ,
10391062 const git_index_entry * src )
10401063{
1041- if (index_entry_create (out , INDEX_OWNER (index ), src -> path , false) < 0 )
1064+ if (index_entry_create (out , INDEX_OWNER (index ), src -> path , NULL , false) < 0 )
10421065 return -1 ;
10431066
10441067 index_entry_cpy_nocache (* out , src );
@@ -1461,9 +1484,6 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
14611484 struct stat st ;
14621485 int error ;
14631486
1464- if (index_entry_create (& entry , INDEX_OWNER (index ), path , true) < 0 )
1465- return -1 ;
1466-
14671487 if ((error = git_buf_joinpath (& abspath , git_repository_workdir (repo ), path )) < 0 )
14681488 return error ;
14691489
@@ -1472,6 +1492,9 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
14721492 return -1 ;
14731493 }
14741494
1495+ if (index_entry_create (& entry , INDEX_OWNER (index ), path , & st , true) < 0 )
1496+ return -1 ;
1497+
14751498 git_index_entry__init_from_stat (entry , & st , !index -> distrust_filemode );
14761499
14771500 if ((error = git_repository_open (& sub , abspath .ptr )) < 0 )
@@ -2965,7 +2988,7 @@ static int read_tree_cb(
29652988 if (git_buf_joinpath (& path , root , tentry -> filename ) < 0 )
29662989 return -1 ;
29672990
2968- if (index_entry_create (& entry , INDEX_OWNER (data -> index ), path .ptr , false) < 0 )
2991+ if (index_entry_create (& entry , INDEX_OWNER (data -> index ), path .ptr , NULL , false) < 0 )
29692992 return -1 ;
29702993
29712994 entry -> mode = tentry -> attr ;
0 commit comments