{"thread":{"id":"45074","subject":"[PATCH/RFC v2] WIP: log: allow \"-\" as a short-hand for \"previous branch\"","startedAt":"2017-02-07T20:51:46Z","lastAt":"2017-02-07T20:51:46Z","messageCount":1,"participants":["Siddharth Kannan"],"isPatch":true,"patchVersion":2,"patchTotal":null},"messages":[{"id":"311006","messageId":"1486500686-6623-1-git-send-email-kannan.siddharth12@gmail.com","threadId":"45074","inReplyTo":null,"subject":"[PATCH/RFC v2] WIP: log: allow \"-\" as a short-hand for \"previous branch\"","fromName":"Siddharth Kannan","fromEmail":"kannan.siddharth12@gmail.com","sentAt":"2017-02-07T20:51:26Z","receivedAt":"2017-02-07T20:51:46Z","isPatch":true,"sender":{"key":"kannan.siddharth12@gmail.com","avatar":"https://gravatar.com/avatar/f555b34abc9cec49fd7e7ca4a55b58be0e0af0fc58e75b7887210199b7d4ca5a?d=mp&s=160"},"body":"Teach revision.c:setup_revisions that an argument starting with \"-\" can be an\nargument also. `left` variable needs to be incremented only when the supplied\narg is neither an argument, nor an option.\n\nTeach sha1_name.c:get_sha1_1 that \"-\" is equivalent to \"@{-1}\"\n\nSigned-off-by: Siddharth Kannan <kannan.siddharth12@gmail.com>\n---\nI have run the test suite locally and on Travis CI. [1]\n\nWhenever the argument begins with a \"-\" then the function \"handle_revision_arg\" \nis called twice. I can fix this using a variable that would store whether the\nfunction has been called earlier or not. For doing that I have to investigate\nsome more on what the valid return values for \"handle_revision_arg\" are. (Or\na simpler approach would be to use an integer flag, this would also not be\naffected if in case \"handle_revision_arg\" is changed in the future)\n\nI have also written a very basic test for git-log only. I have based this on the\ntests that were added in 696acf4 (checkout: implement \"-\" abbreviation, add docs\nand tests, 2009-01-17). It tests revisions, revision ranges, and open-ended\nrevision ranges (where the start or the finish argument is inferred) If the\ncode in this patch is okay, or close to okay, then please tell me if further\ntests need to be added for git-log and/or other commands.\n\nThis change touches a few commands, other than log. notably, git-format-patch,\ngit-whatchanged and git-show, all of which are defined inside builtin/log.c. In\ngeneral, it affects commands that call setup_revisions at some point in their\ncodepath. (eg: git-diff-index)\n\nThanks a lot, Junio, for your comments on the previous version of this patch and\nfurther discussion on that thread!\n\n[1]: https://travis-ci.org/icyflame/git/builds/199350136\n\n revision.c               |  9 +++++--\n sha1_name.c              |  5 ++++\n t/t4214-log-shorthand.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 74 insertions(+), 2 deletions(-)\n create mode 100755 t/t4214-log-shorthand.sh\n\ndiff --git a/revision.c b/revision.c\nindex b37dbec..e14f62c 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2206,7 +2206,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tfor (left = i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n \t\tif (*arg == '-') {\n-\t\t\tint opts;\n+\t\t\tint opts, args;\n \n \t\t\topts = handle_revision_pseudo_opt(submodule,\n \t\t\t\t\t\trevs, argc - i, argv + i,\n@@ -2234,7 +2234,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \t\t\t}\n \t\t\tif (opts < 0)\n \t\t\t\texit(128);\n-\t\t\tcontinue;\n+\n+\t\t\targs = handle_revision_arg(arg, revs, flags, revarg_opt);\n+\t\t\tif (args)\n+\t\t\t\tcontinue;\n+\t\t\telse\n+\t\t\t\t--left;\n \t\t}\n \n \ndiff --git a/sha1_name.c b/sha1_name.c\nindex 73a915f..d774e46 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -947,6 +947,11 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned l\n \tif (!ret)\n \t\treturn 0;\n \n+\tif (!strcmp(name, \"-\")) {\n+\t\tname = \"@{-1}\";\n+\t\tlen = 5;\n+\t}\n+\n \tret = get_sha1_basic(name, len, sha1, lookup_flags);\n \tif (!ret)\n \t\treturn 0;\ndiff --git a/t/t4214-log-shorthand.sh b/t/t4214-log-shorthand.sh\nnew file mode 100755\nindex 0000000..95cf2d4\n--- /dev/null\n+++ b/t/t4214-log-shorthand.sh\n@@ -0,0 +1,62 @@\n+#!/bin/sh\n+\n+test_description='log can show previous branch using shorthand - for @{-1}'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup' '\n+\techo hello >world &&\n+\tgit add world &&\n+\tgit commit -m initial &&\n+\techo \"hello second time\" >>world &&\n+\tgit add world &&\n+\tgit commit -m second &&\n+\techo \"hello other file\" >>planet &&\n+\tgit add planet &&\n+\tgit commit -m third &&\n+\techo \"hello yet another file\" >>city &&\n+\tgit add city &&\n+\tgit commit -m fourth\n+'\n+\n+test_expect_success '\"log -\" should not work initially' '\n+\ttest_must_fail git log -\n+'\n+\n+test_expect_success '\"log -\" should work' '\n+\tgit checkout -b testing-1 master^ &&\n+\tgit checkout -b testing-2 master~2 &&\n+\tgit checkout master &&\n+\n+\tgit log testing-2 >expect &&\n+\tgit log - >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'revision range should work when one end is left empty' '\n+\tgit checkout testing-2 &&\n+\tgit checkout master &&\n+\tgit log ...@{-1} > expect.first_empty &&\n+\tgit log @{-1}... > expect.last_empty &&\n+\tgit log ...- > actual.first_empty &&\n+\tgit log -... > actual.last_empty &&\n+\ttest_cmp expect.first_empty actual.first_empty &&\n+\ttest_cmp expect.last_empty actual.last_empty\n+'\n+\n+test_expect_success 'symmetric revision range should work when both ends are given' '\n+\tgit checkout testing-2 &&\n+\tgit checkout master &&\n+\tgit log -...testing-1 >expect &&\n+\tgit log testing-2...testing-1 >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'asymmetric revision range should work when both ends are given' '\n+\tgit checkout testing-2 &&\n+\tgit checkout master &&\n+\tgit log -..testing-1 >expect &&\n+\tgit log testing-2..testing-1 >actual &&\n+\ttest_cmp expect actual\n+'\n+test_done\n-- \n2.1.4\n\n"}]}