Function for updating refs.
A function intended to be called from builtins updating refs by locking them before write, specially those that came from scripts using "git update-ref". [jc: with minor fixups] Signed-off-by: Carlos Rica <jasampler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
1e61b7640d
commit
3d9f037c60
27
refs.c
27
refs.c
@ -1455,3 +1455,30 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
|
||||
{
|
||||
return do_for_each_reflog("", fn, cb_data);
|
||||
}
|
||||
|
||||
int update_ref(const char *action, const char *refname,
|
||||
const unsigned char *sha1, const unsigned char *oldval,
|
||||
int flags, enum action_on_err onerr)
|
||||
{
|
||||
static struct ref_lock *lock;
|
||||
lock = lock_any_ref_for_update(refname, oldval, flags);
|
||||
if (!lock) {
|
||||
const char *str = "Cannot lock the ref '%s'.";
|
||||
switch (onerr) {
|
||||
case MSG_ON_ERR: error(str, refname); break;
|
||||
case DIE_ON_ERR: die(str, refname); break;
|
||||
case QUIET_ON_ERR: break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (write_ref_sha1(lock, sha1, action) < 0) {
|
||||
const char *str = "Cannot update the ref '%s'.";
|
||||
switch (onerr) {
|
||||
case MSG_ON_ERR: error(str, refname); break;
|
||||
case DIE_ON_ERR: die(str, refname); break;
|
||||
case QUIET_ON_ERR: break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user