git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sudhanshu Shekhar <sudshekhar02@gmail.com>
To: git@vger.kernel.org
Cc: Matthieu.Moy@grenoble-inp.fr, gitster@pobox.com,
	davvid@gmail.com, sunshine@sunshineco.com,
	Sudhanshu Shekhar <sudshekhar02@gmail.com>
Subject: [PATCH v5 1/2] reset: enable '-' short-hand for previous branch
Date: Fri, 13 Mar 2015 23:48:35 +0530	[thread overview]
Message-ID: <1426270716-22405-1-git-send-email-sudshekhar02@gmail.com> (raw)

git reset '-' will reset to the previous branch. To reset a file named
"-" use either "git reset ./-" or "git reset -- -".

Change error message to treat single "-" as an ambigous revision or
path rather than a bad flag.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Helped-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
Signed-off-by: Sudhanshu Shekhar <sudshekhar02@gmail.com>
---
I have changed the logic to ensure argv[0] isn't changed at any point.
Creating a modified_argv0 variable let's me do that.

I couldn't think of any other way to achieve this, apart from changing things
directly in the sha1_name.c file (like Junio's changes). Please let me know
if some further changes are needed.

 builtin/reset.c | 17 +++++++++++++----
 setup.c         |  2 +-
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/builtin/reset.c b/builtin/reset.c
index 4c08ddc..bc50e14 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -192,6 +192,7 @@ static void parse_args(struct pathspec *pathspec,
 {
 	const char *rev = "HEAD";
 	unsigned char unused[20];
+	const char *modified_argv0 = argv[0];
 	/*
 	 * Possible arguments are:
 	 *
@@ -205,10 +206,17 @@ static void parse_args(struct pathspec *pathspec,
 	 */
 
 	if (argv[0]) {
+		if (!strcmp(argv[0], "-")) {
+			modified_argv0 = "@{-1}";
+		}
+		else {
+			modified_argv0 = argv[0];
+		}
+
 		if (!strcmp(argv[0], "--")) {
 			argv++; /* reset to HEAD, possibly with paths */
 		} else if (argv[1] && !strcmp(argv[1], "--")) {
-			rev = argv[0];
+			rev = modified_argv0;
 			argv += 2;
 		}
 		/*
@@ -216,14 +224,15 @@ static void parse_args(struct pathspec *pathspec,
 		 * has to be unambiguous. If there is a single argument, it
 		 * can not be a tree
 		 */
-		else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) ||
-			 (argv[1] && !get_sha1_treeish(argv[0], unused))) {
+		else if ((!argv[1] && !get_sha1_committish(modified_argv0, unused)) ||
+			 (argv[1] && !get_sha1_treeish(modified_argv0, unused))) {
 			/*
 			 * Ok, argv[0] looks like a commit/tree; it should not
 			 * be a filename.
 			 */
 			verify_non_filename(prefix, argv[0]);
-			rev = *argv++;
+			rev = modified_argv0;
+			argv++;
 		} else {
 			/* Otherwise we treat this as a filename */
 			verify_filename(prefix, argv[0], 1);
diff --git a/setup.c b/setup.c
index 979b13f..b621b62 100644
--- a/setup.c
+++ b/setup.c
@@ -200,7 +200,7 @@ void verify_filename(const char *prefix,
 		     int diagnose_misspelt_rev)
 {
 	if (*arg == '-')
-		die("bad flag '%s' used after filename", arg);
+		die("ambiguous argument '%s': unknown revision or path", arg);
 	if (check_filename(prefix, arg))
 		return;
 	die_verify_filename(prefix, arg, diagnose_misspelt_rev);
-- 
2.3.1.277.gd67f9d5.dirty

             reply	other threads:[~2015-03-13 18:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-13 18:18 Sudhanshu Shekhar [this message]
2015-03-13 18:18 ` [PATCH v5 2/2] t7102: add 'reset -' tests Sudhanshu Shekhar
2015-03-13 21:10   ` Eric Sunshine
2015-03-16  7:14     ` Sudhanshu Shekhar
2015-03-13 20:48 ` [PATCH v5 1/2] reset: enable '-' short-hand for previous branch Eric Sunshine
2015-03-16 11:40   ` Sudhanshu Shekhar

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=1426270716-22405-1-git-send-email-sudshekhar02@gmail.com \
    --to=sudshekhar02@gmail.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=davvid@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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).