{"thread":{"id":"111","subject":"[PATCH 5/6] show-diff.c: make diff options customizable.","startedAt":"2005-04-18T20:36:20Z","lastAt":"2005-04-18T20:36:20Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"690","messageId":"7vekd7u8jf.fsf@assigned-by-dhcp.cox.net","threadId":"111","inReplyTo":null,"subject":"[PATCH 5/6] show-diff.c: make diff options customizable.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-18T20:36:20Z","receivedAt":"2005-04-18T20:36:20Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This patch is optional.  If makes the diff output formatting\noptions customizable via the environment variables.  The default\nis still the Linux kernel style.\n\nTo be applied on top of:\n\n    [PATCH 1/6] show-diff.c: clean up private buffer use.\n    [PATCH 2/6] show-diff.c: check unreadbale blob.\n    [PATCH 3/6] show-diff.c: simplify show_diff_empty.\n    [PATCH 4/6] show-diff.c: adjust default format for the Linux kernel.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   29 +++++++++++++++++++++++++----\n 1 files changed, 25 insertions(+), 4 deletions(-)\n\n--- a/show-diff.c\n+++ b/show-diff.c\n@@ -5,7 +5,23 @@\n  */\n #include \"cache.h\"\n \n-static char *diff_cmd = \"diff -L 'a/%s' -L 'b/%s' -p -u - '%s'\";\n+static char *diff_cmd = \"diff -L 'a/%s' -L 'b/%s' \";\n+static char *diff_opts = \"-p -u\";\n+\n+static void prepare_diff_cmd(void)\n+{\n+\t/*\n+\t * Default values above are meant to match the\n+\t * Linux kernel development style.  Examples of\n+\t * alternative styles you can specify via environment\n+\t * variables are:\n+\t *\n+\t * GIT_DIFF_CMD=\"diff -L '%s' -L '%s'\"\n+\t * GIT_DIFF_OPTS=\"-c\";\n+\t */\n+\tdiff_cmd = getenv(\"GIT_DIFF_CMD\") ? : diff_cmd;\n+\tdiff_opts = getenv(\"GIT_DIFF_OPTS\") ? : diff_opts;\n+}\n \n /* Help to copy the thing properly quoted for the shell safety.\n  * any single quote is replaced with '\\'', and the caller is\n@@ -49,15 +65,19 @@ static char *sq_expand(char *src)\n static void show_differences(char *name, char *label, void *old_contents,\n \t\t\t     unsigned long long old_size)\n {\n+\tstatic char *diff_arg = \" - '%s'\";\n \tFILE *f;\n \tchar *name_sq = sq_expand(name);\n \tchar *label_sq = (name != label) ? sq_expand(label) : name_sq;\n-\tint cmd_size = strlen(name_sq) +\n-\t\tstrlen(label_sq) * 2 + strlen(diff_cmd);\n+\tint cmd_size = strlen(name_sq) + strlen(label_sq) * 2 +\n+\t\tstrlen(diff_cmd) + strlen(diff_opts) + strlen(diff_arg);\n \tchar *cmd = malloc(cmd_size);\n+\tint next_at;\n \n \tfflush(stdout);\n-\tsnprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq, name_sq);\n+\tnext_at = snprintf(cmd, cmd_size, diff_cmd, label_sq, label_sq);\n+\tnext_at += snprintf(cmd+next_at, cmd_size-next_at, \"%s\", diff_opts);\n+\tnext_at += snprintf(cmd+next_at, cmd_size-next_at, diff_arg, name_sq);\n \tf = popen(cmd, \"w\");\n \tif (old_size)\n \t\tfwrite(old_contents, old_size, 1, f);\n@@ -127,6 +147,7 @@ int main(int argc, char **argv)\n \t\tperror(\"read_cache\");\n \t\texit(1);\n \t}\n+\tprepare_diff_cmd();\n \tfor (i = 0; i < entries; i++) {\n \t\tstruct stat st;\n \t\tstruct cache_entry *ce = active_cache[i];\n\n"}]}