Commit ced8d1420a76c13796d951203c2b35540a49b454

nulltoken 2012-08-22T11:30:55

errors: deploy GIT_EBAREREPO usage

diff --git a/src/blob.c b/src/blob.c
index 699adec..5a4a26b 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 92fe671..e30e112 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 4695edf..4aa8af2 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 f9e16f7..5aaf948 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 7cfff65..d5f8e10 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);
 }