From: Elena Petrashen <elena.petrashen@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, sunshine@sunshineco.com,
Matthieu.Moy@grenoble-inp.fr, sbeller@google.com,
Elena Petrashen <elena.petrashen@gmail.com>
Subject: [PATCH v2] branch -D: allow - as abbreviation of '@{-1}'
Date: Tue, 22 Mar 2016 11:24:16 +0300 [thread overview]
Message-ID: <1458635056-26633-1-git-send-email-elena.petrashen@gmail.com> (raw)
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):
* 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
next reply other threads:[~2016-03-22 8:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-22 8:24 Elena Petrashen [this message]
2016-03-22 10:00 ` [PATCH v2] branch -D: allow - as abbreviation of '@{-1}' Matthieu Moy
2016-03-22 17:07 ` Junio C Hamano
2016-03-22 17:12 ` Junio C Hamano
2016-03-22 18:57 ` Eric Sunshine
2016-03-24 13:00 ` elena petrashen
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1458635056-26633-1-git-send-email-elena.petrashen@gmail.com \
--to=elena.petrashen@gmail.com \
--cc=Matthieu.Moy@grenoble-inp.fr \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=sbeller@google.com \
--cc=sunshine@sunshineco.com \
/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.
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).