Add Linux PPC support to the pre-auto-gc example hook
[git/git.git] / xdiff-interface.c
CommitLineData
d9ea73e0
JH
1#include "cache.h"
2#include "xdiff-interface.h"
8a3f524b
JH
3#include "strbuf.h"
4
5struct xdiff_emit_state {
6 xdiff_emit_consume_fn consume;
7 void *consume_callback_data;
8 struct strbuf remainder;
9};
d9ea73e0 10
a0fd3146 11static int parse_num(char **cp_p, int *num_p)
c1e335a4
JH
12{
13 char *cp = *cp_p;
a0fd3146 14 int num = 0;
c1e335a4
JH
15 int read_some;
16
17 while ('0' <= *cp && *cp <= '9')
18 num = num * 10 + *cp++ - '0';
19 if (!(read_some = cp - *cp_p))
20 return -1;
21 *cp_p = cp;
22 *num_p = num;
23 return 0;
24}
25
26int parse_hunk_header(char *line, int len,
a0fd3146
JH
27 int *ob, int *on,
28 int *nb, int *nn)
c1e335a4
JH
29{
30 char *cp;
31 cp = line + 4;
32 if (parse_num(&cp, ob)) {
33 bad_line:
34 return error("malformed diff output: %s", line);
35 }
36 if (*cp == ',') {
37 cp++;
38 if (parse_num(&cp, on))
39 goto bad_line;
40 }
41 else
42 *on = 1;
43 if (*cp++ != ' ' || *cp++ != '+')
44 goto bad_line;
45 if (parse_num(&cp, nb))
46 goto bad_line;
47 if (*cp == ',') {
48 cp++;
49 if (parse_num(&cp, nn))
50 goto bad_line;
51 }
52 else
53 *nn = 1;
54 return -!!memcmp(cp, " @@", 3);
55}
56
d9ea73e0
JH
57static void consume_one(void *priv_, char *s, unsigned long size)
58{
59 struct xdiff_emit_state *priv = priv_;
60 char *ep;
61 while (size) {
62 unsigned long this_size;
63 ep = memchr(s, '\n', size);
64 this_size = (ep == NULL) ? size : (ep - s + 1);
8a3f524b 65 priv->consume(priv->consume_callback_data, s, this_size);
d9ea73e0
JH
66 size -= this_size;
67 s += this_size;
68 }
69}
70
c99db9d2 71static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
d9ea73e0
JH
72{
73 struct xdiff_emit_state *priv = priv_;
74 int i;
75
76 for (i = 0; i < nbuf; i++) {
77 if (mb[i].ptr[mb[i].size-1] != '\n') {
78 /* Incomplete line */
b4637760 79 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
d9ea73e0
JH
80 continue;
81 }
82
83 /* we have a complete line */
b4637760 84 if (!priv->remainder.len) {
d9ea73e0
JH
85 consume_one(priv, mb[i].ptr, mb[i].size);
86 continue;
87 }
b4637760
BD
88 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
89 consume_one(priv, priv->remainder.buf, priv->remainder.len);
90 strbuf_reset(&priv->remainder);
d9ea73e0 91 }
b4637760
BD
92 if (priv->remainder.len) {
93 consume_one(priv, priv->remainder.buf, priv->remainder.len);
94 strbuf_reset(&priv->remainder);
d9ea73e0
JH
95 }
96 return 0;
97}
7cab5883 98
913b45f5
JH
99/*
100 * Trim down common substring at the end of the buffers,
101 * but leave at least ctx lines at the end.
102 */
29ab27f4 103static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx)
913b45f5
JH
104{
105 const int blk = 1024;
52499977 106 long trimmed = 0, recovered = 0;
913b45f5
JH
107 char *ap = a->ptr + a->size;
108 char *bp = b->ptr + b->size;
109 long smaller = (a->size < b->size) ? a->size : b->size;
110
d2f82950
LT
111 if (ctx)
112 return;
113
913b45f5
JH
114 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
115 trimmed += blk;
116 ap -= blk;
117 bp -= blk;
118 }
119
d2f82950 120 while (recovered < trimmed)
52499977 121 if (ap[recovered++] == '\n')
d2f82950
LT
122 break;
123 a->size -= trimmed - recovered;
124 b->size -= trimmed - recovered;
913b45f5
JH
125}
126
c279d7e9
JH
127int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
128{
913b45f5
JH
129 mmfile_t a = *mf1;
130 mmfile_t b = *mf2;
131
132 trim_common_tail(&a, &b, xecfg->ctxlen);
133
134 return xdl_diff(&a, &b, xpp, xecfg, xecb);
c279d7e9
JH
135}
136
c99db9d2 137int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
8a3f524b
JH
138 xdiff_emit_consume_fn fn, void *consume_callback_data,
139 xpparam_t const *xpp,
c99db9d2
BD
140 xdemitconf_t const *xecfg, xdemitcb_t *xecb)
141{
142 int ret;
8a3f524b
JH
143 struct xdiff_emit_state state;
144
145 memset(&state, 0, sizeof(state));
146 state.consume = fn;
147 state.consume_callback_data = consume_callback_data;
c99db9d2 148 xecb->outf = xdiff_outf;
8a3f524b
JH
149 xecb->priv = &state;
150 strbuf_init(&state.remainder, 0);
c99db9d2 151 ret = xdi_diff(mf1, mf2, xpp, xecfg, xecb);
8a3f524b 152 strbuf_release(&state.remainder);
c99db9d2
BD
153 return ret;
154}
155
7cab5883
JS
156int read_mmfile(mmfile_t *ptr, const char *filename)
157{
158 struct stat st;
159 FILE *f;
dc49cd76 160 size_t sz;
7cab5883
JS
161
162 if (stat(filename, &st))
163 return error("Could not stat %s", filename);
164 if ((f = fopen(filename, "rb")) == NULL)
165 return error("Could not open %s", filename);
dc49cd76 166 sz = xsize_t(st.st_size);
381b851c
JS
167 ptr->ptr = xmalloc(sz ? sz : 1);
168 if (sz && fread(ptr->ptr, sz, 1, f) != 1)
7cab5883
JS
169 return error("Could not read %s", filename);
170 fclose(f);
dc49cd76 171 ptr->size = sz;
7cab5883
JS
172 return 0;
173}
174
6bfce93e
JS
175#define FIRST_FEW_BYTES 8000
176int buffer_is_binary(const char *ptr, unsigned long size)
177{
178 if (FIRST_FEW_BYTES < size)
179 size = FIRST_FEW_BYTES;
180 return !!memchr(ptr, 0, size);
181}
f258475a
JH
182
183struct ff_regs {
184 int nr;
185 struct ff_reg {
186 regex_t re;
187 int negate;
188 } *array;
189};
190
191static long ff_regexp(const char *line, long len,
192 char *buffer, long buffer_size, void *priv)
193{
a5a5a048 194 char *line_buffer;
f258475a
JH
195 struct ff_regs *regs = priv;
196 regmatch_t pmatch[2];
3d8dccd7
JH
197 int i;
198 int result = -1;
f258475a 199
a5a5a048
BC
200 /* Exclude terminating newline (and cr) from matching */
201 if (len > 0 && line[len-1] == '\n') {
202 if (len > 1 && line[len-2] == '\r')
203 len -= 2;
204 else
205 len--;
206 }
207
208 line_buffer = xstrndup(line, len); /* make NUL terminated */
209
f258475a
JH
210 for (i = 0; i < regs->nr; i++) {
211 struct ff_reg *reg = regs->array + i;
3d8dccd7
JH
212 if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
213 if (reg->negate)
214 goto fail;
215 break;
f258475a
JH
216 }
217 }
3d8dccd7
JH
218 if (regs->nr <= i)
219 goto fail;
f258475a
JH
220 i = pmatch[1].rm_so >= 0 ? 1 : 0;
221 line += pmatch[i].rm_so;
222 result = pmatch[i].rm_eo - pmatch[i].rm_so;
223 if (result > buffer_size)
224 result = buffer_size;
225 else
3d8dccd7 226 while (result > 0 && (isspace(line[result - 1])))
f258475a
JH
227 result--;
228 memcpy(buffer, line, result);
3d8dccd7 229 fail:
f258475a
JH
230 free(line_buffer);
231 return result;
232}
233
a013585b 234void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
f258475a
JH
235{
236 int i;
237 struct ff_regs *regs;
238
239 xecfg->find_func = ff_regexp;
240 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
241 for (i = 0, regs->nr = 1; value[i]; i++)
242 if (value[i] == '\n')
243 regs->nr++;
244 regs->array = xmalloc(regs->nr * sizeof(struct ff_reg));
245 for (i = 0; i < regs->nr; i++) {
246 struct ff_reg *reg = regs->array + i;
247 const char *ep = strchr(value, '\n'), *expression;
248 char *buffer = NULL;
249
250 reg->negate = (*value == '!');
251 if (reg->negate && i == regs->nr - 1)
252 die("Last expression must not be negated: %s", value);
253 if (*value == '!')
254 value++;
255 if (ep)
256 expression = buffer = xstrndup(value, ep - value);
257 else
258 expression = value;
a013585b 259 if (regcomp(&reg->re, expression, cflags))
f258475a 260 die("Invalid regexp to look for hunk header: %s", expression);
8e0f7003 261 free(buffer);
f258475a
JH
262 value = ep + 1;
263 }
264}
b5412484
JH
265
266int git_xmerge_style = -1;
267
268int git_xmerge_config(const char *var, const char *value, void *cb)
269{
270 if (!strcasecmp(var, "merge.conflictstyle")) {
271 if (!value)
272 die("'%s' is not a boolean", var);
273 if (!strcmp(value, "diff3"))
274 git_xmerge_style = XDL_MERGE_DIFF3;
275 else if (!strcmp(value, "merge"))
276 git_xmerge_style = 0;
277 else
278 die("unknown style '%s' given for '%s'",
279 value, var);
280 return 0;
281 }
282 return git_default_config(var, value, cb);
283}
284