blob: 9202e75544761f274d3e360a3dc609fff294d0bb [file] [log] [blame]
Johannes Schindelin348ae562018-08-13 04:33:02 -07001#include "cache.h"
2#include "builtin.h"
3#include "parse-options.h"
Johannes Schindelind9c66f02018-08-13 04:33:04 -07004#include "range-diff.h"
Johannes Schindelinc8c5e432018-08-13 04:33:07 -07005#include "config.h"
Johannes Schindelin348ae562018-08-13 04:33:02 -07006
7static const char * const builtin_range_diff_usage[] = {
8N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
9N_("git range-diff [<options>] <old-tip>...<new-tip>"),
10N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
11NULL
12};
13
14int cmd_range_diff(int argc, const char **argv, const char *prefix)
15{
Eric Sunshine25668652018-07-22 05:57:11 -040016 int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
Johannes Schindelinc8c5e432018-08-13 04:33:07 -070017 struct diff_options diffopt = { NULL };
Johannes Schindelin27526792018-08-13 04:33:30 -070018 int simple_color = -1;
Nguyễn Thái Ngọc Duyc380a482019-03-24 15:20:12 +070019 struct option range_diff_options[] = {
Johannes Schindelin348ae562018-08-13 04:33:02 -070020 OPT_INTEGER(0, "creation-factor", &creation_factor,
21 N_("Percentage by which creation is weighted")),
Johannes Schindelin27526792018-08-13 04:33:30 -070022 OPT_BOOL(0, "no-dual-color", &simple_color,
Kyle Meyer72f47be2018-08-23 17:57:48 -040023 N_("use simple diff colors")),
Johannes Schindelin348ae562018-08-13 04:33:02 -070024 OPT_END()
25 };
Nguyễn Thái Ngọc Duyc380a482019-03-24 15:20:12 +070026 struct option *options;
27 int res = 0;
Johannes Schindelind9c66f02018-08-13 04:33:04 -070028 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
Johannes Schindelin348ae562018-08-13 04:33:02 -070029
Johannes Schindelinc8c5e432018-08-13 04:33:07 -070030 git_config(git_diff_ui_config, NULL);
31
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +020032 repo_diff_setup(the_repository, &diffopt);
Johannes Schindelinc8c5e432018-08-13 04:33:07 -070033
Nguyễn Thái Ngọc Duyc380a482019-03-24 15:20:12 +070034 options = parse_options_concat(range_diff_options, diffopt.parseopts);
Jeff Kingd64db5b2019-05-09 17:28:51 -040035 argc = parse_options(argc, argv, prefix, options,
Johannes Schindelin348ae562018-08-13 04:33:02 -070036 builtin_range_diff_usage, 0);
37
Nguyễn Thái Ngọc Duyc380a482019-03-24 15:20:12 +070038 diff_setup_done(&diffopt);
39
Eric Sunshine73a834e2018-07-22 05:57:12 -040040 /* force color when --dual-color was used */
41 if (!simple_color)
42 diffopt.use_color = 1;
Johannes Schindelin31cf61a2018-08-13 04:33:22 -070043
Johannes Schindelind9c66f02018-08-13 04:33:04 -070044 if (argc == 2) {
45 if (!strstr(argv[0], ".."))
46 die(_("no .. in range: '%s'"), argv[0]);
47 strbuf_addstr(&range1, argv[0]);
48
49 if (!strstr(argv[1], ".."))
50 die(_("no .. in range: '%s'"), argv[1]);
51 strbuf_addstr(&range2, argv[1]);
52 } else if (argc == 3) {
53 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
54 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
55 } else if (argc == 1) {
56 const char *b = strstr(argv[0], "..."), *a = argv[0];
57 int a_len;
58
59 if (!b) {
60 error(_("single arg format must be symmetric range"));
61 usage_with_options(builtin_range_diff_usage, options);
62 }
63
64 a_len = (int)(b - a);
65 if (!a_len) {
66 a = "HEAD";
67 a_len = strlen(a);
68 }
69 b += 3;
70 if (!*b)
71 b = "HEAD";
72 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
73 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
74 } else {
75 error(_("need two commit ranges"));
76 usage_with_options(builtin_range_diff_usage, options);
77 }
Nguyễn Thái Ngọc Duyc380a482019-03-24 15:20:12 +070078 FREE_AND_NULL(options);
Johannes Schindelind9c66f02018-08-13 04:33:04 -070079
Johannes Schindelinc8c5e432018-08-13 04:33:07 -070080 res = show_range_diff(range1.buf, range2.buf, creation_factor,
Eric Sunshine73a834e2018-07-22 05:57:12 -040081 simple_color < 1, &diffopt);
Johannes Schindelind9c66f02018-08-13 04:33:04 -070082
83 strbuf_release(&range1);
84 strbuf_release(&range2);
85
86 return res;
Johannes Schindelin348ae562018-08-13 04:33:02 -070087}