{"thread":{"id":"47858","subject":"[RFC PATCH] cherry: teach git cherry to respect pathspec","startedAt":"2018-02-16T01:15:15Z","lastAt":"2018-03-11T00:24:31Z","messageCount":2,"participants":["Rasmus Villemoes"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"339525","messageId":"20180216011501.889-1-rv@rasmusvillemoes.dk","threadId":"47858","inReplyTo":null,"subject":"[RFC PATCH] cherry: teach git cherry to respect pathspec","fromName":"Rasmus Villemoes","fromEmail":"rv@rasmusvillemoes.dk","sentAt":"2018-02-16T01:15:01Z","receivedAt":"2018-02-16T01:15:15Z","isPatch":true,"sender":{"key":"rv@rasmusvillemoes.dk","avatar":"https://avatars.githubusercontent.com/u/4375908?v=4"},"body":"This is a very naive attempt at teaching git cherry to restrict\nattention to a given pathspec. Very much RFC, hence no documentation\nupdate or test cases for now.\n\nThe first problem is the existing signature, with between 0 and 3\narguments. In order to preserve having defaults for limit, head,\nupstream, I decided to make use of -- mandatory. The alternative seems\nto be to only take a pathspec as arguments 4+, forcing the users to\nwrite out the default values for head and/or limit (and how does one\nactually write the default \"no limit\"?).\n\nAt first I just tried parse_pathspec directly to &revs.prune_data, but\nthat didn't seem to have any effect, and from staring at revision.c for\na while, it seems that one has to go through setup_revisions(). But that\nfits well with insisting on the -- above, since we then have an argv[]\nthat ensures we never hit the handle_revision_arg() or\nhandle_revision_pseudo_opt() calls.\n\nThe motivation for this is that I'm in the process of switching a BSP\nfrom a vendor kernel to one much closer to mainline's master branch. I\ndo need to apply/port some out-of-tree patches from the vendor kernel,\nbut it's much more managable if one can do a per-subsystem survey of\nwhat might need porting. So I naively started by doing\n\n  git cherry -v linus/master LSDK-17.12-V4.9 v4.9.62 -- drivers/mtd/spi-nor/\n\nassuming that would Just Work. I was somewhat surprised that this didn't\ngive any output, but digging into the source revealed that (1) there\nisn't actually any pathspec handling and (2) any argc other than 1..3 is\ntreated as 0 - something which is probably also worth fixing.\n\nWith this patch, the command above does give something reasonable, and\nt3500-cherry.sh also seems to pass.\n\nSigned-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>\n---\n builtin/log.c | 33 +++++++++++++++++++++++++++------\n 1 file changed, 27 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/log.c b/builtin/log.c\nindex 46b4ca13e..2d4534b5c 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -1890,6 +1890,8 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)\n \tconst char *head = \"HEAD\";\n \tconst char *limit = NULL;\n \tint verbose = 0, abbrev = 0;\n+\tint pathspec_argc = 0;\n+\tint i;\n \n \tstruct option options[] = {\n \t\tOPT__ABBREV(&abbrev),\n@@ -1897,17 +1899,27 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\targc = parse_options(argc, argv, prefix, options, cherry_usage, 0);\n+\targc = parse_options(argc, argv, prefix, options, cherry_usage,\n+\t\t\t     PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN |\n+\t\t\t     PARSE_OPT_KEEP_DASHDASH);\n+\n+\tfor (i = 1; i < argc; ++i) {\n+\t\tif (!strcmp(argv[i], \"--\")) {\n+\t\t\tpathspec_argc = 1 + argc - i;\n+\t\t\targc = i;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n \n \tswitch (argc) {\n+\tcase 4:\n+\t\tlimit = argv[3];\n+\t\t/* FALLTHROUGH */\n \tcase 3:\n-\t\tlimit = argv[2];\n+\t\thead = argv[2];\n \t\t/* FALLTHROUGH */\n \tcase 2:\n-\t\thead = argv[1];\n-\t\t/* FALLTHROUGH */\n-\tcase 1:\n-\t\tupstream = argv[0];\n+\t\tupstream = argv[1];\n \t\tbreak;\n \tdefault:\n \t\tcurrent_branch = branch_get(NULL);\n@@ -1921,6 +1933,15 @@ int cmd_cherry(int argc, const char **argv, const char *prefix)\n \t}\n \n \tinit_revisions(&revs, prefix);\n+\tif (pathspec_argc) {\n+\t\t/*\n+\t\t * hack: this reuses the element of argv before \"--\"\n+\t\t * as argv[0] - setup_revisions assumes argv[0] is\n+\t\t * present and ignores it, and starts its search for\n+\t\t * \"--\" at argv[1].\n+\t\t */\n+\t\tsetup_revisions(pathspec_argc, &argv[argc-1], &revs, NULL);\n+\t}\n \trevs.max_parents = 1;\n \n \tif (add_pending_commit(head, &revs, 0))\n-- \n2.15.1\n\n"},{"id":"341407","messageId":"CAKwiHFgA2coikLM1OuJ8wpPGrw7QHRhK+a5Bx3XxA+ruEJCThw@mail.gmail.com","threadId":"47858","inReplyTo":"20180216011501.889-1-rv@rasmusvillemoes.dk","subject":"Re: [RFC PATCH] cherry: teach git cherry to respect pathspec","fromName":"Rasmus Villemoes","fromEmail":"rv@rasmusvillemoes.dk","sentAt":"2018-03-11T00:24:24Z","receivedAt":"2018-03-11T00:24:31Z","isPatch":true,"sender":{"key":"rv@rasmusvillemoes.dk","avatar":"https://avatars.githubusercontent.com/u/4375908?v=4"},"body":"On 16 February 2018 at 02:15, Rasmus Villemoes <rv@rasmusvillemoes.dk> wrote:\n> This is a very naive attempt at teaching git cherry to restrict\n> attention to a given pathspec. Very much RFC, hence no documentation\n> update or test cases for now.\n>\n\nPing, any comments on this?\n"}]}