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