Commit 7d633572669fdc7d4a0b16984d7124c3e0e45b3d

Vicent Marti 2014-03-20T20:08:01

Merge pull request #2194 from libgit2/cmn/reflog-bare reflog: follow core.logallrefupdates

diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 25316fe..9120a3e 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -925,16 +925,34 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co
 static int has_reflog(git_repository *repo, const char *name);
 
 /* We only write if it's under heads/, remotes/ or notes/ or if it already has a log */
-static bool should_write_reflog(git_repository *repo, const char *name)
+static int should_write_reflog(int *write, git_repository *repo, const char *name)
 {
-	if (has_reflog(repo, name))
-		return 1;
+	git_config *config;
+	int error, logall, is_bare;
 
-	if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR) ||
-	    !git__strcmp(name, GIT_HEAD_FILE) ||
-	    !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) ||
-	    !git__prefixcmp(name, GIT_REFS_NOTES_DIR))
-		return 1;
+	/* Defaults to the oppsite of being bare */
+	is_bare = git_repository_is_bare(repo);
+	logall = !is_bare;
+
+	if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+		return error;
+
+	error = git_config_get_bool(&logall, config, "core.logallrefupdates");
+	if (error < 0 && error != GIT_ENOTFOUND)
+		return error;
+
+	if (!logall) {
+		*write = 0;
+	} else if (has_reflog(repo, name)) {
+		*write = 1;
+	} else if (!git__prefixcmp(name, GIT_REFS_HEADS_DIR) ||
+		   !git__strcmp(name, GIT_HEAD_FILE) ||
+		   !git__prefixcmp(name, GIT_REFS_REMOTES_DIR) ||
+		   !git__prefixcmp(name, GIT_REFS_NOTES_DIR)) {
+		*write = 1;
+	} else {
+		*write = 0;
+	}
 
 	return 0;
 }
@@ -1030,9 +1048,6 @@ static int maybe_append_head(refdb_fs_backend *backend, const git_reference *ref
 		name = git_reference_name(tmp);
 	}
 
-	if (error < 0)
-		goto cleanup;
-
 	if (strcmp(name, ref->name))
 		goto cleanup;
 
@@ -1056,7 +1071,7 @@ static int refdb_fs_backend__write(
 {
 	refdb_fs_backend *backend = (refdb_fs_backend *)_backend;
 	git_filebuf file = GIT_FILEBUF_INIT;
-	int error = 0, cmp = 0;
+	int error = 0, cmp = 0, should_write;
 	const char *new_target = NULL;
 	const git_oid *new_id = NULL;
 
@@ -1094,7 +1109,10 @@ static int refdb_fs_backend__write(
 		goto on_error; /* not really error */
 	}
 
-	if (should_write_reflog(backend->repo, ref->name)) {
+	if ((error = should_write_reflog(&should_write, backend->repo, ref->name)) < 0)
+		goto on_error;
+
+	if (should_write) {
 		if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
 			goto on_error;
 		if ((error = maybe_append_head(backend, ref, who, message)) < 0)
diff --git a/tests/refs/reflog/reflog.c b/tests/refs/reflog/reflog.c
index a50d40a..792b0f0 100644
--- a/tests/refs/reflog/reflog.c
+++ b/tests/refs/reflog/reflog.c
@@ -261,3 +261,76 @@ void test_refs_reflog_reflog__do_not_append_when_no_update(void)
 
 	cl_assert_equal_i(nlogs_after, nlogs);
 }
+
+static void assert_no_reflog_update(void)
+{
+	size_t nlogs, nlogs_after;
+	size_t nlogs_master, nlogs_master_after;
+	git_reference *ref;
+	git_reflog *log;
+	git_oid id;
+
+	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
+	nlogs = git_reflog_entrycount(log);
+	git_reflog_free(log);
+
+	cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
+	nlogs_master = git_reflog_entrycount(log);
+	git_reflog_free(log);
+
+	/* Move it back */
+	git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
+	cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/master", &id, 1, NULL, NULL));
+	git_reference_free(ref);
+
+	cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
+	nlogs_after = git_reflog_entrycount(log);
+	git_reflog_free(log);
+
+	cl_assert_equal_i(nlogs_after, nlogs);
+
+	cl_git_pass(git_reflog_read(&log, g_repo, "refs/heads/master"));
+	nlogs_master_after = git_reflog_entrycount(log);
+	git_reflog_free(log);
+
+	cl_assert_equal_i(nlogs_after, nlogs);
+	cl_assert_equal_i(nlogs_master_after, nlogs_master);
+
+}
+
+void test_refs_reflog_reflog__logallrefupdates_bare_set_false(void)
+{
+	git_config *config;
+
+	cl_git_pass(git_repository_config(&config, g_repo));
+	cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
+	git_config_free(config);
+
+	assert_no_reflog_update();
+}
+
+void test_refs_reflog_reflog__logallrefupdates_bare_unset(void)
+{
+	git_config *config;
+
+	cl_git_pass(git_repository_config(&config, g_repo));
+	cl_git_pass(git_config_delete_entry(config, "core.logallrefupdates"));
+	git_config_free(config);
+
+	assert_no_reflog_update();
+}
+
+void test_refs_reflog_reflog__logallrefupdates_nonbare_set_false(void)
+{
+	git_config *config;
+
+	cl_git_sandbox_cleanup();
+	g_repo = cl_git_sandbox_init("testrepo");
+
+
+	cl_git_pass(git_repository_config(&config, g_repo));
+	cl_git_pass(git_config_set_bool(config, "core.logallrefupdates", false));
+	git_config_free(config);
+
+	assert_no_reflog_update();
+}