git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Kenneth Cochran <kenneth.cochran101@gmail.com>
To: Kenneth Cochran <kenneth.cochran101@gmail.com>
Cc: git@vger.kernel.org, Sahil Dua <sahildua2305@gmail.com>,
	Duy Nguyen <pclouds@gmail.com>, Jeff King <peff@peff.net>
Subject: Re: [RFC PATCH 4/4] Add alias option to git branch
Date: Mon, 4 Mar 2019 09:49:56 -0600	[thread overview]
Message-ID: <C678086A-93E9-4F0B-AAAA-FBBBC27F2BFC@gmail.com> (raw)
In-Reply-To: <D4829D84-9192-4C7A-8487-0374DAFC324A@gmail.com>

From c1bad54b29ae1c1d8548d248f6ecaa5959e55f7b Mon Sep 17 00:00:00 2001
From: Kenneth Cochran <kenneth.cochran101@gmail.com>
Date: Mon, 4 Mar 2019 01:08:46 -0600
Subject: [RFC PATCH 4/4] branch: disallow accidentally breaking symrefs
Cc: Sahil Dua <sahildua2305@gmail.com>,
    Duy Nguyen <pclouds@gmail.com>,
    Jeff King <peff@peff.net>

Currently, symrefs are ignored when deleting a branch, making
it possible to accidentally create dangling symrefs without any
warning.

These changes give an error if a branch deletion operation would
create a dangling symref, unless the user specifies to force the
delete anyways.

Signed-off-by: Kenneth Cochran <kenneth.cochran101@gmail.com>
---
 builtin/branch.c        | 20 ++++++++++++++++++++
 t/t3207-branch-alias.sh | 12 ++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/builtin/branch.c b/builtin/branch.c
index 4b8b8fc08f..5c05ccd5e9 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -233,6 +233,11 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		if (kinds == FILTER_REFS_BRANCHES) {
 			const struct worktree *wt =
 				find_shared_symref("HEAD", name);
+			char *buf;
+			size_t len;
+			FILE *memstream;
+			int cont = 0;
+
 			if (wt) {
 				error(_("Cannot delete branch '%s' "
 					"checked out at '%s'"),
@@ -240,6 +245,21 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 				ret = 1;
 				continue;
 			}
+
+			memstream = open_memstream(&buf, &len);
+			warn_dangling_symref(memstream, "%s", name);
+			fclose(memstream);
+			buf[strcspn(buf, "\n")] = '\0';
+			if (len > 0 && !force) {
+				cont = 1;
+				error(_("Deleting branch '%s' would leave dangling symrefs.\n"
+					"If you're sure you want to delete it, run 'git branch -D %s'\n"
+					"The following symrefs would be left dangling:\n%s"),
+				      bname.buf, bname.buf, buf);
+			}
+			free(buf);
+			if (cont)
+				continue;
 		}
 
 		target = resolve_refdup(name,
diff --git a/t/t3207-branch-alias.sh b/t/t3207-branch-alias.sh
index c1edeed4eb..2f3a7b398b 100755
--- a/t/t3207-branch-alias.sh
+++ b/t/t3207-branch-alias.sh
@@ -68,4 +68,16 @@ test_expect_success 'git branch -d refuses to delete an indirectly checked out s
 	test_must_fail git branch -d symd
 '
 
+test_expect_success 'git branch -d refuses to create a dangling symref' '
+	git branch dangling_parent &&
+	git branch --alias dangling dangling_parent &&
+	git branch -d dangling_parent &&
+	test_path_is_file .git/refs/heads/dangling_parent
+'
+
+test_expect_success 'git branch -D forces creation of dangling symref' '
+	git branch -D dangling_parent &&
+	test_must_fail test_path_is_file .git/refs/heads/dangling_parent
+'
+
 test_done
-- 
2.17.1



  reply	other threads:[~2019-03-04 15:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-04 15:45 [RFC PATCH 0/4] Add alias option to git branch Kenneth Cochran
2019-03-04 15:47 ` [RFC PATCH 1/4] " Kenneth Cochran
2019-03-04 15:48   ` [RFC PATCH 2/4] " Kenneth Cochran
2019-03-04 15:49     ` [RFC PATCH 3/4] " Kenneth Cochran
2019-03-04 15:49       ` Kenneth Cochran [this message]
2019-03-04 16:38         ` [RFC PATCH 4/4] " Kenneth Cochran
     [not found]         ` <CAJ145vUChd7+5QkmJsOK3bzZsudWfzZYp5wHZDzoq8SKSv0g0A@mail.gmail.com>
2019-03-04 19:33           ` Phil Sainty
2019-03-04 19:52             ` Kenneth Cochran
2019-03-05 13:21 ` [RFC PATCH 0/4] " Junio C Hamano
2019-03-05 17:36   ` Kenneth Cochran
2019-03-06 10:13     ` Phil Sainty

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=C678086A-93E9-4F0B-AAAA-FBBBC27F2BFC@gmail.com \
    --to=kenneth.cochran101@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    --cc=sahildua2305@gmail.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).