{"thread":{"id":"62","subject":"[PATCH] show-diff -z option for machine readable output.","startedAt":"2005-04-16T23:30:39Z","lastAt":"2005-04-16T23:30:39Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"387","messageId":"7vr7ha47vk.fsf@assigned-by-dhcp.cox.net","threadId":"62","inReplyTo":null,"subject":"[PATCH] show-diff -z option for machine readable output.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-16T23:30:39Z","receivedAt":"2005-04-16T23:30:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This patch adds the -z option to the show-diff command,\nprimarily for use by scripts.  The information emitted is\nsimilar to that of -q option, but in a more machine readable\nform.  Records are terminated with NUL instead of LF, so that\nthe scripts can deal with pathnames with embedded newlines.\n\nTo be applied on top of my previous patch:\n\n    [PATCH] Optionally tell show-diff to show only named files.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   28 +++++++++++++++++++---------\n 1 files changed, 19 insertions(+), 9 deletions(-)\n\nshow-diff.c: 0c5fb1a381a6c6689dca3f52d0c66bb591cadb39\n--- show-diff.c\n+++ show-diff.c\t2005-04-16 16:23:40.000000000 -0700\n@@ -55,7 +55,7 @@\n \t}\n }\n \n-static const char *show_diff_usage = \"show-diff [-s] [-q] [paths...]\";\n+static const char *show_diff_usage = \"show-diff [-s] [-q] [-z] [paths...]\";\n \n static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)\n {\n@@ -76,6 +76,7 @@\n {\n \tint silent = 0;\n \tint silent_on_nonexisting_files = 0;\n+\tint machine_readable = 0;\n \tint entries = read_cache();\n \tint i;\n \n@@ -84,6 +85,9 @@\n \t\t\tsilent_on_nonexisting_files = silent = 1;\n \t\telse if (!strcmp(argv[1], \"-q\"))\n \t\t\tsilent_on_nonexisting_files = 1;\n+\t\telse if (!strcmp(argv[1], \"-z\")) {\n+\t\t\tmachine_readable = 1;\n+\t\t}\n \t\telse\n \t\t\tusage(show_diff_usage);\n \t\targv++; argc--;\n@@ -99,7 +103,7 @@\n \tfor (i = 0; i < entries; i++) {\n \t\tstruct stat st;\n \t\tstruct cache_entry *ce = active_cache[i];\n-\t\tint n, changed;\n+\t\tint changed;\n \t\tunsigned long size;\n \t\tchar type[20];\n \t\tvoid *new;\n@@ -111,18 +115,24 @@\n \t\tif (stat(ce->name, &st) < 0) {\n \t\t\tif (errno == ENOENT && silent_on_nonexisting_files)\n \t\t\t\tcontinue;\n-\t\t\tprintf(\"%s: %s\\n\", ce->name, strerror(errno));\n-\t\t\tif (errno == ENOENT)\n-\t\t\t\tshow_diff_empty(ce);\n+\t\t\tif (machine_readable)\n+\t\t\t\tprintf(\"X %s%c\", ce->name, 0);\n+\t\t\telse {\n+\t\t\t\tprintf(\"%s: %s\\n\", ce->name, strerror(errno));\n+\t\t\t\tif (errno == ENOENT)\n+\t\t\t\t\tshow_diff_empty(ce);\n+\t\t\t}\n \t\t\tcontinue;\n \t\t}\n \t\tchanged = cache_match_stat(ce, &st);\n \t\tif (!changed)\n \t\t\tcontinue;\n-\t\tprintf(\"%s:  \", ce->name);\n-\t\tfor (n = 0; n < 20; n++)\n-\t\t\tprintf(\"%02x\", ce->sha1[n]);\n-\t\tprintf(\"\\n\");\n+\t\tif (!machine_readable)\n+\t\t\tprintf(\"%s: %s\\n\", ce->name, sha1_to_hex(ce->sha1));\n+\t\telse {\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"}]}