{"thread":{"id":"6958","subject":"[PATCH 2/8] diff --no-index: also imitate the exit status of diff(1)","startedAt":"2007-02-25T22:34:54Z","lastAt":"2007-02-25T22:34:54Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":8},"messages":[{"id":"35464","messageId":"Pine.LNX.4.63.0702252334400.22628@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"6958","inReplyTo":null,"subject":"[PATCH 2/8] diff --no-index: also imitate the exit status of diff(1)","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2007-02-25T22:34:54Z","receivedAt":"2007-02-25T22:34:54Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\ndiff sets the exit status to 0 when no changes were found, to 1\nwhen changes were found, and 2 means error.\n\nWe imitate this to be able to use \"git diff\" in the test scripts.\n(Actually, keeping in line with the rest of git, -1 is returned\non error, which corresponds to an exit status 255).\n\nTo find out if the diff is not empty, a member called\n\"found_changes\" was introduced in struct diff_options, which is\nset in builtin_diff() and fn_out_consume().\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n diff-lib.c |   11 ++++++++---\n diff.c     |    6 ++++++\n diff.h     |    3 +++\n 3 files changed, 17 insertions(+), 3 deletions(-)\n\ndiff --git a/diff-lib.c b/diff-lib.c\nindex 17b9a56..b164222 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -210,11 +210,16 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)\n \tif (revs->max_count == -2) {\n \t\tif (revs->diffopt.nr_paths != 2)\n \t\t\treturn error(\"need two files/directories with --no-index\");\n-\t\tqueue_diff(&revs->diffopt, revs->diffopt.paths[0],\n-\t\t\t\trevs->diffopt.paths[1]);\n+\t\tif (queue_diff(&revs->diffopt, revs->diffopt.paths[0],\n+\t\t\t\trevs->diffopt.paths[1]))\n+\t\t\treturn -1;\n \t\tdiffcore_std(&revs->diffopt);\n \t\tdiff_flush(&revs->diffopt);\n-\t\treturn 0;\n+\t\t/*\n+\t\t * The return code for --no-index imitates diff(1):\n+\t\t * 0 = no changes, 1 = changes, else error\n+\t\t */\n+\t\treturn revs->diffopt.found_changes;\n \t}\n \n \tif (read_cache() < 0) {\ndiff --git a/diff.c b/diff.c\nindex 6bd456e..5651152 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -382,6 +382,7 @@ struct emit_callback {\n \tint nparents, color_diff;\n \tconst char **label_path;\n \tstruct diff_words_data *diff_words;\n+\tint *found_changesp;\n };\n \n static void free_diff_words_data(struct emit_callback *ecbdata)\n@@ -501,6 +502,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)\n \tconst char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);\n \tconst char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);\n \n+\t*(ecbdata->found_changesp) = 1;\n+\n \tif (ecbdata->label_path[0]) {\n \t\tconst char *name_a_tab, *name_b_tab;\n \n@@ -1098,6 +1101,7 @@ static void builtin_diff(const char *name_a,\n \t\tif (complete_rewrite) {\n \t\t\temit_rewrite_diff(name_a, name_b, one, two,\n \t\t\t\t\to->color_diff);\n+\t\t\to->found_changes = 1;\n \t\t\tgoto free_ab_and_return;\n \t\t}\n \t}\n@@ -1115,6 +1119,7 @@ static void builtin_diff(const char *name_a,\n \t\telse\n \t\t\tprintf(\"Binary files %s and %s differ\\n\",\n \t\t\t       lbl[0], lbl[1]);\n+\t\to->found_changes = 1;\n \t}\n \telse {\n \t\t/* Crazy xdl interfaces.. */\n@@ -1127,6 +1132,7 @@ static void builtin_diff(const char *name_a,\n \t\tmemset(&ecbdata, 0, sizeof(ecbdata));\n \t\tecbdata.label_path = lbl;\n \t\tecbdata.color_diff = o->color_diff;\n+\t\tecbdata.found_changesp = &o->found_changes;\n \t\txpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;\n \t\txecfg.ctxlen = o->context;\n \t\txecfg.flags = XDL_EMIT_FUNCNAMES;\ndiff --git a/diff.h b/diff.h\nindex b608828..4043cec 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -75,6 +75,9 @@ struct diff_options {\n \tint stat_width;\n \tint stat_name_width;\n \n+\t/* this is set by diffcore for DIFF_FORMAT_PATCH */\n+\tint found_changes;\n+\n \tint nr_paths;\n \tconst char **paths;\n \tint *pathlens;\n-- \n1.5.0.1.788.g8ca52\n"}]}