{"thread":{"id":"38924","subject":"[PATCH v4 2/2] reset: add tests for git reset -","startedAt":"2015-03-27T09:04:54Z","lastAt":"2015-03-27T09:04:55Z","messageCount":2,"participants":["Sundararajan R"],"isPatch":true,"patchVersion":4,"patchTotal":2},"messages":[{"id":"258585","messageId":"1427447095-22851-1-git-send-email-dyoucme@gmail.com","threadId":"38924","inReplyTo":null,"subject":"[PATCH v4 1/2] reset: add '-' shorthand for '@{-1}'","fromName":"Sundararajan R","fromEmail":"dyoucme@gmail.com","sentAt":"2015-03-27T09:04:54Z","receivedAt":"2015-03-27T09:04:54Z","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.\nOtherwise, a 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\nWhen none of the above cases hold, - behaves like @{-1}.\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---\nCorrected a minor style error.\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..80dd5d5 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":"258584","messageId":"1427447095-22851-2-git-send-email-dyoucme@gmail.com","threadId":"38924","inReplyTo":"1427447095-22851-1-git-send-email-dyoucme@gmail.com","subject":"[PATCH v4 2/2] reset: add tests for git reset -","fromName":"Sundararajan R","fromEmail":"dyoucme@gmail.com","sentAt":"2015-03-27T09:04:55Z","receivedAt":"2015-03-27T09:04:55Z","isPatch":true,"sender":{"key":"dyoucme@gmail.com","avatar":null},"body":"The failure case which occurs on teaching git the '-' shorthand\nis when there exists no branch pointed to by '@{-1}'. In this case, if there \nis a file named - in the working tree, the user can be unambiguously \nassumed to be referring to it while issuing this command.\n\nThe ambiguous case occurs when the @{-1} branch exists and file named '-' also\nexists in the working tree. This are also treated as a failure case but here \nthe user is given advice as to how he can proceed.\n\nAnother potentially tricky case is when the file '@{-1}' exists. In this case,\nthe command should succeed as the user hasn't mentioned the file '@{-1}' and can\nbe safely assumed to be referring to the @{-1} branch.\n\nAdd tests to check the handling of these cases.\nAlso add a test to verify that reset - behaves like reset @{-1} when none\nof the above cases are true.\n\nHelped-by: Junio C Hamano <gitster@pobox.com>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nHelped-by: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>\nSigned-off-by: Sundararajan R <dyoucme@gmail.com>\n---\nHave made the edits suggested by Matthew and Kevin.\n\n t/t7102-reset.sh | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 74 insertions(+)\n\ndiff --git a/t/t7102-reset.sh b/t/t7102-reset.sh\nindex 98bcfe2..a605c32 100755\n--- a/t/t7102-reset.sh\n+++ b/t/t7102-reset.sh\n@@ -568,4 +568,78 @@ test_expect_success 'reset --mixed sets up work tree' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'reset - with no @{-1} branch should fail' '\n+\ttest_when_finished rm -rf new &&\n+\tgit init new &&\n+\t(\n+\t\tcd new &&\n+\t\ttest_must_fail git reset - 2>../actual\n+\t) && \n+\ttest_i18ngrep \"bad flag\" actual \n+'\n+\n+test_expect_success 'reset - with no @{-1} branch and file named - should succeed' '\n+\ttest_when_finished rm -rf new &&\n+\tgit init new &&\n+\t(\n+\t\tcd new &&\n+\t\techo \"Hello\" >- &&\n+\t\tgit add - &&\n+\t\tgit reset - >../actual \n+\t) &&\n+\ttest_must_be_empty actual\n+'\n+\n+test_expect_success 'reset - with @{-1} branch and file named - should fail' '\n+\ttest_when_finished rm -rf new &&\n+\tgit init new &&\n+\t(\n+\t\tcd new && \n+\t\techo \"Hello\" >- &&\n+\t\tgit add - &&\n+\t\tgit commit -m \"first_commit\" &&\n+\t\tgit checkout -b new_branch &&\n+\t\t>- &&\n+\t\tgit add - &&\n+\t\ttest_must_fail git reset - 2>../actual \n+\t) &&\n+\ttest_i18ngrep \"ambiguous argument\" actual \n+'\n+\n+test_expect_success 'reset - with @{-1} branch and file named @{-1} should succeed' '\n+\ttest_when_finished rm -rf new &&\n+\tgit init new &&\n+\t(\n+\t\tcd new && \n+\t\techo \"Hello\" >@{-1} &&\n+\t\tgit add @{-1} &&\n+\t\tgit commit -m \"first_commit\" &&\n+\t\tgit checkout -b new_branch &&\n+\t\t>@{-1} &&\n+\t\tgit add @{-1} &&\n+\t\tgit reset - >../actual \n+\t) &&\n+\ttest_i18ngrep \"Unstaged\" actual \n+'\n+\n+test_expect_success 'reset - with @{-1} branch and no file named - should succeed' '\n+\ttest_when_finished rm -rf new &&\n+\tgit init new &&\n+\t(\n+\t\tcd new &&\n+\t\techo \"Hey\" >new_file &&\n+\t\tgit add new_file &&\n+\t\tgit commit -m \"first_commit\" &&\n+\t\tgit checkout -b new_branch &&\n+\t\t>new_file &&\n+\t\tgit add new_file &&\n+\t\tgit reset - &&\n+\t\tgit status -uno --porcelain >actual &&\n+\t\tgit add new_file &&\n+\t\tgit reset @{-1} &&\n+\t\tgit status -uno --porcelain >expected &&\n+\t\ttest_cmp actual expected \n+\t)\n+'\n+\n test_done\n-- \n2.1.0\n"}]}