Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / git / xdiff-interface.c
blobbe866d12d38f6f1328f5fae8c7108176d4ecba70
1 #include "cache.h"
2 #include "xdiff-interface.h"
4 static int parse_num(char **cp_p, int *num_p)
6 char *cp = *cp_p;
7 int num = 0;
8 int read_some;
10 while ('0' <= *cp && *cp <= '9')
11 num = num * 10 + *cp++ - '0';
12 if (!(read_some = cp - *cp_p))
13 return -1;
14 *cp_p = cp;
15 *num_p = num;
16 return 0;
19 int parse_hunk_header(char *line, int len,
20 int *ob, int *on,
21 int *nb, int *nn)
23 char *cp;
24 cp = line + 4;
25 if (parse_num(&cp, ob)) {
26 bad_line:
27 return error("malformed diff output: %s", line);
29 if (*cp == ',') {
30 cp++;
31 if (parse_num(&cp, on))
32 goto bad_line;
34 else
35 *on = 1;
36 if (*cp++ != ' ' || *cp++ != '+')
37 goto bad_line;
38 if (parse_num(&cp, nb))
39 goto bad_line;
40 if (*cp == ',') {
41 cp++;
42 if (parse_num(&cp, nn))
43 goto bad_line;
45 else
46 *nn = 1;
47 return -!!memcmp(cp, " @@", 3);
50 static void consume_one(void *priv_, char *s, unsigned long size)
52 struct xdiff_emit_state *priv = priv_;
53 char *ep;
54 while (size) {
55 unsigned long this_size;
56 ep = memchr(s, '\n', size);
57 this_size = (ep == NULL) ? size : (ep - s + 1);
58 priv->consume(priv, s, this_size);
59 size -= this_size;
60 s += this_size;
64 int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
66 struct xdiff_emit_state *priv = priv_;
67 int i;
69 for (i = 0; i < nbuf; i++) {
70 if (mb[i].ptr[mb[i].size-1] != '\n') {
71 /* Incomplete line */
72 priv->remainder = xrealloc(priv->remainder,
73 priv->remainder_size +
74 mb[i].size);
75 memcpy(priv->remainder + priv->remainder_size,
76 mb[i].ptr, mb[i].size);
77 priv->remainder_size += mb[i].size;
78 continue;
81 /* we have a complete line */
82 if (!priv->remainder) {
83 consume_one(priv, mb[i].ptr, mb[i].size);
84 continue;
86 priv->remainder = xrealloc(priv->remainder,
87 priv->remainder_size +
88 mb[i].size);
89 memcpy(priv->remainder + priv->remainder_size,
90 mb[i].ptr, mb[i].size);
91 consume_one(priv, priv->remainder,
92 priv->remainder_size + mb[i].size);
93 free(priv->remainder);
94 priv->remainder = NULL;
95 priv->remainder_size = 0;
97 if (priv->remainder) {
98 consume_one(priv, priv->remainder, priv->remainder_size);
99 free(priv->remainder);
100 priv->remainder = NULL;
101 priv->remainder_size = 0;
103 return 0;
106 int read_mmfile(mmfile_t *ptr, const char *filename)
108 struct stat st;
109 FILE *f;
110 size_t sz;
112 if (stat(filename, &st))
113 return error("Could not stat %s", filename);
114 if ((f = fopen(filename, "rb")) == NULL)
115 return error("Could not open %s", filename);
116 sz = xsize_t(st.st_size);
117 ptr->ptr = xmalloc(sz);
118 if (fread(ptr->ptr, sz, 1, f) != 1)
119 return error("Could not read %s", filename);
120 fclose(f);
121 ptr->size = sz;
122 return 0;
125 #define FIRST_FEW_BYTES 8000
126 int buffer_is_binary(const char *ptr, unsigned long size)
128 if (FIRST_FEW_BYTES < size)
129 size = FIRST_FEW_BYTES;
130 return !!memchr(ptr, 0, size);
133 struct ff_regs {
134 int nr;
135 struct ff_reg {
136 regex_t re;
137 int negate;
138 } *array;
141 static long ff_regexp(const char *line, long len,
142 char *buffer, long buffer_size, void *priv)
144 char *line_buffer = xstrndup(line, len); /* make NUL terminated */
145 struct ff_regs *regs = priv;
146 regmatch_t pmatch[2];
147 int result = 0, i;
149 for (i = 0; i < regs->nr; i++) {
150 struct ff_reg *reg = regs->array + i;
151 if (reg->negate ^ !!regexec(&reg->re,
152 line_buffer, 2, pmatch, 0)) {
153 free(line_buffer);
154 return -1;
157 i = pmatch[1].rm_so >= 0 ? 1 : 0;
158 line += pmatch[i].rm_so;
159 result = pmatch[i].rm_eo - pmatch[i].rm_so;
160 if (result > buffer_size)
161 result = buffer_size;
162 else
163 while (result > 0 && (isspace(line[result - 1]) ||
164 line[result - 1] == '\n'))
165 result--;
166 memcpy(buffer, line, result);
167 free(line_buffer);
168 return result;
171 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value)
173 int i;
174 struct ff_regs *regs;
176 xecfg->find_func = ff_regexp;
177 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
178 for (i = 0, regs->nr = 1; value[i]; i++)
179 if (value[i] == '\n')
180 regs->nr++;
181 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
182 for (i = 0; i < regs->nr; i++) {
183 struct ff_reg *reg = regs->array + i;
184 const char *ep = strchr(value, '\n'), *expression;
185 char *buffer = NULL;
187 reg->negate = (*value == '!');
188 if (reg->negate && i == regs->nr - 1)
189 die("Last expression must not be negated: %s", value);
190 if (*value == '!')
191 value++;
192 if (ep)
193 expression = buffer = xstrndup(value, ep - value);
194 else
195 expression = value;
196 if (regcomp(&reg->re, expression, 0))
197 die("Invalid regexp to look for hunk header: %s", expression);
198 if (buffer)
199 free(buffer);
200 value = ep + 1;