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