1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
5 #include "diff-merges.h"
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;
16 int cmd_diff_index(int argc, const char **argv, const char *prefix)
19 unsigned int option = 0;
23 if (argc == 2 && !strcmp(argv[1], "-h"))
24 usage(diff_cache_usage);
26 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
27 repo_init_revisions(the_repository, &rev, prefix);
29 prefix = precompose_argv_prefix(argc, argv, prefix);
32 * We need no diff for merges options, and we need to avoid conflict
33 * with our own meaning of "-m".
35 diff_merges_suppress_options_parsing();
37 argc = setup_revisions(argc, argv, &rev, NULL);
38 for (i = 1; i < argc; i++) {
39 const char *arg = argv[i];
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;
48 usage(diff_cache_usage);
50 if (!rev.diffopt.output_format)
51 rev.diffopt.output_format = DIFF_FORMAT_RAW;
53 rev.diffopt.rotate_to_strict = 1;
56 * Make sure there is one revision (i.e. pending object),
57 * and there is no revision filtering parameters.
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)) {
64 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
65 perror("read_cache_preload");
68 } else if (read_cache() < 0) {
72 result = run_diff_index(&rev, option);
74 return diff_result_code(&rev.diffopt, result);