Git fork
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "builtin.h"
4#include "gettext.h"
5#include "object-name.h"
6#include "parse-options.h"
7#include "range-diff.h"
8#include "config.h"
9#include "parse.h"
10#include "color.h"
11
12
13static const char * const builtin_range_diff_usage[] = {
14N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
15N_("git range-diff [<options>] <old-tip>...<new-tip>"),
16N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
17NULL
18};
19
20static int parse_max_memory(const struct option *opt, const char *arg, int unset)
21{
22 size_t *max_memory = opt->value;
23 uintmax_t val;
24
25 if (unset)
26 return 0;
27
28 if (!git_parse_unsigned(arg, &val, SIZE_MAX))
29 return error(_("invalid max-memory value: %s"), arg);
30
31 *max_memory = (size_t)val;
32 return 0;
33}
34
35int cmd_range_diff(int argc,
36 const char **argv,
37 const char *prefix,
38 struct repository *repo UNUSED)
39{
40 struct diff_options diffopt = { NULL };
41 struct strvec log_arg = STRVEC_INIT;
42 struct strvec diff_merges_arg = STRVEC_INIT;
43 struct range_diff_options range_diff_opts = {
44 .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
45 .max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT,
46 .diffopt = &diffopt,
47 .log_arg = &log_arg
48 };
49 int simple_color = -1, left_only = 0, right_only = 0;
50 struct option range_diff_options[] = {
51 OPT_INTEGER(0, "creation-factor",
52 &range_diff_opts.creation_factor,
53 N_("percentage by which creation is weighted")),
54 OPT_BOOL(0, "no-dual-color", &simple_color,
55 N_("use simple diff colors")),
56 OPT_PASSTHRU_ARGV(0, "notes", &log_arg,
57 N_("notes"), N_("passed to 'git log'"),
58 PARSE_OPT_OPTARG),
59 OPT_PASSTHRU_ARGV(0, "diff-merges", &diff_merges_arg,
60 N_("style"), N_("passed to 'git log'"), 0),
61 OPT_CALLBACK(0, "max-memory", &range_diff_opts.max_memory,
62 N_("size"),
63 N_("maximum memory for cost matrix (default 4G)"),
64 parse_max_memory),
65 OPT_PASSTHRU_ARGV(0, "remerge-diff", &diff_merges_arg, NULL,
66 N_("passed to 'git log'"), PARSE_OPT_NOARG),
67 OPT_BOOL(0, "left-only", &left_only,
68 N_("only emit output related to the first range")),
69 OPT_BOOL(0, "right-only", &right_only,
70 N_("only emit output related to the second range")),
71 OPT_END()
72 };
73 struct option *options;
74 int i, dash_dash = -1, res = 0;
75 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
76 struct object_id oid;
77 const char *three_dots = NULL;
78
79 repo_config(the_repository, git_diff_ui_config, NULL);
80
81 repo_diff_setup(the_repository, &diffopt);
82
83 options = add_diff_options(range_diff_options, &diffopt);
84 argc = parse_options(argc, argv, prefix, options,
85 builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);
86
87 diff_setup_done(&diffopt);
88
89 /* force color when --dual-color was used */
90 if (!simple_color)
91 diffopt.use_color = GIT_COLOR_ALWAYS;
92
93 /* If `--diff-merges` was specified, imply `--merges` */
94 if (diff_merges_arg.nr) {
95 range_diff_opts.include_merges = 1;
96 strvec_pushv(&log_arg, diff_merges_arg.v);
97 }
98
99 for (i = 0; i < argc; i++)
100 if (!strcmp(argv[i], "--")) {
101 dash_dash = i;
102 break;
103 }
104
105 if (dash_dash == 3 ||
106 (dash_dash < 0 && argc > 2 &&
107 !repo_get_oid_committish(the_repository, argv[0], &oid) &&
108 !repo_get_oid_committish(the_repository, argv[1], &oid) &&
109 !repo_get_oid_committish(the_repository, argv[2], &oid))) {
110 if (dash_dash < 0)
111 ; /* already validated arguments */
112 else if (repo_get_oid_committish(the_repository, argv[0], &oid))
113 usage_msg_optf(_("not a revision: '%s'"),
114 builtin_range_diff_usage, options,
115 argv[0]);
116 else if (repo_get_oid_committish(the_repository, argv[1], &oid))
117 usage_msg_optf(_("not a revision: '%s'"),
118 builtin_range_diff_usage, options,
119 argv[1]);
120 else if (repo_get_oid_committish(the_repository, argv[2], &oid))
121 usage_msg_optf(_("not a revision: '%s'"),
122 builtin_range_diff_usage, options,
123 argv[2]);
124
125 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
126 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
127
128 strvec_pushv(&log_arg, argv +
129 (dash_dash < 0 ? 3 : dash_dash));
130 } else if (dash_dash == 2 ||
131 (dash_dash < 0 && argc > 1 &&
132 is_range_diff_range(argv[0]) &&
133 is_range_diff_range(argv[1]))) {
134 if (dash_dash < 0)
135 ; /* already validated arguments */
136 else if (!is_range_diff_range(argv[0]))
137 usage_msg_optf(_("not a commit range: '%s'"),
138 builtin_range_diff_usage, options,
139 argv[0]);
140 else if (!is_range_diff_range(argv[1]))
141 usage_msg_optf(_("not a commit range: '%s'"),
142 builtin_range_diff_usage, options,
143 argv[1]);
144
145 strbuf_addstr(&range1, argv[0]);
146 strbuf_addstr(&range2, argv[1]);
147
148 strvec_pushv(&log_arg, argv +
149 (dash_dash < 0 ? 2 : dash_dash));
150 } else if (dash_dash == 1 ||
151 (dash_dash < 0 && argc > 0 &&
152 (three_dots = strstr(argv[0], "...")))) {
153 const char *a, *b;
154 int a_len;
155
156 if (dash_dash < 0)
157 ; /* already validated arguments */
158 else if (!(three_dots = strstr(argv[0], "...")))
159 usage_msg_optf(_("not a symmetric range: '%s'"),
160 builtin_range_diff_usage, options,
161 argv[0]);
162
163 if (three_dots == argv[0]) {
164 a = "HEAD";
165 a_len = strlen(a);
166 } else {
167 a = argv[0];
168 a_len = (int)(three_dots - a);
169 }
170
171 if (three_dots[3])
172 b = three_dots + 3;
173 else
174 b = "HEAD";
175
176 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
177 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
178
179 strvec_pushv(&log_arg, argv +
180 (dash_dash < 0 ? 1 : dash_dash));
181 } else
182 usage_msg_opt(_("need two commit ranges"),
183 builtin_range_diff_usage, options);
184 FREE_AND_NULL(options);
185
186 range_diff_opts.dual_color = simple_color < 1;
187 range_diff_opts.left_only = left_only;
188 range_diff_opts.right_only = right_only;
189 res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
190
191 strvec_clear(&log_arg);
192 strvec_clear(&diff_merges_arg);
193 strbuf_release(&range1);
194 strbuf_release(&range2);
195
196 return res;
197}