]>
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, | |
348ae56c | 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 NTND |
40 | struct option *options; |
41 | int res = 0; | |
d9c66f0b | 42 | struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT; |
348ae56c | 43 | |
c8c5e43a JS |
44 | git_config(git_diff_ui_config, NULL); |
45 | ||
e6757652 | 46 | repo_diff_setup(the_repository, &diffopt); |
c8c5e43a | 47 | |
c380a48c | 48 | options = parse_options_concat(range_diff_options, diffopt.parseopts); |
d64db5b3 | 49 | argc = parse_options(argc, argv, prefix, options, |
c380a48c | 50 | builtin_range_diff_usage, 0); |
c8c5e43a | 51 | |
c8c5e43a JS |
52 | diff_setup_done(&diffopt); |
53 | ||
73a834e9 ES |
54 | /* force color when --dual-color was used */ |
55 | if (!simple_color) | |
56 | diffopt.use_color = 1; | |
31cf61a0 | 57 | |
d9c66f0b | 58 | if (argc == 2) { |
679b5916 JS |
59 | if (!is_range_diff_range(argv[0])) |
60 | die(_("not a commit range: '%s'"), argv[0]); | |
d9c66f0b JS |
61 | strbuf_addstr(&range1, argv[0]); |
62 | ||
679b5916 JS |
63 | if (!is_range_diff_range(argv[1])) |
64 | die(_("not a commit range: '%s'"), argv[1]); | |
d9c66f0b JS |
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; | |
72 | ||
73 | if (!b) { | |
74 | error(_("single arg format must be symmetric range")); | |
75 | usage_with_options(builtin_range_diff_usage, options); | |
76 | } | |
77 | ||
78 | a_len = (int)(b - a); | |
79 | if (!a_len) { | |
80 | a = "HEAD"; | |
81 | a_len = strlen(a); | |
82 | } | |
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); | |
91 | } | |
c380a48c | 92 | FREE_AND_NULL(options); |
d9c66f0b | 93 | |
f1ce6c19 | 94 | range_diff_opts.dual_color = simple_color < 1; |
1e79f973 JS |
95 | range_diff_opts.left_only = left_only; |
96 | range_diff_opts.right_only = right_only; | |
f1ce6c19 | 97 | res = show_range_diff(range1.buf, range2.buf, &range_diff_opts); |
d9c66f0b | 98 | |
22f9b7f3 | 99 | strvec_clear(&other_arg); |
d9c66f0b JS |
100 | strbuf_release(&range1); |
101 | strbuf_release(&range2); | |
102 | ||
103 | return res; | |
348ae56c | 104 | } |