refs: factor update_ref steps into helpers

Factor the lock and write steps and error handling into helper functions update_ref_lock and update_ref_write to allow later use elsewhere. Expose lock_any_ref_for_update's type_p to update_ref_lock callers. While at it, drop "static" from the local "lock" variable as it is not necessary to keep across invocations. Signed-off-by: Brad King <brad.king@kitware.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brad King committed Sep 4, 2013 at 11:22 UTC 4738a33338b837d4896b5e8d75e777f5e99a25cb
1 file changed +24 -6
refs.c
+24 -6
@@ -3170,12 +3170,13 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
3170 return retval;
3171 }
3172
3173 -int update_ref(const char *action, const char *refname,
3174 - const unsigned char *sha1, const unsigned char *oldval,
3175 - int flags, enum action_on_err onerr)
3173 +static struct ref_lock *update_ref_lock(const char *refname,
3174 + const unsigned char *oldval,
3175 + int flags, int *type_p,
3176 + enum action_on_err onerr)
3177 {
3177 - static struct ref_lock *lock;
3178 - lock = lock_any_ref_for_update(refname, oldval, flags, NULL);
3178 + struct ref_lock *lock;
3179 + lock = lock_any_ref_for_update(refname, oldval, flags, type_p);
3180 if (!lock) {
3181 const char *str = "Cannot lock the ref '%s'.";
3182 switch (onerr) {
@@ -3183,8 +3184,14 @@ int update_ref(const char *action, const char *refname,
3184 case DIE_ON_ERR: die(str, refname); break;
3185 case QUIET_ON_ERR: break;
3186 }
3186 - return 1;
3187 }
3188 + return lock;
3189 +}
3190 +
3191 +static int update_ref_write(const char *action, const char *refname,
3192 + const unsigned char *sha1, struct ref_lock *lock,
3193 + enum action_on_err onerr)
3194 +{
3195 if (write_ref_sha1(lock, sha1, action) < 0) {
3196 const char *str = "Cannot update the ref '%s'.";
3197 switch (onerr) {
@@ -3197,6 +3204,17 @@ int update_ref(const char *action, const char *refname,
3204 return 0;
3205 }
3206
3207 +int update_ref(const char *action, const char *refname,
3208 + const unsigned char *sha1, const unsigned char *oldval,
3209 + int flags, enum action_on_err onerr)
3210 +{
3211 + struct ref_lock *lock;
3212 + lock = update_ref_lock(refname, oldval, flags, 0, onerr);
3213 + if (!lock)
3214 + return 1;
3215 + return update_ref_write(action, refname, sha1, lock, onerr);
3216 +}
3217 +
3218 struct ref *find_ref_by_name(const struct ref *list, const char *name)
3219 {
3220 for ( ; list; list = list->next)