range-diff: offer --left-only/--right-only options
[git] / builtin / range-diff.c
1 #include "cache.h"
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "range-diff.h"
5 #include "config.h"
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
14 int cmd_range_diff(int argc, const char **argv, const char *prefix)
15 {
16         struct diff_options diffopt = { NULL };
17         struct strvec other_arg = STRVEC_INIT;
18         struct range_diff_options range_diff_opts = {
19                 .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
20                 .diffopt = &diffopt,
21                 .other_arg = &other_arg
22         };
23         int simple_color = -1, left_only = 0, right_only = 0;
24         struct option range_diff_options[] = {
25                 OPT_INTEGER(0, "creation-factor",
26                             &range_diff_opts.creation_factor,
27                             N_("Percentage by which creation is weighted")),
28                 OPT_BOOL(0, "no-dual-color", &simple_color,
29                             N_("use simple diff colors")),
30                 OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
31                                   N_("notes"), N_("passed to 'git log'"),
32                                   PARSE_OPT_OPTARG),
33                 OPT_BOOL(0, "left-only", &left_only,
34                          N_("only emit output related to the first range")),
35                 OPT_BOOL(0, "right-only", &right_only,
36                          N_("only emit output related to the second range")),
37                 OPT_END()
38         };
39         struct option *options;
40         int res = 0;
41         struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
42
43         git_config(git_diff_ui_config, NULL);
44
45         repo_diff_setup(the_repository, &diffopt);
46
47         options = parse_options_concat(range_diff_options, diffopt.parseopts);
48         argc = parse_options(argc, argv, prefix, options,
49                              builtin_range_diff_usage, 0);
50
51         diff_setup_done(&diffopt);
52
53         /* force color when --dual-color was used */
54         if (!simple_color)
55                 diffopt.use_color = 1;
56
57         if (argc == 2) {
58                 if (!strstr(argv[0], ".."))
59                         die(_("no .. in range: '%s'"), argv[0]);
60                 strbuf_addstr(&range1, argv[0]);
61
62                 if (!strstr(argv[1], ".."))
63                         die(_("no .. in range: '%s'"), argv[1]);
64                 strbuf_addstr(&range2, argv[1]);
65         } else if (argc == 3) {
66                 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
67                 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
68         } else if (argc == 1) {
69                 const char *b = strstr(argv[0], "..."), *a = argv[0];
70                 int a_len;
71
72                 if (!b) {
73                         error(_("single arg format must be symmetric range"));
74                         usage_with_options(builtin_range_diff_usage, options);
75                 }
76
77                 a_len = (int)(b - a);
78                 if (!a_len) {
79                         a = "HEAD";
80                         a_len = strlen(a);
81                 }
82                 b += 3;
83                 if (!*b)
84                         b = "HEAD";
85                 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
86                 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
87         } else {
88                 error(_("need two commit ranges"));
89                 usage_with_options(builtin_range_diff_usage, options);
90         }
91         FREE_AND_NULL(options);
92
93         range_diff_opts.dual_color = simple_color < 1;
94         range_diff_opts.left_only = left_only;
95         range_diff_opts.right_only = right_only;
96         res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
97
98         strvec_clear(&other_arg);
99         strbuf_release(&range1);
100         strbuf_release(&range2);
101
102         return res;
103 }