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 new|renames|is 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_]*"
25 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
28 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
29 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
30 "|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
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 "|//|\\*\\*|::|[/<>=]="),
39 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
42 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
43 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
45 "[a-zA-Z_][a-zA-Z0-9_]*"
46 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
48 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
50 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
51 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
53 /* Negate C statements that can look like functions */
54 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
55 /* Objective-C methods */
56 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
58 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
59 /* Objective-C class/protocol definitions */
60 "^(@(implementation|interface|protocol)[ \t].*)$",
62 "[a-zA-Z_][a-zA-Z0-9_]*"
63 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
64 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
66 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
67 "implementation|initialization|finalization)[ \t]*.*)$"
69 "^(.*=[ \t]*(class|record).*)$",
71 "[a-zA-Z_][a-zA-Z0-9_]*"
72 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
73 "|<>|<=|>=|:=|\\.\\."),
76 "^sub [[:alnum:]_':]+[ \t]*"
77 "(\\([^)]*\\)[ \t]*)?" /* prototype */
79 * Attributes. A regex can't count nested parentheses,
80 * so just slurp up whatever we see, taking care not
81 * to accept lines like "sub foo; # defined elsewhere".
83 * An attribute could contain a semicolon, but at that
84 * point it seems reasonable enough to give up.
87 "(\\{[ \t]*)?" /* brace can come here or on the next line */
88 "(#.*)?$\n" /* comment */
89 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
90 "(\\{[ \t]*)?" /* brace can come here or on the next line */
92 "^=head[0-9] .*", /* POD */
94 "[[:alpha:]_'][[:alnum:]_']*"
95 "|0[xb]?[0-9a-fA-F_]*"
96 /* taking care not to interpret 3..5 as (3.)(.5) */
97 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
98 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
99 "|&&=|\\|\\|=|//=|\\*\\*="
100 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
105 "^[\t ]*(((public|protected|private|static)[\t ]+)*function.*)$\n"
108 "[a-zA-Z_][a-zA-Z0-9_]*"
109 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
110 "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"),
111 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
113 "[a-zA-Z_][a-zA-Z0-9_]*"
114 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
115 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
117 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
119 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
120 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
121 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
122 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
123 "[={}\"]|[^={}\" \t]+"),
124 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
125 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
127 /* Jump targets or access declarations */
128 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:.*$\n"
129 /* C/++ functions/methods at top level */
130 "^([A-Za-z_][A-Za-z_0-9]*([ \t*]+[A-Za-z_][A-Za-z_0-9]*([ \t]*::[ \t]*[^[:space:]]+)?){1,}[ \t]*\\([^;]*)$\n"
131 /* compound type at top level */
132 "^((struct|class|enum)[^;]*)$",
134 "[a-zA-Z_][a-zA-Z0-9_]*"
135 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
136 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
139 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
140 /* Methods and constructors */
141 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
143 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
144 /* Type definitions */
145 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
147 "^[ \t]*(namespace[ \t]+.*)$",
149 "[a-zA-Z_][a-zA-Z0-9_]*"
150 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
151 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
152 { "default", NULL
, -1, { NULL
, 0 } },
157 static struct userdiff_driver driver_true
= {
164 static struct userdiff_driver driver_false
= {
171 static struct userdiff_driver
*userdiff_find_by_namelen(const char *k
, int len
)
174 for (i
= 0; i
< ndrivers
; i
++) {
175 struct userdiff_driver
*drv
= drivers
+ i
;
176 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
179 for (i
= 0; i
< ARRAY_SIZE(builtin_drivers
); i
++) {
180 struct userdiff_driver
*drv
= builtin_drivers
+ i
;
181 if (!strncmp(drv
->name
, k
, len
) && !drv
->name
[len
])
187 static int parse_funcname(struct userdiff_funcname
*f
, const char *k
,
188 const char *v
, int cflags
)
190 if (git_config_string(&f
->pattern
, k
, v
) < 0)
196 static int parse_tristate(int *b
, const char *k
, const char *v
)
198 if (v
&& !strcasecmp(v
, "auto"))
201 *b
= git_config_bool(k
, v
);
205 static int parse_bool(int *b
, const char *k
, const char *v
)
207 *b
= git_config_bool(k
, v
);
211 int userdiff_config(const char *k
, const char *v
)
213 struct userdiff_driver
*drv
;
214 const char *name
, *type
;
217 if (parse_config_key(k
, "diff", &name
, &namelen
, &type
) || !name
)
220 drv
= userdiff_find_by_namelen(name
, namelen
);
222 ALLOC_GROW(drivers
, ndrivers
+1, drivers_alloc
);
223 drv
= &drivers
[ndrivers
++];
224 memset(drv
, 0, sizeof(*drv
));
225 drv
->name
= xmemdupz(name
, namelen
);
229 if (!strcmp(type
, "funcname"))
230 return parse_funcname(&drv
->funcname
, k
, v
, 0);
231 if (!strcmp(type
, "xfuncname"))
232 return parse_funcname(&drv
->funcname
, k
, v
, REG_EXTENDED
);
233 if (!strcmp(type
, "binary"))
234 return parse_tristate(&drv
->binary
, k
, v
);
235 if (!strcmp(type
, "command"))
236 return git_config_string(&drv
->external
, k
, v
);
237 if (!strcmp(type
, "textconv"))
238 return git_config_string(&drv
->textconv
, k
, v
);
239 if (!strcmp(type
, "cachetextconv"))
240 return parse_bool(&drv
->textconv_want_cache
, k
, v
);
241 if (!strcmp(type
, "wordregex"))
242 return git_config_string(&drv
->word_regex
, k
, v
);
247 struct userdiff_driver
*userdiff_find_by_name(const char *name
) {
248 int len
= strlen(name
);
249 return userdiff_find_by_namelen(name
, len
);
252 struct userdiff_driver
*userdiff_find_by_path(const char *path
)
254 static struct git_attr
*attr
;
255 struct git_attr_check check
;
258 attr
= git_attr("diff");
263 if (git_check_attr(path
, 1, &check
))
266 if (ATTR_TRUE(check
.value
))
268 if (ATTR_FALSE(check
.value
))
269 return &driver_false
;
270 if (ATTR_UNSET(check
.value
))
272 return userdiff_find_by_name(check
.value
);
275 struct userdiff_driver
*userdiff_get_textconv(struct userdiff_driver
*driver
)
277 if (!driver
->textconv
)
280 if (driver
->textconv_want_cache
&& !driver
->textconv_cache
) {
281 struct notes_cache
*c
= xmalloc(sizeof(*c
));
282 struct strbuf name
= STRBUF_INIT
;
284 strbuf_addf(&name
, "textconv/%s", driver
->name
);
285 notes_cache_init(c
, name
.buf
, driver
->textconv
);
286 driver
->textconv_cache
= c
;