]>
Commit | Line | Data |
---|---|---|
d9ea73e0 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
cbd53a21 | 3 | #include "object-store.h" |
d9ea73e0 | 4 | #include "xdiff-interface.h" |
86295bb6 RS |
5 | #include "xdiff/xtypes.h" |
6 | #include "xdiff/xdiffi.h" | |
7 | #include "xdiff/xemit.h" | |
8 | #include "xdiff/xmacros.h" | |
5ec8274b | 9 | #include "xdiff/xutils.h" |
8a3f524b JH |
10 | |
11 | struct xdiff_emit_state { | |
9346d6d1 JK |
12 | xdiff_emit_hunk_fn hunk_fn; |
13 | xdiff_emit_line_fn line_fn; | |
8a3f524b JH |
14 | void *consume_callback_data; |
15 | struct strbuf remainder; | |
16 | }; | |
d9ea73e0 | 17 | |
9346d6d1 JK |
18 | static int xdiff_out_hunk(void *priv_, |
19 | long old_begin, long old_nr, | |
20 | long new_begin, long new_nr, | |
21 | const char *func, long funclen) | |
c1e335a4 | 22 | { |
9346d6d1 | 23 | struct xdiff_emit_state *priv = priv_; |
c1e335a4 | 24 | |
9346d6d1 JK |
25 | if (priv->remainder.len) |
26 | BUG("xdiff emitted hunk in the middle of a line"); | |
c1e335a4 | 27 | |
9346d6d1 JK |
28 | priv->hunk_fn(priv->consume_callback_data, |
29 | old_begin, old_nr, new_begin, new_nr, | |
30 | func, funclen); | |
31 | return 0; | |
c1e335a4 JH |
32 | } |
33 | ||
d9ea73e0 JH |
34 | static void consume_one(void *priv_, char *s, unsigned long size) |
35 | { | |
36 | struct xdiff_emit_state *priv = priv_; | |
37 | char *ep; | |
38 | while (size) { | |
39 | unsigned long this_size; | |
40 | ep = memchr(s, '\n', size); | |
41 | this_size = (ep == NULL) ? size : (ep - s + 1); | |
9346d6d1 | 42 | priv->line_fn(priv->consume_callback_data, s, this_size); |
d9ea73e0 JH |
43 | size -= this_size; |
44 | s += this_size; | |
45 | } | |
46 | } | |
47 | ||
c99db9d2 | 48 | static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf) |
d9ea73e0 JH |
49 | { |
50 | struct xdiff_emit_state *priv = priv_; | |
51 | int i; | |
52 | ||
7c61e25f JK |
53 | if (!priv->line_fn) |
54 | return 0; | |
55 | ||
d9ea73e0 JH |
56 | for (i = 0; i < nbuf; i++) { |
57 | if (mb[i].ptr[mb[i].size-1] != '\n') { | |
58 | /* Incomplete line */ | |
b4637760 | 59 | strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size); |
d9ea73e0 JH |
60 | continue; |
61 | } | |
62 | ||
63 | /* we have a complete line */ | |
b4637760 | 64 | if (!priv->remainder.len) { |
d9ea73e0 JH |
65 | consume_one(priv, mb[i].ptr, mb[i].size); |
66 | continue; | |
67 | } | |
b4637760 BD |
68 | strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size); |
69 | consume_one(priv, priv->remainder.buf, priv->remainder.len); | |
70 | strbuf_reset(&priv->remainder); | |
d9ea73e0 | 71 | } |
b4637760 BD |
72 | if (priv->remainder.len) { |
73 | consume_one(priv, priv->remainder.buf, priv->remainder.len); | |
74 | strbuf_reset(&priv->remainder); | |
d9ea73e0 JH |
75 | } |
76 | return 0; | |
77 | } | |
7cab5883 | 78 | |
913b45f5 JH |
79 | /* |
80 | * Trim down common substring at the end of the buffers, | |
e0876bca | 81 | * but end on a complete line. |
913b45f5 | 82 | */ |
e0876bca | 83 | static void trim_common_tail(mmfile_t *a, mmfile_t *b) |
913b45f5 JH |
84 | { |
85 | const int blk = 1024; | |
52499977 | 86 | long trimmed = 0, recovered = 0; |
913b45f5 JH |
87 | char *ap = a->ptr + a->size; |
88 | char *bp = b->ptr + b->size; | |
89 | long smaller = (a->size < b->size) ? a->size : b->size; | |
90 | ||
91 | while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) { | |
92 | trimmed += blk; | |
93 | ap -= blk; | |
94 | bp -= blk; | |
95 | } | |
96 | ||
d2f82950 | 97 | while (recovered < trimmed) |
52499977 | 98 | if (ap[recovered++] == '\n') |
d2f82950 LT |
99 | break; |
100 | a->size -= trimmed - recovered; | |
101 | b->size -= trimmed - recovered; | |
913b45f5 JH |
102 | } |
103 | ||
c279d7e9 JH |
104 | int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb) |
105 | { | |
913b45f5 JH |
106 | mmfile_t a = *mf1; |
107 | mmfile_t b = *mf2; | |
108 | ||
dcd1742e JK |
109 | if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE) |
110 | return -1; | |
111 | ||
e0876bca RS |
112 | if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT)) |
113 | trim_common_tail(&a, &b); | |
913b45f5 JH |
114 | |
115 | return xdl_diff(&a, &b, xpp, xecfg, xecb); | |
c279d7e9 JH |
116 | } |
117 | ||
3b40a090 JK |
118 | void discard_hunk_line(void *priv, |
119 | long ob, long on, long nb, long nn, | |
120 | const char *func, long funclen) | |
121 | { | |
122 | } | |
123 | ||
c99db9d2 | 124 | int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2, |
9346d6d1 JK |
125 | xdiff_emit_hunk_fn hunk_fn, |
126 | xdiff_emit_line_fn line_fn, | |
127 | void *consume_callback_data, | |
dfea7900 | 128 | xpparam_t const *xpp, xdemitconf_t const *xecfg) |
c99db9d2 BD |
129 | { |
130 | int ret; | |
8a3f524b | 131 | struct xdiff_emit_state state; |
dfea7900 | 132 | xdemitcb_t ecb; |
8a3f524b JH |
133 | |
134 | memset(&state, 0, sizeof(state)); | |
9346d6d1 JK |
135 | state.hunk_fn = hunk_fn; |
136 | state.line_fn = line_fn; | |
8a3f524b | 137 | state.consume_callback_data = consume_callback_data; |
dfea7900 | 138 | memset(&ecb, 0, sizeof(ecb)); |
9346d6d1 JK |
139 | if (hunk_fn) |
140 | ecb.out_hunk = xdiff_out_hunk; | |
611e42a5 | 141 | ecb.out_line = xdiff_outf; |
dfea7900 | 142 | ecb.priv = &state; |
8a3f524b | 143 | strbuf_init(&state.remainder, 0); |
dfea7900 | 144 | ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb); |
8a3f524b | 145 | strbuf_release(&state.remainder); |
c99db9d2 BD |
146 | return ret; |
147 | } | |
148 | ||
7cab5883 JS |
149 | int read_mmfile(mmfile_t *ptr, const char *filename) |
150 | { | |
151 | struct stat st; | |
152 | FILE *f; | |
dc49cd76 | 153 | size_t sz; |
7cab5883 JS |
154 | |
155 | if (stat(filename, &st)) | |
5118d7f4 | 156 | return error_errno("Could not stat %s", filename); |
7cab5883 | 157 | if ((f = fopen(filename, "rb")) == NULL) |
5118d7f4 | 158 | return error_errno("Could not open %s", filename); |
dc49cd76 | 159 | sz = xsize_t(st.st_size); |
381b851c | 160 | ptr->ptr = xmalloc(sz ? sz : 1); |
5fd89814 RS |
161 | if (sz && fread(ptr->ptr, sz, 1, f) != 1) { |
162 | fclose(f); | |
7cab5883 | 163 | return error("Could not read %s", filename); |
5fd89814 | 164 | } |
7cab5883 | 165 | fclose(f); |
dc49cd76 | 166 | ptr->size = sz; |
7cab5883 JS |
167 | return 0; |
168 | } | |
169 | ||
d449347d | 170 | void read_mmblob(mmfile_t *ptr, const struct object_id *oid) |
06b65939 ML |
171 | { |
172 | unsigned long size; | |
173 | enum object_type type; | |
174 | ||
4a7e27e9 | 175 | if (oideq(oid, &null_oid)) { |
06b65939 ML |
176 | ptr->ptr = xstrdup(""); |
177 | ptr->size = 0; | |
178 | return; | |
179 | } | |
180 | ||
b4f5aca4 | 181 | ptr->ptr = read_object_file(oid, &type, &size); |
06b65939 | 182 | if (!ptr->ptr || type != OBJ_BLOB) |
d449347d | 183 | die("unable to read blob object %s", oid_to_hex(oid)); |
06b65939 ML |
184 | ptr->size = size; |
185 | } | |
186 | ||
6bfce93e JS |
187 | #define FIRST_FEW_BYTES 8000 |
188 | int buffer_is_binary(const char *ptr, unsigned long size) | |
189 | { | |
190 | if (FIRST_FEW_BYTES < size) | |
191 | size = FIRST_FEW_BYTES; | |
192 | return !!memchr(ptr, 0, size); | |
193 | } | |
f258475a JH |
194 | |
195 | struct ff_regs { | |
196 | int nr; | |
197 | struct ff_reg { | |
198 | regex_t re; | |
199 | int negate; | |
200 | } *array; | |
201 | }; | |
202 | ||
203 | static long ff_regexp(const char *line, long len, | |
204 | char *buffer, long buffer_size, void *priv) | |
205 | { | |
f258475a JH |
206 | struct ff_regs *regs = priv; |
207 | regmatch_t pmatch[2]; | |
3d8dccd7 | 208 | int i; |
b7d36ffc | 209 | int result; |
f258475a | 210 | |
a5a5a048 BC |
211 | /* Exclude terminating newline (and cr) from matching */ |
212 | if (len > 0 && line[len-1] == '\n') { | |
46dc1b0e JH |
213 | if (len > 1 && line[len-2] == '\r') |
214 | len -= 2; | |
215 | else | |
216 | len--; | |
217 | } | |
218 | ||
f258475a JH |
219 | for (i = 0; i < regs->nr; i++) { |
220 | struct ff_reg *reg = regs->array + i; | |
b7d36ffc | 221 | if (!regexec_buf(®->re, line, len, 2, pmatch, 0)) { |
3d8dccd7 | 222 | if (reg->negate) |
b7d36ffc | 223 | return -1; |
3d8dccd7 | 224 | break; |
f258475a JH |
225 | } |
226 | } | |
3d8dccd7 | 227 | if (regs->nr <= i) |
b7d36ffc | 228 | return -1; |
f258475a JH |
229 | i = pmatch[1].rm_so >= 0 ? 1 : 0; |
230 | line += pmatch[i].rm_so; | |
231 | result = pmatch[i].rm_eo - pmatch[i].rm_so; | |
232 | if (result > buffer_size) | |
233 | result = buffer_size; | |
1b6ecbad BC |
234 | while (result > 0 && (isspace(line[result - 1]))) |
235 | result--; | |
f258475a | 236 | memcpy(buffer, line, result); |
f258475a JH |
237 | return result; |
238 | } | |
239 | ||
a013585b | 240 | void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags) |
f258475a JH |
241 | { |
242 | int i; | |
243 | struct ff_regs *regs; | |
244 | ||
245 | xecfg->find_func = ff_regexp; | |
246 | regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs)); | |
247 | for (i = 0, regs->nr = 1; value[i]; i++) | |
248 | if (value[i] == '\n') | |
249 | regs->nr++; | |
b32fa95f | 250 | ALLOC_ARRAY(regs->array, regs->nr); |
f258475a JH |
251 | for (i = 0; i < regs->nr; i++) { |
252 | struct ff_reg *reg = regs->array + i; | |
253 | const char *ep = strchr(value, '\n'), *expression; | |
254 | char *buffer = NULL; | |
255 | ||
256 | reg->negate = (*value == '!'); | |
257 | if (reg->negate && i == regs->nr - 1) | |
258 | die("Last expression must not be negated: %s", value); | |
259 | if (*value == '!') | |
260 | value++; | |
261 | if (ep) | |
262 | expression = buffer = xstrndup(value, ep - value); | |
263 | else | |
264 | expression = value; | |
a013585b | 265 | if (regcomp(®->re, expression, cflags)) |
f258475a | 266 | die("Invalid regexp to look for hunk header: %s", expression); |
8e0f7003 | 267 | free(buffer); |
f258475a JH |
268 | value = ep + 1; |
269 | } | |
270 | } | |
b5412484 | 271 | |
8cfe5f1c RS |
272 | void xdiff_clear_find_func(xdemitconf_t *xecfg) |
273 | { | |
274 | if (xecfg->find_func) { | |
275 | int i; | |
276 | struct ff_regs *regs = xecfg->find_func_priv; | |
277 | ||
278 | for (i = 0; i < regs->nr; i++) | |
279 | regfree(®s->array[i].re); | |
280 | free(regs->array); | |
281 | free(regs); | |
282 | xecfg->find_func = NULL; | |
283 | xecfg->find_func_priv = NULL; | |
284 | } | |
285 | } | |
286 | ||
5ec8274b SB |
287 | unsigned long xdiff_hash_string(const char *s, size_t len, long flags) |
288 | { | |
289 | return xdl_hash_record(&s, s + len, flags); | |
290 | } | |
291 | ||
292 | int xdiff_compare_lines(const char *l1, long s1, | |
293 | const char *l2, long s2, long flags) | |
294 | { | |
295 | return xdl_recmatch(l1, s1, l2, s2, flags); | |
296 | } | |
297 | ||
b5412484 JH |
298 | int git_xmerge_style = -1; |
299 | ||
300 | int git_xmerge_config(const char *var, const char *value, void *cb) | |
301 | { | |
8c2be75f | 302 | if (!strcmp(var, "merge.conflictstyle")) { |
b5412484 JH |
303 | if (!value) |
304 | die("'%s' is not a boolean", var); | |
305 | if (!strcmp(value, "diff3")) | |
306 | git_xmerge_style = XDL_MERGE_DIFF3; | |
307 | else if (!strcmp(value, "merge")) | |
308 | git_xmerge_style = 0; | |
309 | else | |
310 | die("unknown style '%s' given for '%s'", | |
311 | value, var); | |
312 | return 0; | |
313 | } | |
314 | return git_default_config(var, value, cb); | |
315 | } |