refdb: don't update when there's no need If the caller wants to update a ref to point to the same target as it currently has, we should return early and avoid writing to the reflog.
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 0c30110..4bcc5fa 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1021,6 +1021,8 @@ 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;
+ const char *new_target = NULL;
+ const git_oid *new_id = NULL;
assert(backend);
@@ -1041,6 +1043,21 @@ static int refdb_fs_backend__write(
goto on_error;
}
+ if (ref->type == GIT_REF_SYMBOLIC)
+ new_target = ref->target.symbolic;
+ else
+ new_id = &ref->target.oid;
+
+ error = cmp_old_ref(&cmp, _backend, ref->name, new_id, new_target);
+ if (error < 0 && error != GIT_ENOTFOUND)
+ goto on_error;
+
+ /* Don't update if we have the same value */
+ if (!error && !cmp) {
+ error = 0;
+ goto on_error; /* not really error */
+ }
+
if (should_write_reflog(backend->repo, ref->name)) {
if ((error = reflog_append(backend, ref, NULL, NULL, who, message)) < 0)
goto on_error;
diff --git a/tests/refs/reflog/reflog.c b/tests/refs/reflog/reflog.c
index 149e982..a50d40a 100644
--- a/tests/refs/reflog/reflog.c
+++ b/tests/refs/reflog/reflog.c
@@ -237,3 +237,27 @@ void test_refs_reflog_reflog__append_to_HEAD_when_changing_current_branch(void)
cl_assert_equal_i(nlogs_after, nlogs + 1);
}
+
+void test_refs_reflog_reflog__do_not_append_when_no_update(void)
+{
+ size_t nlogs, nlogs_after;
+ git_reference *ref, *ref2;
+ git_reflog *log;
+
+ cl_git_pass(git_reflog_read(&log, g_repo, "HEAD"));
+ nlogs = git_reflog_entrycount(log);
+ git_reflog_free(log);
+
+ cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
+ cl_git_pass(git_reference_create(&ref2, g_repo, "refs/heads/master",
+ git_reference_target(ref), 1, NULL, NULL));
+
+ git_reference_free(ref);
+ git_reference_free(ref2);
+
+ 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);
+}
diff --git a/tests/repo/head.c b/tests/repo/head.c
index a246e60..130ed85 100644
--- a/tests/repo/head.c
+++ b/tests/repo/head.c
@@ -341,3 +341,30 @@ void test_repo_head__orphan_branch_does_not_count(void)
git_signature_free(sig);
}
+
+void test_repo_head__set_to_current_target(void)
+{
+ git_signature *sig;
+ const char *msg;
+ git_reflog *log;
+ size_t nentries, nentries_after;
+
+ cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+ nentries = git_reflog_entrycount(log);
+ git_reflog_free(log);
+
+ cl_git_pass(git_signature_now(&sig, "me", "foo@example.com"));
+
+ msg = "message 1";
+ cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg));
+ cl_git_pass(git_repository_set_head(repo, "refs/heads/haacked", sig, msg));
+
+ cl_git_pass(git_reflog_read(&log, repo, GIT_HEAD_FILE));
+ nentries_after = git_reflog_entrycount(log);
+ git_reflog_free(log);
+
+ cl_assert_equal_i(nentries + 1, nentries_after);
+
+ git_signature_free(sig);
+
+}