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>
This commit is contained in:
parent
9bbb0fa1fd
commit
4738a33338
30
refs.c
30
refs.c
@ -3170,12 +3170,13 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_ref(const char *action, const char *refname,
|
static struct ref_lock *update_ref_lock(const char *refname,
|
||||||
const unsigned char *sha1, const unsigned char *oldval,
|
const unsigned char *oldval,
|
||||||
int flags, enum action_on_err onerr)
|
int flags, int *type_p,
|
||||||
|
enum action_on_err onerr)
|
||||||
{
|
{
|
||||||
static struct ref_lock *lock;
|
struct ref_lock *lock;
|
||||||
lock = lock_any_ref_for_update(refname, oldval, flags, NULL);
|
lock = lock_any_ref_for_update(refname, oldval, flags, type_p);
|
||||||
if (!lock) {
|
if (!lock) {
|
||||||
const char *str = "Cannot lock the ref '%s'.";
|
const char *str = "Cannot lock the ref '%s'.";
|
||||||
switch (onerr) {
|
switch (onerr) {
|
||||||
@ -3183,8 +3184,14 @@ int update_ref(const char *action, const char *refname,
|
|||||||
case DIE_ON_ERR: die(str, refname); break;
|
case DIE_ON_ERR: die(str, refname); break;
|
||||||
case QUIET_ON_ERR: break;
|
case QUIET_ON_ERR: break;
|
||||||
}
|
}
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
return lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int update_ref_write(const char *action, const char *refname,
|
||||||
|
const unsigned char *sha1, struct ref_lock *lock,
|
||||||
|
enum action_on_err onerr)
|
||||||
|
{
|
||||||
if (write_ref_sha1(lock, sha1, action) < 0) {
|
if (write_ref_sha1(lock, sha1, action) < 0) {
|
||||||
const char *str = "Cannot update the ref '%s'.";
|
const char *str = "Cannot update the ref '%s'.";
|
||||||
switch (onerr) {
|
switch (onerr) {
|
||||||
@ -3197,6 +3204,17 @@ int update_ref(const char *action, const char *refname,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int update_ref(const char *action, const char *refname,
|
||||||
|
const unsigned char *sha1, const unsigned char *oldval,
|
||||||
|
int flags, enum action_on_err onerr)
|
||||||
|
{
|
||||||
|
struct ref_lock *lock;
|
||||||
|
lock = update_ref_lock(refname, oldval, flags, 0, onerr);
|
||||||
|
if (!lock)
|
||||||
|
return 1;
|
||||||
|
return update_ref_write(action, refname, sha1, lock, onerr);
|
||||||
|
}
|
||||||
|
|
||||||
struct ref *find_ref_by_name(const struct ref *list, const char *name)
|
struct ref *find_ref_by_name(const struct ref *list, const char *name)
|
||||||
{
|
{
|
||||||
for ( ; list; list = list->next)
|
for ( ; list; list = list->next)
|
||||||
|
Loading…
Reference in New Issue
Block a user