Thread (2 messages) flat view 2 messages, 2 authors, 2016-06-15

Re: [PATCH] Functions for updating refs.

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:43:33

Carlos Rica [off-list ref] writes:
quoted hunk
diff --git a/refs.c b/refs.c
index 09a2c87..4fd5065 100644
--- a/refs.c
+++ b/refs.c
@@ -1455,3 +1455,35 @@ int for_each_reflog(each_ref_fn fn, void *cb_data)
 {
 	return do_for_each_reflog("", fn, cb_data);
 }
+
+int update_ref_or_die(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int flags)
+{
+	static struct ref_lock *lock;
+	lock = lock_any_ref_for_update(refname, oldval, flags);
+	if (!lock)
+		die("Cannot lock the ref '%s'.", refname);
+	if (write_ref_sha1(lock, sha1, action) < 0)
+		die("Cannot update the ref '%s'.", refname);
+	return 0;
+}
+
+int update_ref_or_error(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int quiet)
+{
+	static struct ref_lock *lock;
+	lock = lock_any_ref_for_update(refname, oldval, 0);
+	if (!lock) {
+		if (!quiet)
+			error("Cannot lock the ref '%s'.", refname);
+		return 1;
+	}
+	if (write_ref_sha1(lock, sha1, action) < 0) {
+		if (!quiet)
+			error("Cannot update the ref '%s'.", refname);
+		return 1;
+	}
+	return 0;
+}
This makes me wonder three things:

 - Why doesn't "or_error" side allow "flags" as "or_die" one?
   Could the 'quiet' option become part of "flags" perhaps?

 - They look quite similar.  Is it a good idea to refactor them
   further, or they are so small it does not matter?

 - Why isn't lock released with unlock_ref()?
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help