userdiff.con commit git-revert documentation: refer to new HOWTO on reverting faulty merges (b80b5d6)
   1#include "userdiff.h"
   2#include "cache.h"
   3#include "attr.h"
   4
   5static struct userdiff_driver *drivers;
   6static int ndrivers;
   7static int drivers_alloc;
   8
   9#define FUNCNAME(name, pattern) \
  10        { name, NULL, -1, { pattern, REG_EXTENDED } }
  11static struct userdiff_driver builtin_drivers[] = {
  12FUNCNAME("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$"),
  13FUNCNAME("java",
  14         "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
  15         "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$"),
  16FUNCNAME("objc",
  17         /* Negate C statements that can look like functions */
  18         "!^[ \t]*(do|for|if|else|return|switch|while)\n"
  19         /* Objective-C methods */
  20         "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
  21         /* C functions */
  22         "^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\\([^;]*)$\n"
  23         /* Objective-C class/protocol definitions */
  24         "^(@(implementation|interface|protocol)[ \t].*)$"),
  25FUNCNAME("pascal",
  26         "^((procedure|function|constructor|destructor|interface|"
  27                "implementation|initialization|finalization)[ \t]*.*)$"
  28         "\n"
  29         "^(.*=[ \t]*(class|record).*)$"),
  30FUNCNAME("php", "^[\t ]*((function|class).*)"),
  31FUNCNAME("python", "^[ \t]*((class|def)[ \t].*)$"),
  32FUNCNAME("ruby", "^[ \t]*((class|module|def)[ \t].*)$"),
  33FUNCNAME("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$"),
  34FUNCNAME("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$"),
  35{ "default", NULL, -1, { NULL, 0 } },
  36};
  37#undef FUNCNAME
  38
  39static struct userdiff_driver driver_true = {
  40        "diff=true",
  41        NULL,
  42        0,
  43        { NULL, 0 }
  44};
  45
  46static struct userdiff_driver driver_false = {
  47        "!diff",
  48        NULL,
  49        1,
  50        { NULL, 0 }
  51};
  52
  53static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
  54{
  55        int i;
  56        for (i = 0; i < ndrivers; i++) {
  57                struct userdiff_driver *drv = drivers + i;
  58                if (!strncmp(drv->name, k, len) && !drv->name[len])
  59                        return drv;
  60        }
  61        for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
  62                struct userdiff_driver *drv = builtin_drivers + i;
  63                if (!strncmp(drv->name, k, len) && !drv->name[len])
  64                        return drv;
  65        }
  66        return NULL;
  67}
  68
  69static struct userdiff_driver *parse_driver(const char *var,
  70                const char *value, const char *type)
  71{
  72        struct userdiff_driver *drv;
  73        const char *dot;
  74        const char *name;
  75        int namelen;
  76
  77        if (prefixcmp(var, "diff."))
  78                return NULL;
  79        dot = strrchr(var, '.');
  80        if (dot == var + 4)
  81                return NULL;
  82        if (strcmp(type, dot+1))
  83                return NULL;
  84
  85        name = var + 5;
  86        namelen = dot - name;
  87        drv = userdiff_find_by_namelen(name, namelen);
  88        if (!drv) {
  89                ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
  90                drv = &drivers[ndrivers++];
  91                memset(drv, 0, sizeof(*drv));
  92                drv->name = xmemdupz(name, namelen);
  93                drv->binary = -1;
  94        }
  95        return drv;
  96}
  97
  98static int parse_funcname(struct userdiff_funcname *f, const char *k,
  99                const char *v, int cflags)
 100{
 101        if (git_config_string(&f->pattern, k, v) < 0)
 102                return -1;
 103        f->cflags = cflags;
 104        return 1;
 105}
 106
 107static int parse_string(const char **d, const char *k, const char *v)
 108{
 109        if (git_config_string(d, k, v) < 0)
 110                return -1;
 111        return 1;
 112}
 113
 114static int parse_tristate(int *b, const char *k, const char *v)
 115{
 116        if (v && !strcasecmp(v, "auto"))
 117                *b = -1;
 118        else
 119                *b = git_config_bool(k, v);
 120        return 1;
 121}
 122
 123int userdiff_config(const char *k, const char *v)
 124{
 125        struct userdiff_driver *drv;
 126
 127        if ((drv = parse_driver(k, v, "funcname")))
 128                return parse_funcname(&drv->funcname, k, v, 0);
 129        if ((drv = parse_driver(k, v, "xfuncname")))
 130                return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
 131        if ((drv = parse_driver(k, v, "binary")))
 132                return parse_tristate(&drv->binary, k, v);
 133        if ((drv = parse_driver(k, v, "command")))
 134                return parse_string(&drv->external, k, v);
 135        if ((drv = parse_driver(k, v, "textconv")))
 136                return parse_string(&drv->textconv, k, v);
 137
 138        return 0;
 139}
 140
 141struct userdiff_driver *userdiff_find_by_name(const char *name) {
 142        int len = strlen(name);
 143        return userdiff_find_by_namelen(name, len);
 144}
 145
 146struct userdiff_driver *userdiff_find_by_path(const char *path)
 147{
 148        static struct git_attr *attr;
 149        struct git_attr_check check;
 150
 151        if (!attr)
 152                attr = git_attr("diff", 4);
 153        check.attr = attr;
 154
 155        if (!path)
 156                return NULL;
 157        if (git_checkattr(path, 1, &check))
 158                return NULL;
 159
 160        if (ATTR_TRUE(check.value))
 161                return &driver_true;
 162        if (ATTR_FALSE(check.value))
 163                return &driver_false;
 164        if (ATTR_UNSET(check.value))
 165                return NULL;
 166        return userdiff_find_by_name(check.value);
 167}