Commit 08d4b459 by Carlos Martín Nieto Committed by Patrick Steinhardt

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.
parent c7cac088
...@@ -884,12 +884,15 @@ static int index_entry_create( ...@@ -884,12 +884,15 @@ static int index_entry_create(
git_index_entry **out, git_index_entry **out,
git_repository *repo, git_repository *repo,
const char *path, const char *path,
struct stat *st,
bool from_workdir) bool from_workdir)
{ {
size_t pathlen = strlen(path), alloclen; size_t pathlen = strlen(path), alloclen;
struct entry_internal *entry; struct entry_internal *entry;
unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS; unsigned int path_valid_flags = GIT_PATH_REJECT_INDEX_DEFAULTS;
GIT_UNUSED(st);
/* always reject placing `.git` in the index and directory traversal. /* always reject placing `.git` in the index and directory traversal.
* when requested, disallow platform-specific filenames and upgrade to * when requested, disallow platform-specific filenames and upgrade to
* the platform-specific `.git` tests (eg, `git~1`, etc). * the platform-specific `.git` tests (eg, `git~1`, etc).
...@@ -922,15 +925,35 @@ static int index_entry_init( ...@@ -922,15 +925,35 @@ static int index_entry_init(
{ {
int error = 0; int error = 0;
git_index_entry *entry = NULL; git_index_entry *entry = NULL;
git_buf path;
struct stat st; struct stat st;
git_oid oid; git_oid oid;
git_repository *repo;
if (INDEX_OWNER(index) == NULL) if (INDEX_OWNER(index) == NULL)
return create_index_error(-1, return create_index_error(-1,
"could not initialize index entry. " "could not initialize index entry. "
"Index is not backed up by an existing repository."); "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; return -1;
/* write the blob to disk and get the oid and stat info */ /* write the blob to disk and get the oid and stat info */
...@@ -1016,7 +1039,7 @@ static int index_entry_dup( ...@@ -1016,7 +1039,7 @@ static int index_entry_dup(
git_index *index, git_index *index,
const git_index_entry *src) 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; return -1;
index_entry_cpy(*out, src); index_entry_cpy(*out, src);
...@@ -1038,7 +1061,7 @@ static int index_entry_dup_nocache( ...@@ -1038,7 +1061,7 @@ static int index_entry_dup_nocache(
git_index *index, git_index *index,
const git_index_entry *src) 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; return -1;
index_entry_cpy_nocache(*out, src); index_entry_cpy_nocache(*out, src);
...@@ -1461,9 +1484,6 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const ...@@ -1461,9 +1484,6 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
struct stat st; struct stat st;
int error; 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) if ((error = git_buf_joinpath(&abspath, git_repository_workdir(repo), path)) < 0)
return error; return error;
...@@ -1472,6 +1492,9 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const ...@@ -1472,6 +1492,9 @@ static int add_repo_as_submodule(git_index_entry **out, git_index *index, const
return -1; 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); git_index_entry__init_from_stat(entry, &st, !index->distrust_filemode);
if ((error = git_repository_open(&sub, abspath.ptr)) < 0) if ((error = git_repository_open(&sub, abspath.ptr)) < 0)
...@@ -2965,7 +2988,7 @@ static int read_tree_cb( ...@@ -2965,7 +2988,7 @@ static int read_tree_cb(
if (git_buf_joinpath(&path, root, tentry->filename) < 0) if (git_buf_joinpath(&path, root, tentry->filename) < 0)
return -1; 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; return -1;
entry->mode = tentry->attr; entry->mode = tentry->attr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment