t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / builtin / range-diff.c
blob1b33ab66a7b2013224f1c8f29854433d76776a0e
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "gettext.h"
4 #include "object-name.h"
5 #include "parse-options.h"
6 #include "range-diff.h"
7 #include "config.h"
10 static const char * const builtin_range_diff_usage[] = {
11 N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
12 N_("git range-diff [<options>] <old-tip>...<new-tip>"),
13 N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
14 NULL
17 int cmd_range_diff(int argc,
18 const char **argv,
19 const char *prefix,
20 struct repository *repo UNUSED)
22 struct diff_options diffopt = { NULL };
23 struct strvec other_arg = STRVEC_INIT;
24 struct range_diff_options range_diff_opts = {
25 .creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
26 .diffopt = &diffopt,
27 .other_arg = &other_arg
29 int simple_color = -1, left_only = 0, right_only = 0;
30 struct option range_diff_options[] = {
31 OPT_INTEGER(0, "creation-factor",
32 &range_diff_opts.creation_factor,
33 N_("percentage by which creation is weighted")),
34 OPT_BOOL(0, "no-dual-color", &simple_color,
35 N_("use simple diff colors")),
36 OPT_PASSTHRU_ARGV(0, "notes", &other_arg,
37 N_("notes"), N_("passed to 'git log'"),
38 PARSE_OPT_OPTARG),
39 OPT_BOOL(0, "left-only", &left_only,
40 N_("only emit output related to the first range")),
41 OPT_BOOL(0, "right-only", &right_only,
42 N_("only emit output related to the second range")),
43 OPT_END()
45 struct option *options;
46 int i, dash_dash = -1, res = 0;
47 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
48 struct object_id oid;
49 const char *three_dots = NULL;
51 git_config(git_diff_ui_config, NULL);
53 repo_diff_setup(the_repository, &diffopt);
55 options = add_diff_options(range_diff_options, &diffopt);
56 argc = parse_options(argc, argv, prefix, options,
57 builtin_range_diff_usage, PARSE_OPT_KEEP_DASHDASH);
59 diff_setup_done(&diffopt);
61 /* force color when --dual-color was used */
62 if (!simple_color)
63 diffopt.use_color = 1;
65 for (i = 0; i < argc; i++)
66 if (!strcmp(argv[i], "--")) {
67 dash_dash = i;
68 break;
71 if (dash_dash == 3 ||
72 (dash_dash < 0 && argc > 2 &&
73 !repo_get_oid_committish(the_repository, argv[0], &oid) &&
74 !repo_get_oid_committish(the_repository, argv[1], &oid) &&
75 !repo_get_oid_committish(the_repository, argv[2], &oid))) {
76 if (dash_dash < 0)
77 ; /* already validated arguments */
78 else if (repo_get_oid_committish(the_repository, argv[0], &oid))
79 usage_msg_optf(_("not a revision: '%s'"),
80 builtin_range_diff_usage, options,
81 argv[0]);
82 else if (repo_get_oid_committish(the_repository, argv[1], &oid))
83 usage_msg_optf(_("not a revision: '%s'"),
84 builtin_range_diff_usage, options,
85 argv[1]);
86 else if (repo_get_oid_committish(the_repository, argv[2], &oid))
87 usage_msg_optf(_("not a revision: '%s'"),
88 builtin_range_diff_usage, options,
89 argv[2]);
91 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
92 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
94 strvec_pushv(&other_arg, argv +
95 (dash_dash < 0 ? 3 : dash_dash));
96 } else if (dash_dash == 2 ||
97 (dash_dash < 0 && argc > 1 &&
98 is_range_diff_range(argv[0]) &&
99 is_range_diff_range(argv[1]))) {
100 if (dash_dash < 0)
101 ; /* already validated arguments */
102 else if (!is_range_diff_range(argv[0]))
103 usage_msg_optf(_("not a commit range: '%s'"),
104 builtin_range_diff_usage, options,
105 argv[0]);
106 else if (!is_range_diff_range(argv[1]))
107 usage_msg_optf(_("not a commit range: '%s'"),
108 builtin_range_diff_usage, options,
109 argv[1]);
111 strbuf_addstr(&range1, argv[0]);
112 strbuf_addstr(&range2, argv[1]);
114 strvec_pushv(&other_arg, argv +
115 (dash_dash < 0 ? 2 : dash_dash));
116 } else if (dash_dash == 1 ||
117 (dash_dash < 0 && argc > 0 &&
118 (three_dots = strstr(argv[0], "...")))) {
119 const char *a, *b;
120 int a_len;
122 if (dash_dash < 0)
123 ; /* already validated arguments */
124 else if (!(three_dots = strstr(argv[0], "...")))
125 usage_msg_optf(_("not a symmetric range: '%s'"),
126 builtin_range_diff_usage, options,
127 argv[0]);
129 if (three_dots == argv[0]) {
130 a = "HEAD";
131 a_len = strlen(a);
132 } else {
133 a = argv[0];
134 a_len = (int)(three_dots - a);
137 if (three_dots[3])
138 b = three_dots + 3;
139 else
140 b = "HEAD";
142 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
143 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
145 strvec_pushv(&other_arg, argv +
146 (dash_dash < 0 ? 1 : dash_dash));
147 } else
148 usage_msg_opt(_("need two commit ranges"),
149 builtin_range_diff_usage, options);
150 FREE_AND_NULL(options);
152 range_diff_opts.dual_color = simple_color < 1;
153 range_diff_opts.left_only = left_only;
154 range_diff_opts.right_only = right_only;
155 res = show_range_diff(range1.buf, range2.buf, &range_diff_opts);
157 strvec_clear(&other_arg);
158 strbuf_release(&range1);
159 strbuf_release(&range2);
161 return res;