Merge branch 'js/mingw-getcwd'
[git.git] / builtin / range-diff.c
blobf01a0be8513412789ca49e28b0cc2519b7c04760
1 #include "cache.h"
2 #include "builtin.h"
3 #include "parse-options.h"
4 #include "range-diff.h"
5 #include "config.h"
7 static const char * const builtin_range_diff_usage[] = {
8 N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
9 N_("git range-diff [<options>] <old-tip>...<new-tip>"),
10 N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
11 NULL
14 int cmd_range_diff(int argc, const char **argv, const char *prefix)
16 int creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT;
17 struct diff_options diffopt = { NULL };
18 int simple_color = -1;
19 struct option options[] = {
20 OPT_INTEGER(0, "creation-factor", &creation_factor,
21 N_("Percentage by which creation is weighted")),
22 OPT_BOOL(0, "no-dual-color", &simple_color,
23 N_("use simple diff colors")),
24 OPT_END()
26 int i, j, res = 0;
27 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
29 git_config(git_diff_ui_config, NULL);
31 repo_diff_setup(the_repository, &diffopt);
33 argc = parse_options(argc, argv, NULL, options,
34 builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
35 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
37 for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
38 int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
40 if (!c)
41 argv[j++] = argv[i++];
42 else
43 i += c;
45 while (i < argc)
46 argv[j++] = argv[i++];
47 argc = j;
48 diff_setup_done(&diffopt);
50 /* Make sure that there are no unparsed options */
51 argc = parse_options(argc, argv, NULL,
52 options + ARRAY_SIZE(options) - 1, /* OPT_END */
53 builtin_range_diff_usage, 0);
55 /* force color when --dual-color was used */
56 if (!simple_color)
57 diffopt.use_color = 1;
59 if (argc == 2) {
60 if (!strstr(argv[0], ".."))
61 die(_("no .. in range: '%s'"), argv[0]);
62 strbuf_addstr(&range1, argv[0]);
64 if (!strstr(argv[1], ".."))
65 die(_("no .. in range: '%s'"), argv[1]);
66 strbuf_addstr(&range2, argv[1]);
67 } else if (argc == 3) {
68 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
69 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
70 } else if (argc == 1) {
71 const char *b = strstr(argv[0], "..."), *a = argv[0];
72 int a_len;
74 if (!b) {
75 error(_("single arg format must be symmetric range"));
76 usage_with_options(builtin_range_diff_usage, options);
79 a_len = (int)(b - a);
80 if (!a_len) {
81 a = "HEAD";
82 a_len = strlen(a);
84 b += 3;
85 if (!*b)
86 b = "HEAD";
87 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
88 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
89 } else {
90 error(_("need two commit ranges"));
91 usage_with_options(builtin_range_diff_usage, options);
94 res = show_range_diff(range1.buf, range2.buf, creation_factor,
95 simple_color < 1, &diffopt);
97 strbuf_release(&range1);
98 strbuf_release(&range2);
100 return res;