reset: prevent hard reset in a bare repository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
diff --git a/src/reset.c b/src/reset.c
index c536e75..dfa095b 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -39,9 +39,11 @@ 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__ensure_not_bare(repo, "reset mixed") < 0)
- return GIT_EBAREREPO;
+ if (reset_type != GIT_RESET_SOFT
+ && git_repository__ensure_not_bare(
+ repo,
+ reset_type == GIT_RESET_MIXED ? "reset mixed" : "reset hard") < 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/hard.c b/tests-clar/reset/hard.c
index ad3badb..fdab9c5 100644
--- a/tests-clar/reset/hard.c
+++ b/tests-clar/reset/hard.c
@@ -44,3 +44,17 @@ void test_reset_hard__resetting_culls_empty_directories(void)
git_buf_free(&subfile_path);
git_buf_free(&newdir_path);
}
+
+void test_reset_hard__cannot_reset_in_a_bare_repository(void)
+{
+ git_repository *bare;
+
+ cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
+ cl_assert(git_repository_is_bare(bare) == true);
+
+ retrieve_target_from_oid(&target, bare, KNOWN_COMMIT_IN_BARE_REPO);
+
+ cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD));
+
+ git_repository_free(bare);
+}