builtin / range-diff.con commit range-diff: suppress the diff headers (1cdde29)
   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.flags.suppress_diff_headers = 1;
  37        diffopt.output_prefix = output_prefix_cb;
  38        strbuf_addstr(&four_spaces, "    ");
  39        diffopt.output_prefix_data = &four_spaces;
  40
  41        argc = parse_options(argc, argv, NULL, options,
  42                             builtin_range_diff_usage, PARSE_OPT_KEEP_UNKNOWN |
  43                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
  44
  45        for (i = j = 1; i < argc && strcmp("--", argv[i]); ) {
  46                int c = diff_opt_parse(&diffopt, argv + i, argc - i, prefix);
  47
  48                if (!c)
  49                        argv[j++] = argv[i++];
  50                else
  51                        i += c;
  52        }
  53        while (i < argc)
  54                argv[j++] = argv[i++];
  55        argc = j;
  56        diff_setup_done(&diffopt);
  57
  58        /* Make sure that there are no unparsed options */
  59        argc = parse_options(argc, argv, NULL,
  60                             options + ARRAY_SIZE(options) - 1, /* OPT_END */
  61                             builtin_range_diff_usage, 0);
  62
  63        if (argc == 2) {
  64                if (!strstr(argv[0], ".."))
  65                        die(_("no .. in range: '%s'"), argv[0]);
  66                strbuf_addstr(&range1, argv[0]);
  67
  68                if (!strstr(argv[1], ".."))
  69                        die(_("no .. in range: '%s'"), argv[1]);
  70                strbuf_addstr(&range2, argv[1]);
  71        } else if (argc == 3) {
  72                strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
  73                strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
  74        } else if (argc == 1) {
  75                const char *b = strstr(argv[0], "..."), *a = argv[0];
  76                int a_len;
  77
  78                if (!b) {
  79                        error(_("single arg format must be symmetric range"));
  80                        usage_with_options(builtin_range_diff_usage, options);
  81                }
  82
  83                a_len = (int)(b - a);
  84                if (!a_len) {
  85                        a = "HEAD";
  86                        a_len = strlen(a);
  87                }
  88                b += 3;
  89                if (!*b)
  90                        b = "HEAD";
  91                strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
  92                strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
  93        } else {
  94                error(_("need two commit ranges"));
  95                usage_with_options(builtin_range_diff_usage, options);
  96        }
  97
  98        res = show_range_diff(range1.buf, range2.buf, creation_factor,
  99                              &diffopt);
 100
 101        strbuf_release(&range1);
 102        strbuf_release(&range2);
 103        strbuf_release(&four_spaces);
 104
 105        return res;
 106}