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 <mhagger@alum.mit.edu> Reviewed-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Mar 2, 2015 at 10:29 UTC 706d5f816fbcb1299c19f4d41bea60f1e229165e
1 file changed +9 -9
refs.c
+9 -9
@@ -2878,7 +2878,6 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2878 error("unable to lock %s for update", newrefname);
2879 goto rollback;
2880 }
2881 - lock->force_write = 1;
2881 hashcpy(lock->old_sha1, orig_sha1);
2882 if (write_ref_sha1(lock, orig_sha1, logmsg)) {
2883 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
2893 goto rollbacklog;
2894 }
2895
2897 - lock->force_write = 1;
2896 flag = log_all_ref_updates;
2897 log_all_ref_updates = 0;
2898 if (write_ref_sha1(lock, orig_sha1, NULL))
@@ -3080,10 +3078,6 @@ static int write_ref_sha1(struct ref_lock *lock,
3078 static char term = '\n';
3079 struct object *o;
3080
3083 - if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3084 - unlock_ref(lock);
3085 - return 0;
3086 - }
3081 o = parse_object(sha1);
3082 if (!o) {
3083 error("Trying to write ref %s with nonexistent object %s",
@@ -3797,15 +3791,21 @@ int ref_transaction_commit(struct ref_transaction *transaction,
3791 struct ref_update *update = updates[i];
3792
3793 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)) {
3800 update->lock = NULL; /* freed by write_ref_sha1 */
3801 strbuf_addf(err, "Cannot update the ref '%s'.",
3802 update->refname);
3803 ret = TRANSACTION_GENERIC_ERROR;
3804 goto cleanup;
3805 + } else {
3806 + /* freed by write_ref_sha1(): */
3807 + update->lock = NULL;
3808 }
3808 - update->lock = NULL; /* freed by write_ref_sha1 */
3809 }
3810 }
3811