Skip to content

Commit 4738a33

Browse files
bradkinggitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9bbb0fa commit 4738a33

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

refs.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,21 +3170,28 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
31703170
return retval;
31713171
}
31723172

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)
31763177
{
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);
31793180
if (!lock) {
31803181
const char *str = "Cannot lock the ref '%s'.";
31813182
switch (onerr) {
31823183
case MSG_ON_ERR: error(str, refname); break;
31833184
case DIE_ON_ERR: die(str, refname); break;
31843185
case QUIET_ON_ERR: break;
31853186
}
3186-
return 1;
31873187
}
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+
{
31883195
if (write_ref_sha1(lock, sha1, action) < 0) {
31893196
const char *str = "Cannot update the ref '%s'.";
31903197
switch (onerr) {
@@ -3197,6 +3204,17 @@ int update_ref(const char *action, const char *refname,
31973204
return 0;
31983205
}
31993206

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+
32003218
struct ref *find_ref_by_name(const struct ref *list, const char *name)
32013219
{
32023220
for ( ; list; list = list->next)

0 commit comments

Comments
 (0)