Skip to content

Commit 706d5f8

Browse files
mhaggergitster
authored andcommitted
write_ref_sha1(): move write elision test to callers
write_ref_sha1() previously skipped the write if the reference already had the desired value, unless lock->force_write was set. Instead, perform that test at the callers. Two of the callers (in rename_ref()) unconditionally set force_write just before calling write_ref_sha1(), so they don't need the extra check at all. Nor do they need to set force_write anymore. The last caller, in ref_transaction_commit(), still needs the test. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8280bbe commit 706d5f8

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

refs.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
28782878
error("unable to lock %s for update", newrefname);
28792879
goto rollback;
28802880
}
2881-
lock->force_write = 1;
28822881
hashcpy(lock->old_sha1, orig_sha1);
28832882
if (write_ref_sha1(lock, orig_sha1, logmsg)) {
28842883
error("unable to write current sha1 into %s", newrefname);
@@ -2894,7 +2893,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
28942893
goto rollbacklog;
28952894
}
28962895

2897-
lock->force_write = 1;
28982896
flag = log_all_ref_updates;
28992897
log_all_ref_updates = 0;
29002898
if (write_ref_sha1(lock, orig_sha1, NULL))
@@ -3080,10 +3078,6 @@ static int write_ref_sha1(struct ref_lock *lock,
30803078
static char term = '\n';
30813079
struct object *o;
30823080

3083-
if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3084-
unlock_ref(lock);
3085-
return 0;
3086-
}
30873081
o = parse_object(sha1);
30883082
if (!o) {
30893083
error("Trying to write ref %s with nonexistent object %s",
@@ -3797,15 +3791,21 @@ int ref_transaction_commit(struct ref_transaction *transaction,
37973791
struct ref_update *update = updates[i];
37983792

37993793
if (!is_null_sha1(update->new_sha1)) {
3800-
if (write_ref_sha1(update->lock, update->new_sha1,
3801-
update->msg)) {
3794+
if (!update->lock->force_write &&
3795+
!hashcmp(update->lock->old_sha1, update->new_sha1)) {
3796+
unlock_ref(update->lock);
3797+
update->lock = NULL;
3798+
} else if (write_ref_sha1(update->lock, update->new_sha1,
3799+
update->msg)) {
38023800
update->lock = NULL; /* freed by write_ref_sha1 */
38033801
strbuf_addf(err, "Cannot update the ref '%s'.",
38043802
update->refname);
38053803
ret = TRANSACTION_GENERIC_ERROR;
38063804
goto cleanup;
3805+
} else {
3806+
/* freed by write_ref_sha1(): */
3807+
update->lock = NULL;
38073808
}
3808-
update->lock = NULL; /* freed by write_ref_sha1 */
38093809
}
38103810
}
38113811

0 commit comments

Comments
 (0)