Commit 86746b4b3ae1508c980a7adcdd088ab87a92af7a

Ben Straub 2014-02-03T15:06:47

Add reset tests for reflog

diff --git a/src/refs.c b/src/refs.c
index ca5f24e..3330c3a 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -1032,8 +1032,10 @@ static int reference__update_terminal(
 			nesting+1, signature, log_message);
 		git_reference_free(ref);
 	} else {
+		/* If we're not moving the target, don't recreate the ref */
+		if (0 != git_oid_cmp(git_reference_target(ref), oid))
+			error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message);
 		git_reference_free(ref);
-		error = git_reference_create(NULL, repo, ref_name, oid, 1, signature, log_message);
 	}
 
 	return error;
diff --git a/tests/reset/hard.c b/tests/reset/hard.c
index 9724360..d4c7db4 100644
--- a/tests/reset/hard.c
+++ b/tests/reset/hard.c
@@ -197,3 +197,30 @@ void test_reset_hard__cleans_up_merge(void)
 	git_buf_free(&merge_mode_path);
 	git_buf_free(&orig_head_path);
 }
+
+void test_reset_hard__reflog_is_correct(void)
+{
+	const char *exp_msg = "commit: Add a file which name should appear before the "
+		"\"subdir/\" folder while being dealt with by the treewalker";
+
+	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+	/* Branch not moving, no reflog entry */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
+
+	/* Moved branch, expect default message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, NULL));
+	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 4, NULL, "reset: moving");
+
+	/* Moved branch, expect custom message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL, "message1"));
+	reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 5, NULL, "message1");
+}
diff --git a/tests/reset/mixed.c b/tests/reset/mixed.c
index 7a2605a..25272a7 100644
--- a/tests/reset/mixed.c
+++ b/tests/reset/mixed.c
@@ -47,3 +47,29 @@ void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
 	cl_git_pass(git_status_file(&status, repo, "macro_bad"));
 	cl_assert(status == GIT_STATUS_WT_NEW);
 }
+
+void test_reset_mixed__reflog_is_correct(void)
+{
+	const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+	/* Branch not moving, no reflog entry */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+	/* Moved branch, expect default message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, NULL));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving");
+
+	/* Moved branch, expect custom message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL, "message1"));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 11, NULL, "message1");
+}
diff --git a/tests/reset/reset_helpers.c b/tests/reset/reset_helpers.c
index a792c03..7a335a6 100644
--- a/tests/reset/reset_helpers.c
+++ b/tests/reset/reset_helpers.c
@@ -1,3 +1,20 @@
 #include "clar_libgit2.h"
 #include "reset_helpers.h"
 
+void reflog_check(git_repository *repo, const char *refname,
+		size_t exp_count, const char *exp_email, const char *exp_msg)
+{
+	git_reflog *log;
+	const git_reflog_entry *entry;
+
+	cl_git_pass(git_reflog_read(&log, repo, refname));
+	cl_assert_equal_i(exp_count, git_reflog_entrycount(log));
+	entry = git_reflog_entry_byindex(log, 0);
+
+	if (exp_email)
+		cl_assert_equal_s(exp_email, git_reflog_entry_committer(entry)->email);
+	if (exp_msg)
+		cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry));
+
+	git_reflog_free(log);
+}
diff --git a/tests/reset/reset_helpers.h b/tests/reset/reset_helpers.h
index 82249ff..e7e0485 100644
--- a/tests/reset/reset_helpers.h
+++ b/tests/reset/reset_helpers.h
@@ -3,3 +3,5 @@
 #define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d"
 #define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0"
 
+void reflog_check(git_repository *repo, const char *refname,
+		size_t exp_count, const char *exp_email, const char *exp_msg);
diff --git a/tests/reset/soft.c b/tests/reset/soft.c
index 7c02b80..6469fce 100644
--- a/tests/reset/soft.c
+++ b/tests/reset/soft.c
@@ -154,3 +154,29 @@ void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE
 
 	cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
 }
+
+void test_reset_soft_reflog_is_correct(void)
+{
+	const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
+
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+	/* Branch not moving, no reflog entry */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
+
+	/* Moved branch, expect default message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, NULL));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 10, NULL, "reset: moving");
+
+	/* Moved branch, expect custom message */
+	cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
+	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL, "message1"));
+	reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
+	reflog_check(repo, "refs/heads/master", 11, NULL, "message1");
+}