{"thread":{"id":"53358","subject":"[PATCH v2] branch: add '-' to delete previous branch","startedAt":"2020-04-30T15:53:16Z","lastAt":"2020-04-30T15:53:16Z","messageCount":1,"participants":["Ivan Tham"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"396666","messageId":"20200430155240.161888-1-pickfire@riseup.net","threadId":"53358","inReplyTo":null,"subject":"[PATCH v2] branch: add '-' to delete previous branch","fromName":"Ivan Tham","fromEmail":"pickfire@riseup.net","sentAt":"2020-04-30T15:52:40Z","receivedAt":"2020-04-30T15:53:16Z","isPatch":true,"sender":{"key":"pickfire@riseup.net","avatar":null},"body":"Add support to delete previous branch from git checkout/switch to have\nfeature parity with git switch -.\n\nSigned-off-by: Ivan Tham <pickfire@riseup.net>\n---\n Documentation/git-branch.txt | 10 ++++++++++\n builtin/branch.c             |  6 +++++-\n t/t3200-branch.sh            | 12 ++++++++++++\n 3 files changed, 27 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt\nindex 135206ff4a..37e7cbbc52 100644\n--- a/Documentation/git-branch.txt\n+++ b/Documentation/git-branch.txt\n@@ -265,6 +265,10 @@ start-point is either a local or remote-tracking branch.\n \tThe new branch name must pass all checks defined by\n \tlinkgit:git-check-ref-format[1].  Some of these checks\n \tmay restrict the characters allowed in a branch name.\n++\n+You can use the `@{-N}` syntax to refer to the N-th last branch checked out\n+using \"git checkout\" operation. You may also specify `-` which is synonymous to\n+`@{-1}`.\n \n <start-point>::\n \tThe new branch head will point to this commit.  It may be\n@@ -334,6 +338,12 @@ $ git branch -D test                                    <2>\n <2> Delete the \"test\" branch even if the \"master\" branch (or whichever branch\n     is currently checked out) does not have all commits from the test branch.\n \n+To delete the previous branch::\n++\n+------------\n+$ git branch -D -\n+------------\n+\n Listing branches from a specific remote::\n +\n ------------\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex d8297f80ff..5537f819a6 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -227,9 +227,13 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,\n \t}\n \tfor (i = 0; i < argc; i++, strbuf_reset(&bname)) {\n \t\tchar *target = NULL;\n+\t\tconst char *arg = argv[i];\n \t\tint flags = 0;\n \n-\t\tstrbuf_branchname(&bname, argv[i], allowed_interpret);\n+\t\tif (!strcmp(arg, \"-\"))\n+\t\t\targ = \"@{-1}\";\n+\n+\t\tstrbuf_branchname(&bname, arg, allowed_interpret);\n \t\tfree(name);\n \t\tname = mkpathdup(fmt, bname.buf);\n \ndiff --git a/t/t3200-branch.sh b/t/t3200-branch.sh\nindex 411a70b0ce..46dfb41e65 100755\n--- a/t/t3200-branch.sh\n+++ b/t/t3200-branch.sh\n@@ -1387,4 +1387,16 @@ test_expect_success 'invalid sort parameter in configuration' '\n \t)\n '\n \n+test_expect_success 'delete previous branch' '\n+\tgit checkout -b a &&\n+\tgit checkout -b b &&\n+\tgit branch -D - &&\n+\tgit branch >actual &&\n+\tcat >expect <<-\\EOF &&\n+\t* b\n+\t  master\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.26.2\n\n"}]}