bf98866c4f5aa2027135741cb07bc1ec04b11e9f
[git/git.git] / xdiff-interface.c
1 #include "cache.h"
2 #include "xdiff-interface.h"
3
4 static int parse_num(char **cp_p, int *num_p)
5 {
6 char *cp = *cp_p;
7 int num = 0;
8 int read_some;
9
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;
17 }
18
19 int parse_hunk_header(char *line, int len,
20 int *ob, int *on,
21 int *nb, int *nn)
22 {
23 char *cp;
24 cp = line + 4;
25 if (parse_num(&cp, ob)) {
26 bad_line:
27 return error("malformed diff output: %s", line);
28 }
29 if (*cp == ',') {
30 cp++;
31 if (parse_num(&cp, on))
32 goto bad_line;
33 }
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;
44 }
45 else
46 *nn = 1;
47 return -!!memcmp(cp, " @@", 3);
48 }
49
50 static void consume_one(void *priv_, char *s, unsigned long size)
51 {
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;
61 }
62 }
63
64 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
65 {
66 struct xdiff_emit_state *priv = priv_;
67 int i;
68
69 for (i = 0; i < nbuf; i++) {
70 if (mb[i].ptr[mb[i].size-1] != '\n') {
71 /* Incomplete line */
72 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
73 continue;
74 }
75
76 /* we have a complete line */
77 if (!priv->remainder.len) {
78 consume_one(priv, mb[i].ptr, mb[i].size);
79 continue;
80 }
81 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
82 consume_one(priv, priv->remainder.buf, priv->remainder.len);
83 strbuf_reset(&priv->remainder);
84 }
85 if (priv->remainder.len) {
86 consume_one(priv, priv->remainder.buf, priv->remainder.len);
87 strbuf_reset(&priv->remainder);
88 }
89 return 0;
90 }
91
92 /*
93 * Trim down common substring at the end of the buffers,
94 * but leave at least ctx lines at the end.
95 */
96 static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
97 {
98 const int blk = 1024;
99 long trimmed = 0, recovered = 0;
100 char *ap = a->ptr + a->size;
101 char *bp = b->ptr + b->size;
102 long smaller = (a->size < b->size) ? a->size : b->size;
103
104 if (ctx)
105 return;
106
107 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
108 trimmed += blk;
109 ap -= blk;
110 bp -= blk;
111 }
112
113 while (recovered < trimmed)
114 if (ap[recovered++] == '\n')
115 break;
116 a->size -= trimmed - recovered;
117 b->size -= trimmed - recovered;
118 }
119
120 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
121 {
122 mmfile_t a = *mf1;
123 mmfile_t b = *mf2;
124
125 trim_common_tail(&a, &b, xecfg->ctxlen);
126
127 return xdl_diff(&a, &b, xpp, xecfg, xecb);
128 }
129
130 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
131 struct xdiff_emit_state *state, xpparam_t const *xpp,
132 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
133 {
134 int ret;
135 xecb->outf = xdiff_outf;
136 xecb->priv = state;
137 strbuf_init(&state->remainder, 0);
138 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
139 strbuf_release(&state->remainder);
140 return ret;
141 }
142
143 int read_mmfile(mmfile_t *ptr, const char *filename)
144 {
145 struct stat st;
146 FILE *f;
147 size_t sz;
148
149 if (stat(filename, &st))
150 return error("Could not stat %s", filename);
151 if ((f = fopen(filename, "rb")) == NULL)
152 return error("Could not open %s", filename);
153 sz = xsize_t(st.st_size);
154 ptr->ptr = xmalloc(sz ? sz : 1);
155 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
156 return error("Could not read %s", filename);
157 fclose(f);
158 ptr->size = sz;
159 return 0;
160 }
161
162 #define FIRST_FEW_BYTES 8000
163 int buffer_is_binary(const char *ptr, unsigned long size)
164 {
165 if (FIRST_FEW_BYTES < size)
166 size = FIRST_FEW_BYTES;
167 return !!memchr(ptr, 0, size);
168 }
169
170 struct ff_regs {
171 int nr;
172 struct ff_reg {
173 regex_t re;
174 int negate;
175 } *array;
176 };
177
178 static long ff_regexp(const char *line, long len,
179 char *buffer, long buffer_size, void *priv)
180 {
181 char *line_buffer = xstrndup(line, len); /* make NUL terminated */
182 struct ff_regs *regs = priv;
183 regmatch_t pmatch[2];
184 int result = 0, i;
185
186 for (i = 0; i < regs->nr; i++) {
187 struct ff_reg *reg = regs->array + i;
188 if (reg->negate ^ !!regexec(&reg->re,
189 line_buffer, 2, pmatch, 0)) {
190 free(line_buffer);
191 return -1;
192 }
193 }
194 i = pmatch[1].rm_so >= 0 ? 1 : 0;
195 line += pmatch[i].rm_so;
196 result = pmatch[i].rm_eo - pmatch[i].rm_so;
197 if (result > buffer_size)
198 result = buffer_size;
199 else
200 while (result > 0 && (isspace(line[result - 1]) ||
201 line[result - 1] == '\n'))
202 result--;
203 memcpy(buffer, line, result);
204 free(line_buffer);
205 return result;
206 }
207
208 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value)
209 {
210 int i;
211 struct ff_regs *regs;
212
213 xecfg->find_func = ff_regexp;
214 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
215 for (i = 0, regs->nr = 1; value[i]; i++)
216 if (value[i] == '\n')
217 regs->nr++;
218 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
219 for (i = 0; i < regs->nr; i++) {
220 struct ff_reg *reg = regs->array + i;
221 const char *ep = strchr(value, '\n'), *expression;
222 char *buffer = NULL;
223
224 reg->negate = (*value == '!');
225 if (reg->negate && i == regs->nr - 1)
226 die("Last expression must not be negated: %s", value);
227 if (*value == '!')
228 value++;
229 if (ep)
230 expression = buffer = xstrndup(value, ep - value);
231 else
232 expression = value;
233 if (regcomp(&reg->re, expression, 0))
234 die("Invalid regexp to look for hunk header: %s", expression);
235 free(buffer);
236 value = ep + 1;
237 }
238 }