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