{"thread":{"id":"109","subject":"[PATCH 3/6] show-diff.c: simplify show_diff_empty.","startedAt":"2005-04-18T20:34:42Z","lastAt":"2005-04-18T20:34:42Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"688","messageId":"7vmzrvu8m5.fsf@assigned-by-dhcp.cox.net","threadId":"109","inReplyTo":null,"subject":"[PATCH 3/6] show-diff.c: simplify show_diff_empty.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-18T20:34:42Z","receivedAt":"2005-04-18T20:34:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"[PATCH 3/6] show-diff.c: simplify show_diff_empty.\n\nThis patch removes the custom diff generation code from the\nshow_diff_empty() function.  Instead, just use show_differences().\n\nThis reduces the code size; but more importantly, it is needed for\nthe later patch to give diff options.\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\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   44 ++++++++++----------------------------------\n 1 files changed, 10 insertions(+), 34 deletions(-)\n\n--- a/show-diff.c\n+++ b/show-diff.c\n@@ -46,19 +46,23 @@ static char *sq_expand(char *src)\n \treturn buf;\n }\n \n-static void show_differences(char *name, void *old_contents,\n+static void show_differences(char *name, char *label, void *old_contents,\n \t\t\t     unsigned long long old_size)\n {\n \tFILE *f;\n \tchar *name_sq = sq_expand(name);\n-\tint cmd_size = strlen(name_sq) * 2 + strlen(diff_cmd);\n+\tchar *label_sq = (name != label) ? sq_expand(label) : name_sq;\n+\tint cmd_size = strlen(name_sq) + strlen(label_sq) + strlen(diff_cmd);\n \tchar *cmd = malloc(cmd_size);\n \n-\tsnprintf(cmd, cmd_size, diff_cmd, name_sq, name_sq);\n+\tfflush(stdout);\n+\tsnprintf(cmd, cmd_size, diff_cmd, label_sq, name_sq);\n \tf = popen(cmd, \"w\");\n \tif (old_size)\n \t\tfwrite(old_contents, old_size, 1, f);\n \tpclose(f);\n+\tif (label_sq != name_sq)\n+\t\tfree(label_sq);\n \tfree(name_sq);\n \tfree(cmd);\n }\n@@ -67,8 +71,7 @@ static void show_diff_empty(struct cache\n {\n \tchar *old;\n \tunsigned long int size;\n-\tint lines=0;\n-\tunsigned char type[20], *p, *end;\n+\tunsigned char type[20];\n \n \told = read_sha1_file(ce->sha1, type, &size);\n \tif (! old) {\n@@ -76,33 +79,7 @@ static void show_diff_empty(struct cache\n \t\t      sha1_to_hex(ce->sha1));\n \t\treturn;\n \t}\n-\tif (size > 0) {\n-\t\tint startline = 1;\n-\t\tint c = 0;\n-\n-\t\tprintf(\"--- %s\\n\", ce->name);\n-\t\tprintf(\"+++ /dev/null\\n\");\n-\t\tp = old;\n-\t\tend = old + size;\n-\t\twhile (p < end)\n-\t\t\tif (*p++ == '\\n')\n-\t\t\t\tlines ++;\n-\t\tprintf(\"@@ -1,%d +0,0 @@\\n\", lines);\n-\t\tp = old;\n-\t\twhile (p < end) {\n-\t\t\tc = *p++;\n-\t\t\tif (startline) {\n-\t\t\t\tputchar('-');\n-\t\t\t\tstartline = 0;\n-\t\t\t}\n-\t\t\tputchar(c);\n-\t\t\tif (c == '\\n')\n-\t\t\t\tstartline = 1;\n-\t\t}\n-\t\tif (c!='\\n')\n-\t\t\tprintf(\"\\n\");\n-\t\tfflush(stdout);\n-\t}\n+\tshow_differences(\"/dev/null\", ce->name, old, size);\n }\n \n static const char *show_diff_usage = \"show-diff [-q] [-s] [-z] [paths...]\";\n@@ -195,7 +172,6 @@ int main(int argc, char **argv)\n \t\t\tprintf(\"%s %s%c\", sha1_to_hex(ce->sha1), ce->name, 0);\n \t\t\tcontinue;\n \t\t}\n-\t\tfflush(stdout);\n \t\tif (silent)\n \t\t\tcontinue;\n \n@@ -204,7 +180,7 @@ int main(int argc, char **argv)\n \t\t\terror(\"unable to read blob object for %s (%s)\",\n \t\t\t      ce->name, sha1_to_hex(ce->sha1));\n \t\telse\n-\t\t\tshow_differences(ce->name, old, size);\n+\t\t\tshow_differences(ce->name, ce->name, old, size);\n \t\tfree(old);\n \t}\n \treturn 0;\n\n"}]}