Commit 3b6a5bac20a9cb46cd29d74baf06cf8a8d1248fb

Vicent Marti 2014-02-03T10:36:04

Merge pull request #2095 from libgit2/update-head-reflog Correct "new" id for reattached-HEAD reflog entry

diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 41ff019..89c77c1 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1452,7 +1452,13 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co
 	if (error < 0)
 		return error;
 
-	if (git_reference_target(ref) != NULL)
+	if (git_reference_symbolic_target(ref) != NULL) {
+		error = git_reference_name_to_id(&new_id, repo, git_reference_symbolic_target(ref));
+		if (error != 0 && error != GIT_ENOTFOUND)
+			goto cleanup;
+		giterr_clear();
+	}
+	else if (git_reference_target(ref) != NULL)
 		git_oid_cpy(&new_id, git_reference_target(ref));
 
 	if ((error = serialize_reflog_entry(&buf, &old_id, &new_id, who, message)) < 0)
diff --git a/tests/repo/head.c b/tests/repo/head.c
index 71cfc3c..8ea9a0f 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -195,10 +195,41 @@ void test_repo_head__can_tell_if_an_unborn_head_is_detached(void)
 	cl_assert_equal_i(false, git_repository_head_detached(repo));
 }
 
-void test_repo_head__setting_head_updates_reflog(void)
+static void test_reflog(git_repository *repo, size_t idx,
+		const char *old_spec, const char *new_spec,
+		const char *email, const char *message)
 {
 	git_reflog *log;
-	const git_reflog_entry *entry1, *entry2, *entry3;
+	git_reflog_entry *entry;
+
+	cl_git_pass(git_reflog_read(&log, repo, "HEAD"));
+	entry = git_reflog_entry_byindex(log, idx);
+
+	if (old_spec) {
+		git_object *obj;
+		cl_git_pass(git_revparse_single(&obj, repo, old_spec));
+		cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)));
+		git_object_free(obj);
+	}
+	if (new_spec) {
+		git_object *obj;
+		cl_git_pass(git_revparse_single(&obj, repo, new_spec));
+		cl_assert_equal_i(0, git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)));
+		git_object_free(obj);
+	}
+
+	if (email) {
+		cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
+	}
+	if (message) {
+		cl_assert_equal_s(message, git_reflog_entry_message(entry));
+	}
+
+	git_reflog_free(log);
+}
+
+void test_repo_head__setting_head_updates_reflog(void)
+{
 	git_object *tag;
 	git_signature *sig;
 
@@ -208,19 +239,13 @@ void test_repo_head__setting_head_updates_reflog(void)
 	cl_git_pass(git_repository_set_head(repo, "refs/heads/unborn", sig, "message2"));
 	cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
 	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag), sig, "message3"));
+	cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, "message4"));
 
-	cl_git_pass(git_reflog_read(&log, repo, "HEAD"));
-	entry1 = git_reflog_entry_byindex(log, 2);
-	entry2 = git_reflog_entry_byindex(log, 1);
-	entry3 = git_reflog_entry_byindex(log, 0);
-	cl_assert_equal_s("message1", git_reflog_entry_message(entry1));
-	cl_assert_equal_s("message2", git_reflog_entry_message(entry2));
-	cl_assert_equal_s("message3", git_reflog_entry_message(entry3));
-	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry1)->email);
-	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry2)->email);
-	cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry3)->email);
+	test_reflog(repo, 3, NULL, "refs/heads/haacked", "foo@example.com", "message1");
+	test_reflog(repo, 2, "refs/heads/haacked", NULL, "foo@example.com", "message2");
+	test_reflog(repo, 1, NULL, "tags/test^{commit}", "foo@example.com", "message3");
+	test_reflog(repo, 0, "tags/test^{commit}", "refs/heads/haacked", "foo@example.com", "message4");
 
-	git_reflog_free(log);
 	git_object_free(tag);
 	git_signature_free(sig);
 }