builtin / range-diff.con commit range-diff: first rudimentary implementation (d9c66f0)
   1#include "cache.h"
   2#include "builtin.h"
   3#include "parse-options.h"
   4#include "range-diff.h"
   5
   6static const char * const builtin_range_diff_usage[] = {
   7N_("git range-diff [<options>] <old-base>..<old-tip> <new-base>..<new-tip>"),
   8N_("git range-diff [<options>] <old-tip>...<new-tip>"),
   9N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
  10NULL
  11};
  12
  13int cmd_range_diff(int argc, const char **argv, const char *prefix)
  14{
  15        int creation_factor = 60;
  16        struct option options[] = {
  17                OPT_INTEGER(0, "creation-factor", &creation_factor,
  18                            N_("Percentage by which creation is weighted")),
  19                OPT_END()
  20        };
  21        int res = 0;
  22        struct strbuf range1 = STRBUF_INIT, range2 = STRBUF_INIT;
  23
  24        argc = parse_options(argc, argv, NULL, options,
  25                             builtin_range_diff_usage, 0);
  26
  27        if (argc == 2) {
  28                if (!strstr(argv[0], ".."))
  29                        die(_("no .. in range: '%s'"), argv[0]);
  30                strbuf_addstr(&range1, argv[0]);
  31
  32                if (!strstr(argv[1], ".."))
  33                        die(_("no .. in range: '%s'"), argv[1]);
  34                strbuf_addstr(&range2, argv[1]);
  35        } else if (argc == 3) {
  36                strbuf_addf(&range1, "%s..%s", argv[0], argv[1]);
  37                strbuf_addf(&range2, "%s..%s", argv[0], argv[2]);
  38        } else if (argc == 1) {
  39                const char *b = strstr(argv[0], "..."), *a = argv[0];
  40                int a_len;
  41
  42                if (!b) {
  43                        error(_("single arg format must be symmetric range"));
  44                        usage_with_options(builtin_range_diff_usage, options);
  45                }
  46
  47                a_len = (int)(b - a);
  48                if (!a_len) {
  49                        a = "HEAD";
  50                        a_len = strlen(a);
  51                }
  52                b += 3;
  53                if (!*b)
  54                        b = "HEAD";
  55                strbuf_addf(&range1, "%s..%.*s", b, a_len, a);
  56                strbuf_addf(&range2, "%.*s..%s", a_len, a, b);
  57        } else {
  58                error(_("need two commit ranges"));
  59                usage_with_options(builtin_range_diff_usage, options);
  60        }
  61
  62        res = show_range_diff(range1.buf, range2.buf, creation_factor);
  63
  64        strbuf_release(&range1);
  65        strbuf_release(&range2);
  66
  67        return res;
  68}