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