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