Allow git-diff exit with codes similar to diff(1)
This introduces a new command-line option: --exit-code. The diff programs will return 1 for differences, return 0 for equality, and something else for errors. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Alex Riesen committed
Mar 14, 2007 at 01:17 UTC
41bbf9d58575095234c64df979ee884334469758
9 files changed
+118
-14
Documentation/diff-options.txt
+5
@@ -159,5 +159,10 @@
159
-w::
160
Shorthand for "--ignore-all-space".
161
162
+--exit-code::
163
+ Make the program exit with codes similar to diff(1).
164
+ That is, it exits with 1 if there were differences and
165
+ 0 means no differences.
166
+
167
For more detailed explanation on these common options, see also
168
link:diffcore.html[diffcore documentation].
builtin-diff-files.c
+3
-1
@@ -17,6 +17,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
17
{
18
struct rev_info rev;
19
int nongit = 0;
20
+ int result;
21
22
prefix = setup_git_directory_gently(&nongit);
23
init_revisions(&rev, prefix);
@@ -29,5 +30,6 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
30
argc = setup_revisions(argc, argv, &rev, NULL);
31
if (!rev.diffopt.output_format)
32
rev.diffopt.output_format = DIFF_FORMAT_RAW;
32
- return run_diff_files_cmd(&rev, argc, argv);
33
+ result = run_diff_files_cmd(&rev, argc, argv);
34
+ return rev.diffopt.exit_with_status ? rev.diffopt.has_changes: result;
35
}
builtin-diff-index.c
+3
-1
@@ -14,6 +14,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
14
struct rev_info rev;
15
int cached = 0;
16
int i;
17
+ int result;
18
19
init_revisions(&rev, prefix);
20
git_config(git_default_config); /* no "diff" UI options */
@@ -42,5 +43,6 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
43
perror("read_cache");
44
return -1;
45
}
45
- return run_diff_index(&rev, cached);
46
+ result = run_diff_index(&rev, cached);
47
+ return rev.diffopt.exit_with_status ? rev.diffopt.has_changes: result;
48
}
builtin-diff-tree.c
+3
-2
@@ -118,7 +118,8 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
118
}
119
120
if (!read_stdin)
121
- return 0;
121
+ return opt->diffopt.exit_with_status ?
122
+ opt->diffopt.has_changes: 0;
123
124
if (opt->diffopt.detect_rename)
125
opt->diffopt.setup |= (DIFF_SETUP_USE_SIZE_CACHE |
@@ -133,5 +134,5 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
134
else
135
diff_tree_stdin(line);
136
}
136
- return 0;
137
+ return opt->diffopt.exit_with_status ? opt->diffopt.has_changes: 0;
138
}
builtin-diff.c
+11
-8
@@ -190,6 +190,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
190
const char *path = NULL;
191
struct blobinfo blob[2];
192
int nongit = 0;
193
+ int result = 0;
194
195
/*
196
* We could get N tree-ish in the rev.pending_objects list.
@@ -292,17 +293,17 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
293
if (!ents) {
294
switch (blobs) {
295
case 0:
295
- return run_diff_files_cmd(&rev, argc, argv);
296
+ result = run_diff_files_cmd(&rev, argc, argv);
297
break;
298
case 1:
299
if (paths != 1)
300
usage(builtin_diff_usage);
300
- return builtin_diff_b_f(&rev, argc, argv, blob, path);
301
+ result = builtin_diff_b_f(&rev, argc, argv, blob, path);
302
break;
303
case 2:
304
if (paths)
305
usage(builtin_diff_usage);
305
- return builtin_diff_blobs(&rev, argc, argv, blob);
306
+ result = builtin_diff_blobs(&rev, argc, argv, blob);
307
break;
308
default:
309
usage(builtin_diff_usage);
@@ -311,19 +312,21 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
312
else if (blobs)
313
usage(builtin_diff_usage);
314
else if (ents == 1)
314
- return builtin_diff_index(&rev, argc, argv);
315
+ result = builtin_diff_index(&rev, argc, argv);
316
else if (ents == 2)
316
- return builtin_diff_tree(&rev, argc, argv, ent);
317
+ result = builtin_diff_tree(&rev, argc, argv, ent);
318
else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
319
/* diff A...B where there is one sane merge base between
320
* A and B. We have ent[0] == merge-base, ent[1] == A,
321
* and ent[2] == B. Show diff between the base and B.
322
*/
323
ent[1] = ent[2];
323
- return builtin_diff_tree(&rev, argc, argv, ent);
324
+ result = builtin_diff_tree(&rev, argc, argv, ent);
325
}
326
else
326
- return builtin_diff_combined(&rev, argc, argv,
327
+ result = builtin_diff_combined(&rev, argc, argv,
328
ent, ents);
328
- usage(builtin_diff_usage);
329
+ if (rev.diffopt.exit_with_status)
330
+ result = rev.diffopt.has_changes;
331
+ return result;
332
}
diff-lib.c
+4
-1
@@ -170,8 +170,10 @@ static int handle_diff_files_args(struct rev_info *revs,
170
else if (!strcmp(argv[1], "--theirs"))
171
revs->max_count = 3;
172
else if (!strcmp(argv[1], "-n") ||
173
- !strcmp(argv[1], "--no-index"))
173
+ !strcmp(argv[1], "--no-index")) {
174
revs->max_count = -2;
175
+ revs->diffopt.exit_with_status = 1;
176
+ }
177
else if (!strcmp(argv[1], "-q"))
178
*silent = 1;
179
else
@@ -237,6 +239,7 @@ int setup_diff_no_index(struct rev_info *revs,
239
break;
240
} else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
241
i = argc - 3;
242
+ revs->diffopt.exit_with_status = 1;
243
break;
244
}
245
if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
diff.c
+6
@@ -2134,6 +2134,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2134
options->color_diff = options->color_diff_words = 1;
2135
else if (!strcmp(arg, "--no-renames"))
2136
options->detect_rename = 0;
2137
+ else if (!strcmp(arg, "--exit-code"))
2138
+ options->exit_with_status = 1;
2139
else
2140
return 0;
2141
return 1;
@@ -2910,6 +2912,8 @@ void diffcore_std(struct diff_options *options)
2912
diffcore_order(options->orderfile);
2913
diff_resolve_rename_copy();
2914
diffcore_apply_filter(options->filter);
2915
+ if (options->exit_with_status)
2916
+ options->has_changes = !!diff_queued_diff.nr;
2917
}
2918
2919
@@ -2920,6 +2924,8 @@ void diffcore_std_no_resolve(struct diff_options *options)
2924
if (options->orderfile)
2925
diffcore_order(options->orderfile);
2926
diffcore_apply_filter(options->filter);
2927
+ if (options->exit_with_status)
2928
+ options->has_changes = !!diff_queued_diff.nr;
2929
}
2930
2931
void diff_addremove(struct diff_options *options,
diff.h
+4
-1
@@ -56,7 +56,8 @@ struct diff_options {
56
silent_on_remove:1,
57
find_copies_harder:1,
58
color_diff:1,
59
- color_diff_words:1;
59
+ color_diff_words:1,
60
+ exit_with_status:1;
61
int context;
62
int break_opt;
63
int detect_rename;
@@ -71,6 +72,8 @@ struct diff_options {
72
const char *msg_sep;
73
const char *stat_sep;
74
long xdl_opts;
75
+ /* 0 - no differences; only meaningful if exit_with_status set */
76
+ int has_changes;
77
78
int stat_width;
79
int stat_name_width;
t/t4017-diff-retval.sh
new
+79
@@ -0,0 +1,79 @@
1
+#!/bin/sh
2
+
3
+test_description='Return value of diffs'
4
+
5
+. ./test-lib.sh
6
+
7
+test_expect_success 'setup' '
8
+ echo 1 >a &&
9
+ git add . &&
10
+ git commit -m first &&
11
+ echo 2 >b &&
12
+ git add . &&
13
+ git commit -a -m second
14
+'
15
+
16
+test_expect_success 'git diff-tree HEAD^ HEAD' '
17
+ git diff-tree --exit-code HEAD^ HEAD
18
+ test $? = 1
19
+'
20
+test_expect_success 'git diff-tree HEAD^ HEAD -- a' '
21
+ git diff-tree --exit-code HEAD^ HEAD -- a
22
+ test $? = 0
23
+'
24
+test_expect_success 'git diff-tree HEAD^ HEAD -- b' '
25
+ git diff-tree --exit-code HEAD^ HEAD -- b
26
+ test $? = 1
27
+'
28
+test_expect_success 'echo HEAD | git diff-tree --stdin' '
29
+ echo $(git rev-parse HEAD) | git diff-tree --exit-code --stdin
30
+ test $? = 1
31
+'
32
+test_expect_success 'git diff-tree HEAD HEAD' '
33
+ git diff-tree --exit-code HEAD HEAD
34
+ test $? = 0
35
+'
36
+test_expect_success 'git diff-files' '
37
+ git diff-files --exit-code
38
+ test $? = 0
39
+'
40
+test_expect_success 'git diff-index --cached HEAD' '
41
+ git diff-index --exit-code --cached HEAD
42
+ test $? = 0
43
+'
44
+test_expect_success 'git diff-index --cached HEAD^' '
45
+ git diff-index --exit-code --cached HEAD^
46
+ test $? = 1
47
+'
48
+test_expect_success 'git diff-index --cached HEAD^' '
49
+ echo text >>b &&
50
+ echo 3 >c &&
51
+ git add . && {
52
+ git diff-index --exit-code --cached HEAD^
53
+ test $? = 1
54
+ }
55
+'
56
+test_expect_success 'git diff-tree -Stext HEAD^ HEAD -- b' '
57
+ git commit -m "text in b" && {
58
+ git diff-tree -p --exit-code -Stext HEAD^ HEAD -- b
59
+ test $? = 1
60
+ }
61
+'
62
+test_expect_success 'git diff-tree -Snot-found HEAD^ HEAD -- b' '
63
+ git diff-tree -p --exit-code -Snot-found HEAD^ HEAD -- b
64
+ test $? = 0
65
+'
66
+test_expect_success 'git diff-files' '
67
+ echo 3 >>c && {
68
+ git diff-files --exit-code
69
+ test $? = 1
70
+ }
71
+'
72
+test_expect_success 'git diff-index --cached HEAD' '
73
+ git update-index c && {
74
+ git diff-index --exit-code --cached HEAD
75
+ test $? = 1
76
+ }
77
+'
78
+
79
+test_done