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.

296 lines
6.6KB

  1. #include "git-compat-util.h"
  2. #include "line-range.h"
  3. #include "xdiff-interface.h"
  4. #include "strbuf.h"
  5. #include "userdiff.h"
  6. /*
  7. * Parse one item in the -L option
  8. *
  9. * 'begin' is applicable only to relative range anchors. Absolute anchors
  10. * ignore this value.
  11. *
  12. * When parsing "-L A,B", parse_loc() is called once for A and once for B.
  13. *
  14. * When parsing A, 'begin' must be a negative number, the absolute value of
  15. * which is the line at which relative start-of-range anchors should be
  16. * based. Beginning of file is represented by -1.
  17. *
  18. * When parsing B, 'begin' must be the positive line number immediately
  19. * following the line computed for 'A'.
  20. */
  21. static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
  22. void *data, long lines, long begin, long *ret)
  23. {
  24. char *term;
  25. const char *line;
  26. long num;
  27. int reg_error;
  28. regex_t regexp;
  29. regmatch_t match[1];
  30. /* Allow "-L <something>,+20" to mean starting at <something>
  31. * for 20 lines, or "-L <something>,-5" for 5 lines ending at
  32. * <something>.
  33. */
  34. if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) {
  35. num = strtol(spec + 1, &term, 10);
  36. if (term != spec + 1) {
  37. if (!ret)
  38. return term;
  39. if (num == 0)
  40. die("-L invalid empty range");
  41. if (spec[0] == '-')
  42. num = 0 - num;
  43. if (0 < num)
  44. *ret = begin + num - 2;
  45. else if (!num)
  46. *ret = begin;
  47. else
  48. *ret = begin + num > 0 ? begin + num : 1;
  49. return term;
  50. }
  51. return spec;
  52. }
  53. num = strtol(spec, &term, 10);
  54. if (term != spec) {
  55. if (ret) {
  56. if (num <= 0)
  57. die("-L invalid line number: %ld", num);
  58. *ret = num;
  59. }
  60. return term;
  61. }
  62. if (begin < 0) {
  63. if (spec[0] != '^')
  64. begin = -begin;
  65. else {
  66. begin = 1;
  67. spec++;
  68. }
  69. }
  70. if (spec[0] != '/')
  71. return spec;
  72. /* it could be a regexp of form /.../ */
  73. for (term = (char *) spec + 1; *term && *term != '/'; term++) {
  74. if (*term == '\\')
  75. term++;
  76. }
  77. if (*term != '/')
  78. return spec;
  79. /* in the scan-only case we are not interested in the regex */
  80. if (!ret)
  81. return term+1;
  82. /* try [spec+1 .. term-1] as regexp */
  83. *term = 0;
  84. begin--; /* input is in human terms */
  85. line = nth_line(data, begin);
  86. if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
  87. !(reg_error = regexec(&regexp, line, 1, match, 0))) {
  88. const char *cp = line + match[0].rm_so;
  89. const char *nline;
  90. while (begin++ < lines) {
  91. nline = nth_line(data, begin);
  92. if (line <= cp && cp < nline)
  93. break;
  94. line = nline;
  95. }
  96. *ret = begin;
  97. regfree(&regexp);
  98. *term++ = '/';
  99. return term;
  100. }
  101. else {
  102. char errbuf[1024];
  103. regerror(reg_error, &regexp, errbuf, 1024);
  104. die("-L parameter '%s' starting at line %ld: %s",
  105. spec + 1, begin + 1, errbuf);
  106. }
  107. }
  108. static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
  109. {
  110. if (xecfg) {
  111. char buf[1];
  112. return xecfg->find_func(bol, eol - bol, buf, 1,
  113. xecfg->find_func_priv) >= 0;
  114. }
  115. if (bol == eol)
  116. return 0;
  117. if (isalpha(*bol) || *bol == '_' || *bol == '$')
  118. return 1;
  119. return 0;
  120. }
  121. static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
  122. regex_t *regexp)
  123. {
  124. int reg_error;
  125. regmatch_t match[1];
  126. while (1) {
  127. const char *bol, *eol;
  128. reg_error = regexec(regexp, start, 1, match, 0);
  129. if (reg_error == REG_NOMATCH)
  130. return NULL;
  131. else if (reg_error) {
  132. char errbuf[1024];
  133. regerror(reg_error, regexp, errbuf, 1024);
  134. die("-L parameter: regexec() failed: %s", errbuf);
  135. }
  136. /* determine extent of line matched */
  137. bol = start+match[0].rm_so;
  138. eol = start+match[0].rm_eo;
  139. while (bol > start && *bol != '\n')
  140. bol--;
  141. if (*bol == '\n')
  142. bol++;
  143. while (*eol && *eol != '\n')
  144. eol++;
  145. if (*eol == '\n')
  146. eol++;
  147. /* is it a funcname line? */
  148. if (match_funcname(xecfg, (char*) bol, (char*) eol))
  149. return bol;
  150. start = eol;
  151. }
  152. }
  153. static const char *parse_range_funcname(
  154. const char *arg, nth_line_fn_t nth_line_cb,
  155. void *cb_data, long lines, long anchor, long *begin, long *end,
  156. const char *path, struct index_state *istate)
  157. {
  158. char *pattern;
  159. const char *term;
  160. struct userdiff_driver *drv;
  161. xdemitconf_t *xecfg = NULL;
  162. const char *start;
  163. const char *p;
  164. int reg_error;
  165. regex_t regexp;
  166. if (*arg == '^') {
  167. anchor = 1;
  168. arg++;
  169. }
  170. assert(*arg == ':');
  171. term = arg+1;
  172. while (*term && *term != ':') {
  173. if (*term == '\\' && *(term+1))
  174. term++;
  175. term++;
  176. }
  177. if (term == arg+1)
  178. return NULL;
  179. if (!begin) /* skip_range_arg case */
  180. return term;
  181. pattern = xstrndup(arg+1, term-(arg+1));
  182. anchor--; /* input is in human terms */
  183. start = nth_line_cb(cb_data, anchor);
  184. drv = userdiff_find_by_path(istate, path);
  185. if (drv && drv->funcname.pattern) {
  186. const struct userdiff_funcname *pe = &drv->funcname;
  187. xecfg = xcalloc(1, sizeof(*xecfg));
  188. xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
  189. }
  190. reg_error = regcomp(&regexp, pattern, REG_NEWLINE);
  191. if (reg_error) {
  192. char errbuf[1024];
  193. regerror(reg_error, &regexp, errbuf, 1024);
  194. die("-L parameter '%s': %s", pattern, errbuf);
  195. }
  196. p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
  197. if (!p)
  198. die("-L parameter '%s' starting at line %ld: no match",
  199. pattern, anchor + 1);
  200. *begin = 0;
  201. while (p > nth_line_cb(cb_data, *begin))
  202. (*begin)++;
  203. if (*begin >= lines)
  204. die("-L parameter '%s' matches at EOF", pattern);
  205. *end = *begin+1;
  206. while (*end < lines) {
  207. const char *bol = nth_line_cb(cb_data, *end);
  208. const char *eol = nth_line_cb(cb_data, *end+1);
  209. if (match_funcname(xecfg, bol, eol))
  210. break;
  211. (*end)++;
  212. }
  213. regfree(&regexp);
  214. free(xecfg);
  215. free(pattern);
  216. /* compensate for 1-based numbering */
  217. (*begin)++;
  218. return term;
  219. }
  220. int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
  221. void *cb_data, long lines, long anchor,
  222. long *begin, long *end,
  223. const char *path, struct index_state *istate)
  224. {
  225. *begin = *end = 0;
  226. if (anchor < 1)
  227. anchor = 1;
  228. if (anchor > lines)
  229. anchor = lines + 1;
  230. if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) {
  231. arg = parse_range_funcname(arg, nth_line_cb, cb_data,
  232. lines, anchor, begin, end,
  233. path, istate);
  234. if (!arg || *arg)
  235. return -1;
  236. return 0;
  237. }
  238. arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
  239. if (*arg == ',')
  240. arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
  241. if (*arg)
  242. return -1;
  243. if (*begin && *end && *end < *begin) {
  244. SWAP(*end, *begin);
  245. }
  246. return 0;
  247. }
  248. const char *skip_range_arg(const char *arg, struct index_state *istate)
  249. {
  250. if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':'))
  251. return parse_range_funcname(arg, NULL, NULL,
  252. 0, 0, NULL, NULL,
  253. NULL, istate);
  254. arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
  255. if (*arg == ',')
  256. arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);
  257. return arg;
  258. }