diff --no-index: also imitate the exit status of diff(1)
diff sets the exit status to 0 when no changes were found, to 1 when changes were found, and 2 means error. We imitate this to be able to use "git diff" in the test scripts. (Actually, keeping in line with the rest of git, -1 is returned on error, which corresponds to an exit status 255). To find out if the diff is not empty, a member called "found_changes" was introduced in struct diff_options, which is set in builtin_diff() and fn_out_consume(). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Feb 25, 2007 at 23:34 UTC
34a5e1a2d900b5dd9b9c446a3db9fdb3b29e0575
3 files changed
+17
-3
diff-lib.c
+8
-3
@@ -210,11 +210,16 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
210
if (revs->max_count == -2) {
211
if (revs->diffopt.nr_paths != 2)
212
return error("need two files/directories with --no-index");
213
- queue_diff(&revs->diffopt, revs->diffopt.paths[0],
214
- revs->diffopt.paths[1]);
213
+ if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
214
+ revs->diffopt.paths[1]))
215
+ return -1;
216
diffcore_std(&revs->diffopt);
217
diff_flush(&revs->diffopt);
217
- return 0;
218
+ /*
219
+ * The return code for --no-index imitates diff(1):
220
+ * 0 = no changes, 1 = changes, else error
221
+ */
222
+ return revs->diffopt.found_changes;
223
}
224
225
if (read_cache() < 0) {
diff.c
+6
@@ -382,6 +382,7 @@ struct emit_callback {
382
int nparents, color_diff;
383
const char **label_path;
384
struct diff_words_data *diff_words;
385
+ int *found_changesp;
386
};
387
388
static void free_diff_words_data(struct emit_callback *ecbdata)
@@ -501,6 +502,8 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
502
const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
503
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
504
505
+ *(ecbdata->found_changesp) = 1;
506
+
507
if (ecbdata->label_path[0]) {
508
const char *name_a_tab, *name_b_tab;
509
@@ -1098,6 +1101,7 @@ static void builtin_diff(const char *name_a,
1101
if (complete_rewrite) {
1102
emit_rewrite_diff(name_a, name_b, one, two,
1103
o->color_diff);
1104
+ o->found_changes = 1;
1105
goto free_ab_and_return;
1106
}
1107
}
@@ -1115,6 +1119,7 @@ static void builtin_diff(const char *name_a,
1119
else
1120
printf("Binary files %s and %s differ\n",
1121
lbl[0], lbl[1]);
1122
+ o->found_changes = 1;
1123
}
1124
else {
1125
/* Crazy xdl interfaces.. */
@@ -1127,6 +1132,7 @@ static void builtin_diff(const char *name_a,
1132
memset(&ecbdata, 0, sizeof(ecbdata));
1133
ecbdata.label_path = lbl;
1134
ecbdata.color_diff = o->color_diff;
1135
+ ecbdata.found_changesp = &o->found_changes;
1136
xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1137
xecfg.ctxlen = o->context;
1138
xecfg.flags = XDL_EMIT_FUNCNAMES;
diff.h
+3
@@ -75,6 +75,9 @@ struct diff_options {
75
int stat_width;
76
int stat_name_width;
77
78
+ /* this is set by diffcore for DIFF_FORMAT_PATCH */
79
+ int found_changes;
80
+
81
int nr_paths;
82
const char **paths;
83
int *pathlens;