write_ref_to_lockfile(): new function, extracted from write_ref_sha1()

This is the first step towards separating the checking and writing of the new reference value to committing the change. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed May 10, 2015 at 04:45 UTC 1d455231a0f823dc75cd5c7e32b818a4dc3ec020
1 file changed +26 -12
refs.c
+26 -12
@@ -3048,23 +3048,15 @@ int is_branch(const char *refname)
3048 }
3049
3050 /*
3051 - * Write sha1 into the ref specified by the lock. Make sure that errno
3052 - * is sane on error.
3051 + * Write sha1 into the open lockfile, then close the lockfile. On
3052 + * errors, rollback the lockfile and set errno to reflect the problem.
3053 */
3054 -static int write_ref_sha1(struct ref_lock *lock,
3055 - const unsigned char *sha1, const char *logmsg)
3054 +static int write_ref_to_lockfile(struct ref_lock *lock,
3055 + const unsigned char *sha1)
3056 {
3057 static char term = '\n';
3058 struct object *o;
3059
3060 - if (!lock) {
3061 - errno = EINVAL;
3062 - return -1;
3063 - }
3064 - if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3065 - unlock_ref(lock);
3066 - return 0;
3067 - }
3060 o = parse_object(sha1);
3061 if (!o) {
3062 error("Trying to write ref %s with nonexistent object %s",
@@ -3089,6 +3081,28 @@ static int write_ref_sha1(struct ref_lock *lock,
3081 errno = save_errno;
3082 return -1;
3083 }
3084 + return 0;
3085 +}
3086 +
3087 +/*
3088 + * Write sha1 into the ref specified by the lock. Make sure that errno
3089 + * is sane on error.
3090 + */
3091 +static int write_ref_sha1(struct ref_lock *lock,
3092 + const unsigned char *sha1, const char *logmsg)
3093 +{
3094 + if (!lock) {
3095 + errno = EINVAL;
3096 + return -1;
3097 + }
3098 + if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3099 + unlock_ref(lock);
3100 + return 0;
3101 + }
3102 +
3103 + if (write_ref_to_lockfile(lock, sha1))
3104 + return -1;
3105 +
3106 clear_loose_ref_cache(&ref_cache);
3107 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
3108 (strcmp(lock->ref_name, lock->orig_ref_name) &&