Coverage Report

Created: 2026-01-10 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/userdiff.c
Line
Count
Source
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
   "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$",
348
   /*
349
    * R7RS valid identifiers include any sequence enclosed
350
    * within vertical lines having no backslashes
351
    */
352
   "\\|([^\\\\]*)\\|"
353
   /* All other words should be delimited by spaces or parentheses */
354
   "|([^][)(}{[ \t])+"),
355
PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
356
   "\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
357
{ .name = "default", .binary = -1 },
358
};
359
#undef PATTERNS
360
#undef IPATTERN
361
362
static struct userdiff_driver driver_true = {
363
  .name = "diff=true",
364
  .binary = 0,
365
};
366
367
static struct userdiff_driver driver_false = {
368
  .name = "!diff",
369
  .binary = 1,
370
};
371
372
struct find_by_namelen_data {
373
  const char *name;
374
  size_t len;
375
  struct userdiff_driver *driver;
376
};
377
378
static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver,
379
               enum userdiff_driver_type type UNUSED,
380
               void *priv)
381
0
{
382
0
  struct find_by_namelen_data *cb_data = priv;
383
384
0
  if (!xstrncmpz(driver->name, cb_data->name, cb_data->len)) {
385
0
    cb_data->driver = driver;
386
0
    return 1; /* tell the caller to stop iterating */
387
0
  }
388
0
  return 0;
389
0
}
390
391
static int regexec_supports_multi_byte_chars(void)
392
0
{
393
0
  static const char not_space[] = "[^[:space:]]";
394
0
  static const char utf8_multi_byte_char[] = "\xc2\xa3";
395
0
  regex_t re;
396
0
  regmatch_t match;
397
0
  static int result = -1;
398
399
0
  if (result != -1)
400
0
    return result;
401
0
  if (regcomp(&re, not_space, REG_EXTENDED))
402
0
    BUG("invalid regular expression: %s", not_space);
403
0
  result = !regexec(&re, utf8_multi_byte_char, 1, &match, 0) &&
404
0
    match.rm_so == 0 &&
405
0
    match.rm_eo == strlen(utf8_multi_byte_char);
406
0
  regfree(&re);
407
0
  return result;
408
0
}
409
410
static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len)
411
0
{
412
0
  struct find_by_namelen_data udcbdata = {
413
0
    .name = name,
414
0
    .len = len,
415
0
  };
416
0
  for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata);
417
0
  return udcbdata.driver;
418
0
}
419
420
static int parse_funcname(struct userdiff_funcname *f, const char *k,
421
    const char *v, int cflags)
422
0
{
423
0
  f->pattern = NULL;
424
0
  FREE_AND_NULL(f->pattern_owned);
425
0
  if (git_config_string(&f->pattern_owned, k, v) < 0)
426
0
    return -1;
427
0
  f->pattern = f->pattern_owned;
428
0
  f->cflags = cflags;
429
0
  return 0;
430
0
}
431
432
static int parse_tristate(int *b, const char *k, const char *v)
433
0
{
434
0
  if (v && !strcasecmp(v, "auto"))
435
0
    *b = -1;
436
0
  else
437
0
    *b = git_config_bool(k, v);
438
0
  return 0;
439
0
}
440
441
static int parse_bool(int *b, const char *k, const char *v)
442
0
{
443
0
  *b = git_config_bool(k, v);
444
0
  return 0;
445
0
}
446
447
int userdiff_config(const char *k, const char *v)
448
0
{
449
0
  struct userdiff_driver *drv;
450
0
  const char *name, *type;
451
0
  size_t namelen;
452
453
0
  if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
454
0
    return 0;
455
456
0
  drv = userdiff_find_by_namelen(name, namelen);
457
0
  if (!drv) {
458
0
    ALLOC_GROW(drivers, ndrivers+1, drivers_alloc);
459
0
    drv = &drivers[ndrivers++];
460
0
    memset(drv, 0, sizeof(*drv));
461
0
    drv->name = xmemdupz(name, namelen);
462
0
    drv->binary = -1;
463
0
  }
464
465
0
  if (!strcmp(type, "funcname"))
466
0
    return parse_funcname(&drv->funcname, k, v, 0);
467
0
  if (!strcmp(type, "xfuncname"))
468
0
    return parse_funcname(&drv->funcname, k, v, REG_EXTENDED);
469
0
  if (!strcmp(type, "binary"))
470
0
    return parse_tristate(&drv->binary, k, v);
471
0
  if (!strcmp(type, "command")) {
472
0
    FREE_AND_NULL(drv->external.cmd);
473
0
    return git_config_string(&drv->external.cmd, k, v);
474
0
  }
475
0
  if (!strcmp(type, "trustexitcode")) {
476
0
    drv->external.trust_exit_code = git_config_bool(k, v);
477
0
    return 0;
478
0
  }
479
0
  if (!strcmp(type, "textconv")) {
480
0
    int ret;
481
0
    FREE_AND_NULL(drv->textconv_owned);
482
0
    ret = git_config_string(&drv->textconv_owned, k, v);
483
0
    drv->textconv = drv->textconv_owned;
484
0
    return ret;
485
0
  }
486
0
  if (!strcmp(type, "cachetextconv"))
487
0
    return parse_bool(&drv->textconv_want_cache, k, v);
488
0
  if (!strcmp(type, "wordregex")) {
489
0
    int ret;
490
0
    FREE_AND_NULL(drv->word_regex_owned);
491
0
    ret = git_config_string(&drv->word_regex_owned, k, v);
492
0
    drv->word_regex = drv->word_regex_owned;
493
0
    return ret;
494
0
  }
495
0
  if (!strcmp(type, "algorithm")) {
496
0
    int ret;
497
0
    FREE_AND_NULL(drv->algorithm_owned);
498
0
    ret = git_config_string(&drv->algorithm_owned, k, v);
499
0
    drv->algorithm = drv->algorithm_owned;
500
0
    return ret;
501
0
  }
502
503
0
  return 0;
504
0
}
505
506
struct userdiff_driver *userdiff_find_by_name(const char *name)
507
0
{
508
0
  int len = strlen(name);
509
0
  struct userdiff_driver *driver = userdiff_find_by_namelen(name, len);
510
0
  if (driver && driver->word_regex_multi_byte) {
511
0
    if (regexec_supports_multi_byte_chars())
512
0
      driver->word_regex = driver->word_regex_multi_byte;
513
0
    driver->word_regex_multi_byte = NULL;
514
0
  }
515
0
  return driver;
516
0
}
517
518
struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
519
                const char *path)
520
0
{
521
0
  static struct attr_check *check;
522
523
0
  if (!check)
524
0
    check = attr_check_initl("diff", NULL);
525
0
  if (!path)
526
0
    return NULL;
527
0
  git_check_attr(istate, path, check);
528
529
0
  if (ATTR_TRUE(check->items[0].value))
530
0
    return &driver_true;
531
0
  if (ATTR_FALSE(check->items[0].value))
532
0
    return &driver_false;
533
0
  if (ATTR_UNSET(check->items[0].value))
534
0
    return NULL;
535
0
  return userdiff_find_by_name(check->items[0].value);
536
0
}
537
538
struct userdiff_driver *userdiff_get_textconv(struct repository *r,
539
                struct userdiff_driver *driver)
540
0
{
541
0
  if (!driver->textconv)
542
0
    return NULL;
543
544
0
  if (driver->textconv_want_cache && !driver->textconv_cache &&
545
0
      have_git_dir()) {
546
0
    struct notes_cache *c = xmalloc(sizeof(*c));
547
0
    struct strbuf name = STRBUF_INIT;
548
549
0
    strbuf_addf(&name, "textconv/%s", driver->name);
550
0
    notes_cache_init(r, c, name.buf, driver->textconv);
551
0
    driver->textconv_cache = c;
552
0
    strbuf_release(&name);
553
0
  }
554
555
0
  return driver;
556
0
}
557
558
static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn,
559
           enum userdiff_driver_type type, void *cb_data,
560
           struct userdiff_driver *drv,
561
           int drv_size)
562
0
{
563
0
  int i;
564
0
  int ret;
565
0
  for (i = 0; i < drv_size; i++) {
566
0
    struct userdiff_driver *item = drv + i;
567
0
    if ((ret = fn(item, type, cb_data)))
568
0
      return ret;
569
0
  }
570
0
  return 0;
571
0
}
572
573
int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data)
574
0
{
575
0
  int ret;
576
577
0
  ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM,
578
0
              cb_data, drivers, ndrivers);
579
0
  if (ret)
580
0
    return ret;
581
582
0
  ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN,
583
0
              cb_data, builtin_drivers,
584
0
              ARRAY_SIZE(builtin_drivers));
585
0
  if (ret)
586
0
    return ret;
587
588
0
  return 0;
589
0
}