git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Ivan Tham <pickfire@riseup.net>
To: git@vger.kernel.org
Cc: "brian m . carlson" <sandals@crustytoothpaste.net>
Subject: [PATCH] branch: add '-' to delete previous branch
Date: Wed, 29 Apr 2020 21:01:33 +0800	[thread overview]
Message-ID: <20200429130133.520981-1-pickfire@riseup.net> (raw)

Add support to delete previous branch from git checkout/switch to have
feature parity with git switch -.

Signed-off-by: Ivan Tham <pickfire@riseup.net>
---
 Documentation/git-branch.txt | 10 ++++++++++
 builtin/branch.c             |  6 +++++-
 t/t3200-branch.sh            |  7 +++++++
 3 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 135206ff4a..37e7cbbc52 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -265,6 +265,10 @@ start-point is either a local or remote-tracking branch.
 	The new branch name must pass all checks defined by
 	linkgit:git-check-ref-format[1].  Some of these checks
 	may restrict the characters allowed in a branch name.
++
+You can use the `@{-N}` syntax to refer to the N-th last branch checked out
+using "git checkout" operation. You may also specify `-` which is synonymous to
+`@{-1}`.
 
 <start-point>::
 	The new branch head will point to this commit.  It may be
@@ -334,6 +338,12 @@ $ git branch -D test                                    <2>
 <2> Delete the "test" branch even if the "master" branch (or whichever branch
     is currently checked out) does not have all commits from the test branch.
 
+To delete the previous branch::
++
+------------
+$ git branch -D -
+------------
+
 Listing branches from a specific remote::
 +
 ------------
diff --git a/builtin/branch.c b/builtin/branch.c
index d8297f80ff..5537f819a6 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -227,9 +227,13 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	}
 	for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
 		char *target = NULL;
+		const char *arg = argv[i];
 		int flags = 0;
 
-		strbuf_branchname(&bname, argv[i], allowed_interpret);
+		if (!strcmp(arg, "-"))
+			arg = "@{-1}";
+
+		strbuf_branchname(&bname, arg, allowed_interpret);
 		free(name);
 		name = mkpathdup(fmt, bname.buf);
 
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 411a70b0ce..6819107c1d 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1387,4 +1387,11 @@ test_expect_success 'invalid sort parameter in configuration' '
 	)
 '
 
+test_expect_success 'delete previous branch' '
+	git checkout -b a &&
+	git checkout -b b &&
+	git branch -D - &&
+	test_path_is_missing .git/refs/heads/a
+'
+
 test_done
-- 
2.26.2


             reply	other threads:[~2020-04-29 13:02 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-29 13:01 Ivan Tham [this message]
2020-04-29 13:37 ` [PATCH] branch: add '-' to delete previous branch Sergey Organov
2020-04-29 19:00   ` Taylor Blau
2020-04-29 19:06     ` Eric Sunshine
2020-04-29 19:50     ` Sergey Organov
2020-04-29 19:57       ` Taylor Blau
2020-04-29 20:22         ` Junio C Hamano
2020-04-29 20:35           ` Taylor Blau
2020-04-29 20:39             ` Junio C Hamano
2020-04-29 20:37           ` Junio C Hamano
2020-04-29 20:26         ` Sergey Organov
2020-04-30 16:27           ` Konstantin Khomoutov
2020-04-30 19:52             ` Junio C Hamano
2020-05-01  9:18               ` Sergey Organov
2020-05-01  9:44                 ` Konstantin Khomoutov
2020-05-01 10:22                   ` Sergey Organov
2020-05-01 22:22                     ` Taylor Blau
2020-04-30  3:43       ` Ivan Tham
2020-04-29 19:27   ` Junio C Hamano
2020-04-29 19:31     ` Randall S. Becker
2020-04-29 19:55     ` Sergey Organov
2020-04-29 18:58 ` Taylor Blau
2020-04-30 14:52   ` Ivan Tham
2020-04-30 15:59     ` Taylor Blau
2020-04-30 19:51       ` 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=20200429130133.520981-1-pickfire@riseup.net \
    --to=pickfire@riseup.net \
    --cc=git@vger.kernel.org \
    --cc=sandals@crustytoothpaste.net \
    /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).