summaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorCarlos Rica <jasampler@gmail.com>2007-09-05 03:38:24 +0200
committerJunio C Hamano <gitster@pobox.com>2007-09-05 11:29:33 -0700
commit3d9f037c60ceae1bd60ee3c861564812a89b05b1 (patch)
tree6bf932b630728e2313ffacd8ef573eb63fdc383e /refs.c
parent1e61b7640d09015213dbcae3564fa27ac6a8c151 (diff)
downloadgit-3d9f037c60ceae1bd60ee3c861564812a89b05b1.tar.gz
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>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c27
1 files changed, 27 insertions, 0 deletions
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;
+}