Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/diff-index.c
Line
Count
Source (jump to first uncovered line)
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
0
{
19
0
  struct rev_info rev;
20
0
  unsigned int option = 0;
21
0
  int i;
22
0
  int result;
23
24
0
  if (argc == 2 && !strcmp(argv[1], "-h"))
25
0
    usage(diff_cache_usage);
26
27
0
  git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
28
29
0
  prepare_repo_settings(the_repository);
30
0
  the_repository->settings.command_requires_full_index = 0;
31
32
0
  repo_init_revisions(the_repository, &rev, prefix);
33
0
  rev.abbrev = 0;
34
0
  prefix = precompose_argv_prefix(argc, argv, prefix);
35
36
  /*
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".
39
   */
40
0
  diff_merges_suppress_m_parsing();
41
42
0
  argc = setup_revisions(argc, argv, &rev, NULL);
43
0
  for (i = 1; i < argc; i++) {
44
0
    const char *arg = argv[i];
45
46
0
    if (!strcmp(arg, "--cached"))
47
0
      option |= DIFF_INDEX_CACHED;
48
0
    else if (!strcmp(arg, "--merge-base"))
49
0
      option |= DIFF_INDEX_MERGE_BASE;
50
0
    else if (!strcmp(arg, "-m"))
51
0
      rev.match_missing = 1;
52
0
    else
53
0
      usage(diff_cache_usage);
54
0
  }
55
0
  if (!rev.diffopt.output_format)
56
0
    rev.diffopt.output_format = DIFF_FORMAT_RAW;
57
58
0
  rev.diffopt.rotate_to_strict = 1;
59
60
  /*
61
   * Make sure there is one revision (i.e. pending object),
62
   * and there is no revision filtering parameters.
63
   */
64
0
  if (rev.pending.nr != 1 ||
65
0
      rev.max_count != -1 || rev.min_age != -1 || rev.max_age != -1)
66
0
    usage(diff_cache_usage);
67
0
  if (!(option & DIFF_INDEX_CACHED)) {
68
0
    setup_work_tree();
69
0
    if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
70
0
      perror("repo_read_index_preload");
71
0
      return -1;
72
0
    }
73
0
  } else if (repo_read_index(the_repository) < 0) {
74
0
    perror("repo_read_index");
75
0
    return -1;
76
0
  }
77
0
  run_diff_index(&rev, option);
78
0
  result = diff_result_code(&rev.diffopt);
79
0
  release_revisions(&rev);
80
0
  return result;
81
0
}