git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH][Outreachy] branch -D: allow - as abbreviation of @{-1}

From
Elena Petrashen <elena.petrashen@gmail.com>
Date
Mar 21, 2016, 15:15 UTC
Message-ID
<1458573317-15532-1-git-send-email-elena.petrashen@gmail.com>
Signed-off-by: Elena Petrashen <elena.petrashen@gmail.com>

--- This micro-patch is meant to allow “-“ as a short-hand for “@{-1} for branch -D (Cf. $gmane/230828):

* git branch (-d | -D) is not supposed to accept any other
arguments except for branch name so it makes sense to replace
the argv[i] with @{-1}. We will not lose the opportunity to
use it for something different for other git branch uses if
we will decide it’s required.
* the small allow_dash_as_prev_branch_alias function can be
reused to teach git branch -m to allow “-“ as a short-hand for
“@{-1}  as well and possibly makes it easy to understand what’s
going on in the code
* if there’s no previous branch in the repository yet, a
specific warning message is given
Thank you! Looking forward to any feedback.
 Documentation/git-branch.txt |  5 ++++-
 builtin/branch.c             | 18 +++++++++++++++---
 t/t3200-branch.sh            | 10 ++++++++++
 3 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 4a7037f..9f43665 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -64,7 +64,10 @@ to happen.
 
 With a `-d` or `-D` option, `<branchname>` will be deleted.  You may
 specify more than one branch for deletion.  If the branch currently
-has a reflog then the reflog will also be deleted.
+has a reflog then the reflog will also be deleted. 
+As a special case, the "@{-N}" syntax for the N-th last branch
+deletes the specified branch.  You may also specify - which is synonymous
+with "@{-1}".
 
 Use `-r` together with `-d` to delete remote-tracking branches. Note, that it
 only makes sense to delete remote-tracking branches if they no longer exist
diff --git a/builtin/branch.c b/builtin/branch.c
index 7b45b6b..9614d18 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -169,6 +169,8 @@ static int check_branch_commit(const char *branchname, const char *refname,
 	return 0;
 }
 
+
+
 static void delete_branch_config(const char *branchname)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -178,6 +180,12 @@ static void delete_branch_config(const char *branchname)
 	strbuf_release(&buf);
 }
 
+static void allow_dash_as_prev_branch_alias(const char **argv, int dash_position)
+{
+	if (!strcmp(argv[dash_position], "-"))
+		argv[dash_position] = "@{-1}";
+}
+
 static int delete_branches(int argc, const char **argv, int force, int kinds,
 			   int quiet)
 {
@@ -210,10 +218,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		if (!head_rev)
 			die(_("Couldn't look up commit object for HEAD"));
 	}
+
 	for (i = 0; i < argc; i++, strbuf_release(&bname)) {
 		const char *target;
 		int flags = 0;
 
+		allow_dash_as_prev_branch_alias(argv, i);
 		strbuf_branchname(&bname, argv[i]);
 		if (kinds == FILTER_REFS_BRANCHES && !strcmp(head, bname.buf)) {
 			error(_("Cannot delete the branch '%s' "
@@ -231,9 +241,11 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 					    | RESOLVE_REF_ALLOW_BAD_NAME,
 					    sha1, &flags);
 		if (!target) {
-			error(remote_branch
-			      ? _("remote-tracking branch '%s' not found.")
-			      : _("branch '%s' not found."), bname.buf);
+			error(!strcmp(bname.buf, "@{-1}")
+				? _("There is no previous branch that could be referred to at the moment.")
+				: remote_branch
+					? _("remote-tracking branch '%s' not found.")
+					: _("branch '%s' not found."), bname.buf);
 			ret = 1;
 			continue;
 		}
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index a897248..d21369f 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -403,6 +403,16 @@ test_expect_success 'test deleting branch without config' '
 	test_i18ncmp expect actual
 '
 
+test_expect_success 'test deleting "-" deletes previous branch' '
+	git checkout -b prev &&
+	test_commit prev &&
+	git checkout master &&
+	git branch -D - >actual &&
+	sha1=$(git rev-parse prev | cut -c 1-7) &&
+	echo "Deleted branch prev (was $sha1)." >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'test --track without .fetch entries' '
 	git branch --track my8 &&
 	test "$(git config branch.my8.remote)" &&
-- 
2.8.0.rc3.12.g047057b.dirty
Next: Matthieu Moy
Message 1 of 8 in “[Outreachy] branch -D: allow - as abbreviation of @{-1}”
  1. [Outreachy] branch -D: allow - as abbreviation of @{-1}Elena Petrashen, Mar 21, 2016
  2. Matthieu MoyMar 21, 2016
  3. Junio C HamanoMar 21, 2016
  4. Eric SunshineMar 21, 2016
  5. Junio C HamanoMar 21, 2016
  6. Eric SunshineMar 21, 2016
  7. Eric SunshineMar 21, 2016
  8. Stefan BellerMar 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.