From ced8d1420a76c13796d951203c2b35540a49b454 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Wed, 22 Aug 2012 11:30:55 +0200 Subject: [PATCH] errors: deploy GIT_EBAREREPO usage --- src/blob.c | 4 +++- src/iterator.c | 7 ++----- src/repository.h | 15 +++++++++++++++ src/reset.c | 5 +++-- tests-clar/reset/mixed.c | 2 +- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/blob.c b/src/blob.c index 699adec6b..5a4a26bfa 100644 --- a/src/blob.c +++ b/src/blob.c @@ -212,8 +212,10 @@ int git_blob_create_fromfile(git_oid *oid, git_repository *repo, const char *pat const char *workdir; int error; + if ((error = git_repository__ensure_not_bare(repo, "create blob from file")) < 0) + return error; + workdir = git_repository_workdir(repo); - assert(workdir); /* error to call this on bare repo */ if (git_buf_joinpath(&full_path, workdir, path) < 0) { git_buf_free(&full_path); diff --git a/src/iterator.c b/src/iterator.c index 92fe67134..e30e11220 100644 --- a/src/iterator.c +++ b/src/iterator.c @@ -659,11 +659,8 @@ int git_iterator_for_workdir_range( assert(iter && repo); - if (git_repository_is_bare(repo)) { - giterr_set(GITERR_INVALID, - "Cannot scan working directory for bare repo"); - return -1; - } + if ((error = git_repository__ensure_not_bare(repo, "scan working directory")) < 0) + return error; ITERATOR_BASE_INIT(wi, workdir, WORKDIR); diff --git a/src/repository.h b/src/repository.h index 4695edf3a..4aa8af292 100644 --- a/src/repository.h +++ b/src/repository.h @@ -149,4 +149,19 @@ void git_repository__cvar_cache_clear(git_repository *repo); */ extern void git_submodule_config_free(git_repository *repo); +GIT_INLINE(int) git_repository__ensure_not_bare( + git_repository *repo, + const char *operation_name) +{ + if (!git_repository_is_bare(repo)) + return 0; + + giterr_set( + GITERR_REPOSITORY, + "Cannot %s. This operation is not allowed against bare repositories.", + operation_name); + + return GIT_EBAREREPO; +} + #endif diff --git a/src/reset.c b/src/reset.c index f9e16f7c6..5aaf94840 100644 --- a/src/reset.c +++ b/src/reset.c @@ -34,8 +34,9 @@ int git_reset( if (git_object_owner(target) != repo) return reset_error_invalid("The given target does not belong to this repository."); - if (reset_type == GIT_RESET_MIXED && git_repository_is_bare(repo)) - return reset_error_invalid("Mixed reset is not allowed in a bare repository."); + if (reset_type == GIT_RESET_MIXED + && git_repository__ensure_not_bare(repo, "reset mixed") < 0) + return GIT_EBAREREPO; if (git_object_peel(&commit, target, GIT_OBJ_COMMIT) < 0) { reset_error_invalid("The given target does not resolve to a commit"); diff --git a/tests-clar/reset/mixed.c b/tests-clar/reset/mixed.c index 7cfff65d4..d5f8e10c5 100644 --- a/tests-clar/reset/mixed.c +++ b/tests-clar/reset/mixed.c @@ -27,7 +27,7 @@ void test_reset_mixed__cannot_reset_in_a_bare_repository(void) retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO); - cl_git_fail(git_reset(bare, target, GIT_RESET_MIXED)); + cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED)); git_repository_free(bare); }