6 static struct userdiff_driver
*drivers
;
8 static int drivers_alloc
;
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]+" }
16 static struct userdiff_driver builtin_drivers
[] = {
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]+.*)$",
23 "[a-zA-Z][a-zA-Z0-9_]*"
24 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
28 /* lines beginning with a word optionally preceded by '&' or the root */
29 "^[ \t]*((/|&?[a-zA-Z_]).*)",
31 /* Property names and math operators */
33 "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"),
36 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
37 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
38 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
40 "[a-zA-Z][a-zA-Z0-9_]*"
41 "|\\.([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])\\."
42 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
43 * Don't worry about format statements without leading digits since
44 * they would have been matched above as a variable anyway. */
45 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
46 "|//|\\*\\*|::|[/<>=]="),
47 IPATTERN("fountain", "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$",
51 "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n"
52 /* Structs and interfaces */
53 "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)",
55 "[a-zA-Z_][a-zA-Z0-9_]*"
56 "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?"
57 "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"),
58 PATTERNS("html", "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$",
61 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
62 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
64 "[a-zA-Z_][a-zA-Z0-9_]*"
65 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
67 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
70 * Octave pattern is mostly the same as matlab, except that '%%%' and
71 * '##' can also be used to begin code sections, in addition to '%%'
72 * that is understood by both.
74 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$",
75 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
77 /* Negate C statements that can look like functions */
78 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
79 /* Objective-C methods */
80 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
82 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
83 /* Objective-C class/protocol definitions */
84 "^(@(implementation|interface|protocol)[ \t].*)$",
86 "[a-zA-Z_][a-zA-Z0-9_]*"
87 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
88 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
90 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
91 "implementation|initialization|finalization)[ \t]*.*)$"
93 "^(.*=[ \t]*(class|record).*)$",
95 "[a-zA-Z_][a-zA-Z0-9_]*"
96 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
97 "|<>|<=|>=|:=|\\.\\."),
100 "^sub [[:alnum:]_':]+[ \t]*"
101 "(\\([^)]*\\)[ \t]*)?" /* prototype */
103 * Attributes. A regex can't count nested parentheses,
104 * so just slurp up whatever we see, taking care not
105 * to accept lines like "sub foo; # defined elsewhere".
107 * An attribute could contain a semicolon, but at that
108 * point it seems reasonable enough to give up.
111 "(\\{[ \t]*)?" /* brace can come here or on the next line */
112 "(#.*)?$\n" /* comment */
113 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
114 "(\\{[ \t]*)?" /* brace can come here or on the next line */
116 "^=head[0-9] .*", /* POD */
118 "[[:alpha:]_'][[:alnum:]_']*"
119 "|0[xb]?[0-9a-fA-F_]*"
120 /* taking care not to interpret 3..5 as (3.)(.5) */
121 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
122 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
123 "|&&=|\\|\\|=|//=|\\*\\*="
124 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
129 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
130 "^[\t ]*((((final|abstract)[\t ]+)?class|interface|trait).*)$",
132 "[a-zA-Z_][a-zA-Z0-9_]*"
133 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
134 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
135 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
137 "[a-zA-Z_][a-zA-Z0-9_]*"
138 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
139 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
141 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
143 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
144 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
145 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
147 "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl)[< \t]+[^;]*)$",
149 "[a-zA-Z_][a-zA-Z0-9_]*"
150 "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?"
151 "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"),
152 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
153 "[={}\"]|[^={}\" \t]+"),
154 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
155 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
157 /* Jump targets or access declarations */
158 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
159 /* functions/methods, variables, and compounds at top level */
160 "^((::[[:space:]]*)?[A-Za-z_].*)$",
162 "[a-zA-Z_][a-zA-Z0-9_]*"
163 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
164 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
167 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
168 /* Methods and constructors */
169 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
171 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
172 /* Type definitions */
173 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
175 "^[ \t]*(namespace[ \t]+.*)$",
177 "[a-zA-Z_][a-zA-Z0-9_]*"
178 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
179 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
181 "![:;][[:space:]]*$\n"
185 * This regex comes from W3C CSS specs. Should theoretically also
186 * allow ISO 10646 characters U+00A0 and higher,
187 * but they are not handled in this regex.
189 "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
190 "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
192 { "default", NULL
, -1, { NULL
, 0 } },
197 static struct userdiff_driver driver_true
= {
204 static struct userdiff_driver driver_false
= {
211 static struct userdiff_driver
*userdiff_find_by_namelen(const char *k
, int len
)
214 for (i
= 0; i
< ndrivers
; i
++) {
215 struct userdiff_driver
*drv
= drivers
+ i
;
216 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
219 for (i
= 0; i
< ARRAY_SIZE(builtin_drivers
); i
++) {
220 struct userdiff_driver
*drv
= builtin_drivers
+ i
;
221 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
227 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
228 const char *v
, int cflags
)
230 if (git_config_string(&f
->pattern
, k
, v
) < 0)
236 static int parse_tristate(int *b
, const char *k
, const char *v
)
238 if (v
&& !strcasecmp(v
, "auto"))
241 *b
= git_config_bool(k
, v
);
245 static int parse_bool(int *b
, const char *k
, const char *v
)
247 *b
= git_config_bool(k
, v
);
251 int userdiff_config(const char *k
, const char *v
)
253 struct userdiff_driver
*drv
;
254 const char *name
, *type
;
257 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
260 drv
= userdiff_find_by_namelen(name
, namelen
);
262 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
263 drv
= &drivers
[ndrivers
++];
264 memset(drv
, 0, sizeof(*drv
));
265 drv
->name
= xmemdupz(name
, namelen
);
269 if (!strcmp(type
, "funcname"))
270 return parse_funcname(&drv
->funcname
, k
, v
, 0);
271 if (!strcmp(type
, "xfuncname"))
272 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
273 if (!strcmp(type
, "binary"))
274 return parse_tristate(&drv
->binary
, k
, v
);
275 if (!strcmp(type
, "command"))
276 return git_config_string(&drv
->external
, k
, v
);
277 if (!strcmp(type
, "textconv"))
278 return git_config_string(&drv
->textconv
, k
, v
);
279 if (!strcmp(type
, "cachetextconv"))
280 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
281 if (!strcmp(type
, "wordregex"))
282 return git_config_string(&drv
->word_regex
, k
, v
);
287 struct userdiff_driver
*userdiff_find_by_name(const char *name
)
289 int len
= strlen(name
);
290 return userdiff_find_by_namelen(name
, len
);
293 struct userdiff_driver
*userdiff_find_by_path(struct index_state
*istate
,
296 static struct attr_check
*check
;
299 check
= attr_check_initl("diff", NULL
);
302 git_check_attr(istate
, path
, check
);
304 if (ATTR_TRUE(check
->items
[0].value
))
306 if (ATTR_FALSE(check
->items
[0].value
))
307 return &driver_false
;
308 if (ATTR_UNSET(check
->items
[0].value
))
310 return userdiff_find_by_name(check
->items
[0].value
);
313 struct userdiff_driver
*userdiff_get_textconv(struct repository
*r
,
314 struct userdiff_driver
*driver
)
316 if (!driver
->textconv
)
319 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
320 struct notes_cache
*c
= xmalloc(sizeof(*c
));
321 struct strbuf name
= STRBUF_INIT
;
323 strbuf_addf(&name
, "textconv/%s", driver
->name
);
324 notes_cache_init(r
, c
, name
.buf
, driver
->textconv
);
325 driver
->textconv_cache
= c
;
326 strbuf_release(&name
);