From 3d9f037c60ceae1bd60ee3c861564812a89b05b1 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Wed, 5 Sep 2007 03:38:24 +0200 Subject: 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 Signed-off-by: Junio C Hamano --- refs.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 09a2c87fc2..7fb3350789 100644 --- a/refs.c +++ b/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; +} -- cgit v1.2.3