refs: factor out old value comparison We will reuse this later for deletion.
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
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 879e485..d1b20f8 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -930,6 +930,30 @@ static bool should_write_reflog(git_repository *repo, const char *name)
return 0;
}
+static int cmp_old_ref(int *cmp, git_refdb_backend *backend, const git_reference *ref,
+ const git_oid *old_id, const char *old_target)
+{
+ int error = 0;
+ git_reference *old_ref = NULL;
+
+ *cmp = 0;
+ if (old_id || old_target) {
+ if ((error = refdb_fs_backend__lookup(&old_ref, backend, ref->name)) < 0)
+ goto out;
+ }
+
+ if (old_id && old_ref->type == GIT_REF_OID)
+ *cmp = git_oid_cmp(old_id, &old_ref->target.oid);
+
+ if (old_target && old_ref->type == GIT_REF_SYMBOLIC)
+ *cmp = git__strcmp(old_target, old_ref->target.symbolic);
+
+out:
+ git_reference_free(old_ref);
+
+ return error;
+}
+
static int refdb_fs_backend__write(
git_refdb_backend *_backend,
const git_reference *ref,
@@ -954,20 +978,8 @@ static int refdb_fs_backend__write(
if ((error = loose_lock(&file, backend, ref)) < 0)
return error;
- if (old_id || old_target) {
- if ((error = refdb_fs_backend__lookup(&old_ref, _backend, ref->name)) < 0)
- goto on_error;
- }
-
- if (old_id && old_ref->type == GIT_REF_OID) {
- cmp = git_oid_cmp(old_id, &old_ref->target.oid);
- git_reference_free(old_ref);
- }
-
- if (old_target && old_ref->type == GIT_REF_SYMBOLIC) {
- cmp = git__strcmp(old_target, old_ref->target.symbolic);
- git_reference_free(old_ref);
- }
+ if ((error = cmp_old_ref(&cmp, _backend, ref, old_id, old_target)) < 0)
+ goto on_error;
if (cmp) {
giterr_set(GITERR_REFERENCE, "old reference value does not match");