userdiff.con commit object: convert create_object() to use object_id (a378509)
   1#include "cache.h"
   2#include "config.h"
   3#include "userdiff.h"
   4#include "attr.h"
   5
   6static struct userdiff_driver *drivers;
   7static int ndrivers;
   8static int drivers_alloc;
   9
  10#define PATTERNS(name, pattern, word_regex)                     \
  11        { name, NULL, -1, { pattern, REG_EXTENDED },            \
  12          word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
  13#define IPATTERN(name, pattern, word_regex)                     \
  14        { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \
  15          word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" }
  16static struct userdiff_driver builtin_drivers[] = {
  17IPATTERN("ada",
  18         "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
  19         "!^[ \t]*with[ \t].*$\n"
  20         "^[ \t]*((procedure|function)[ \t]+.*)$\n"
  21         "^[ \t]*((package|protected|task)[ \t]+.*)$",
  22         /* -- */
  23         "[a-zA-Z][a-zA-Z0-9_]*"
  24         "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
  25         "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
  26IPATTERN("fortran",
  27         "!^([C*]|[ \t]*!)\n"
  28         "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
  29         "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
  30                "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
  31         /* -- */
  32         "[a-zA-Z][a-zA-Z0-9_]*"
  33         "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
  34         /* numbers and format statements like 2E14.4, or ES12.6, 9X.
  35          * Don't worry about format statements without leading digits since
  36          * they would have been matched above as a variable anyway. */
  37         "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
  38         "|//|\\*\\*|::|[/<>=]="),
  39IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
  40         "[^ \t-]+"),
  41PATTERNS("golang",
  42         /* Functions */
  43         "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
  44         /* Structs and interfaces */
  45         "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
  46         /* -- */
  47         "[a-zA-Z_][a-zA-Z0-9_]*"
  48         "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
  49         "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
  50PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
  51         "[^<>= \t]+"),
  52PATTERNS("java",
  53         "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
  54         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
  55         /* -- */
  56         "[a-zA-Z_][a-zA-Z0-9_]*"
  57         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  58         "|[-+*/<>%&^|=!]="
  59         "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
  60PATTERNS("matlab",
  61         /*
  62          * Octave pattern is mostly the same as matlab, except that '%%%' and
  63          * '##' can also be used to begin code sections, in addition to '%%'
  64          * that is understood by both.
  65          */
  66         "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
  67         "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
  68PATTERNS("objc",
  69         /* Negate C statements that can look like functions */
  70         "!^[ \t]*(do|for|if|else|return|switch|while)\n"
  71         /* Objective-C methods */
  72         "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
  73         /* C functions */
  74         "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
  75         /* Objective-C class/protocol definitions */
  76         "^(@(implementation|interface|protocol)[ \t].*)$",
  77         /* -- */
  78         "[a-zA-Z_][a-zA-Z0-9_]*"
  79         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
  80         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
  81PATTERNS("pascal",
  82         "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
  83                "implementation|initialization|finalization)[ \t]*.*)$"
  84         "\n"
  85         "^(.*=[ \t]*(class|record).*)$",
  86         /* -- */
  87         "[a-zA-Z_][a-zA-Z0-9_]*"
  88         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
  89         "|<>|<=|>=|:=|\\.\\."),
  90PATTERNS("perl",
  91         "^package .*\n"
  92         "^sub [[:alnum:]_':]+[ \t]*"
  93                "(\\([^)]*\\)[ \t]*)?" /* prototype */
  94                /*
  95                 * Attributes.  A regex can't count nested parentheses,
  96                 * so just slurp up whatever we see, taking care not
  97                 * to accept lines like "sub foo; # defined elsewhere".
  98                 *
  99                 * An attribute could contain a semicolon, but at that
 100                 * point it seems reasonable enough to give up.
 101                 */
 102                "(:[^;#]*)?"
 103                "(\\{[ \t]*)?" /* brace can come here or on the next line */
 104                "(#.*)?$\n" /* comment */
 105         "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
 106                "(\\{[ \t]*)?" /* brace can come here or on the next line */
 107                "(#.*)?$\n"
 108         "^=head[0-9] .*",      /* POD */
 109         /* -- */
 110         "[[:alpha:]_'][[:alnum:]_']*"
 111         "|0[xb]?[0-9a-fA-F_]*"
 112         /* taking care not to interpret 3..5 as (3.)(.5) */
 113         "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
 114         "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
 115         "|&&=|\\|\\|=|//=|\\*\\*="
 116         "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
 117         "|[-+*/%.^&<>=!|]="
 118         "|=~|!~"
 119         "|<<|<>|<=>|>>"),
 120PATTERNS("php",
 121         "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
 122         "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
 123         /* -- */
 124         "[a-zA-Z_][a-zA-Z0-9_]*"
 125         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
 126         "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
 127PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
 128         /* -- */
 129         "[a-zA-Z_][a-zA-Z0-9_]*"
 130         "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 131         "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 132         /* -- */
 133PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
 134         /* -- */
 135         "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
 136         "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
 137         "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
 138PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
 139         "[={}\"]|[^={}\" \t]+"),
 140PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
 141         "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
 142PATTERNS("cpp",
 143         /* Jump targets or access declarations */
 144         "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
 145         /* functions/methods, variables, and compounds at top level */
 146         "^((::[[:space:]]*)?[A-Za-z_].*)$",
 147         /* -- */
 148         "[a-zA-Z_][a-zA-Z0-9_]*"
 149         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
 150         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
 151PATTERNS("csharp",
 152         /* Keywords */
 153         "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
 154         /* Methods and constructors */
 155         "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
 156         /* Properties */
 157         "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
 158         /* Type definitions */
 159         "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
 160         /* Namespace */
 161         "^[ \t]*(namespace[ \t]+.*)$",
 162         /* -- */
 163         "[a-zA-Z_][a-zA-Z0-9_]*"
 164         "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
 165         "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
 166IPATTERN("css",
 167         "![:;][[:space:]]*$\n"
 168         "^[_a-z0-9].*$",
 169         /* -- */
 170         /*
 171          * This regex comes from W3C CSS specs. Should theoretically also
 172          * allow ISO 10646 characters U+00A0 and higher,
 173          * but they are not handled in this regex.
 174          */
 175         "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
 176         "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
 177),
 178{ "default", NULL, -1, { NULL, 0 } },
 179};
 180#undef PATTERNS
 181#undef IPATTERN
 182
 183static struct userdiff_driver driver_true = {
 184        "diff=true",
 185        NULL,
 186        0,
 187        { NULL, 0 }
 188};
 189
 190static struct userdiff_driver driver_false = {
 191        "!diff",
 192        NULL,
 193        1,
 194        { NULL, 0 }
 195};
 196
 197static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
 198{
 199        int i;
 200        for (i = 0; i < ndrivers; i++) {
 201                struct userdiff_driver *drv = drivers + i;
 202                if (!strncmp(drv->name, k, len) && !drv->name[len])
 203                        return drv;
 204        }
 205        for (i = 0; i < ARRAY_SIZE(builtin_drivers); i++) {
 206                struct userdiff_driver *drv = builtin_drivers + i;
 207                if (!strncmp(drv->name, k, len) && !drv->name[len])
 208                        return drv;
 209        }
 210        return NULL;
 211}
 212
 213static int parse_funcname(struct userdiff_funcname *f, const char *k,
 214                const char *v, int cflags)
 215{
 216        if (git_config_string(&f->pattern, k, v) < 0)
 217                return -1;
 218        f->cflags = cflags;
 219        return 0;
 220}
 221
 222static int parse_tristate(int *b, const char *k, const char *v)
 223{
 224        if (v && !strcasecmp(v, "auto"))
 225                *b = -1;
 226        else
 227                *b = git_config_bool(k, v);
 228        return 0;
 229}
 230
 231static int parse_bool(int *b, const char *k, const char *v)
 232{
 233        *b = git_config_bool(k, v);
 234        return 0;
 235}
 236
 237int userdiff_config(const char *k, const char *v)
 238{
 239        struct userdiff_driver *drv;
 240        const char *name, *type;
 241        int namelen;
 242
 243        if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
 244                return 0;
 245
 246        drv = userdiff_find_by_namelen(name, namelen);
 247        if (!drv) {
 248                ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
 249                drv = &drivers[ndrivers++];
 250                memset(drv, 0, sizeof(*drv));
 251                drv->name = xmemdupz(name, namelen);
 252                drv->binary = -1;
 253        }
 254
 255        if (!strcmp(type, "funcname"))
 256                return parse_funcname(&drv->funcname, k, v, 0);
 257        if (!strcmp(type, "xfuncname"))
 258                return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
 259        if (!strcmp(type, "binary"))
 260                return parse_tristate(&drv->binary, k, v);
 261        if (!strcmp(type, "command"))
 262                return git_config_string(&drv->external, k, v);
 263        if (!strcmp(type, "textconv"))
 264                return git_config_string(&drv->textconv, k, v);
 265        if (!strcmp(type, "cachetextconv"))
 266                return parse_bool(&drv->textconv_want_cache, k, v);
 267        if (!strcmp(type, "wordregex"))
 268                return git_config_string(&drv->word_regex, k, v);
 269
 270        return 0;
 271}
 272
 273struct userdiff_driver *userdiff_find_by_name(const char *name)
 274{
 275        int len = strlen(name);
 276        return userdiff_find_by_namelen(name, len);
 277}
 278
 279struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
 280                                              const char *path)
 281{
 282        static struct attr_check *check;
 283
 284        if (!check)
 285                check = attr_check_initl("diff", NULL);
 286        if (!path)
 287                return NULL;
 288        git_check_attr(istate, path, check);
 289
 290        if (ATTR_TRUE(check->items[0].value))
 291                return &driver_true;
 292        if (ATTR_FALSE(check->items[0].value))
 293                return &driver_false;
 294        if (ATTR_UNSET(check->items[0].value))
 295                return NULL;
 296        return userdiff_find_by_name(check->items[0].value);
 297}
 298
 299struct userdiff_driver *userdiff_get_textconv(struct repository *r,
 300                                              struct userdiff_driver *driver)
 301{
 302        if (!driver->textconv)
 303                return NULL;
 304
 305        if (driver->textconv_want_cache && !driver->textconv_cache) {
 306                struct notes_cache *c = xmalloc(sizeof(*c));
 307                struct strbuf name = STRBUF_INIT;
 308
 309                strbuf_addf(&name, "textconv/%s", driver->name);
 310                notes_cache_init(r, c, name.buf, driver->textconv);
 311                driver->textconv_cache = c;
 312                strbuf_release(&name);
 313        }
 314
 315        return driver;
 316}