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