Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/userdiff.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / userdiff.c` 609 lines ยท 19909 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "config.h" 6 #include "userdiff.h" 7 #include "attr.h" 8 #include "strbuf.h" 9 #include "environment.h" 10 11 static struct userdiff_driver *drivers; 12 static int ndrivers; 13 static int drivers_alloc; 14 15 #define PATTERNS(lang, rx, wrx) { \ 16 .name = lang, \ 17 .binary = -1, \ 18 .funcname = { \ 19 .pattern = rx, \ 20 .cflags = REG_EXTENDED, \ 21 }, \ 22 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ 23 .word_regex_multi_byte = wrx "|[^[:space:]]", \ 24 } 25 #define IPATTERN(lang, rx, wrx) { \ 26 .name = lang, \ 27 .binary = -1, \ 28 .funcname = { \ 29 .pattern = rx, \ 30 .cflags = REG_EXTENDED | REG_ICASE, \ 31 }, \ 32 .word_regex = wrx "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+", \ 33 .word_regex_multi_byte = wrx "|[^[:space:]]", \ 34 } 35 36 /* 37 * Built-in drivers for various languages, sorted by their names 38 * (except that the "default" is left at the end). 39 * 40 * When writing or updating patterns, assume that the contents these 41 * patterns are applied to are syntactically correct. The patterns 42 * can be simple without implementing all syntactical corner cases, as 43 * long as they are sufficiently permissive. 44 */ 45 static struct userdiff_driver builtin_drivers[] = { 46 IPATTERN("ada", 47 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" 48 "!^[ \t]*with[ \t].*$\n" 49 "^[ \t]*((procedure|function)[ \t]+.*)$\n" 50 "^[ \t]*((package|protected|task)[ \t]+.*)$", 51 /* -- */ 52 "[a-zA-Z][a-zA-Z0-9_]*" 53 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" 54 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), 55 PATTERNS("bash", 56 /* Optional leading indentation */ 57 "^[ \t]*" 58 /* Start of captured text */ 59 "(" 60 "(" 61 /* POSIX identifier with mandatory parentheses */ 62 "([a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))" 63 "|" 64 /* Bashism identifier with optional parentheses */ 65 "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+)))" 66 ")" 67 /* Everything after the function header is captured */ 68 ".*$" 69 /* End of captured text */ 70 ")", 71 /* -- */ 72 /* Identifiers: variable and function names */ 73 "[a-zA-Z_][a-zA-Z0-9_]*" 74 /* Shell variables: $VAR, ${VAR} */ 75 "|\\$[a-zA-Z0-9_]+|\\$\\{" 76 /*Command list separators and redirection operators */ 77 "|\\|\\||&&|<<|>>" 78 /* Operators ending in '=' (comparison + compound assignment) */ 79 "|==|!=|<=|>=|[-+*/%&|^]=" 80 /* Additional parameter expansion operators */ 81 "|:=|:-|:\\+|:\\?|##|%%|\\^\\^|,," 82 /* Command-line options (to avoid splitting -option) */ 83 "|[-a-zA-Z0-9_]+" 84 /* Brackets and grouping symbols */ 85 "|\\(|\\)|\\{|\\}|\\[|\\]"), 86 PATTERNS("bibtex", 87 "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", 88 /* -- */ 89 "[={}\"]|[^={}\" \t]+"), 90 PATTERNS("cpp", 91 /* Jump targets or access declarations */ 92 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" 93 /* functions/methods, variables, and compounds at top level */ 94 "^((::[[:space:]]*)?[A-Za-z_].*)$", 95 /* -- */ 96 /* identifiers and keywords */ 97 "[a-zA-Z_][a-zA-Z0-9_]*" 98 /* decimal and octal integers as well as floatingpoint numbers */ 99 "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*" 100 /* hexadecimal and binary integers */ 101 "|0[xXbB][0-9a-fA-F]+[lLuU]*" 102 /* floatingpoint numbers that begin with a decimal point */ 103 "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?" 104 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"), 105 PATTERNS("csharp", 106 /* 107 * Jump over reserved keywords which are illegal method names, but which 108 * can be followed by parentheses without special characters in between, 109 * making them look like methods. 110 */ 111 "!(^|[ \t]+)" /* Start of line or whitespace. */ 112 "(do|while|for|foreach|if|else|new|default|return|switch|case|throw" 113 "|catch|using|lock|fixed)" 114 "([ \t(]+|$)\n" /* Whitespace, "(", or end of line. */ 115 /* 116 * Methods/constructors: 117 * The strategy is to identify a minimum of two groups (any combination 118 * of keywords/type/name) before the opening parenthesis, and without 119 * final unexpected characters, normally only used in ordinary statements. 120 */ 121 "^[ \t]*" /* Remove leading whitespace. */ 122 "(" /* Start chunk header capture. */ 123 "(" /* First group. */ 124 "[][[:alnum:]@_.]" /* Name. */ 125 "(<[][[:alnum:]@_, \t<>]+>)?" /* Optional generic parameters. */ 126 ")+" 127 "([ \t]+" /* Subsequent groups, prepended with space. */ 128 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 129 ")+" 130 "[ \t]*" /* Optional space before parameters start. */ 131 "\\(" /* Start of method parameters. */ 132 "[^;]*" /* Allow complex parameters, but exclude statements (;). */ 133 ")$\n" /* Close chunk header capture. */ 134 /* 135 * Properties: 136 * As with methods, expect a minimum of two groups. But, more trivial than 137 * methods, the vast majority of properties long enough to be worth 138 * showing a chunk header for don't include "=:;,()" on the line they are 139 * defined, since they don't have a parameter list. 140 */ 141 "^[ \t]*(" 142 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 143 "([ \t]+" 144 "([][[:alnum:]@_.](<[][[:alnum:]@_, \t<>]+>)?)+" 145 ")+" /* Up to here, same as methods regex. */ 146 "[^;=:,()]*" /* Compared to methods, no parameter list allowed. */ 147 ")$\n" 148 /* Type definitions */ 149 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n" 150 /* Namespace */ 151 "^[ \t]*(namespace[ \t]+.*)$", 152 /* -- */ 153 "[a-zA-Z_][a-zA-Z0-9_]*" 154 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 155 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), 156 IPATTERN("css", 157 "![:;][[:space:]]*$\n" 158 "^[:[@.#]?[_a-z0-9].*$", 159 /* -- */ 160 /* 161 * This regex comes from W3C CSS specs. Should theoretically also 162 * allow ISO 10646 characters U+00A0 and higher, 163 * but they are not handled in this regex. 164 */ 165 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ 166 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ 167 ), 168 PATTERNS("dts", 169 "!;\n" 170 "!=\n" 171 /* lines beginning with a word optionally preceded by '&' or the root */ 172 "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", 173 /* -- */ 174 /* Property names and math operators */ 175 "[a-zA-Z0-9,._+?#-]+" 176 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), 177 PATTERNS("elixir", 178 "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", 179 /* -- */ 180 /* Atoms, names, and module attributes */ 181 "[@:]?[a-zA-Z0-9@_?!]+" 182 /* Numbers with specific base */ 183 "|[-+]?0[xob][0-9a-fA-F]+" 184 /* Numbers */ 185 "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" 186 /* Operators and atoms that represent them */ 187 "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^||<<?>>|<?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" 188 /* Not real operators, but should be grouped */ 189 "|:?%[A-Za-z0-9_.]\\{\\}?"), 190 IPATTERN("fortran", 191 /* Don't match comment lines */ 192 "!^([C*]|[ \t]*!)\n" 193 /* Don't match 'module procedure' lines */ 194 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" 195 /* Program, module, block data */ 196 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" 197 /* Subroutines and functions */ 198 "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", 199 /* -- */ 200 "[a-zA-Z][a-zA-Z0-9_]*" 201 "|\\.([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])\\." 202 /* numbers and format statements like 2E14.4, or ES12.6, 9X. 203 * Don't worry about format statements without leading digits since 204 * they would have been matched above as a variable anyway. */ 205 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" 206 "|//|\\*\\*|::|[/<>=]="), 207 IPATTERN("fountain", 208 "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", 209 /* -- */ 210 "[^ \t-]+"), 211 PATTERNS("golang", 212 /* Functions */ 213 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" 214 /* Structs and interfaces */ 215 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", 216 /* -- */ 217 "[a-zA-Z_][a-zA-Z0-9_]*" 218 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" 219 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), 220 PATTERNS("html", 221 "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", 222 /* -- */ 223 "[^<>= \t]+"), 224 PATTERNS("ini", 225 "^[ \t]*\\[[^]]+\\]", 226 /* -- */ 227 "[^ \t]+"), 228 PATTERNS("java", 229 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" 230 /* Class, enum, interface, and record declarations */ 231 "^[ \t]*(([a-z-]+[ \t]+)*(class|enum|interface|record)[ \t]+.*)$\n" 232 /* Method definitions; note that constructor signatures are not */ 233 /* matched because they are indistinguishable from method calls. */ 234 "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", 235 /* -- */ 236 "[a-zA-Z_][a-zA-Z0-9_]*" 237 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 238 "|[-+*/<>%&^|=!]=" 239 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), 240 PATTERNS("kotlin", 241 "^[ \t]*(([a-z]+[ \t]+)*(fun|class|interface)[ \t]+.*)$", 242 /* -- */ 243 "[a-zA-Z_][a-zA-Z0-9_]*" 244 /* hexadecimal and binary numbers */ 245 "|0[xXbB][0-9a-fA-F_]+[lLuU]*" 246 /* integers and floats */ 247 "|[0-9][0-9_]*([.][0-9_]*)?([Ee][-+]?[0-9]+)?[fFlLuU]*" 248 /* floating point numbers beginning with decimal point */ 249 "|[.][0-9][0-9_]*([Ee][-+]?[0-9]+)?[fFlLuU]?" 250 /* unary and binary operators */ 251 "|[-+*/<>%&^|=!]==?|--|\\+\\+|<<=|>>=|&&|\\|\\||->|\\.\\*|!!|[?:.][.:]"), 252 PATTERNS("markdown", 253 "^ {0,3}#{1,6}[ \t].*", 254 /* -- */ 255 "[^<>= \t]+"), 256 PATTERNS("matlab", 257 /* 258 * Octave pattern is mostly the same as matlab, except that '%%%' and 259 * '##' can also be used to begin code sections, in addition to '%%' 260 * that is understood by both. 261 */ 262 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", 263 /* -- */ 264 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), 265 PATTERNS("objc", 266 /* Negate C statements that can look like functions */ 267 "!^[ \t]*(do|for|if|else|return|switch|while)\n" 268 /* Objective-C methods */ 269 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" 270 /* C functions */ 271 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" 272 /* Objective-C class/protocol definitions */ 273 "^(@(implementation|interface|protocol)[ \t].*)$", 274 /* -- */ 275 "[a-zA-Z_][a-zA-Z0-9_]*" 276 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" 277 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), 278 PATTERNS("pascal", 279 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface" 280 "|implementation|initialization|finalization)[ \t]*.*)$\n" 281 "^(.*=[ \t]*(class|record).*)$", 282 /* -- */ 283 "[a-zA-Z_][a-zA-Z0-9_]*" 284 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" 285 "|<>|<=|>=|:=|\\.\\."), 286 PATTERNS("perl", 287 "^package .*\n" 288 "^sub [[:alnum:]_':]+[ \t]*" 289 "(\\([^)]*\\)[ \t]*)?" /* prototype */ 290 /* 291 * Attributes. A regex can't count nested parentheses, 292 * so just slurp up whatever we see, taking care not 293 * to accept lines like "sub foo; # defined elsewhere". 294 * 295 * An attribute could contain a semicolon, but at that 296 * point it seems reasonable enough to give up. 297 */ 298 "(:[^;#]*)?" 299 "(\\{[ \t]*)?" /* brace can come here or on the next line */ 300 "(#.*)?$\n" /* comment */ 301 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" 302 "(\\{[ \t]*)?" /* brace can come here or on the next line */ 303 "(#.*)?$\n" 304 "^=head[0-9] .*", /* POD */ 305 /* -- */ 306 "[[:alpha:]_'][[:alnum:]_']*" 307 "|0[xb]?[0-9a-fA-F_]*" 308 /* taking care not to interpret 3..5 as (3.)(.5) */ 309 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" 310 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" 311 "|&&=|\\|\\|=|//=|\\*\\*=" 312 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" 313 "|[-+*/%.^&<>=!|]=" 314 "|=~|!~" 315 "|<<|<>|<=>|>>"), 316 PATTERNS("php", 317 "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n" 318 "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$", 319 /* -- */ 320 "[a-zA-Z_][a-zA-Z0-9_]*" 321 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" 322 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), 323 PATTERNS("python", 324 "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", 325 /* -- */ 326 "[a-zA-Z_][a-zA-Z0-9_]*" 327 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" 328 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), 329 /* -- */ 330 PATTERNS("r", 331 "^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$", 332 /* -- */ 333 "[^ \t]+"), 334 PATTERNS("ruby", 335 "^[ \t]*((class|module|def)[ \t].*)$", 336 /* -- */ 337 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" 338 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." 339 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), 340 PATTERNS("rust", 341 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$", 342 /* -- */ 343 "[a-zA-Z_][a-zA-Z0-9_]*" 344 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" 345 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), 346 PATTERNS("scheme", 347 /* 348 * An unindented opening parenthesis identifies a top-level 349 * expression in all Lisp dialects. 350 */ 351 "^(\\(.*)$\n" 352 /* For Scheme: a possibly indented left paren followed by a keyword. */ 353 "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$\n" 354 /* 355 * For all Lisp dialects: a slightly indented line starting with "(def". 356 */ 357 "^ ?(\\([Dd][Ee][Ff].*)$", 358 /* 359 * The union of R7RS and Common Lisp symbol syntax: allows arbitrary 360 * strings between vertical bars, including any escaped characters. 361 */ 362 "\\|([^|\\\\]|\\\\.)*\\|" 363 /* All other words should be delimited by spaces or parentheses. */ 364 "|([^][)(}{ \t])+"), 365 PATTERNS("swift", 366 "^[ \t]*((@[A-Za-z_][A-Za-z0-9_]*(\\([^()]*\\))?[ \t]+)*([a-z]+[ \t]+)*(func|init|deinit|subscript|class|struct|enum|protocol|extension|actor)[ \t(?!<].*)$", 367 /* -- */ 368 "[a-zA-Z_][a-zA-Z0-9_]*" 369 /* hexadecimal, octal, and binary literals */ 370 "|0[xX][0-9a-fA-F_]+|0[oO][0-7_]+|0[bB][01_]+" 371 /* integers and floating-point numbers */ 372 "|[0-9][0-9_]*([.][0-9_]+)?([eE][-+]?[0-9]+)?" 373 /* unary and binary operators */ 374 "|[-+*/%<>=!&|^~?]=|&&|\\|\\||<<=?|>>=?|\\?\\?|\\.\\.[.<]|->"), 375 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", 376 "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"), 377 { .name = "default", .binary = -1 }, 378 }; 379 #undef PATTERNS 380 #undef IPATTERN 381 382 static struct userdiff_driver driver_true = { 383 .name = "diff=true", 384 .binary = 0, 385 }; 386 387 static struct userdiff_driver driver_false = { 388 .name = "!diff", 389 .binary = 1, 390 }; 391 392 struct find_by_namelen_data { 393 const char *name; 394 size_t len; 395 struct userdiff_driver *driver; 396 }; 397 398 static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, 399 enum userdiff_driver_type type UNUSED, 400 void *priv) 401 { 402 struct find_by_namelen_data *cb_data = priv; 403 404 if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) { 405 cb_data->driver = driver; 406 return 1; /* tell the caller to stop iterating */ 407 } 408 return 0; 409 } 410 411 static int regexec_supports_multi_byte_chars(void) 412 { 413 static const char not_space[] = "[^[:space:]]"; 414 static const char utf8_multi_byte_char[] = "\xc2\xa3"; 415 regex_t re; 416 regmatch_t match; 417 static int result = -1; 418 419 if (result != -1) 420 return result; 421 if (regcomp(&re, not_space, REG_EXTENDED)) 422 BUG("invalid regular expression: %s", not_space); 423 result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) && 424 match.rm_so == 0 && 425 match.rm_eo == strlen(utf8_multi_byte_char); 426 regfree(&re); 427 return result; 428 } 429 430 static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len) 431 { 432 struct find_by_namelen_data udcbdata = { 433 .name = name, 434 .len = len, 435 }; 436 for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata); 437 return udcbdata.driver; 438 } 439 440 static int parse_funcname(struct userdiff_funcname *f, const char *k, 441 const char *v, int cflags) 442 { 443 f->pattern = NULL; 444 FREE_AND_NULL(f->pattern_owned); 445 if (git_config_string(&f->pattern_owned, k, v) < 0) 446 return -1; 447 f->pattern = f->pattern_owned; 448 f->cflags = cflags; 449 return 0; 450 } 451 452 static int parse_tristate(int *b, const char *k, const char *v) 453 { 454 if (v && !strcasecmp(v, "auto")) 455 *b = -1; 456 else 457 *b = git_config_bool(k, v); 458 return 0; 459 } 460 461 static int parse_bool(int *b, const char *k, const char *v) 462 { 463 *b = git_config_bool(k, v); 464 return 0; 465 } 466 467 int userdiff_config(const char *k, const char *v) 468 { 469 struct userdiff_driver *drv; 470 const char *name, *type; 471 size_t namelen; 472 473 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) 474 return 0; 475 476 drv = userdiff_find_by_namelen(name, namelen); 477 if (!drv) { 478 ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); 479 drv = &drivers[ndrivers++]; 480 memset(drv, 0, sizeof(*drv)); 481 drv->name = xmemdupz(name, namelen); 482 drv->binary = -1; 483 } 484 485 if (!strcmp(type, "funcname")) 486 return parse_funcname(&drv->funcname, k, v, 0); 487 if (!strcmp(type, "xfuncname")) 488 return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); 489 if (!strcmp(type, "binary")) 490 return parse_tristate(&drv->binary, k, v); 491 if (!strcmp(type, "command")) { 492 FREE_AND_NULL(drv->external.cmd); 493 return git_config_string(&drv->external.cmd, k, v); 494 } 495 if (!strcmp(type, "trustexitcode")) { 496 drv->external.trust_exit_code = git_config_bool(k, v); 497 return 0; 498 } 499 if (!strcmp(type, "textconv")) { 500 int ret; 501 FREE_AND_NULL(drv->textconv_owned); 502 ret = git_config_string(&drv->textconv_owned, k, v); 503 drv->textconv = drv->textconv_owned; 504 return ret; 505 } 506 if (!strcmp(type, "cachetextconv")) 507 return parse_bool(&drv->textconv_want_cache, k, v); 508 if (!strcmp(type, "wordregex")) { 509 int ret; 510 FREE_AND_NULL(drv->word_regex_owned); 511 ret = git_config_string(&drv->word_regex_owned, k, v); 512 drv->word_regex = drv->word_regex_owned; 513 return ret; 514 } 515 if (!strcmp(type, "algorithm")) { 516 int ret; 517 FREE_AND_NULL(drv->algorithm_owned); 518 ret = git_config_string(&drv->algorithm_owned, k, v); 519 drv->algorithm = drv->algorithm_owned; 520 return ret; 521 } 522 523 return 0; 524 } 525 526 struct userdiff_driver *userdiff_find_by_name(const char *name) 527 { 528 int len = strlen(name); 529 struct userdiff_driver *driver = userdiff_find_by_namelen(name, len); 530 if (driver && driver->word_regex_multi_byte) { 531 if (regexec_supports_multi_byte_chars()) 532 driver->word_regex = driver->word_regex_multi_byte; 533 driver->word_regex_multi_byte = NULL; 534 } 535 return driver; 536 } 537 538 struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, 539 const char *path) 540 { 541 static struct attr_check *check; 542 543 if (!check) 544 check = attr_check_initl("diff", NULL); 545 if (!path) 546 return NULL; 547 git_check_attr(istate, path, check); 548 549 if (ATTR_TRUE(check->items[0].value)) 550 return &driver_true; 551 if (ATTR_FALSE(check->items[0].value)) 552 return &driver_false; 553 if (ATTR_UNSET(check->items[0].value)) 554 return NULL; 555 return userdiff_find_by_name(check->items[0].value); 556 } 557 558 struct userdiff_driver *userdiff_get_textconv(struct repository *r, 559 struct userdiff_driver *driver) 560 { 561 if (!driver->textconv) 562 return NULL; 563 564 if (driver->textconv_want_cache && !driver->textconv_cache && 565 have_git_dir()) { 566 struct notes_cache *c = xmalloc(sizeof(*c)); 567 struct strbuf name = STRBUF_INIT; 568 569 strbuf_addf(&name, "textconv/%s", driver->name); 570 notes_cache_init(r, c, name.buf, driver->textconv); 571 driver->textconv_cache = c; 572 strbuf_release(&name); 573 } 574 575 return driver; 576 } 577 578 static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn, 579 enum userdiff_driver_type type, void *cb_data, 580 struct userdiff_driver *drv, 581 int drv_size) 582 { 583 int i; 584 int ret; 585 for (i = 0; i < drv_size; i++) { 586 struct userdiff_driver *item = drv + i; 587 if ((ret = fn(item, type, cb_data))) 588 return ret; 589 } 590 return 0; 591 } 592 593 int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data) 594 { 595 int ret; 596 597 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM, 598 cb_data, drivers, ndrivers); 599 if (ret) 600 return ret; 601 602 ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN, 603 cb_data, builtin_drivers, 604 ARRAY_SIZE(builtin_drivers)); 605 if (ret) 606 return ret; 607 608 return 0; 609 }