Commit 1c3acf148b97c952fcb942a73cda9f891f04f1b5

Ben Straub 2012-10-15T12:02:20

Merge pull request #978 from nulltoken/fix/reset-hard reset: prevent hard reset in a bare repository

diff --git a/src/fetch.c b/src/fetch.c
index f9cc8aa..737a1b4 100644
--- a/src/fetch.c
+++ b/src/fetch.c
@@ -58,10 +58,11 @@ static int filter_wants(git_remote *remote)
 {
 	struct filter_payload p;
 	git_refspec tagspec;
+	int error = -1;
 
 	git_vector_clear(&remote->refs);
 	if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
-		return -1;
+		return error;
 
 	/*
 	 * The fetch refspec can be NULL, and what this means is that the
@@ -75,9 +76,14 @@ static int filter_wants(git_remote *remote)
 	p.remote = remote;
 
 	if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
-		return -1;
+		goto cleanup;
+
+	error = git_remote_ls(remote, filter_ref__cb, &p);
+
+cleanup:
+	git_refspec__free(&tagspec);
 
-	return git_remote_ls(remote, filter_ref__cb, &p);
+	return error;
 }
 
 /* Wait until we get an ack from the */
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/src/transports/http.c b/src/transports/http.c
index 7b77c92..93dd0c3 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -93,6 +93,7 @@ static int send_request(transport_http *t, const char *service, void *data, ssiz
 #ifndef GIT_WINHTTP
 	git_buf request = GIT_BUF_INIT;
 	const char *verb;
+	int error = -1;
 
 	verb = ls ? "GET" : "POST";
 	/* Generate and send the HTTP request */
@@ -102,17 +103,20 @@ static int send_request(transport_http *t, const char *service, void *data, ssiz
 	}
 
 
-	if (gitno_send((git_transport *) t, request.ptr, request.size, 0) < 0) {
-		git_buf_free(&request);
-		return -1;
-	}
+	if (gitno_send((git_transport *) t, request.ptr, request.size, 0) < 0)
+		goto cleanup;
 
 	if (content_length) {
 		if (gitno_send((git_transport *) t, data, content_length, 0) < 0)
-			return -1;
+			goto cleanup;
 	}
 
-	return 0;
+	error = 0;
+
+cleanup:
+	git_buf_free(&request);
+	return error;
+
 #else
 	wchar_t *verb;
 	wchar_t url[GIT_WIN_PATH], ct[GIT_WIN_PATH];
diff --git a/tests-clar/network/fetch.c b/tests-clar/network/fetch.c
index 63be3de..5ff7b0a 100644
--- a/tests-clar/network/fetch.c
+++ b/tests-clar/network/fetch.c
@@ -47,6 +47,8 @@ static void do_fetch(const char *url, int flag, int n)
 	git_remote_disconnect(remote);
 	cl_git_pass(git_remote_update_tips(remote));
 	cl_assert_equal_i(counter, n);
+
+	git_remote_free(remote);
 }
 
 void test_network_fetch__default_git(void)
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);
+}