]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/range-diff.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / builtin / range-diff.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "gettext.h"
4 #include "object-name.h"
5 #include "parse-options.h"
6 #include "range-diff.h"
7 #include "config.h"
8 #include "revision.h"
9
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
15 };
16
17 int cmd_range_diff(int argc, const char **argv, const char *prefix)
18 {
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
25 };
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()
41 };
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;
47
48 git_config(git_diff_ui_config, NULL);
49
50 repo_diff_setup(the_repository, &diffopt);
51
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);
55
56 diff_setup_done(&diffopt);
57
58 /* force color when --dual-color was used */
59 if (!simple_color)
60 diffopt.use_color = 1;
61
62 for (i = 0; i < argc; i++)
63 if (!strcmp(argv[i], "--")) {
64 dash_dash = i;
65 break;
66 }
67
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]);
87
88 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
89 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
90
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]);
107
108 strbuf_addstr(&range1, argv[0]);
109 strbuf_addstr(&range2, argv[1]);
110
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;
118
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]);
125
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);
132 }
133
134 if (three_dots[3])
135 b = three_dots + 3;
136 else
137 b = "HEAD";
138
139 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
140 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
141
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);
148
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);
153
154 strvec_clear(&other_arg);
155 strbuf_release(&range1);
156 strbuf_release(&range2);
157
158 return res;
159 }