This makes write_ref_sha1 only write the the lock file, committing
needs to be done outside of that function. This will help us change
the ref_transaction_commit in a later patch.

Signed-off-by: Stefan Beller <sbel...@google.com>
---
 refs.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/refs.c b/refs.c
index 6f3cd7b..c108c95 100644
--- a/refs.c
+++ b/refs.c
@@ -2815,8 +2815,11 @@ static int close_ref(struct ref_lock *lock)
        return 0;
 }
 
-static int commit_ref(struct ref_lock *lock)
+static int commit_ref(struct ref_lock *lock, const unsigned char *sha1)
 {
+       if (!lock->force_write && !hashcmp(lock->old_sha1, sha1))
+               return 0;
+
        if (commit_lock_file(lock->lk))
                return -1;
        return 0;
@@ -2879,7 +2882,8 @@ int rename_ref(const char *oldrefname, const char 
*newrefname, const char *logms
        }
        lock->force_write = 1;
        hashcpy(lock->old_sha1, orig_sha1);
-       if (write_ref_sha1(lock, orig_sha1, logmsg)) {
+       if (write_ref_sha1(lock, orig_sha1, logmsg)
+           || commit_ref(lock, orig_sha1)) {
                unlock_ref(lock);
                error("unable to write current sha1 into %s", newrefname);
                goto rollback;
@@ -2898,8 +2902,10 @@ int rename_ref(const char *oldrefname, const char 
*newrefname, const char *logms
        lock->force_write = 1;
        flag = log_all_ref_updates;
        log_all_ref_updates = 0;
-       if (write_ref_sha1(lock, orig_sha1, NULL))
+       if (write_ref_sha1(lock, orig_sha1, NULL)
+           || commit_ref(lock, orig_sha1))
                error("unable to write current sha1 into %s", oldrefname);
+
        unlock_ref(lock);
        log_all_ref_updates = flag;
 
@@ -3137,10 +3143,6 @@ static int write_ref_sha1(struct ref_lock *lock,
                    !strcmp(head_ref, lock->ref_name))
                        log_ref_write("HEAD", lock->old_sha1, sha1, logmsg);
        }
-       if (commit_ref(lock)) {
-               error("Couldn't set %s", lock->ref_name);
-               return -1;
-       }
        return 0;
 }
 
@@ -3775,7 +3777,8 @@ int ref_transaction_commit(struct ref_transaction 
*transaction,
 
                if (!is_null_sha1(update->new_sha1)) {
                        if (write_ref_sha1(update->lock, update->new_sha1,
-                                          update->msg)) {
+                                          update->msg)
+                           || commit_ref(update->lock, update->new_sha1)) {
                                strbuf_addf(err, "Cannot update the ref '%s'.",
                                            update->refname);
                                ret = TRANSACTION_GENERIC_ERROR;
@@ -4064,7 +4067,8 @@ int reflog_expire(const char *refname, const unsigned 
char *sha1,
                } else if (commit_lock_file(&reflog_lock)) {
                        status |= error("unable to commit reflog '%s' (%s)",
                                        log_file, strerror(errno));
-               } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF) && 
commit_ref(lock)) {
+               } else if ((flags & EXPIRE_REFLOGS_UPDATE_REF)
+                           && commit_ref(lock, cb.last_kept_sha1)) {
                        status |= error("couldn't set %s", lock->ref_name);
                }
        }
-- 
2.2.1.62.g3f15098

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to