builtin / range-diff.con commit range-diff: offer to dual-color the diffs (31cf61a)
   1#include "cache.h"
   2#include "builtin.h"
   3#include "parse-options.h"
   4#include "range-diff.h"
   5#include "config.h"
   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
  14static struct strbuf *output_prefix_cb(struct diff_options *opt, void *data)
  15{
  16        return data;
  17}
  18
  19int cmd_range_diff(int argc, const char **argv, const char *prefix)
  20{
  21        int creation_factor = 60;
  22        struct diff_options diffopt = { NULL };
  23        int dual_color = 0;
  24        struct option options[] = {
  25                OPT_INTEGER(0, "creation-factor", &creation_factor,
  26                            N_("Percentage by which creation is weighted")),
  27                OPT_BOOL(0, "dual-color", &dual_color,
  28                            N_("color both diff and diff-between-diffs")),
  29                OPT_END()
  30        };
  31        int i, j, res = 0;
  32        struct strbuf four_spaces = STRBUF_INIT;
  33        struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
  34
  35        git_config(git_diff_ui_config, NULL);
  36
  37        diff_setup(&diffopt);
  38        diffopt.output_format = DIFF_FORMAT_PATCH;
  39        diffopt.flags.suppress_diff_headers = 1;
  40        diffopt.output_prefix = output_prefix_cb;
  41        strbuf_addstr(&four_spaces, "    ");
  42        diffopt.output_prefix_data = &four_spaces;
  43
  44        argc = parse_options(argc, argv, NULL, options,
  45                             builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
  46                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
  47
  48        for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
  49                int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
  50
  51                if (!c)
  52                        argv[j++] = argv[i++];
  53                else
  54                        i += c;
  55        }
  56        while (i < argc)
  57                argv[j++] = argv[i++];
  58        argc = j;
  59        diff_setup_done(&diffopt);
  60
  61        /* Make sure that there are no unparsed options */
  62        argc = parse_options(argc, argv, NULL,
  63                             options + ARRAY_SIZE(options) - 1, /* OPT_END */
  64                             builtin_range_diff_usage, 0);
  65
  66        if (dual_color) {
  67                diffopt.use_color = 1;
  68                diffopt.flags.dual_color_diffed_diffs = 1;
  69        }
  70
  71        if (argc == 2) {
  72                if (!strstr(argv[0], ".."))
  73                        die(_("no .. in range: '%s'"), argv[0]);
  74                strbuf_addstr(&range1, argv[0]);
  75
  76                if (!strstr(argv[1], ".."))
  77                        die(_("no .. in range: '%s'"), argv[1]);
  78                strbuf_addstr(&range2, argv[1]);
  79        } else if (argc == 3) {
  80                strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
  81                strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
  82        } else if (argc == 1) {
  83                const char *b = strstr(argv[0], "..."), *a = argv[0];
  84                int a_len;
  85
  86                if (!b) {
  87                        error(_("single arg format must be symmetric range"));
  88                        usage_with_options(builtin_range_diff_usage, options);
  89                }
  90
  91                a_len = (int)(b - a);
  92                if (!a_len) {
  93                        a = "HEAD";
  94                        a_len = strlen(a);
  95                }
  96                b += 3;
  97                if (!*b)
  98                        b = "HEAD";
  99                strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
 100                strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
 101        } else {
 102                error(_("need two commit ranges"));
 103                usage_with_options(builtin_range_diff_usage, options);
 104        }
 105
 106        res = show_range_diff(range1.buf, range2.buf, creation_factor,
 107                              &diffopt);
 108
 109        strbuf_release(&range1);
 110        strbuf_release(&range2);
 111        strbuf_release(&four_spaces);
 112
 113        return res;
 114}