]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/diff-index.c
Merge branch 'rs/parse-options-with-keep-unknown-abbrev-fix'
[thirdparty/git.git] / builtin / diff-index.c
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"
10
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;
16
17 int cmd_diff_index(int argc, const char **argv, const char *prefix)
18 {
19 struct rev_info rev;
20 unsigned int option = 0;
21 int i;
22 int result;
23
24 if (argc == 2 && !strcmp(argv[1], "-h"))
25 usage(diff_cache_usage);
26
27 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
28 repo_init_revisions(the_repository, &rev, prefix);
29 rev.abbrev = 0;
30 prefix = precompose_argv_prefix(argc, argv, prefix);
31
32 /*
33 * We need (some of) diff for merges options (e.g., --cc), and we need
34 * to avoid conflict with our own meaning of "-m".
35 */
36 diff_merges_suppress_m_parsing();
37
38 argc = setup_revisions(argc, argv, &rev, NULL);
39 for (i = 1; i < argc; i++) {
40 const char *arg = argv[i];
41
42 if (!strcmp(arg, "--cached"))
43 option |= DIFF_INDEX_CACHED;
44 else if (!strcmp(arg, "--merge-base"))
45 option |= DIFF_INDEX_MERGE_BASE;
46 else if (!strcmp(arg, "-m"))
47 rev.match_missing = 1;
48 else
49 usage(diff_cache_usage);
50 }
51 if (!rev.diffopt.output_format)
52 rev.diffopt.output_format = DIFF_FORMAT_RAW;
53
54 rev.diffopt.rotate_to_strict = 1;
55
56 /*
57 * Make sure there is one revision (i.e. pending object),
58 * and there is no revision filtering parameters.
59 */
60 if (rev.pending.nr != 1 ||
61 rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
62 usage(diff_cache_usage);
63 if (!(option & DIFF_INDEX_CACHED)) {
64 setup_work_tree();
65 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
66 perror("repo_read_index_preload");
67 return -1;
68 }
69 } else if (repo_read_index(the_repository) < 0) {
70 perror("repo_read_index");
71 return -1;
72 }
73 run_diff_index(&rev, option);
74 result = diff_result_code(&rev.diffopt);
75 release_revisions(&rev);
76 return result;
77 }