summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-11-10 21:01:05 +0100
committernulltoken <emeric.fermas@gmail.com>2012-11-10 21:01:05 +0100
commit33f95a9b32804958226f5af6a4773199f21fc5ea (patch)
tree391b07b736f6dcff51f8543d0cedf17b3fe58d18
parent69c068c79f5b6d2fe5cf1ca707a130a51939abc2 (diff)
downloadlibgit2-33f95a9b32804958226f5af6a4773199f21fc5ea.tar.gz
index: refine add_from_workdir() error report
-rw-r--r--src/index.c48
-rw-r--r--tests-clar/index/inmemory.c11
-rw-r--r--tests-clar/index/tests.c13
3 files changed, 48 insertions, 24 deletions
diff --git a/src/index.c b/src/index.c
index 6c04177e5..c68d0dc35 100644
--- a/src/index.c
+++ b/src/index.c
@@ -348,6 +348,12 @@ void git_index_clear(git_index *index)
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 old_ignore_case;
@@ -362,11 +368,8 @@ int git_index_set_caps(git_index *index, unsigned int caps)
if (INDEX_OWNER(index) == NULL ||
git_repository_config__weakptr(&cfg, INDEX_OWNER(index)) < 0)
- {
- giterr_set(GITERR_INDEX,
- "Cannot get repository config to set index caps");
- return -1;
- }
+ return create_index_error(-1,
+ "Cannot get repository config to set index caps");
if (git_config_get_bool(&val, cfg, "core.ignorecase") == 0)
index->ignore_case = (val != 0);
@@ -402,11 +405,9 @@ int git_index_read(git_index *index)
git_buf buffer = GIT_BUF_INIT;
git_futils_filestamp stamp;
- if (!index->index_file_path) {
- giterr_set(GITERR_INDEX,
+ if (!index->index_file_path)
+ return create_index_error(-1,
"Failed to read index: The index is in-memory only");
- return -1;
- }
if (!index->on_disk || git_path_exists(index->index_file_path) == false) {
git_index_clear(index);
@@ -437,11 +438,9 @@ int git_index_write(git_index *index)
git_filebuf file = GIT_FILEBUF_INIT;
int error;
- if (!index->index_file_path) {
- giterr_set(GITERR_INDEX,
- "Failed to write index: The index is in-memory only");
- return -1;
- }
+ if (!index->index_file_path)
+ return create_index_error(-1,
+ "Failed to read index: The index is in-memory only");
git_vector_sort(&index->entries);
git_vector_sort(&index->reuc);
@@ -474,11 +473,9 @@ int git_index_write_tree(git_oid *oid, git_index *index)
repo = (git_repository *)GIT_REFCOUNT_OWNER(index);
- if (repo == NULL) {
- giterr_set(GITERR_INDEX, "Failed to write tree. "
+ if (repo == NULL)
+ return create_index_error(-1, "Failed to write tree. "
"The index file is not backed up by an existing repository");
- return -1;
- }
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
git_buf full_path = GIT_BUF_INIT;
int error;
- if (INDEX_OWNER(index) == NULL ||
- (workdir = git_repository_workdir(INDEX_OWNER(index))) == NULL)
- {
- giterr_set(GITERR_INDEX,
+ if (INDEX_OWNER(index) == NULL)
+ return create_index_error(-1,
+ "Could not initialize index entry. "
+ "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");
- return -1;
- }
if ((error = git_buf_joinpath(&full_path, workdir, rel_path)) < 0)
return error;
diff --git a/tests-clar/index/inmemory.c b/tests-clar/index/inmemory.c
index 9c5c0b7a4..c997b965f 100644
--- a/tests-clar/index/inmemory.c
+++ b/tests-clar/index/inmemory.c
@@ -9,3 +9,14 @@ void test_index_inmemory__can_create_an_inmemory_index(void)
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);
+}
diff --git a/tests-clar/index/tests.c b/tests-clar/index/tests.c
index cf971e1dd..d3f6f2582 100644
--- a/tests-clar/index/tests.c
+++ b/tests-clar/index/tests.c
@@ -248,3 +248,16 @@ void test_index_tests__add(void)
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);
+}