Commit 29c16698 by Vicent Martí

Merge pull request #1060 from nulltoken/topic/explicit-index-errors

Topic/explicit index errors
parents 8a328cf4 95d73de1
...@@ -259,7 +259,7 @@ int git_index_open(git_index **index_out, const char *index_path) ...@@ -259,7 +259,7 @@ int git_index_open(git_index **index_out, const char *index_path)
{ {
git_index *index; git_index *index;
assert(index_out && index_path); assert(index_out);
index = git__calloc(1, sizeof(git_index)); index = git__calloc(1, sizeof(git_index));
GITERR_CHECK_ALLOC(index); GITERR_CHECK_ALLOC(index);
...@@ -348,6 +348,12 @@ void git_index_clear(git_index *index) ...@@ -348,6 +348,12 @@ void git_index_clear(git_index *index)
index->tree = NULL; index->tree = NULL;
} }
static int create_index_error(int error, const char *msg)
{
giterr_set(GITERR_INDEX, msg);
return error;
}
int git_index_set_caps(git_index *index, unsigned int caps) int git_index_set_caps(git_index *index, unsigned int caps)
{ {
int old_ignore_case; int old_ignore_case;
...@@ -362,11 +368,8 @@ int git_index_set_caps(git_index *index, unsigned int caps) ...@@ -362,11 +368,8 @@ int git_index_set_caps(git_index *index, unsigned int caps)
if (INDEX_OWNER(index) == NULL || if (INDEX_OWNER(index) == NULL ||
git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0) git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0)
{ return create_index_error(-1,
giterr_set(GITERR_INDEX, "Cannot get repository config to set index caps");
"Cannot get repository config to set index caps");
return -1;
}
if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0) if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0)
index->ignore_case = (val != 0); index->ignore_case = (val != 0);
...@@ -402,11 +405,9 @@ int git_index_read(git_index *index) ...@@ -402,11 +405,9 @@ int git_index_read(git_index *index)
git_buf buffer = GIT_BUF_INIT; git_buf buffer = GIT_BUF_INIT;
git_futils_filestamp stamp; git_futils_filestamp stamp;
if (!index->index_file_path) { if (!index->index_file_path)
giterr_set(GITERR_INDEX, return create_index_error(-1,
"Failed to read index: The index is in-memory only"); "Failed to read index: The index is in-memory only");
return -1;
}
if (!index->on_disk || git_path_exists(index->index_file_path) == false) { if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
git_index_clear(index); git_index_clear(index);
...@@ -437,11 +438,9 @@ int git_index_write(git_index *index) ...@@ -437,11 +438,9 @@ int git_index_write(git_index *index)
git_filebuf file = GIT_FILEBUF_INIT; git_filebuf file = GIT_FILEBUF_INIT;
int error; int error;
if (!index->index_file_path) { if (!index->index_file_path)
giterr_set(GITERR_INDEX, return create_index_error(-1,
"Failed to write index: The index is in-memory only"); "Failed to read index: The index is in-memory only");
return -1;
}
git_vector_sort(&index->entries); git_vector_sort(&index->entries);
git_vector_sort(&index->reuc); git_vector_sort(&index->reuc);
...@@ -472,13 +471,11 @@ int git_index_write_tree(git_oid *oid, git_index *index) ...@@ -472,13 +471,11 @@ int git_index_write_tree(git_oid *oid, git_index *index)
assert(oid && index); assert(oid && index);
repo = (git_repository *)GIT_REFCOUNT_OWNER(index); repo = INDEX_OWNER(index);
if (repo == NULL) { if (repo == NULL)
giterr_set(GITERR_INDEX, "Failed to write tree. " return create_index_error(-1, "Failed to write tree. "
"The index file is not backed up by an existing repository"); "The index file is not backed up by an existing repository");
return -1;
}
return git_tree__write_index(oid, index, repo); return git_tree__write_index(oid, index, repo);
} }
...@@ -539,13 +536,16 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const ...@@ -539,13 +536,16 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const
git_buf full_path = GIT_BUF_INIT; git_buf full_path = GIT_BUF_INIT;
int error; int error;
if (INDEX_OWNER(index) == NULL || if (INDEX_OWNER(index) == NULL)
(workdir = git_repository_workdir(INDEX_OWNER(index))) == NULL) return create_index_error(-1,
{ "Could not initialize index entry. "
giterr_set(GITERR_INDEX, "Index is not backed up by an existing repository.");
workdir = git_repository_workdir(INDEX_OWNER(index));
if (!workdir)
return create_index_error(GIT_EBAREREPO,
"Could not initialize index entry. Repository is bare"); "Could not initialize index entry. Repository is bare");
return -1;
}
if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0) if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0)
return error; return error;
......
#include "clar_libgit2.h"
void test_index_inmemory__can_create_an_inmemory_index(void)
{
git_index *index;
cl_git_pass(git_index_new(&index));
cl_assert_equal_i(0, git_index_entrycount(index));
git_index_free(index);
}
void test_index_inmemory__cannot_add_from_workdir_to_an_inmemory_index(void)
{
git_index *index;
cl_git_pass(git_index_new(&index));
cl_assert_equal_i(GIT_ERROR, git_index_add_from_workdir(index, "test.txt"));
git_index_free(index);
}
...@@ -248,3 +248,16 @@ void test_index_tests__add(void) ...@@ -248,3 +248,16 @@ void test_index_tests__add(void)
git_repository_free(repo); git_repository_free(repo);
} }
void test_index_tests__add_from_workdir_to_a_bare_repository_returns_EBAREPO(void)
{
git_repository *bare_repo;
git_index *index;
cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_index(&index, bare_repo));
cl_assert_equal_i(GIT_EBAREREPO, git_index_add_from_workdir(index, "test.txt"));
git_index_free(index);
git_repository_free(bare_repo);
}
...@@ -246,8 +246,8 @@ void test_stash_save__cannot_stash_when_there_are_no_local_change(void) ...@@ -246,8 +246,8 @@ void test_stash_save__cannot_stash_when_there_are_no_local_change(void)
* 'what' and 'who' are being committed. * 'what' and 'who' are being committed.
* 'when' remain untracked. * 'when' remain untracked.
*/ */
git_index_add_from_workdir(index, "what"); cl_git_pass(git_index_add_from_workdir(index, "what"));
git_index_add_from_workdir(index, "who"); cl_git_pass(git_index_add_from_workdir(index, "who"));
cl_git_pass(git_index_write(index)); cl_git_pass(git_index_write(index));
commit_staged_files(&commit_oid, index, signature); commit_staged_files(&commit_oid, index, signature);
git_index_free(index); git_index_free(index);
...@@ -356,7 +356,7 @@ void test_stash_save__can_stage_normal_then_stage_untracked(void) ...@@ -356,7 +356,7 @@ void test_stash_save__can_stage_normal_then_stage_untracked(void)
void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void) void test_stash_save__including_untracked_without_any_untracked_file_creates_an_empty_tree(void)
{ {
p_unlink("stash/when"); cl_git_pass(p_unlink("stash/when"));
assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED); assert_status("what", GIT_STATUS_WT_MODIFIED | GIT_STATUS_INDEX_MODIFIED);
assert_status("how", GIT_STATUS_INDEX_MODIFIED); assert_status("how", GIT_STATUS_INDEX_MODIFIED);
......
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