Coverage Report

Created: 2024-09-08 06:23

/src/git/builtin/range-diff.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "gettext.h"
3
#include "object-name.h"
4
#include "parse-options.h"
5
#include "range-diff.h"
6
#include "config.h"
7
#include "repository.h"
8
9
static const char * const builtin_range_diff_usage[] = {
10
N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
11
N_("git range-diff [<options>] <old-tip>...<new-tip>"),
12
N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
13
NULL
14
};
15
16
int cmd_range_diff(int argc, const char **argv, const char *prefix)
17
0
{
18
0
  struct diff_options diffopt = { NULL };
19
0
  struct strvec other_arg = STRVEC_INIT;
20
0
  struct range_diff_options range_diff_opts = {
21
0
    .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
22
0
    .diffopt = &diffopt,
23
0
    .other_arg = &other_arg
24
0
  };
25
0
  int simple_color = -1, left_only = 0, right_only = 0;
26
0
  struct option range_diff_options[] = {
27
0
    OPT_INTEGER(0, "creation-factor",
28
0
          &range_diff_opts.creation_factor,
29
0
          N_("percentage by which creation is weighted")),
30
0
    OPT_BOOL(0, "no-dual-color", &simple_color,
31
0
          N_("use simple diff colors")),
32
0
    OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
33
0
          N_("notes"), N_("passed to 'git log'"),
34
0
          PARSE_OPT_OPTARG),
35
0
    OPT_BOOL(0, "left-only", &left_only,
36
0
       N_("only emit output related to the first range")),
37
0
    OPT_BOOL(0, "right-only", &right_only,
38
0
       N_("only emit output related to the second range")),
39
0
    OPT_END()
40
0
  };
41
0
  struct option *options;
42
0
  int i, dash_dash = -1, res = 0;
43
0
  struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
44
0
  struct object_id oid;
45
0
  const char *three_dots = NULL;
46
47
0
  git_config(git_diff_ui_config, NULL);
48
49
0
  repo_diff_setup(the_repository, &diffopt);
50
51
0
  options = add_diff_options(range_diff_options, &diffopt);
52
0
  argc = parse_options(argc, argv, prefix, options,
53
0
           builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);
54
55
0
  diff_setup_done(&diffopt);
56
57
  /* force color when --dual-color was used */
58
0
  if (!simple_color)
59
0
    diffopt.use_color = 1;
60
61
0
  for (i = 0; i < argc; i++)
62
0
    if (!strcmp(argv[i], "--")) {
63
0
      dash_dash = i;
64
0
      break;
65
0
    }
66
67
0
  if (dash_dash == 3 ||
68
0
      (dash_dash < 0 && argc > 2 &&
69
0
       !repo_get_oid_committish(the_repository, argv[0], &oid) &&
70
0
       !repo_get_oid_committish(the_repository, argv[1], &oid) &&
71
0
       !repo_get_oid_committish(the_repository, argv[2], &oid))) {
72
0
    if (dash_dash < 0)
73
0
      ; /* already validated arguments */
74
0
    else if (repo_get_oid_committish(the_repository, argv[0], &oid))
75
0
      usage_msg_optf(_("not a revision: '%s'"),
76
0
               builtin_range_diff_usage, options,
77
0
               argv[0]);
78
0
    else if (repo_get_oid_committish(the_repository, argv[1], &oid))
79
0
      usage_msg_optf(_("not a revision: '%s'"),
80
0
               builtin_range_diff_usage, options,
81
0
               argv[1]);
82
0
    else if (repo_get_oid_committish(the_repository, argv[2], &oid))
83
0
      usage_msg_optf(_("not a revision: '%s'"),
84
0
               builtin_range_diff_usage, options,
85
0
               argv[2]);
86
87
0
    strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
88
0
    strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
89
90
0
    strvec_pushv(&other_arg, argv +
91
0
           (dash_dash < 0 ? 3 : dash_dash));
92
0
  } else if (dash_dash == 2 ||
93
0
       (dash_dash < 0 && argc > 1 &&
94
0
        is_range_diff_range(argv[0]) &&
95
0
        is_range_diff_range(argv[1]))) {
96
0
    if (dash_dash < 0)
97
0
      ; /* already validated arguments */
98
0
    else if (!is_range_diff_range(argv[0]))
99
0
      usage_msg_optf(_("not a commit range: '%s'"),
100
0
               builtin_range_diff_usage, options,
101
0
               argv[0]);
102
0
    else if (!is_range_diff_range(argv[1]))
103
0
      usage_msg_optf(_("not a commit range: '%s'"),
104
0
               builtin_range_diff_usage, options,
105
0
               argv[1]);
106
107
0
    strbuf_addstr(&range1, argv[0]);
108
0
    strbuf_addstr(&range2, argv[1]);
109
110
0
    strvec_pushv(&other_arg, argv +
111
0
           (dash_dash < 0 ? 2 : dash_dash));
112
0
  } else if (dash_dash == 1 ||
113
0
       (dash_dash < 0 && argc > 0 &&
114
0
        (three_dots = strstr(argv[0], "...")))) {
115
0
    const char *a, *b;
116
0
    int a_len;
117
118
0
    if (dash_dash < 0)
119
0
      ; /* already validated arguments */
120
0
    else if (!(three_dots = strstr(argv[0], "...")))
121
0
      usage_msg_optf(_("not a symmetric range: '%s'"),
122
0
           builtin_range_diff_usage, options,
123
0
           argv[0]);
124
125
0
    if (three_dots == argv[0]) {
126
0
      a = "HEAD";
127
0
      a_len = strlen(a);
128
0
    } else {
129
0
      a = argv[0];
130
0
      a_len = (int)(three_dots - a);
131
0
    }
132
133
0
    if (three_dots[3])
134
0
      b = three_dots + 3;
135
0
    else
136
0
      b = "HEAD";
137
138
0
    strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
139
0
    strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
140
141
0
    strvec_pushv(&other_arg, argv +
142
0
           (dash_dash < 0 ? 1 : dash_dash));
143
0
  } else
144
0
    usage_msg_opt(_("need two commit ranges"),
145
0
            builtin_range_diff_usage, options);
146
0
  FREE_AND_NULL(options);
147
148
0
  range_diff_opts.dual_color = simple_color < 1;
149
0
  range_diff_opts.left_only = left_only;
150
0
  range_diff_opts.right_only = right_only;
151
0
  res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
152
153
0
  strvec_clear(&other_arg);
154
0
  strbuf_release(&range1);
155
0
  strbuf_release(&range2);
156
157
0
  return res;
158
0
}