subtree: disallow --no-{help,quiet,debug,branch,message}
[git.git] / xdiff-interface.c
blobadcea109fa9a54eaa96c67c1b5e8a1689af253af
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "hex.h"
4 #include "object-store-ll.h"
5 #include "strbuf.h"
6 #include "xdiff-interface.h"
7 #include "xdiff/xtypes.h"
8 #include "xdiff/xdiffi.h"
9 #include "xdiff/xemit.h"
10 #include "xdiff/xmacros.h"
11 #include "xdiff/xutils.h"
13 struct xdiff_emit_state {
14 xdiff_emit_hunk_fn hunk_fn;
15 xdiff_emit_line_fn line_fn;
16 void *consume_callback_data;
17 struct strbuf remainder;
20 static int xdiff_out_hunk(void *priv_,
21 long old_begin, long old_nr,
22 long new_begin, long new_nr,
23 const char *func, long funclen)
25 struct xdiff_emit_state *priv = priv_;
27 if (priv->remainder.len)
28 BUG("xdiff emitted hunk in the middle of a line");
30 priv->hunk_fn(priv->consume_callback_data,
31 old_begin, old_nr, new_begin, new_nr,
32 func, funclen);
33 return 0;
36 static int consume_one(void *priv_, char *s, unsigned long size)
38 struct xdiff_emit_state *priv = priv_;
39 char *ep;
40 while (size) {
41 unsigned long this_size;
42 int ret;
43 ep = memchr(s, '\n', size);
44 this_size = (ep == NULL) ? size : (ep - s + 1);
45 ret = priv->line_fn(priv->consume_callback_data, s, this_size);
46 if (ret)
47 return ret;
48 size -= this_size;
49 s += this_size;
51 return 0;
54 static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
56 struct xdiff_emit_state *priv = priv_;
57 int i;
58 int stop = 0;
60 if (!priv->line_fn)
61 return 0;
63 for (i = 0; i < nbuf; i++) {
64 if (stop)
65 return 1;
66 if (mb[i].ptr[mb[i].size-1] != '\n') {
67 /* Incomplete line */
68 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
69 continue;
72 /* we have a complete line */
73 if (!priv->remainder.len) {
74 stop = consume_one(priv, mb[i].ptr, mb[i].size);
75 continue;
77 strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
78 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
79 strbuf_reset(&priv->remainder);
81 if (stop)
82 return -1;
83 if (priv->remainder.len) {
84 stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
85 strbuf_reset(&priv->remainder);
87 if (stop)
88 return -1;
89 return 0;
93 * Trim down common substring at the end of the buffers,
94 * but end on a complete line.
96 static void trim_common_tail(mmfile_t *a, mmfile_t *b)
98 const int blk = 1024;
99 long trimmed = 0, recovered = 0;
100 char *ap = a->size ? a->ptr + a->size : a->ptr;
101 char *bp = b->size ? b->ptr + b->size : b->ptr;
102 long smaller = (a->size < b->size) ? a->size : b->size;
104 while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) {
105 trimmed += blk;
106 ap -= blk;
107 bp -= blk;
110 while (recovered < trimmed)
111 if (ap[recovered++] == '\n')
112 break;
113 a->size -= trimmed - recovered;
114 b->size -= trimmed - recovered;
117 int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb)
119 mmfile_t a = *mf1;
120 mmfile_t b = *mf2;
122 if (mf1->size > MAX_XDIFF_SIZE || mf2->size > MAX_XDIFF_SIZE)
123 return -1;
125 if (!xecfg->ctxlen && !(xecfg->flags & XDL_EMIT_FUNCCONTEXT))
126 trim_common_tail(&a, &b);
128 return xdl_diff(&a, &b, xpp, xecfg, xecb);
131 int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
132 xdiff_emit_hunk_fn hunk_fn,
133 xdiff_emit_line_fn line_fn,
134 void *consume_callback_data,
135 xpparam_t const *xpp, xdemitconf_t const *xecfg)
137 int ret;
138 struct xdiff_emit_state state;
139 xdemitcb_t ecb;
141 memset(&state, 0, sizeof(state));
142 state.hunk_fn = hunk_fn;
143 state.line_fn = line_fn;
144 state.consume_callback_data = consume_callback_data;
145 memset(&ecb, 0, sizeof(ecb));
146 if (hunk_fn)
147 ecb.out_hunk = xdiff_out_hunk;
148 ecb.out_line = xdiff_outf;
149 ecb.priv = &state;
150 strbuf_init(&state.remainder, 0);
151 ret = xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
152 strbuf_release(&state.remainder);
153 return ret;
156 int read_mmfile(mmfile_t *ptr, const char *filename)
158 struct stat st;
159 FILE *f;
160 size_t sz;
162 if (stat(filename, &st))
163 return error_errno("Could not stat %s", filename);
164 if (!(f = fopen(filename, "rb")))
165 return error_errno("Could not open %s", filename);
166 sz = xsize_t(st.st_size);
167 ptr->ptr = xmalloc(sz ? sz : 1);
168 if (sz && fread(ptr->ptr, sz, 1, f) != 1) {
169 fclose(f);
170 return error("Could not read %s", filename);
172 fclose(f);
173 ptr->size = sz;
174 return 0;
177 void read_mmblob(mmfile_t *ptr, const struct object_id *oid)
179 unsigned long size;
180 enum object_type type;
182 if (oideq(oid, null_oid())) {
183 ptr->ptr = xstrdup("");
184 ptr->size = 0;
185 return;
188 ptr->ptr = repo_read_object_file(the_repository, oid, &type, &size);
189 if (!ptr->ptr || type != OBJ_BLOB)
190 die("unable to read blob object %s", oid_to_hex(oid));
191 ptr->size = size;
194 #define FIRST_FEW_BYTES 8000
195 int buffer_is_binary(const char *ptr, unsigned long size)
197 if (FIRST_FEW_BYTES < size)
198 size = FIRST_FEW_BYTES;
199 return !!memchr(ptr, 0, size);
202 struct ff_regs {
203 int nr;
204 struct ff_reg {
205 regex_t re;
206 int negate;
207 } *array;
210 static long ff_regexp(const char *line, long len,
211 char *buffer, long buffer_size, void *priv)
213 struct ff_regs *regs = priv;
214 regmatch_t pmatch[2];
215 int i;
216 int result;
218 /* Exclude terminating newline (and cr) from matching */
219 if (len > 0 && line[len-1] == '\n') {
220 if (len > 1 && line[len-2] == '\r')
221 len -= 2;
222 else
223 len--;
226 for (i = 0; i < regs->nr; i++) {
227 struct ff_reg *reg = regs->array + i;
228 if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
229 if (reg->negate)
230 return -1;
231 break;
234 if (regs->nr <= i)
235 return -1;
236 i = pmatch[1].rm_so >= 0 ? 1 : 0;
237 line += pmatch[i].rm_so;
238 result = pmatch[i].rm_eo - pmatch[i].rm_so;
239 if (result > buffer_size)
240 result = buffer_size;
241 while (result > 0 && (isspace(line[result - 1])))
242 result--;
243 memcpy(buffer, line, result);
244 return result;
247 void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)
249 int i;
250 struct ff_regs *regs;
252 xecfg->find_func = ff_regexp;
253 regs = xecfg->find_func_priv = xmalloc(sizeof(struct ff_regs));
254 for (i = 0, regs->nr = 1; value[i]; i++)
255 if (value[i] == '\n')
256 regs->nr++;
257 ALLOC_ARRAY(regs->array, regs->nr);
258 for (i = 0; i < regs->nr; i++) {
259 struct ff_reg *reg = regs->array + i;
260 const char *ep, *expression;
261 char *buffer = NULL;
263 if (!value)
264 BUG("mismatch between line count and parsing");
265 ep = strchr(value, '\n');
267 reg->negate = (*value == '!');
268 if (reg->negate && i == regs->nr - 1)
269 die("Last expression must not be negated: %s", value);
270 if (*value == '!')
271 value++;
272 if (ep)
273 expression = buffer = xstrndup(value, ep - value);
274 else
275 expression = value;
276 if (regcomp(&reg->re, expression, cflags))
277 die("Invalid regexp to look for hunk header: %s", expression);
278 free(buffer);
279 value = ep ? ep + 1 : NULL;
283 void xdiff_clear_find_func(xdemitconf_t *xecfg)
285 if (xecfg->find_func) {
286 int i;
287 struct ff_regs *regs = xecfg->find_func_priv;
289 for (i = 0; i < regs->nr; i++)
290 regfree(&regs->array[i].re);
291 free(regs->array);
292 free(regs);
293 xecfg->find_func = NULL;
294 xecfg->find_func_priv = NULL;
298 unsigned long xdiff_hash_string(const char *s, size_t len, long flags)
300 return xdl_hash_record(&s, s + len, flags);
303 int xdiff_compare_lines(const char *l1, long s1,
304 const char *l2, long s2, long flags)
306 return xdl_recmatch(l1, s1, l2, s2, flags);
309 int git_xmerge_style = -1;
311 int git_xmerge_config(const char *var, const char *value,
312 const struct config_context *ctx, void *cb)
314 if (!strcmp(var, "merge.conflictstyle")) {
315 if (!value)
316 die("'%s' is not a boolean", var);
317 if (!strcmp(value, "diff3"))
318 git_xmerge_style = XDL_MERGE_DIFF3;
319 else if (!strcmp(value, "zdiff3"))
320 git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3;
321 else if (!strcmp(value, "merge"))
322 git_xmerge_style = 0;
324 * Please update _git_checkout() in
325 * git-completion.bash when you add new merge config
327 else
328 die("unknown style '%s' given for '%s'",
329 value, var);
330 return 0;
332 return git_default_config(var, value, ctx, cb);