+/*
+ * Write sha1 as the new value of the reference specified by the
+ * (open) lock. On error, roll back the lockfile and set errno
+ * appropriately.
+ */
+static int write_ref_sha1(struct ref_lock *lock,
+ const unsigned char *sha1, const char *logmsg)
+{
+ if (!lock) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
+ unlock_ref(lock);
+ return 0;
+ }
+
+ if (write_ref_to_lockfile(lock, sha1) ||
+ commit_ref_update(lock, sha1, logmsg))
+ return -1;
+
+ return 0;
+}
+