]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/range-diff.c
range-diff: also show the diff between patches
[thirdparty/git.git] / builtin / range-diff.c
CommitLineData
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"
348ae56c
JS
6
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{
16 int creation_factor = 60;
c8c5e43a 17 struct diff_options diffopt = { NULL };
348ae56c
JS
18 struct option options[] = {
19 OPT_INTEGER(0, "creation-factor", &creation_factor,
20 N_("Percentage by which creation is weighted")),
21 OPT_END()
22 };
c8c5e43a 23 int i, j, res = 0;
d9c66f0b 24 struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
348ae56c 25
c8c5e43a
JS
26 git_config(git_diff_ui_config, NULL);
27
28 diff_setup(&diffopt);
29 diffopt.output_format = DIFF_FORMAT_PATCH;
30
348ae56c 31 argc = parse_options(argc, argv, NULL, options,
c8c5e43a
JS
32 builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
33 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
34
35 for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
36 int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
37
38 if (!c)
39 argv[j++] = argv[i++];
40 else
41 i += c;
42 }
43 while (i < argc)
44 argv[j++] = argv[i++];
45 argc = j;
46 diff_setup_done(&diffopt);
47
48 /* Make sure that there are no unparsed options */
49 argc = parse_options(argc, argv, NULL,
50 options + ARRAY_SIZE(options) - 1, /* OPT_END */
348ae56c
JS
51 builtin_range_diff_usage, 0);
52
d9c66f0b
JS
53 if (argc == 2) {
54 if (!strstr(argv[0], ".."))
55 die(_("no .. in range: '%s'"), argv[0]);
56 strbuf_addstr(&range1, argv[0]);
57
58 if (!strstr(argv[1], ".."))
59 die(_("no .. in range: '%s'"), argv[1]);
60 strbuf_addstr(&range2, argv[1]);
61 } else if (argc == 3) {
62 strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
63 strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
64 } else if (argc == 1) {
65 const char *b = strstr(argv[0], "..."), *a = argv[0];
66 int a_len;
67
68 if (!b) {
69 error(_("single arg format must be symmetric range"));
70 usage_with_options(builtin_range_diff_usage, options);
71 }
72
73 a_len = (int)(b - a);
74 if (!a_len) {
75 a = "HEAD";
76 a_len = strlen(a);
77 }
78 b += 3;
79 if (!*b)
80 b = "HEAD";
81 strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
82 strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
83 } else {
84 error(_("need two commit ranges"));
85 usage_with_options(builtin_range_diff_usage, options);
86 }
87
c8c5e43a
JS
88 res = show_range_diff(range1.buf, range2.buf, creation_factor,
89 &diffopt);
d9c66f0b
JS
90
91 strbuf_release(&range1);
92 strbuf_release(&range2);
93
94 return res;
348ae56c 95}