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

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 38e50e81e3ee1d0e329245a3c63512cbd8a1ab44
1 file changed +29 -16
refs.c
+29 -16
@@ -3085,24 +3085,13 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
3085 }
3086
3087 /*
3088 - * Write sha1 into the ref specified by the lock. Make sure that errno
3089 - * is sane on error.
3088 + * Commit a change to a loose reference that has already been written
3089 + * to the loose reference lockfile. Also update the reflogs if
3090 + * necessary, using the specified lockmsg (which can be NULL).
3091 */
3091 -static int write_ref_sha1(struct ref_lock *lock,
3092 - const unsigned char *sha1, const char *logmsg)
3092 +static int commit_ref_update(struct ref_lock *lock,
3093 + const unsigned char *sha1, const char *logmsg)
3094 {
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 -
3095 clear_loose_ref_cache(&ref_cache);
3096 if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
3097 (strcmp(lock->ref_name, lock->orig_ref_name) &&
@@ -3141,6 +3130,30 @@ static int write_ref_sha1(struct ref_lock *lock,
3130 return 0;
3131 }
3132
3133 +/*
3134 + * Write sha1 as the new value of the reference specified by the
3135 + * (open) lock. On error, roll back the lockfile and set errno
3136 + * appropriately.
3137 + */
3138 +static int write_ref_sha1(struct ref_lock *lock,
3139 + const unsigned char *sha1, const char *logmsg)
3140 +{
3141 + if (!lock) {
3142 + errno = EINVAL;
3143 + return -1;
3144 + }
3145 + if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3146 + unlock_ref(lock);
3147 + return 0;
3148 + }
3149 +
3150 + if (write_ref_to_lockfile(lock, sha1) ||
3151 + commit_ref_update(lock, sha1, logmsg))
3152 + return -1;
3153 +
3154 + return 0;
3155 +}
3156 +
3157 int create_symref(const char *ref_target, const char *refs_heads_master,
3158 const char *logmsg)
3159 {