http: refactor finish_http_pack_request()
[git.git] / xdiff-interface.c
blob4d20069302b25a133869380bd685e921ddc0bacc
1 #include "cache.h"
2 #include "config.h"
3 #include "object-store.h"
4 #include "xdiff-interface.h"
5 #include "xdiff/xtypes.h"
6 #include "xdiff/xdiffi.h"
7 #include "xdiff/xemit.h"
8 #include "xdiff/xmacros.h"
9 #include "xdiff/xutils.h"
11 struct xdiff_emit_state {
12 xdiff_emit_hunk_fn hunk_fn;
13 xdiff_emit_line_fn line_fn;
14 void *consume_callback_data;
15 struct strbuf remainder;
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)
23 struct xdiff_emit_state *priv = priv_;
25 if (priv->remainder.len)
26 BUG("xdiff emitted hunk in the middle of a line");
28 priv->hunk_fn(priv->consume_callback_data,
29 old_begin, old_nr, new_begin, new_nr,
30 func, funclen);
31 return 0;
34 static void consume_one(void *priv_, char *s, unsigned long size)
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);
42 priv->line_fn(priv->consume_callback_data, s, this_size);
43 size -= this_size;
44 s += this_size;
48 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
50 struct xdiff_emit_state *priv = priv_;
51 int i;
53 if (!priv->line_fn)
54 return 0;
56 for (i = 0; i < nbuf; i++) {
57 if (mb[i].ptr[mb[i].size-1] != '\n') {
58 /* Incomplete line */
59 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
60 continue;
63 /* we have a complete line */
64 if (!priv->remainder.len) {
65 consume_one(priv, mb[i].ptr, mb[i].size);
66 continue;
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);
72 if (priv->remainder.len) {
73 consume_one(priv, priv->remainder.buf, priv->remainder.len);
74 strbuf_reset(&priv->remainder);
76 return 0;
80 * Trim down common substring at the end of the buffers,
81 * but end on a complete line.
83 static void trim_common_tail(mmfile_t *a, mmfile_t *b)
85 const int blk = 1024;
86 long trimmed = 0, recovered = 0;
87 char *ap = a->size ? a->ptr + a->size : a->ptr;
88 char *bp = b->size ? b->ptr + b->size : b->ptr;
89 long smaller = (a->size < b->size) ? a->size : b->size;
91 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
92 trimmed += blk;
93 ap -= blk;
94 bp -= blk;
97 while (recovered < trimmed)
98 if (ap[recovered++] == '\n')
99 break;
100 a->size -= trimmed - recovered;
101 b->size -= trimmed - recovered;
104 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
106 mmfile_t a = *mf1;
107 mmfile_t b = *mf2;
109 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
110 return -1;
112 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
113 trim_common_tail(&a, &b);
115 return xdl_diff(&a, &b, xpp, xecfg, xecb);
118 void discard_hunk_line(void *priv,
119 long ob, long on, long nb, long nn,
120 const char *func, long funclen)
124 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
125 xdiff_emit_hunk_fn hunk_fn,
126 xdiff_emit_line_fn line_fn,
127 void *consume_callback_data,
128 xpparam_t const *xpp, xdemitconf_t const *xecfg)
130 int ret;
131 struct xdiff_emit_state state;
132 xdemitcb_t ecb;
134 memset(&state, 0, sizeof(state));
135 state.hunk_fn = hunk_fn;
136 state.line_fn = line_fn;
137 state.consume_callback_data = consume_callback_data;
138 memset(&ecb, 0, sizeof(ecb));
139 if (hunk_fn)
140 ecb.out_hunk = xdiff_out_hunk;
141 ecb.out_line = xdiff_outf;
142 ecb.priv = &state;
143 strbuf_init(&state.remainder, 0);
144 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
145 strbuf_release(&state.remainder);
146 return ret;
149 int read_mmfile(mmfile_t *ptr, const char *filename)
151 struct stat st;
152 FILE *f;
153 size_t sz;
155 if (stat(filename, &st))
156 return error_errno("Could not stat %s", filename);
157 if ((f = fopen(filename, "rb")) == NULL)
158 return error_errno("Could not open %s", filename);
159 sz = xsize_t(st.st_size);
160 ptr->ptr = xmalloc(sz ? sz : 1);
161 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
162 fclose(f);
163 return error("Could not read %s", filename);
165 fclose(f);
166 ptr->size = sz;
167 return 0;
170 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
172 unsigned long size;
173 enum object_type type;
175 if (oideq(oid, &null_oid)) {
176 ptr->ptr = xstrdup("");
177 ptr->size = 0;
178 return;
181 ptr->ptr = read_object_file(oid, &type, &size);
182 if (!ptr->ptr || type != OBJ_BLOB)
183 die("unable to read blob object %s", oid_to_hex(oid));
184 ptr->size = size;
187 #define FIRST_FEW_BYTES 8000
188 int buffer_is_binary(const char *ptr, unsigned long size)
190 if (FIRST_FEW_BYTES < size)
191 size = FIRST_FEW_BYTES;
192 return !!memchr(ptr, 0, size);
195 struct ff_regs {
196 int nr;
197 struct ff_reg {
198 regex_t re;
199 int negate;
200 } *array;
203 static long ff_regexp(const char *line, long len,
204 char *buffer, long buffer_size, void *priv)
206 struct ff_regs *regs = priv;
207 regmatch_t pmatch[2];
208 int i;
209 int result;
211 /* Exclude terminating newline (and cr) from matching */
212 if (len > 0 && line[len-1] == '\n') {
213 if (len > 1 && line[len-2] == '\r')
214 len -= 2;
215 else
216 len--;
219 for (i = 0; i < regs->nr; i++) {
220 struct ff_reg *reg = regs->array + i;
221 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
222 if (reg->negate)
223 return -1;
224 break;
227 if (regs->nr <= i)
228 return -1;
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;
234 while (result > 0 && (isspace(line[result - 1])))
235 result--;
236 memcpy(buffer, line, result);
237 return result;
240 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
242 int i;
243 struct ff_regs *regs;
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++;
250 ALLOC_ARRAY(regs->array, regs->nr);
251 for (i = 0; i < regs->nr; i++) {
252 struct ff_reg *reg = regs->array + i;
253 const char *ep, *expression;
254 char *buffer = NULL;
256 if (!value)
257 BUG("mismatch between line count and parsing");
258 ep = strchr(value, '\n');
260 reg->negate = (*value == '!');
261 if (reg->negate && i == regs->nr - 1)
262 die("Last expression must not be negated: %s", value);
263 if (*value == '!')
264 value++;
265 if (ep)
266 expression = buffer = xstrndup(value, ep - value);
267 else
268 expression = value;
269 if (regcomp(&reg->re, expression, cflags))
270 die("Invalid regexp to look for hunk header: %s", expression);
271 free(buffer);
272 value = ep ? ep + 1 : NULL;
276 void xdiff_clear_find_func(xdemitconf_t *xecfg)
278 if (xecfg->find_func) {
279 int i;
280 struct ff_regs *regs = xecfg->find_func_priv;
282 for (i = 0; i < regs->nr; i++)
283 regfree(&regs->array[i].re);
284 free(regs->array);
285 free(regs);
286 xecfg->find_func = NULL;
287 xecfg->find_func_priv = NULL;
291 unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
293 return xdl_hash_record(&s, s + len, flags);
296 int xdiff_compare_lines(const char *l1, long s1,
297 const char *l2, long s2, long flags)
299 return xdl_recmatch(l1, s1, l2, s2, flags);
302 int git_xmerge_style = -1;
304 int git_xmerge_config(const char *var, const char *value, void *cb)
306 if (!strcmp(var, "merge.conflictstyle")) {
307 if (!value)
308 die("'%s' is not a boolean", var);
309 if (!strcmp(value, "diff3"))
310 git_xmerge_style = XDL_MERGE_DIFF3;
311 else if (!strcmp(value, "merge"))
312 git_xmerge_style = 0;
314 * Please update _git_checkout() in
315 * git-completion.bash when you add new merge config
317 else
318 die("unknown style '%s' given for '%s'",
319 value, var);
320 return 0;
322 return git_default_config(var, value, cb);