]>
Commit | Line | Data |
---|---|---|
348ae56c JS |
1 | #include "cache.h" |
2 | #include "builtin.h" | |
3 | #include "parse-options.h" | |
d9c66f0b | 4 | #include "range-diff.h" |
c8c5e43a | 5 | #include "config.h" |
348ae56c JS |
6 | |
7 | static const char * const builtin_range_diff_usage[] = { | |
8 | N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"), | |
9 | N_("git range-diff [<options>] <old-tip>...<new-tip>"), | |
10 | N_("git range-diff [<options>] <base> <old-tip> <new-tip>"), | |
11 | NULL | |
12 | }; | |
13 | ||
5e242e63 JS |
14 | static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data) |
15 | { | |
16 | return data; | |
17 | } | |
18 | ||
348ae56c JS |
19 | int cmd_range_diff(int argc, const char **argv, const char *prefix) |
20 | { | |
21 | int creation_factor = 60; | |
c8c5e43a | 22 | struct diff_options diffopt = { NULL }; |
27526793 | 23 | int simple_color = -1; |
348ae56c JS |
24 | struct option options[] = { |
25 | OPT_INTEGER(0, "creation-factor", &creation_factor, | |
26 | N_("Percentage by which creation is weighted")), | |
27526793 | 27 | OPT_BOOL(0, "no-dual-color", &simple_color, |
72f47be2 | 28 | N_("use simple diff colors")), |
348ae56c JS |
29 | OPT_END() |
30 | }; | |
c8c5e43a | 31 | int i, j, res = 0; |
5e242e63 | 32 | struct strbuf four_spaces = STRBUF_INIT; |
d9c66f0b | 33 | struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT; |
348ae56c | 34 | |
c8c5e43a JS |
35 | git_config(git_diff_ui_config, NULL); |
36 | ||
37 | diff_setup(&diffopt); | |
38 | diffopt.output_format = DIFF_FORMAT_PATCH; | |
1cdde296 | 39 | diffopt.flags.suppress_diff_headers = 1; |
5e242e63 JS |
40 | diffopt.output_prefix = output_prefix_cb; |
41 | strbuf_addstr(&four_spaces, " "); | |
42 | diffopt.output_prefix_data = &four_spaces; | |
c8c5e43a | 43 | |
348ae56c | 44 | argc = parse_options(argc, argv, NULL, options, |
c8c5e43a JS |
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 */ | |
348ae56c JS |
64 | builtin_range_diff_usage, 0); |
65 | ||
27526793 JS |
66 | if (simple_color < 1) { |
67 | if (!simple_color) | |
68 | /* force color when --dual-color was used */ | |
69 | diffopt.use_color = 1; | |
31cf61a0 JS |
70 | diffopt.flags.dual_color_diffed_diffs = 1; |
71 | } | |
72 | ||
d9c66f0b JS |
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 | ||
c8c5e43a JS |
108 | res = show_range_diff(range1.buf, range2.buf, creation_factor, |
109 | &diffopt); | |
d9c66f0b JS |
110 | |
111 | strbuf_release(&range1); | |
112 | strbuf_release(&range2); | |
5e242e63 | 113 | strbuf_release(&four_spaces); |
d9c66f0b JS |
114 | |
115 | return res; | |
348ae56c | 116 | } |