The second batch
[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 "diff-merges.h"
6 #include "commit.h"
7 #include "revision.h"
8 #include "builtin.h"
9 #include "submodule.h"
10
11 static const char diff_cache_usage[] =
12 "git diff-index [-m] [--cached] "
13 "[<common-diff-options>] <tree-ish> [<path>...]"
14 COMMON_DIFF_OPTIONS_HELP;
15
16 int cmd_diff_index(int argc, const char **argv, const char *prefix)
17 {
18         struct rev_info rev;
19         unsigned int option = 0;
20         int i;
21         int result;
22
23         if (argc == 2 && !strcmp(argv[1], "-h"))
24                 usage(diff_cache_usage);
25
26         git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
27         repo_init_revisions(the_repository, &rev, prefix);
28         rev.abbrev = 0;
29         prefix = precompose_argv_prefix(argc, argv, prefix);
30
31         /*
32          * We need no diff for merges options, and we need to avoid conflict
33          * with our own meaning of "-m".
34          */
35         diff_merges_suppress_options_parsing();
36
37         argc = setup_revisions(argc, argv, &rev, NULL);
38         for (i = 1; i < argc; i++) {
39                 const char *arg = argv[i];
40
41                 if (!strcmp(arg, "--cached"))
42                         option |= DIFF_INDEX_CACHED;
43                 else if (!strcmp(arg, "--merge-base"))
44                         option |= DIFF_INDEX_MERGE_BASE;
45                 else if (!strcmp(arg, "-m"))
46                         rev.match_missing = 1;
47                 else
48                         usage(diff_cache_usage);
49         }
50         if (!rev.diffopt.output_format)
51                 rev.diffopt.output_format = DIFF_FORMAT_RAW;
52
53         rev.diffopt.rotate_to_strict = 1;
54
55         /*
56          * Make sure there is one revision (i.e. pending object),
57          * and there is no revision filtering parameters.
58          */
59         if (rev.pending.nr != 1 ||
60             rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
61                 usage(diff_cache_usage);
62         if (!(option & DIFF_INDEX_CACHED)) {
63                 setup_work_tree();
64                 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
65                         perror("read_cache_preload");
66                         return -1;
67                 }
68         } else if (read_cache() < 0) {
69                 perror("read_cache");
70                 return -1;
71         }
72         result = run_diff_index(&rev, option);
73         UNLEAK(rev);
74         return diff_result_code(&rev.diffopt, result);
75 }