{"thread":{"id":"1307","subject":"[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF","startedAt":"2005-07-23T02:08:32Z","lastAt":"2005-07-23T02:08:32Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"6359","messageId":"7v7jfi8ean.fsf@assigned-by-dhcp.cox.net","threadId":"1307","inReplyTo":null,"subject":"[PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-07-23T02:08:32Z","receivedAt":"2005-07-23T02:08:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"The git-rev-parse command uses LF to separate each argument it\nparses, so its users at least need to set IFS to LF to be able\nto handle filenames with embedded SPs and TABs.  Some commands,\nhowever, can take and do expect arguments with embedded LF,\nnotably, \"-S\" (pickaxe) of diff family, so even this workaround\ndoes not work for them.\n\nWhen --sq flag to git-rev-parse is given, instead of showing one\nargument per line, it outputs arguments quoted for consumption\nwith \"eval\" by the caller, to remedy this situation.\n\nAs an example, this patch converts git-whatchanged to use this\nnew feature.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n git-whatchanged |    9 ++++++---\n rev-parse.c     |   34 +++++++++++++++++++++++++++++++---\n 2 files changed, 37 insertions(+), 6 deletions(-)\n\n707db3532150cc9b1a6a61c66e7533dd7383093d\ndiff --git a/git-whatchanged b/git-whatchanged\n--- a/git-whatchanged\n+++ b/git-whatchanged\n@@ -1,4 +1,7 @@\n #!/bin/sh\n-git-rev-list $(git-rev-parse --default HEAD --revs-only \"$@\") |\n-\tgit-diff-tree --stdin --pretty -r $(git-rev-parse --no-revs \"$@\") |\n-\tLESS=\"$LESS -S\" ${PAGER:-less}\n+rev_list_args=$(git-rev-parse --sq --default HEAD --revs-only \"$@\") &&\n+diff_tree_args=$(git-rev-parse --sq --no-revs \"$@\") &&\n+\n+eval \"git-rev-list $rev_list_args\" |\n+eval \"git-diff-tree --stdin --pretty -r $diff_tree_args\" |\n+LESS=\"$LESS -S\" ${PAGER:-less}\ndiff --git a/rev-parse.c b/rev-parse.c\n--- a/rev-parse.c\n+++ b/rev-parse.c\n@@ -15,6 +15,7 @@ static int do_rev_argument = 1;\n static int output_revs = 0;\n static int flags_only = 0;\n static int no_flags = 0;\n+static int output_sq = 0;\n \n #define NORMAL 0\n #define REVERSED 1\n@@ -49,19 +50,42 @@ static int is_rev_argument(const char *a\n \t}\n }\n \n+static void show(const char *arg)\n+{\n+\tif (output_sq) {\n+\t\tint sq = '\\'', ch;\n+\n+\t\tputchar(sq);\n+\t\twhile ((ch = *arg++)) {\n+\t\t\tif (ch == sq)\n+\t\t\t\tfputs(\"'\\\\'\", stdout);\n+\t\t\tputchar(ch);\n+\t\t}\n+\t\tputchar(sq);\n+\t\tputchar(' ');\n+\t}\n+\telse\n+\t\tputs(arg);\n+}\n+\n static void show_rev(int type, const unsigned char *sha1)\n {\n \tif (no_revs)\n \t\treturn;\n \toutput_revs++;\n-\tprintf(\"%s%s\\n\", type == show_type ? \"\" : \"^\", sha1_to_hex(sha1));\n+\n+\t/* Hexadecimal string plus possibly a carret;\n+\t * this does not have to be quoted even under output_sq.\n+\t */\n+\tprintf(\"%s%s%c\", type == show_type ? \"\" : \"^\", sha1_to_hex(sha1),\n+\t       output_sq ? ' ' : '\\n');\n }\n \n static void show_rev_arg(char *rev)\n {\n \tif (no_revs)\n \t\treturn;\n-\tputs(rev);\n+\tshow(rev);\n }\n \n static void show_norev(char *norev)\n@@ -70,7 +94,7 @@ static void show_norev(char *norev)\n \t\treturn;\n \tif (revs_only)\n \t\treturn;\n-\tputs(norev);\n+\tshow(norev);\n }\n \n static void show_arg(char *arg)\n@@ -328,6 +352,10 @@ int main(int argc, char **argv)\n \t\t\t\tsingle_rev = 1;\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strcmp(arg, \"--sq\")) {\n+\t\t\t\toutput_sq = 1;\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (!strcmp(arg, \"--not\")) {\n \t\t\t\tshow_type ^= REVERSED;\n \t\t\t\tcontinue;\n"}]}