Sync with 'maint'
[git/gitster.git] / builtin / diff-index.c
blob8bd5aa464bdd60e5fbde1536c9d761b16a512788
1 #include "builtin.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "diff-merges.h"
5 #include "commit.h"
6 #include "preload-index.h"
7 #include "repository.h"
8 #include "revision.h"
9 #include "setup.h"
11 static const char diff_cache_usage[] =
12 "git diff-index [-m] [--cached] [--merge-base] "
13 "[<common-diff-options>] <tree-ish> [<path>...]"
14 "\n"
15 COMMON_DIFF_OPTIONS_HELP;
17 int cmd_diff_index(int argc, const char **argv, const char *prefix)
19 struct rev_info rev;
20 unsigned int option = 0;
21 int i;
22 int result;
24 if (argc == 2 && !strcmp(argv[1], "-h"))
25 usage(diff_cache_usage);
27 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
29 prepare_repo_settings(the_repository);
30 the_repository->settings.command_requires_full_index = 0;
32 repo_init_revisions(the_repository, &rev, prefix);
33 rev.abbrev = 0;
34 prefix = precompose_argv_prefix(argc, argv, prefix);
37 * We need (some of) diff for merges options (e.g., --cc), and we need
38 * to avoid conflict with our own meaning of "-m".
40 diff_merges_suppress_m_parsing();
42 argc = setup_revisions(argc, argv, &rev, NULL);
43 for (i = 1; i < argc; i++) {
44 const char *arg = argv[i];
46 if (!strcmp(arg, "--cached"))
47 option |= DIFF_INDEX_CACHED;
48 else if (!strcmp(arg, "--merge-base"))
49 option |= DIFF_INDEX_MERGE_BASE;
50 else if (!strcmp(arg, "-m"))
51 rev.match_missing = 1;
52 else
53 usage(diff_cache_usage);
55 if (!rev.diffopt.output_format)
56 rev.diffopt.output_format = DIFF_FORMAT_RAW;
58 rev.diffopt.rotate_to_strict = 1;
61 * Make sure there is one revision (i.e. pending object),
62 * and there is no revision filtering parameters.
64 if (rev.pending.nr != 1 ||
65 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
66 usage(diff_cache_usage);
67 if (!(option & DIFF_INDEX_CACHED)) {
68 setup_work_tree();
69 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
70 perror("repo_read_index_preload");
71 return -1;
73 } else if (repo_read_index(the_repository) < 0) {
74 perror("repo_read_index");
75 return -1;
77 run_diff_index(&rev, option);
78 result = diff_result_code(&rev);
79 release_revisions(&rev);
80 return result;