{"thread":{"id":"58593","subject":"[PATCH] git.c: fix, stop passing options after --help","startedAt":"2022-10-10T16:18:04Z","lastAt":"2022-10-10T18:58:36Z","messageCount":2,"participants":["Daniel Sonbolian via GitGitGadget","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"464494","messageId":"pull.1357.git.git.1665418677535.gitgitgadget@gmail.com","threadId":"58593","inReplyTo":null,"subject":"[PATCH] git.c: fix, stop passing options after --help","fromName":"Daniel Sonbolian via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2022-10-10T16:17:57Z","receivedAt":"2022-10-10T16:18:04Z","isPatch":true,"sender":{"key":"name:Daniel Sonbolian","avatar":null},"body":"From: Daniel Sonbolian <dsal3389@gmail.com>\n\nSince commit c6b6d9f7d8a when passing --help option\nto a Git command, we try to open that command man page, we\ndo it for both commands and concepts, it is done by\nconverting the entered command to a help command\nfor the given Git command, for example:\n\n\t\"git commit --help -i\" into \"git help --exclude-guides commit -i\"\n\nBut the options after --help option are also\npassed to the new command (-i option from the example)\nwhich can lead to unexpected output, because the\nhelp command will try to execute those extra options.\n\nThis fixed by building the argv statically, meaning\ninstead of switching places between argv arguments and then\npassing all the arguments to a new memory vector that will\nlater be used as the new argv, we directly passing that\nmemory vector only the required arguments and in the\ncorrect order, after that we also updating the\nargc to a static number:\n\n\tstrvec_push(&args, \"help\");  // argv[0]\n\tstrvec_push(&args, \"--exclude-guides\");  // argv[1]\n\tstrvec_push(&args, argv[0]);  // argv[2]\n\targv = args.v;\n\targc = 3;  // update based on the amount of pushs we did\n\nNow no matter how many arguments we pass after\nthe --help, the results will always stay the same:\n\n\t\"git commit --help foo-hello-world\"\n\t\"git commit --help pull -i -f\"\n\t\"git commit --help -i\"\n\t|\n\tv\n\t\"git help --exclude-guides commit\"\n\nSigned-off-by: Daniel Sonbolian <dsal3389@gmail.com>\n---\n    git.c: fix, passing options after --help\n    \n    Since commit c6b6d9f7d8a when passing --help option to a Git command, we\n    try to open that command man page, we do it for both commands and\n    concepts, it is done by converting the entered command to a help command\n    for the given Git command, for example:\n    \n    \"git commit --help -i\" into \"git help --exclude-guides commit -i\"\n    \n    \n    But the options after --help option are also passed to the new command\n    (-i option from the example) which can lead to unexpected output,\n    because the help command will try to execute those extra options.\n    \n    This fixed by building the argv statically, meaning instead of switching\n    places between argv arguments and then passing all the arguments to a\n    new memory vector that will later be used as the new argv, we directly\n    passing that memory vector only the required arguments and in the\n    correct order, after that we also updating the argc to a static number:\n    \n    strvec_push(&args, \"help\");  // argv[0]\n    strvec_push(&args, \"--exclude-guides\");  // argv[1]\n    strvec_push(&args, argv[0]);  // argv[2]\n    argv = args.v;\n    argc = 3;  // update based on the amount of pushs we did\n    \n    \n    Now no matter how many arguments we pass after the --help, the results\n    will always stay the same:\n    \n    \"git commit --help foo-hello-world\"\n    \"git commit --help pull -i -f\"\n    \"git commit --help -i\"\n    |\n    v\n    \"git help --exclude-guides commit\"\n    \n    \n    Signed-off-by: Daniel Sonbolian dsal3389@gmail.com\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1357%2Fdsal3389%2Fcmd-help-tweaks-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1357/dsal3389/cmd-help-tweaks-v1\nPull-Request: https://github.com/git/git/pull/1357\n\n git.c | 18 ++++++------------\n 1 file changed, 6 insertions(+), 12 deletions(-)\n\ndiff --git a/git.c b/git.c\nindex da411c53822..75dcefa534d 100644\n--- a/git.c\n+++ b/git.c\n@@ -697,25 +697,19 @@ static void handle_builtin(int argc, const char **argv)\n \tstruct cmd_struct *builtin;\n \n \tstrip_extension(argv);\n-\tcmd = argv[0];\n \n \t/* Turn \"git cmd --help\" into \"git help --exclude-guides cmd\" */\n \tif (argc > 1 && !strcmp(argv[1], \"--help\")) {\n-\t\tint i;\n-\n-\t\targv[1] = argv[0];\n-\t\targv[0] = cmd = \"help\";\n-\n-\t\tfor (i = 0; i < argc; i++) {\n-\t\t\tstrvec_push(&args, argv[i]);\n-\t\t\tif (!i)\n-\t\t\t\tstrvec_push(&args, \"--exclude-guides\");\n-\t\t}\n+\t\tstrvec_push(&args, \"help\");\n+\t\tstrvec_push(&args, \"--exclude-guides\");\n+\t\tstrvec_push(&args, argv[0]);\n \n-\t\targc++;\n \t\targv = args.v;\n+\t\targc = 3;\n \t}\n \n+\tcmd = argv[0];\n+\n \tbuiltin = get_builtin(cmd);\n \tif (builtin)\n \t\texit(run_builtin(builtin, argc, argv));\n\nbase-commit: 3dcec76d9df911ed8321007b1d197c1a206dc164\n-- \ngitgitgadget\n"},{"id":"464523","messageId":"xmqq35bv33qk.fsf@gitster.g","threadId":"58593","inReplyTo":"pull.1357.git.git.1665418677535.gitgitgadget@gmail.com","subject":"Re: [PATCH] git.c: fix, stop passing options after --help","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-10-10T18:58:27Z","receivedAt":"2022-10-10T18:58:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Daniel Sonbolian via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Daniel Sonbolian <dsal3389@gmail.com>\n>\n> Since commit c6b6d9f7d8a when passing --help option\n\nWhen referring to an old work, use\n\n    $(git show -s --pretty=reference $that_commit)\n\nI suspect that 2c6b6d9f (help: make option --help open man pages\nonly for Git commands, 2016-08-26) is what you meant (the author\nCC'ed).\n\n> to a Git command, we try to open that command man page, we\n> do it for both commands and concepts, it is done by\n> converting the entered command to a help command\n> for the given Git command, for example:\n>\n> \t\"git commit --help -i\" into \"git help --exclude-guides commit -i\"\n>\n> But the options after --help option are also\n> passed to the new command (-i option from the example)\n\n\"new command\" meaning the \"git help\" command?\n\n> which can lead to unexpected output, because the\n> help command will try to execute those extra options.\n\nMeaning \"git commit --help -i\" becomes \"git help ... commit -i\", and\nbecause the command line parser of \"git help\" accepts dashed options\nafter \"commit\", it works just like \"git help ... -i commit\" does?\n\nIt is a request to read the help information for \"git commit\" using\nthe \"--info\" backend, right?  Similarly, \"git commit --help -m\"\nwould do the manpage and \"git commit --help -w\" would show the\nmanpage in the browser?\n\nIt sounds like a sensible behaviour to me (even though relying on\nthe behaviour of \"git help\" that takes dashed options after \"commit\"\nmakes me feel somewhat dirty).  So ...\n\n> This fixed by building the argv statically, meaning\n\n... I am not sure what you are fixing.  Puzzled...\n"}]}