THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

290 lines
9.2 KiB

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