1#include "cache.h"
2#include "builtin.h"
3#include "parse-options.h"
4#include "range-diff.h"
5#include "config.h"
6
7static const char * const builtin_range_diff_usage[] = {
8N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
9N_("git range-diff [<options>] <old-tip>...<new-tip>"),
10N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
11NULL
12};
13
14static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
15{
16 return data;
17}
18
19int cmd_range_diff(int argc, const char **argv, const char *prefix)
20{
21 int creation_factor = 60;
22 struct diff_options diffopt = { NULL };
23 int simple_color = -1;
24 struct option options[] = {
25 OPT_INTEGER(0, "creation-factor", &creation_factor,
26 N_("Percentage by which creation is weighted")),
27 OPT_BOOL(0, "no-dual-color", &simple_color,
28 N_("color both diff and diff-between-diffs")),
29 OPT_END()
30 };
31 int i, j, res = 0;
32 struct strbuf four_spaces = STRBUF_INIT;
33 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
34
35 git_config(git_diff_ui_config, NULL);
36
37 diff_setup(&diffopt);
38 diffopt.output_format = DIFF_FORMAT_PATCH;
39 diffopt.flags.suppress_diff_headers = 1;
40 diffopt.output_prefix = output_prefix_cb;
41 strbuf_addstr(&four_spaces, " ");
42 diffopt.output_prefix_data = &four_spaces;
43
44 argc = parse_options(argc, argv, NULL, options,
45 builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
46 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
47
48 for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
49 int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
50
51 if (!c)
52 argv[j++] = argv[i++];
53 else
54 i += c;
55 }
56 while (i < argc)
57 argv[j++] = argv[i++];
58 argc = j;
59 diff_setup_done(&diffopt);
60
61 /* Make sure that there are no unparsed options */
62 argc = parse_options(argc, argv, NULL,
63 options + ARRAY_SIZE(options) - 1, /* OPT_END */
64 builtin_range_diff_usage, 0);
65
66 if (simple_color < 1) {
67 if (!simple_color)
68 /* force color when --dual-color was used */
69 diffopt.use_color = 1;
70 diffopt.flags.dual_color_diffed_diffs = 1;
71 }
72
73 if (argc == 2) {
74 if (!strstr(argv[0], ".."))
75 die(_("no .. in range: '%s'"), argv[0]);
76 strbuf_addstr(&range1, argv[0]);
77
78 if (!strstr(argv[1], ".."))
79 die(_("no .. in range: '%s'"), argv[1]);
80 strbuf_addstr(&range2, argv[1]);
81 } else if (argc == 3) {
82 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
83 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
84 } else if (argc == 1) {
85 const char *b = strstr(argv[0], "..."), *a = argv[0];
86 int a_len;
87
88 if (!b) {
89 error(_("single arg format must be symmetric range"));
90 usage_with_options(builtin_range_diff_usage, options);
91 }
92
93 a_len = (int)(b - a);
94 if (!a_len) {
95 a = "HEAD";
96 a_len = strlen(a);
97 }
98 b += 3;
99 if (!*b)
100 b = "HEAD";
101 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
102 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
103 } else {
104 error(_("need two commit ranges"));
105 usage_with_options(builtin_range_diff_usage, options);
106 }
107
108 res = show_range_diff(range1.buf, range2.buf, creation_factor,
109 &diffopt);
110
111 strbuf_release(&range1);
112 strbuf_release(&range2);
113 strbuf_release(&four_spaces);
114
115 return res;
116}