{"thread":{"id":"112","subject":"[PATCH 6/6] show-diff.c: -R option for reverse diff.","startedAt":"2005-04-18T20:36:58Z","lastAt":"2005-04-18T20:36:58Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"691","messageId":"7vacnvu8id.fsf@assigned-by-dhcp.cox.net","threadId":"112","inReplyTo":null,"subject":"[PATCH 6/6] show-diff.c: -R option for reverse diff.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-18T20:36:58Z","receivedAt":"2005-04-18T20:36:58Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This patch is optional.  It adds -R option to obtain reverse diff.\n\nIt may be useful in the merge workflow.  After the base of the working\ndirectory is merged and commited, in the working directory:\n\n    $ read-tree <tree-id-of-merged-tree>\n    $ show-diff -R\n\nto re-validate if upstream changes make sense, and/or revert or\nconflict with local changes you have in the working files.\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    [PATCH 5/6] show-diff.c: make diff options customizable.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   20 +++++++++++++-------\n 1 files changed, 13 insertions(+), 7 deletions(-)\n\n--- a/show-diff.c\n+++ b/show-diff.c\n@@ -7,6 +7,8 @@\n \n static char *diff_cmd = \"diff -L 'a/%s' -L 'b/%s' \";\n static char *diff_opts = \"-p -u\";\n+static char *diff_arg_forward  = \" - '%s'\";\n+static char *diff_arg_reverse  = \" '%s' -\";\n \n static void prepare_diff_cmd(void)\n {\n@@ -63,12 +65,12 @@ static char *sq_expand(char *src)\n }\n \n static void show_differences(char *name, char *label, void *old_contents,\n-\t\t\t     unsigned long long old_size)\n+\t\t\t     unsigned long long old_size, int reverse)\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+\tchar *diff_arg = reverse ? diff_arg_reverse : diff_arg_forward;\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@@ -88,7 +90,7 @@ static void show_differences(char *name,\n \tfree(cmd);\n }\n \n-static void show_diff_empty(struct cache_entry *ce)\n+static void show_diff_empty(struct cache_entry *ce, int reverse)\n {\n \tchar *old;\n \tunsigned long int size;\n@@ -100,7 +102,7 @@ static void show_diff_empty(struct cache\n \t\t      sha1_to_hex(ce->sha1));\n \t\treturn;\n \t}\n-\tshow_differences(\"/dev/null\", ce->name, old, size);\n+\tshow_differences(\"/dev/null\", ce->name, old, size, reverse);\n }\n \n static const char *show_diff_usage = \"show-diff [-q] [-s] [-z] [paths...]\";\n@@ -125,11 +127,14 @@ int main(int argc, char **argv)\n \tint silent = 0;\n \tint silent_on_nonexisting_files = 0;\n \tint machine_readable = 0;\n+\tint reverse = 0;\n \tint entries = read_cache();\n \tint i;\n \n \twhile (1 < argc && argv[1][0] == '-') {\n-\t\tif (!strcmp(argv[1], \"-s\"))\n+\t\tif  (!strcmp(argv[1], \"-R\"))\n+\t\t\treverse = 1;\n+\t\telse if (!strcmp(argv[1], \"-s\"))\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@@ -181,7 +186,7 @@ int main(int argc, char **argv)\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\t\tshow_diff_empty(ce, reverse);\n \t\t\t}\n \t\t\tcontinue;\n \t\t}\n@@ -202,7 +207,8 @@ 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, ce->name, old, size);\n+\t\t\tshow_differences(ce->name, ce->name, old, size,\n+\t\t\t\t\t reverse);\n \t\tfree(old);\n \t}\n \treturn 0;\n\n"}]}