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 Apr 24, 2015 at 13:35 UTC e6fd3c67308cb388effba646b52b7ba461ce79a7
1 file changed +17 -4
refs.c
+17 -4
@@ -3022,11 +3022,11 @@ int is_branch(const char *refname)
3022 }
3023
3024 /*
3025 - * Write sha1 into the ref specified by the lock. Make sure that errno
3026 - * is sane on error.
3025 + * Write sha1 into the open lockfile, then close the lockfile. On
3026 + * errors, rollback the lockfile and set errno to reflect the problem.
3027 */
3028 -static int write_ref_sha1(struct ref_lock *lock,
3029 - const unsigned char *sha1, const char *logmsg)
3028 +static int write_ref_to_lockfile(struct ref_lock *lock,
3029 + const unsigned char *sha1)
3030 {
3031 static char term = '\n';
3032 struct object *o;
@@ -3055,6 +3055,19 @@ static int write_ref_sha1(struct ref_lock *lock,
3055 errno = save_errno;
3056 return -1;
3057 }
3058 + return 0;
3059 +}
3060 +
3061 +/*
3062 + * Write sha1 into the ref specified by the lock. Make sure that errno
3063 + * is sane on error.
3064 + */
3065 +static int write_ref_sha1(struct ref_lock *lock,
3066 + const unsigned char *sha1, const char *logmsg)
3067 +{
3068 + if (write_ref_to_lockfile(lock, sha1))
3069 + return -1;
3070 +
3071 clear_loose_ref_cache(&ref_cache);
3072 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
3073 (strcmp(lock->ref_name, lock->orig_ref_name) &&