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