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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Elena Petrashen <elena.petrashen@xxxxxxxxx>
---
This micro-patch is meant to allow “-“ as a short-hand for
“@{-1} for branch -D (Cf. $gmane/230828):

* based on the discussion on the previous version of the patch,
added the advice on how to restore the deleted branch using
git branch deleted_name sha1 - to ensure safety and 
newbie-friendliness

* 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 expand_dash_shortcut function can be reused to teach
git branch -m or other modifications 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 |  2 ++
 builtin/branch.c             | 26 ++++++++++++++++++++++----
 t/t3200-branch.sh            | 16 ++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 4a7037f..42b96ed 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -65,6 +65,8 @@ 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.
+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..8b33533 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -178,6 +178,15 @@ static void delete_branch_config(const char *branchname)
 	strbuf_release(&buf);
 }
 
+static int expand_dash_shortcut(const char **argv, int dash_position)
+{
+	if (!strcmp(argv[dash_position], "-")){
+		argv[dash_position] = "@{-1}";
+		return 1;
+	}
+	return 0;
+}
+
 static int delete_branches(int argc, const char **argv, int force, int kinds,
 			   int quiet)
 {
@@ -187,6 +196,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	const char *fmt;
 	int i;
 	int ret = 0;
+	int dash_shortcut = 0;
 	int remote_branch = 0;
 	struct strbuf bname = STRBUF_INIT;
 
@@ -213,7 +223,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	for (i = 0; i < argc; i++, strbuf_release(&bname)) {
 		const char *target;
 		int flags = 0;
-
+		if (expand_dash_shortcut (argv, i))
+			dash_shortcut = 1;
 		strbuf_branchname(&bname, argv[i]);
 		if (kinds == FILTER_REFS_BRANCHES && !strcmp(head, bname.buf)) {
 			error(_("Cannot delete the branch '%s' "
@@ -231,9 +242,12 @@ 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(dash_shortcut
+				? _("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;
 		}
@@ -262,6 +276,10 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 			       (flags & REF_ISBROKEN) ? "broken"
 			       : (flags & REF_ISSYMREF) ? target
 			       : find_unique_abbrev(sha1, DEFAULT_ABBREV));
+			if (dash_shortcut == 1)
+			       printf( _("\nIf that happened by mistake, you may want to restore"
+				" it with:\n\ngit branch %s %s\n"), bname.buf,
+				find_unique_abbrev(sha1, DEFAULT_ABBREV));
 		}
 		delete_branch_config(bname.buf);
 	}
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index a897248..535a507 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -403,6 +403,22 @@ 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 --short prev) &&
+	cat >expected <<EOF &&
+Deleted branch prev (was $sha1).
+
+If that happened by mistake, you may want to restore it with:
+
+git branch prev $sha1
+EOF
+	test_cmp expected 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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]