clone_submodule: avoid using `access()` on directories
[git/gitster.git] / builtin / diff-index.c
blob35dc9b23eef32d81e1e0a1b42a810ca6d95f58a2
1 #include "cache.h"
2 #include "config.h"
3 #include "diff.h"
4 #include "diff-merges.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "builtin.h"
8 #include "submodule.h"
10 static const char diff_cache_usage[] =
11 "git diff-index [-m] [--cached] [--merge-base] "
12 "[<common-diff-options>] <tree-ish> [<path>...]"
13 "\n"
14 COMMON_DIFF_OPTIONS_HELP;
16 int cmd_diff_index(int argc, const char **argv, const char *prefix)
18 struct rev_info rev;
19 unsigned int option = 0;
20 int i;
21 int result;
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);
28 rev.abbrev = 0;
29 prefix = precompose_argv_prefix(argc, argv, prefix);
32 * We need (some of) diff for merges options (e.g., --cc), and we need
33 * to avoid conflict with our own meaning of "-m".
35 diff_merges_suppress_m_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;
47 else
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)) {
63 setup_work_tree();
64 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
65 perror("repo_read_index_preload");
66 return -1;
68 } else if (repo_read_index(the_repository) < 0) {
69 perror("repo_read_index");
70 return -1;
72 result = run_diff_index(&rev, option);
73 result = diff_result_code(&rev.diffopt, result);
74 release_revisions(&rev);
75 return result;