{"thread":{"id":"38841","subject":"[v3 PATCH 1/2] reset: add '-' shorthand for '@{-1}'","startedAt":"2015-03-18T08:29:44Z","lastAt":"2015-03-18T19:17:20Z","messageCount":2,"participants":["Sundararajan R","Kenny Lee Sin Cheong"],"isPatch":true,"patchVersion":3,"patchTotal":2},"messages":[{"id":"257918","messageId":"1426667384-26288-1-git-send-email-dyoucme@gmail.com","threadId":"38841","inReplyTo":null,"subject":"[v3 PATCH 1/2] reset: add '-' shorthand for '@{-1}'","fromName":"Sundararajan R","fromEmail":"dyoucme@gmail.com","sentAt":"2015-03-18T08:29:44Z","receivedAt":"2015-03-18T08:29:44Z","isPatch":true,"sender":{"key":"dyoucme@gmail.com","avatar":null},"body":"Teaching reset the - shorthand involves checking if any file named '-' exists.\ncheck_filename() is used to perform this check.\n\nWhen the @{-1} branch does not exist then it can be safely assumed that the\nuser is referring to the file '-',if any. If this file exists then it is reset or else\na bad flag error is shown.\n\nBut if the @{-1} branch exists then it becomes ambiguous without the explicit \n'--' disambiguation as to whether the user wants to reset the file '-' or if \nhe wants to reset the working tree to the previous branch. Hence the program dies\nwith a message about the ambiguous argument.\n\nHelped-by: Junio C Hamano <gitster@pobox.com>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Sundararajan R <dyoucme@gmail.com>\n---\nThank you Eric and Junio for your patient feedback.\nAs verify_filename() and verify_non_filename() die and return,respectively when \npassed the argument '-' without actually checking if such a file exists, \ncheck_filename() has been used to perform this check. I hope it is okay.\n\n builtin/reset.c | 21 +++++++++++++++++++--\n 1 file changed, 19 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex 4c08ddc..a126b38 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -192,6 +192,8 @@ static void parse_args(struct pathspec *pathspec,\n {\n \tconst char *rev = \"HEAD\";\n \tunsigned char unused[20];\n+\tint file_named_minus = 0;\n+\tint shorthand = 0;\n \t/*\n \t * Possible arguments are:\n \t *\n@@ -205,6 +207,12 @@ static void parse_args(struct pathspec *pathspec,\n \t */\n \n \tif (argv[0]) {\n+\t\tif (!strcmp(argv[0], \"-\") && !argv[1]) {\n+\t\t\targv[0] = \"@{-1}\";\n+\t\t\tshorthand = 1;\n+\t\t\tif(check_filename(prefix, \"-\"))\n+\t\t\t\tfile_named_minus = 1;\n+\t\t}\n \t\tif (!strcmp(argv[0], \"--\")) {\n \t\t\targv++; /* reset to HEAD, possibly with paths */\n \t\t} else if (argv[1] && !strcmp(argv[1], \"--\")) {\n@@ -222,11 +230,20 @@ static void parse_args(struct pathspec *pathspec,\n \t\t\t * Ok, argv[0] looks like a commit/tree; it should not\n \t\t\t * be a filename.\n \t\t\t */\n-\t\t\tverify_non_filename(prefix, argv[0]);\n+\t\t\tif (file_named_minus) {\n+\t\t\t\tdie(_(\"ambiguous argument '-': both revision and filename\\n\"\n+\t\t\t\t\"Use '--' to separate paths from revisions, like this:\\n\"\n+\t\t\t\t\"'git <command> [<revision>...] -- [<file>...]'\"));\n+\t\t\t}\n+\t\t\telse if (!shorthand) \n+\t\t\t\tverify_non_filename(prefix, argv[0]);\n \t\t\trev = *argv++;\n \t\t} else {\n \t\t\t/* Otherwise we treat this as a filename */\n-\t\t\tverify_filename(prefix, argv[0], 1);\n+\t\t\tif (shorthand)\n+\t\t\t\targv[0] = \"-\";\n+\t\t\tif (!file_named_minus)\n+\t\t\t\tverify_filename(prefix, argv[0], 1);\n \t\t}\n \t}\n \t*rev_ret = rev;\n-- \n2.1.0\n"},{"id":"257973","messageId":"87egomm7y7.fsf@gmail.com","threadId":"38841","inReplyTo":"87iodym82z.fsf@gmail.com","subject":"Re: [v3 PATCH 1/2] reset: add '-' shorthand for '@{-1}'","fromName":"Kenny Lee Sin Cheong","fromEmail":"kenny.lee28@gmail.com","sentAt":"2015-03-18T19:17:20Z","receivedAt":"2015-03-18T19:17:20Z","isPatch":true,"sender":{"key":"kenny.lee28@gmail.com","avatar":null},"body":"On Wed, Mar 18 2015 at 04:29:44 AM, Sundararajan R <dyoucme@gmail.com> wrote:\n> Teaching reset the - shorthand involves checking if any file named '-' exists.\n> check_filename() is used to perform this check.\n>\n> When the @{-1} branch does not exist then it can be safely assumed that the\n> user is referring to the file '-',if any. If this file exists then it is reset or else\n> a bad flag error is shown.\n>\n> But if the @{-1} branch exists then it becomes ambiguous without the explicit \n> '--' disambiguation as to whether the user wants to reset the file '-' or if \n> he wants to reset the working tree to the previous branch. Hence the program dies\n> with a message about the ambiguous argument.\n>\nI might be wrong but I think any pathspec that begins with \"-\" needs to\nbe preceded by either a \"--\" marker or be specified as \"./-filename\",\nelse verify_filename just die. Therefore you would need to do something\nlike git reset ./- if you wanted to reset a file. I don't know if given\nsimply \"-\" as filename is desired since options starts with \"-\".\n\nI don't know if you saw but Junio posted a while ago about about\nallowing \"-\" as a stand-in everywhere a revision was allowed. He updated\na version on pu : \"d40f108d\"\n\n> On Tue, Mar 17 2015 at 02:49:48 AM, Junio C Hamano <gitster@pobox.com> wrote:\n>> Junio C Hamano <gitster@pobox.com> writes:\n>>\n>> \tif (try to see if it is a revision or regvision range) {\n>>         \t/* if failed ... */\n>> \t\tif (starts with '-') {\n>>                 \tdo the option thing;\n>>                         continue;\n>> \t\t}\n>> \t\t/* args must be pathspecs from here on */\n>>                 check the  '--' disambiguation;\n>>                 add pathspec to prune-data;\n>> \t} else {\n>> \t\tgot_rev_arg = 1;\n>> \t}\n>>\n\nSee $gmane/265672\n"}]}