index: stat before creating the entry This is so we have it available for the path validity checking. In a later commit we will start rejecting `.gitmodules` files as symlinks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
diff --git a/src/index.c b/src/index.c
index a867547..4a08713 100644
--- a/src/index.c
+++ b/src/index.c
@@ -884,12 +884,15 @@ static int index_entry_create(
git_index_entry **out,
git_repository *repo,
const char *path,
+ struct stat *st,
bool from_workdir)
{
size_t pathlen = strlen(path), alloclen;
struct entry_internal *entry;
unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
+ GIT_UNUSED(st);
+
/* always reject placing `.git` in the index and directory traversal.
* when requested, disallow platform-specific filenames and upgrade to
* the platform-specific `.git` tests (eg, `git~1`, etc).
@@ -922,15 +925,35 @@ static int index_entry_init(
{
int error = 0;
git_index_entry *entry = NULL;
+ git_buf path;
struct stat st;
git_oid oid;
+ git_repository *repo;
if (INDEX_OWNER(index) == NULL)
return create_index_error(-1,
"could not initialize index entry. "
"Index is not backed up by an existing repository.");
- if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, true) < 0)
+ /*
+ * FIXME: this is duplicated with the work in
+ * git_blob__create_from_paths. It should accept an optional stat
+ * structure so we can pass in the one we have to do here.
+ */
+ repo = INDEX_OWNER(index);
+ if (git_repository__ensure_not_bare(repo, "create blob from file") < 0)
+ return GIT_EBAREREPO;
+
+ if (git_buf_joinpath(&path, git_repository_workdir(repo), rel_path) < 0)
+ return -1;
+
+ error = git_path_lstat(path.ptr, &st);
+ git_buf_free(&path);
+
+ if (error < 0)
+ return error;
+
+ if (index_entry_create(&entry, INDEX_OWNER(index), rel_path, &st, true) < 0)
return -1;
/* write the blob to disk and get the oid and stat info */
@@ -1016,7 +1039,7 @@ static int index_entry_dup(
git_index *index,
const git_index_entry *src)
{
- if (index_entry_create(out, INDEX_OWNER(index), src->path, false) < 0)
+ if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
return -1;
index_entry_cpy(*out, src);
@@ -1038,7 +1061,7 @@ static int index_entry_dup_nocache(
git_index *index,
const git_index_entry *src)
{
- if (index_entry_create(out, INDEX_OWNER(index), src->path, false) < 0)
+ if (index_entry_create(out, INDEX_OWNER(index), src->path, NULL, false) < 0)
return -1;
index_entry_cpy_nocache(*out, src);
@@ -1461,9 +1484,6 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
struct stat st;
int error;
- if (index_entry_create(&entry, INDEX_OWNER(index), path, true) < 0)
- return -1;
-
if ((error = git_buf_joinpath(&abspath, git_repository_workdir(repo), path)) < 0)
return error;
@@ -1472,6 +1492,9 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
return -1;
}
+ if (index_entry_create(&entry, INDEX_OWNER(index), path, &st, true) < 0)
+ return -1;
+
git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
if ((error = git_repository_open(&sub, abspath.ptr)) < 0)
@@ -2965,7 +2988,7 @@ static int read_tree_cb(
if (git_buf_joinpath(&path, root, tentry->filename) < 0)
return -1;
- if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, false) < 0)
+ if (index_entry_create(&entry, INDEX_OWNER(data->index), path.ptr, NULL, false) < 0)
return -1;
entry->mode = tentry->attr;