Merge branch 'ps/reflog-list' into HEAD
[alt-git.git] / xdiff-interface.c
blob3162f517434353609e1fa45ab67ea3cf4623f8a3
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "object-store-ll.h"
6 #include "strbuf.h"
7 #include "xdiff-interface.h"
8 #include "xdiff/xtypes.h"
9 #include "xdiff/xdiffi.h"
10 #include "xdiff/xutils.h"
12 struct xdiff_emit_state {
13 xdiff_emit_hunk_fn hunk_fn;
14 xdiff_emit_line_fn line_fn;
15 void *consume_callback_data;
16 struct strbuf remainder;
19 static int xdiff_out_hunk(void *priv_,
20 long old_begin, long old_nr,
21 long new_begin, long new_nr,
22 const char *func, long funclen)
24 struct xdiff_emit_state *priv = priv_;
26 if (priv->remainder.len)
27 BUG("xdiff emitted hunk in the middle of a line");
29 priv->hunk_fn(priv->consume_callback_data,
30 old_begin, old_nr, new_begin, new_nr,
31 func, funclen);
32 return 0;
35 static int consume_one(void *priv_, char *s, unsigned long size)
37 struct xdiff_emit_state *priv = priv_;
38 char *ep;
39 while (size) {
40 unsigned long this_size;
41 int ret;
42 ep = memchr(s, '\n', size);
43 this_size = (ep == NULL) ? size : (ep - s + 1);
44 ret = priv->line_fn(priv->consume_callback_data, s, this_size);
45 if (ret)
46 return ret;
47 size -= this_size;
48 s += this_size;
50 return 0;
53 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
55 struct xdiff_emit_state *priv = priv_;
56 int i;
57 int stop = 0;
59 if (!priv->line_fn)
60 return 0;
62 for (i = 0; i < nbuf; i++) {
63 if (stop)
64 return 1;
65 if (mb[i].ptr[mb[i].size-1] != '\n') {
66 /* Incomplete line */
67 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
68 continue;
71 /* we have a complete line */
72 if (!priv->remainder.len) {
73 stop = consume_one(priv, mb[i].ptr, mb[i].size);
74 continue;
76 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
77 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
78 strbuf_reset(&priv->remainder);
80 if (stop)
81 return -1;
82 if (priv->remainder.len) {
83 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
84 strbuf_reset(&priv->remainder);
86 if (stop)
87 return -1;
88 return 0;
92 * Trim down common substring at the end of the buffers,
93 * but end on a complete line.
95 static void trim_common_tail(mmfile_t *a, mmfile_t *b)
97 const int blk = 1024;
98 long trimmed = 0, recovered = 0;
99 char *ap = a->size ? a->ptr + a->size : a->ptr;
100 char *bp = b->size ? b->ptr + b->size : b->ptr;
101 long smaller = (a->size < b->size) ? a->size : b->size;
103 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
104 trimmed += blk;
105 ap -= blk;
106 bp -= blk;
109 while (recovered < trimmed)
110 if (ap[recovered++] == '\n')
111 break;
112 a->size -= trimmed - recovered;
113 b->size -= trimmed - recovered;
116 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
118 mmfile_t a = *mf1;
119 mmfile_t b = *mf2;
121 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
122 return -1;
124 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
125 trim_common_tail(&a, &b);
127 return xdl_diff(&a, &b, xpp, xecfg, xecb);
130 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
131 xdiff_emit_hunk_fn hunk_fn,
132 xdiff_emit_line_fn line_fn,
133 void *consume_callback_data,
134 xpparam_t const *xpp, xdemitconf_t const *xecfg)
136 int ret;
137 struct xdiff_emit_state state;
138 xdemitcb_t ecb;
140 memset(&state, 0, sizeof(state));
141 state.hunk_fn = hunk_fn;
142 state.line_fn = line_fn;
143 state.consume_callback_data = consume_callback_data;
144 memset(&ecb, 0, sizeof(ecb));
145 if (hunk_fn)
146 ecb.out_hunk = xdiff_out_hunk;
147 ecb.out_line = xdiff_outf;
148 ecb.priv = &state;
149 strbuf_init(&state.remainder, 0);
150 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
151 strbuf_release(&state.remainder);
152 return ret;
155 int read_mmfile(mmfile_t *ptr, const char *filename)
157 struct stat st;
158 FILE *f;
159 size_t sz;
161 if (stat(filename, &st))
162 return error_errno("Could not stat %s", filename);
163 if (!(f = fopen(filename, "rb")))
164 return error_errno("Could not open %s", filename);
165 sz = xsize_t(st.st_size);
166 ptr->ptr = xmalloc(sz ? sz : 1);
167 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
168 fclose(f);
169 return error("Could not read %s", filename);
171 fclose(f);
172 ptr->size = sz;
173 return 0;
176 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
178 unsigned long size;
179 enum object_type type;
181 if (oideq(oid, null_oid())) {
182 ptr->ptr = xstrdup("");
183 ptr->size = 0;
184 return;
187 ptr->ptr = repo_read_object_file(the_repository, oid, &type, &size);
188 if (!ptr->ptr || type != OBJ_BLOB)
189 die("unable to read blob object %s", oid_to_hex(oid));
190 ptr->size = size;
193 #define FIRST_FEW_BYTES 8000
194 int buffer_is_binary(const char *ptr, unsigned long size)
196 if (FIRST_FEW_BYTES < size)
197 size = FIRST_FEW_BYTES;
198 return !!memchr(ptr, 0, size);
201 struct ff_regs {
202 int nr;
203 struct ff_reg {
204 regex_t re;
205 int negate;
206 } *array;
209 static long ff_regexp(const char *line, long len,
210 char *buffer, long buffer_size, void *priv)
212 struct ff_regs *regs = priv;
213 regmatch_t pmatch[2];
214 int i;
215 int result;
217 /* Exclude terminating newline (and cr) from matching */
218 if (len > 0 && line[len-1] == '\n') {
219 if (len > 1 && line[len-2] == '\r')
220 len -= 2;
221 else
222 len--;
225 for (i = 0; i < regs->nr; i++) {
226 struct ff_reg *reg = regs->array + i;
227 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
228 if (reg->negate)
229 return -1;
230 break;
233 if (regs->nr <= i)
234 return -1;
235 i = pmatch[1].rm_so >= 0 ? 1 : 0;
236 line += pmatch[i].rm_so;
237 result = pmatch[i].rm_eo - pmatch[i].rm_so;
238 if (result > buffer_size)
239 result = buffer_size;
240 while (result > 0 && (isspace(line[result - 1])))
241 result--;
242 memcpy(buffer, line, result);
243 return result;
246 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
248 int i;
249 struct ff_regs *regs;
251 xecfg->find_func = ff_regexp;
252 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
253 for (i = 0, regs->nr = 1; value[i]; i++)
254 if (value[i] == '\n')
255 regs->nr++;
256 ALLOC_ARRAY(regs->array, regs->nr);
257 for (i = 0; i < regs->nr; i++) {
258 struct ff_reg *reg = regs->array + i;
259 const char *ep, *expression;
260 char *buffer = NULL;
262 if (!value)
263 BUG("mismatch between line count and parsing");
264 ep = strchr(value, '\n');
266 reg->negate = (*value == '!');
267 if (reg->negate && i == regs->nr - 1)
268 die("Last expression must not be negated: %s", value);
269 if (*value == '!')
270 value++;
271 if (ep)
272 expression = buffer = xstrndup(value, ep - value);
273 else
274 expression = value;
275 if (regcomp(&reg->re, expression, cflags))
276 die("Invalid regexp to look for hunk header: %s", expression);
277 free(buffer);
278 value = ep ? ep + 1 : NULL;
282 void xdiff_clear_find_func(xdemitconf_t *xecfg)
284 if (xecfg->find_func) {
285 int i;
286 struct ff_regs *regs = xecfg->find_func_priv;
288 for (i = 0; i < regs->nr; i++)
289 regfree(&regs->array[i].re);
290 free(regs->array);
291 free(regs);
292 xecfg->find_func = NULL;
293 xecfg->find_func_priv = NULL;
297 unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
299 return xdl_hash_record(&s, s + len, flags);
302 int xdiff_compare_lines(const char *l1, long s1,
303 const char *l2, long s2, long flags)
305 return xdl_recmatch(l1, s1, l2, s2, flags);
308 int git_xmerge_style = -1;
310 int git_xmerge_config(const char *var, const char *value,
311 const struct config_context *ctx, void *cb)
313 if (!strcmp(var, "merge.conflictstyle")) {
314 if (!value)
315 return config_error_nonbool(var);
316 if (!strcmp(value, "diff3"))
317 git_xmerge_style = XDL_MERGE_DIFF3;
318 else if (!strcmp(value, "zdiff3"))
319 git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3;
320 else if (!strcmp(value, "merge"))
321 git_xmerge_style = 0;
323 * Please update _git_checkout() in
324 * git-completion.bash when you add new merge config
326 else
327 return error(_("unknown style '%s' given for '%s'"),
328 value, var);
329 return 0;
331 return git_default_config(var, value, ctx, cb);