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