git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ronnie Sahlberg <sahlberg@google.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, Ronnie Sahlberg <sahlberg@google.com>
Subject: [PATCH 15/15] refs.c: allow deleting refs with a broken sha1
Date: Wed, 23 Jul 2014 10:03:55 -0700	[thread overview]
Message-ID: <1406135035-26441-16-git-send-email-sahlberg@google.com> (raw)
In-Reply-To: <1406135035-26441-1-git-send-email-sahlberg@google.com>

Add (back?) support to make it possible to delete refs that are broken.

Add a new flag REF_ALLOWBROKEN that can be passed to the functions to
lock a ref. If this flag is set we allow locking the ref even if the
ref points to a broken sha1. For example a sha1 that is created by :

   echo "Broken ref" > .git/refs/heads/foo-broken-1

Use this flag when calling from branch.c dusing a ref delete so that we
only allow locking those broken refs IFF when called during a branch
delete.

Signed-off-by: Ronnie Sahlberg <sahlberg@google.com>
---
 builtin/branch.c |  6 ++++--
 refs.c           | 10 +++++++---
 refs.h           |  3 ++-
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 5c95656..6d70037 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -236,6 +236,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		name = mkpathdup(fmt, bname.buf);
 		target = resolve_ref_unsafe(name, sha1,
 					    RESOLVE_REF_ALLOW_BAD_NAME, &flags);
+		if (!target && (flags & REF_ISBROKEN))
+			target = name;
 		if (!target ||
 		    (!(flags & REF_ISSYMREF) && is_null_sha1(sha1))) {
 			error(remote_branch
@@ -245,14 +247,14 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 			continue;
 		}
 
-		if (!(flags & REF_ISSYMREF) &&
+		if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
 		    check_branch_commit(bname.buf, name, sha1, head_rev, kinds,
 					force)) {
 			ret = 1;
 			continue;
 		}
 
-		if (delete_ref(name, sha1, REF_NODEREF)) {
+		if (delete_ref(name, sha1, REF_NODEREF|REF_ALLOWBROKEN)) {
 			error(remote_branch
 			      ? _("Error deleting remote branch '%s'")
 			      : _("Error deleting branch '%s'"),
diff --git a/refs.c b/refs.c
index 0ead11f..2662ef6 100644
--- a/refs.c
+++ b/refs.c
@@ -2122,12 +2122,12 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 	int resolve_flags;
 	int missing = 0;
 	int attempts_remaining = 3;
-	int bad_refname;
+	int bad_ref;
 
 	lock = xcalloc(1, sizeof(struct ref_lock));
 	lock->lock_fd = -1;
 
-	bad_refname = check_refname_format(refname, REFNAME_ALLOW_ONELEVEL);
+	bad_ref = check_refname_format(refname, REFNAME_ALLOW_ONELEVEL);
 
 	resolve_flags = RESOLVE_REF_ALLOW_BAD_NAME;
 	if (mustexist)
@@ -2150,6 +2150,10 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 		refname = resolve_ref_unsafe(orig_refname, lock->old_sha1,
 					     resolve_flags, &type);
 	}
+	if (!refname && (flags & REF_ALLOWBROKEN) && (type & REF_ISBROKEN)) {
+		bad_ref = 1;
+		refname = orig_refname;
+	}
 	if (type_p)
 	    *type_p = type;
 	if (!refname) {
@@ -2212,7 +2216,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
 		else
 			unable_to_lock_index_die(ref_file, errno);
 	}
-	if (bad_refname)
+	if (bad_ref)
 		return lock;
 	return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 
diff --git a/refs.h b/refs.h
index 712fc32..0172f48 100644
--- a/refs.h
+++ b/refs.h
@@ -174,10 +174,11 @@ extern int peel_ref(const char *refname, unsigned char *sha1);
  * Flags controlling transaction_update_sha1(), transaction_create_sha1(), etc.
  * REF_NODEREF: act on the ref directly, instead of dereferencing
  *              symbolic references.
- *
+ * REF_ALLOWBROKEN: allow locking refs that are broken.
  * Flags >= 0x100 are reserved for internal use.
  */
 #define REF_NODEREF	0x01
+#define REF_ALLOWBROKEN 0x02
 
 /** Reads log for the value of ref during at_time. **/
 extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
-- 
2.0.1.508.g763ab16

  parent reply	other threads:[~2014-07-23 17:04 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-23 17:03 [PATCH 00/15] ref-transactions for reflogs Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 01/15] refs.c make ref_transaction_create a wrapper to ref_transaction_update Ronnie Sahlberg
2014-07-23 23:07   ` Junio C Hamano
2014-07-23 17:03 ` [PATCH 02/15] refs.c: make ref_transaction_delete a wrapper for ref_transaction_update Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 03/15] refs.c: rename the transaction functions Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 04/15] refs.c: add a new update_type field to ref_update Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 05/15] refs.c: add a function to append a reflog entry to a fd Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 06/15] lockfile.c: make hold_lock_file_for_append preserve meaningful errno Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 07/15] refs.c: add a transaction function to append a reflog entry Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 08/15] refs.c: add a flag to allow reflog updates to truncate the log Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 09/15] refs.c: only write reflog update if msg is non-NULL Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 10/15] refs.c: allow multiple reflog updates during a single transaction Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 11/15] reflog.c: use a reflog transaction when writing during expire Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 12/15] refs.c: rename log_ref_setup to create_reflog Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 13/15] refs.c: make unlock_ref/close_ref/commit_ref static Ronnie Sahlberg
2014-07-23 17:03 ` [PATCH 14/15] refs.c: remove lock_any_ref_for_update Ronnie Sahlberg
2014-07-23 17:03 ` Ronnie Sahlberg [this message]
2014-07-23 21:22   ` [PATCH 15/15] refs.c: allow deleting refs with a broken sha1 Eric Sunshine
2014-07-23 23:11 ` [PATCH 00/15] ref-transactions for reflogs Junio C Hamano
  -- strict thread matches above, loose matches on Subject: below --
2014-10-21 19:24 [PATCH 00/15] ref-transactions-reflog Ronnie Sahlberg
2014-10-21 19:24 ` [PATCH 15/15] refs.c: allow deleting refs with a broken sha1 Ronnie Sahlberg

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

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

  git send-email \
    --in-reply-to=1406135035-26441-16-git-send-email-sahlberg@google.com \
    --to=sahlberg@google.com \
    --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.
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 NNTP newsgroup(s).