Skip to contentSkip to conversation

reset: add '-' shorthand for '@{-1}'

PATCH Superseded Sundararajan R 2 participants 2 messages Jun 15, 2016 1 message not in the archive

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 reset: add '-' shorthand for '@{-1}'

Teaching reset the - shorthand involves checking if any file named '-' exists. check_filename() is used to perform this check.

When the @{-1} branch does not exist then it can be safely assumed that the user is referring to the file '-',if any. If this file exists then it is reset or else a bad flag error is shown.

But if the @{-1} branch exists then it becomes ambiguous without the explicit '--' disambiguation as to whether the user wants to reset the file '-' or if he wants to reset the working tree to the previous branch. Hence the program dies with a message about the ambiguous argument.

---
Thank you Eric and Junio for your patient feedback.
As verify_filename() and verify_non_filename() die and return,respectively when passed the argument '-' without actually checking if such a file exists, check_filename() has been used to perform this check. I hope it is okay.

 builtin/reset.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
builtin/reset.c +19 -2
index 4c08ddc..a126b38 100644
@@ -192,6 +192,8 @@ static void parse_args(struct pathspec *pathspec,
 {
 	const char *rev = "HEAD";
 	unsigned char unused[20];
+	int file_named_minus = 0;
+	int shorthand = 0;
 	/*
 	 * Possible arguments are:
 	 *
@@ -205,6 +207,12 @@ static void parse_args(struct pathspec *pathspec,
 	 */
 
 	if (argv[0]) {
+		if (!strcmp(argv[0], "-") && !argv[1]) {
+			argv[0] = "@{-1}";
+			shorthand = 1;
+			if(check_filename(prefix, "-"))
+				file_named_minus = 1;
+		}
 		if (!strcmp(argv[0], "--")) {
 			argv++; /* reset to HEAD, possibly with paths */
 		} else if (argv[1] && !strcmp(argv[1], "--")) {
@@ -222,11 +230,20 @@ static void parse_args(struct pathspec *pathspec,
 			 * Ok, argv[0] looks like a commit/tree; it should not
 			 * be a filename.
 			 */
-			verify_non_filename(prefix, argv[0]);
+			if (file_named_minus) {
+				die(_("ambiguous argument '-': both revision and filename\n"
+				"Use '--' to separate paths from revisions, like this:\n"
+				"'git <command> [<revision>...] -- [<file>...]'"));
+			}
+			else if (!shorthand) 
+				verify_non_filename(prefix, argv[0]);
 			rev = *argv++;
 		} else {
 			/* Otherwise we treat this as a filename */
-			verify_filename(prefix, argv[0], 1);
+			if (shorthand)
+				argv[0] = "-";
+			if (!file_named_minus)
+				verify_filename(prefix, argv[0], 1);
 		}
 	}
 	*rev_ret = rev;
Signature · 1 line
2.1.0
#2 lore raw
In reply to a message not in the archive
Quoted 12 lines · Sundararajan R

Teaching reset the - shorthand involves checking if any file named '-' exists. check_filename() is used to perform this check.

When the @{-1} branch does not exist then it can be safely assumed that the user is referring to the file '-',if any. If this file exists then it is reset or else a bad flag error is shown.

But if the @{-1} branch exists then it becomes ambiguous without the explicit '--' disambiguation as to whether the user wants to reset the file '-' or if he wants to reset the working tree to the previous branch. Hence the program dies with a message about the ambiguous argument.

I might be wrong but I think any pathspec that begins with "-" needs to be preceded by either a "--" marker or be specified as "./-filename", else verify_filename just die. Therefore you would need to do something like git reset ./- if you wanted to reset a file. I don't know if given simply "-" as filename is desired since options starts with "-".

I don't know if you saw but Junio posted a while ago about about allowing "-" as a stand-in everywhere a revision was allowed. He updated a version on pu : "d40f108d"

Quoted 16 lines · Junio C Hamano
Quoted 15 lines

Junio C Hamano <[email protected]> writes:

	if (try to see if it is a revision or regvision range) {
        	/* if failed ... */
		if (starts with '-') {
                	do the option thing;
                        continue;
		}
		/* args must be pathspecs from here on */
                check the  '--' disambiguation;
                add pathspec to prune-data;
	} else {
		got_rev_arg = 1;
	}

See $gmane/265672