Merge branch 'jk/two-way-merge-corner-case-fix' into maint
[git] / builtin / diff-index.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "revision.h"
5 #include "builtin.h"
6 #include "submodule.h"
7
8 static const char diff_cache_usage[] =
9 "git diff-index [-m] [--cached] "
10 "[<common diff options>] <tree-ish> [<path>...]"
11 COMMON_DIFF_OPTIONS_HELP;
12
13 int cmd_diff_index(int argc, const char **argv, const char *prefix)
14 {
15         struct rev_info rev;
16         int cached = 0;
17         int i;
18         int result;
19
20         init_revisions(&rev, prefix);
21         gitmodules_config();
22         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
23         rev.abbrev = 0;
24
25         argc = setup_revisions(argc, argv, &rev, NULL);
26         for (i = 1; i < argc; i++) {
27                 const char *arg = argv[i];
28
29                 if (!strcmp(arg, "--cached"))
30                         cached = 1;
31                 else
32                         usage(diff_cache_usage);
33         }
34         if (!rev.diffopt.output_format)
35                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
36
37         /*
38          * Make sure there is one revision (i.e. pending object),
39          * and there is no revision filtering parameters.
40          */
41         if (rev.pending.nr != 1 ||
42             rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
43                 usage(diff_cache_usage);
44         if (!cached) {
45                 setup_work_tree();
46                 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
47                         perror("read_cache_preload");
48                         return -1;
49                 }
50         } else if (read_cache() < 0) {
51                 perror("read_cache");
52                 return -1;
53         }
54         result = run_diff_index(&rev, cached);
55         return diff_result_code(&rev.diffopt, result);
56 }