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