git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Carlos Rica <jasampler@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: [PATCH] Functions for updating refs.
Date: Tue, 04 Sep 2007 15:39:44 +0200	[thread overview]
Message-ID: <46DD6020.4050401@gmail.com> (raw)

Signed-off-by: Carlos Rica <jasampler@gmail.com>
---

   They are designed to be reused also from other builtins,
   like the recently changed builtin-tag.c and the upcoming
   builtin-reset.c, and perhaps also from builtin-fetch--tool.c.

 builtin-update-ref.c |    8 ++------
 refs.c               |   32 ++++++++++++++++++++++++++++++++
 refs.h               |    8 ++++++++
 send-pack.c          |   12 ++++--------
 4 files changed, 46 insertions(+), 14 deletions(-)

diff --git a/builtin-update-ref.c b/builtin-update-ref.c
index 8339cf1..bd7fe4d 100644
--- a/builtin-update-ref.c
+++ b/builtin-update-ref.c
@@ -62,10 +62,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 	if (oldval && *oldval && get_sha1(oldval, oldsha1))
 		die("%s: not a valid old SHA1", oldval);

-	lock = lock_any_ref_for_update(refname, oldval ? oldsha1 : NULL, ref_flags);
-	if (!lock)
-		die("%s: cannot lock the ref", refname);
-	if (write_ref_sha1(lock, sha1, msg) < 0)
-		die("%s: cannot update the ref", refname);
-	return 0;
+	return update_ref_or_die(msg, refname, sha1,
+				oldval ? oldsha1 : NULL, ref_flags);
 }
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;
+}
diff --git a/refs.h b/refs.h
index f234eb7..3d0100e 100644
--- a/refs.h
+++ b/refs.h
@@ -64,4 +64,12 @@ extern int rename_ref(const char *oldref, const char *newref, const char *logmsg
 /** resolve ref in nested "gitlink" repository */
 extern int resolve_gitlink_ref(const char *name, const char *refname, unsigned char *result);

+/** lock a ref and then write its file */
+int update_ref_or_die(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int flags);
+int update_ref_or_error(const char *action, const char *refname,
+				const unsigned char *sha1,
+				const unsigned char *oldval, int quiet);
+
 #endif /* REFS_H */
diff --git a/send-pack.c b/send-pack.c
index 9fc8a81..1907684 100644
--- a/send-pack.c
+++ b/send-pack.c
@@ -313,14 +313,10 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
 					if (delete_ref(rs.dst, NULL)) {
 						error("Failed to delete");
 					}
-				} else {
-					lock = lock_any_ref_for_update(rs.dst, NULL, 0);
-					if (!lock)
-						error("Failed to lock");
-					else
-						write_ref_sha1(lock, ref->new_sha1,
-							       "update by push");
-				}
+				} else
+					update_ref_or_error("update by push",
+							rs.dst, ref->new_sha1,
+							NULL, 0);
 				free(rs.dst);
 			}
 		}
-- 
1.5.0

             reply	other threads:[~2007-09-04 13:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-04 13:39 Carlos Rica [this message]
2007-09-04 13:45 ` [PATCH] Functions for updating refs Johannes Schindelin
2007-09-04 14:28   ` Johannes Sixt
2007-09-04 14:58     ` Johannes Schindelin
2007-09-04 15:08       ` Johannes Sixt
2007-09-04 15:29         ` Karl Hasselström
2007-09-04 15:33         ` Johannes Schindelin
2007-09-04 17:52 ` Junio C Hamano
2007-09-04 23:32   ` Carlos Rica
2007-09-05  0:16     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46DD6020.4050401@gmail.com \
    --to=jasampler@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).