Merge branch 'ab/diff-no-index-tests'
[alt-git.git] / builtin / range-diff.c
blob78bc9fa770624adc3d08857d1b5004e500ead2cb
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"
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
15 int cmd_range_diff(int argc, const char **argv, const char *prefix)
17 struct diff_options diffopt = { NULL };
18 struct strvec other_arg = STRVEC_INIT;
19 struct range_diff_options range_diff_opts = {
20 .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
21 .diffopt = &diffopt,
22 .other_arg = &other_arg
24 int simple_color = -1, left_only = 0, right_only = 0;
25 struct option range_diff_options[] = {
26 OPT_INTEGER(0, "creation-factor",
27 &range_diff_opts.creation_factor,
28 N_("Percentage by which creation is weighted")),
29 OPT_BOOL(0, "no-dual-color", &simple_color,
30 N_("use simple diff colors")),
31 OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
32 N_("notes"), N_("passed to 'git log'"),
33 PARSE_OPT_OPTARG),
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")),
38 OPT_END()
40 struct option *options;
41 int res = 0;
42 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
44 git_config(git_diff_ui_config, NULL);
46 repo_diff_setup(the_repository, &diffopt);
48 options = parse_options_concat(range_diff_options, diffopt.parseopts);
49 argc = parse_options(argc, argv, prefix, options,
50 builtin_range_diff_usage, 0);
52 diff_setup_done(&diffopt);
54 /* force color when --dual-color was used */
55 if (!simple_color)
56 diffopt.use_color = 1;
58 if (argc == 2) {
59 if (!is_range_diff_range(argv[0]))
60 die(_("not a commit range: '%s'"), argv[0]);
61 strbuf_addstr(&range1, argv[0]);
63 if (!is_range_diff_range(argv[1]))
64 die(_("not a commit range: '%s'"), argv[1]);
65 strbuf_addstr(&range2, argv[1]);
66 } else if (argc == 3) {
67 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
68 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
69 } else if (argc == 1) {
70 const char *b = strstr(argv[0], "..."), *a = argv[0];
71 int a_len;
73 if (!b) {
74 error(_("single arg format must be symmetric range"));
75 usage_with_options(builtin_range_diff_usage, options);
78 a_len = (int)(b - a);
79 if (!a_len) {
80 a = "HEAD";
81 a_len = strlen(a);
83 b += 3;
84 if (!*b)
85 b = "HEAD";
86 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
87 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
88 } else {
89 error(_("need two commit ranges"));
90 usage_with_options(builtin_range_diff_usage, options);
92 FREE_AND_NULL(options);
94 range_diff_opts.dual_color = simple_color < 1;
95 range_diff_opts.left_only = left_only;
96 range_diff_opts.right_only = right_only;
97 res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
99 strvec_clear(&other_arg);
100 strbuf_release(&range1);
101 strbuf_release(&range2);
103 return res;