git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] branch: add '-' to delete previous branch
@ 2020-04-30 15:52 Ivan Tham
  0 siblings, 0 replies; only message in thread
From: Ivan Tham @ 2020-04-30 15:52 UTC (permalink / raw)
  To: git; +Cc: brian m . carlson, Taylor Blau

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            | 12 ++++++++++++
 3 files changed, 27 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..46dfb41e65 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -1387,4 +1387,16 @@ 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 - &&
+	git branch >actual &&
+	cat >expect <<-\EOF &&
+	* b
+	  master
+	EOF
+	test_cmp expect actual
+'
+
 test_done
-- 
2.26.2


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-04-30 15:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-30 15:52 [PATCH v2] branch: add '-' to delete previous branch Ivan Tham

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).