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

[PATCH v5 1/2] reset: enable '-' short-hand for previous branch

From
SSSudhanshu Shekhar <sudshekhar02@gmail.com>
Date
Mar 13, 2015, 18:18 UTC
Message-ID
<1426270716-22405-1-git-send-email-sudshekhar02@gmail.com>

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
Next: Sudhanshu Shekhar
Message 1 of 6 in “reset: enable '-' short-hand for previous branch”
  1. 1/2 reset: enable '-' short-hand for previous branchSudhanshu Shekhar, Mar 13, 2015
  2. 2/2 t7102: add 'reset -' testsSudhanshu Shekhar, Mar 13, 2015
  3. Eric SunshineMar 13, 2015
  4. Sudhanshu ShekharMar 16, 2015
  5. Eric SunshineMar 13, 2015
  6. Sudhanshu ShekharMar 16, 2015

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.