]>
Commit | Line | Data |
---|---|---|
348ae56c JS |
1 | #include "cache.h" |
2 | #include "builtin.h" | |
f394e093 | 3 | #include "gettext.h" |
dabab1d6 | 4 | #include "object-name.h" |
348ae56c | 5 | #include "parse-options.h" |
d9c66f0b | 6 | #include "range-diff.h" |
c8c5e43a | 7 | #include "config.h" |
679b5916 | 8 | #include "revision.h" |
348ae56c JS |
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 | { | |
c8c5e43a | 19 | struct diff_options diffopt = { NULL }; |
22f9b7f3 | 20 | struct strvec other_arg = STRVEC_INIT; |
f1ce6c19 JS |
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 | }; | |
1e79f973 | 26 | int simple_color = -1, left_only = 0, right_only = 0; |
c380a48c | 27 | struct option range_diff_options[] = { |
f1ce6c19 JS |
28 | OPT_INTEGER(0, "creation-factor", |
29 | &range_diff_opts.creation_factor, | |
5ee90326 | 30 | N_("percentage by which creation is weighted")), |
27526793 | 31 | OPT_BOOL(0, "no-dual-color", &simple_color, |
72f47be2 | 32 | N_("use simple diff colors")), |
bd361918 DL |
33 | OPT_PASSTHRU_ARGV(0, "notes", &other_arg, |
34 | N_("notes"), N_("passed to 'git log'"), | |
35 | PARSE_OPT_OPTARG), | |
1e79f973 JS |
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")), | |
348ae56c JS |
40 | OPT_END() |
41 | }; | |
c380a48c | 42 | struct option *options; |
b7574782 | 43 | int i, dash_dash = -1, res = 0; |
d9c66f0b | 44 | struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT; |
0087d7df | 45 | struct object_id oid; |
b7574782 | 46 | const char *three_dots = NULL; |
348ae56c | 47 | |
c8c5e43a JS |
48 | git_config(git_diff_ui_config, NULL); |
49 | ||
e6757652 | 50 | repo_diff_setup(the_repository, &diffopt); |
c8c5e43a | 51 | |
c5630c48 | 52 | options = add_diff_options(range_diff_options, &diffopt); |
d64db5b3 | 53 | argc = parse_options(argc, argv, prefix, options, |
b7574782 | 54 | builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH); |
c8c5e43a | 55 | |
c8c5e43a JS |
56 | diff_setup_done(&diffopt); |
57 | ||
73a834e9 ES |
58 | /* force color when --dual-color was used */ |
59 | if (!simple_color) | |
60 | diffopt.use_color = 1; | |
31cf61a0 | 61 | |
b7574782 JS |
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 && | |
d850b7a5 ÆAB |
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))) { | |
b7574782 JS |
73 | if (dash_dash < 0) |
74 | ; /* already validated arguments */ | |
d850b7a5 | 75 | else if (repo_get_oid_committish(the_repository, argv[0], &oid)) |
0087d7df JS |
76 | usage_msg_optf(_("not a revision: '%s'"), |
77 | builtin_range_diff_usage, options, | |
78 | argv[0]); | |
d850b7a5 | 79 | else if (repo_get_oid_committish(the_repository, argv[1], &oid)) |
0087d7df JS |
80 | usage_msg_optf(_("not a revision: '%s'"), |
81 | builtin_range_diff_usage, options, | |
82 | argv[1]); | |
d850b7a5 | 83 | else if (repo_get_oid_committish(the_repository, argv[2], &oid)) |
0087d7df JS |
84 | usage_msg_optf(_("not a revision: '%s'"), |
85 | builtin_range_diff_usage, options, | |
86 | argv[2]); | |
87 | ||
edd6a31f JS |
88 | strbuf_addf(&range1, "%s..%s", argv[0], argv[1]); |
89 | strbuf_addf(&range2, "%s..%s", argv[0], argv[2]); | |
b7574782 JS |
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])) | |
0087d7df JS |
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]); | |
d9c66f0b | 107 | |
0087d7df | 108 | strbuf_addstr(&range1, argv[0]); |
d9c66f0b | 109 | strbuf_addstr(&range2, argv[1]); |
b7574782 JS |
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; | |
d9c66f0b JS |
117 | int a_len; |
118 | ||
b7574782 JS |
119 | if (dash_dash < 0) |
120 | ; /* already validated arguments */ | |
121 | else if (!(three_dots = strstr(argv[0], "..."))) | |
0087d7df | 122 | usage_msg_optf(_("not a symmetric range: '%s'"), |
b7574782 JS |
123 | builtin_range_diff_usage, options, |
124 | argv[0]); | |
d9c66f0b | 125 | |
b7574782 | 126 | if (three_dots == argv[0]) { |
d9c66f0b JS |
127 | a = "HEAD"; |
128 | a_len = strlen(a); | |
b7574782 JS |
129 | } else { |
130 | a = argv[0]; | |
131 | a_len = (int)(three_dots - a); | |
d9c66f0b | 132 | } |
b7574782 JS |
133 | |
134 | if (three_dots[3]) | |
135 | b = three_dots + 3; | |
136 | else | |
d9c66f0b | 137 | b = "HEAD"; |
b7574782 | 138 | |
d9c66f0b JS |
139 | strbuf_addf(&range1, "%s..%.*s", b, a_len, a); |
140 | strbuf_addf(&range2, "%.*s..%s", a_len, a, b); | |
b7574782 JS |
141 | |
142 | strvec_pushv(&other_arg, argv + | |
143 | (dash_dash < 0 ? 1 : dash_dash)); | |
0087d7df JS |
144 | } else |
145 | usage_msg_opt(_("need two commit ranges"), | |
146 | builtin_range_diff_usage, options); | |
c380a48c | 147 | FREE_AND_NULL(options); |
d9c66f0b | 148 | |
f1ce6c19 | 149 | range_diff_opts.dual_color = simple_color < 1; |
1e79f973 JS |
150 | range_diff_opts.left_only = left_only; |
151 | range_diff_opts.right_only = right_only; | |
f1ce6c19 | 152 | res = show_range_diff(range1.buf, range2.buf, &range_diff_opts); |
d9c66f0b | 153 | |
22f9b7f3 | 154 | strvec_clear(&other_arg); |
d9c66f0b JS |
155 | strbuf_release(&range1); |
156 | strbuf_release(&range2); | |
157 | ||
158 | return res; | |
348ae56c | 159 | } |