Mercurial > hg
changeset 36763:90f8fe72446c
xdiff: remove xemit related logic
xemit handles "diff formatting and output" with options like context lines,
whether show function names, etc. That is handled more cleanly at a higher
level in hg.
Removing context line parameters would also make the trimming logic (D2686)
cleaner and more confident. See [1].
[1]: https://github.com/git/git/commit/d2f82950a9226ae1102a7a97f03440a4bf8c6c09
Differential Revision: https://phab.mercurial-scm.org/D2705
author | Jun Wu <quark@fb.com> |
---|---|
date | Tue, 06 Mar 2018 18:41:08 -0800 |
parents | b5bb0f99064d |
children | 3cf40112efb7 |
files | mercurial/cext/bdiff.c mercurial/thirdparty/xdiff/xdiff.h mercurial/thirdparty/xdiff/xdiffi.c mercurial/thirdparty/xdiff/xdiffi.h mercurial/thirdparty/xdiff/xemit.c mercurial/thirdparty/xdiff/xemit.h mercurial/thirdparty/xdiff/xinclude.h mercurial/thirdparty/xdiff/xutils.c mercurial/thirdparty/xdiff/xutils.h setup.py |
diffstat | 10 files changed, 57 insertions(+), 469 deletions(-) [+] |
line wrap: on
line diff
--- a/mercurial/cext/bdiff.c Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/cext/bdiff.c Tue Mar 06 18:41:08 2018 -0800 @@ -280,16 +280,11 @@ XDF_INDENT_HEURISTIC, /* flags */ }; xdemitconf_t xecfg = { - 0, /* ctxlen */ - 0, /* interhunkctxlen */ XDL_EMIT_BDIFFHUNK, /* flags */ - NULL, /* find_func */ - NULL, /* find_func_priv */ hunk_consumer, /* hunk_consume_func */ }; xdemitcb_t ecb = { NULL, /* priv */ - NULL, /* outf */ }; if (!PyArg_ParseTuple(args, PY23("s#s#", "y#y#"), &a.ptr, &la, &b.ptr,
--- a/mercurial/thirdparty/xdiff/xdiff.h Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xdiff.h Tue Mar 06 18:41:08 2018 -0800 @@ -34,9 +34,6 @@ #define XDF_INDENT_HEURISTIC (1 << 23) -/* xdemitconf_t.flags */ -#define XDL_EMIT_FUNCNAMES (1 << 0) -#define XDL_EMIT_FUNCCONTEXT (1 << 2) /* emit bdiff-style "matched" (a1, a2, b1, b2) hunks instead of "different" * (a1, a2 - a1, b1, b2 - b1) hunks */ #define XDL_EMIT_BDIFFHUNK (1 << 4) @@ -71,21 +68,14 @@ typedef struct s_xdemitcb { void *priv; - int (*outf)(void *, mmbuffer_t *, int); } xdemitcb_t; -typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv); - typedef int (*xdl_emit_hunk_consume_func_t)(long start_a, long count_a, long start_b, long count_b, void *cb_data); typedef struct s_xdemitconf { - long ctxlen; - long interhunkctxlen; unsigned long flags; - find_func_t find_func; - void *find_func_priv; xdl_emit_hunk_consume_func_t hunk_func; } xdemitconf_t;
--- a/mercurial/thirdparty/xdiff/xdiffi.c Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xdiffi.c Tue Mar 06 18:41:08 2018 -0800 @@ -1007,10 +1007,66 @@ } } + +/* + * Starting at the passed change atom, find the latest change atom to be included + * inside the differential hunk according to the specified configuration. + * Also advance xscr if the first changes must be discarded. + */ +xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg) +{ + xdchange_t *xch, *xchp, *lxch; + long max_common = 0; + long max_ignorable = 0; + unsigned long ignored = 0; /* number of ignored blank lines */ + + /* remove ignorable changes that are too far before other changes */ + for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { + xch = xchp->next; + + if (xch == NULL || + xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) + *xscr = xch; + } + + if (*xscr == NULL) + return NULL; + + lxch = *xscr; + + for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { + long distance = xch->i1 - (xchp->i1 + xchp->chg1); + if (distance > max_common) + break; + + if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) { + lxch = xch; + ignored = 0; + } else if (distance < max_ignorable && xch->ignore) { + ignored += xch->chg2; + } else if (lxch != xchp && + xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) { + break; + } else if (!xch->ignore) { + lxch = xch; + ignored = 0; + } else { + ignored += xch->chg2; + } + } + + return lxch; +} + + static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg) { xdchange_t *xch, *xche; + + if (!xecfg->hunk_func) + return -1; + if ((xecfg->flags & XDL_EMIT_BDIFFHUNK) != 0) { long i1 = 0, i2 = 0, n1 = xe->xdf1.nrec, n2 = xe->xdf2.nrec; for (xch = xscr; xch; xch = xche->next) { @@ -1045,7 +1101,6 @@ xdemitconf_t const *xecfg, xdemitcb_t *ecb) { xdchange_t *xscr; xdfenv_t xe; - emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff; if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { @@ -1059,7 +1114,7 @@ return -1; } - if (ef(&xe, xscr, ecb, xecfg) < 0) { + if (xdl_call_hunk_func(&xe, xscr, ecb, xecfg) < 0) { xdl_free_script(xscr); xdl_free_env(&xe); return -1;
--- a/mercurial/thirdparty/xdiff/xdiffi.h Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xdiffi.h Tue Mar 06 18:41:08 2018 -0800 @@ -54,7 +54,5 @@ int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags); int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr); void xdl_free_script(xdchange_t *xscr); -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); #endif /* #if !defined(XDIFFI_H) */
--- a/mercurial/thirdparty/xdiff/xemit.c Sun Mar 04 00:17:49 2018 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,312 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - -static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) { - - *rec = xdf->recs[ri]->ptr; - - return xdf->recs[ri]->size; -} - - -static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) { - long size, psize = strlen(pre); - char const *rec; - - size = xdl_get_rec(xdf, ri, &rec); - if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { - - return -1; - } - - return 0; -} - - -/* - * Starting at the passed change atom, find the latest change atom to be included - * inside the differential hunk according to the specified configuration. - * Also advance xscr if the first changes must be discarded. - */ -xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg) -{ - xdchange_t *xch, *xchp, *lxch; - long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen; - long max_ignorable = xecfg->ctxlen; - unsigned long ignored = 0; /* number of ignored blank lines */ - - /* remove ignorable changes that are too far before other changes */ - for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { - xch = xchp->next; - - if (xch == NULL || - xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) - *xscr = xch; - } - - if (*xscr == NULL) - return NULL; - - lxch = *xscr; - - for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { - long distance = xch->i1 - (xchp->i1 + xchp->chg1); - if (distance > max_common) - break; - - if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) { - lxch = xch; - ignored = 0; - } else if (distance < max_ignorable && xch->ignore) { - ignored += xch->chg2; - } else if (lxch != xchp && - xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) { - break; - } else if (!xch->ignore) { - lxch = xch; - ignored = 0; - } else { - ignored += xch->chg2; - } - } - - return lxch; -} - - -static long def_ff(const char *rec, long len, char *buf, long sz, void *priv) -{ - if (len > 0 && - (isalpha((unsigned char)*rec) || /* identifier? */ - *rec == '_' || /* also identifier? */ - *rec == '$')) { /* identifiers from VMS and other esoterico */ - if (len > sz) - len = sz; - while (0 < len && isspace((unsigned char)rec[len - 1])) - len--; - memcpy(buf, rec, len); - return len; - } - return -1; -} - -static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri, - char *buf, long sz) -{ - const char *rec; - long len = xdl_get_rec(xdf, ri, &rec); - if (!xecfg->find_func) - return def_ff(rec, len, buf, sz, xecfg->find_func_priv); - return xecfg->find_func(rec, len, buf, sz, xecfg->find_func_priv); -} - -static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri) -{ - char dummy[1]; - return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0; -} - -struct func_line { - long len; - char buf[80]; -}; - -static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg, - struct func_line *func_line, long start, long limit) -{ - long l, size, step = (start > limit) ? -1 : 1; - char *buf, dummy[1]; - - buf = func_line ? func_line->buf : dummy; - size = func_line ? sizeof(func_line->buf) : sizeof(dummy); - - for (l = start; l != limit && 0 <= l && l < xe->xdf1.nrec; l += step) { - long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size); - if (len >= 0) { - if (func_line) - func_line->len = len; - return l; - } - } - return -1; -} - -static int is_empty_rec(xdfile_t *xdf, long ri) -{ - const char *rec; - long len = xdl_get_rec(xdf, ri, &rec); - - while (len > 0 && XDL_ISSPACE(*rec)) { - rec++; - len--; - } - return !len; -} - -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg) { - long s1, s2, e1, e2, lctx; - xdchange_t *xch, *xche; - long funclineprev = -1; - struct func_line func_line = { 0 }; - - for (xch = xscr; xch; xch = xche->next) { - xche = xdl_get_hunk(&xch, xecfg); - if (!xch) - break; - - s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0); - s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0); - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fs1, i1 = xch->i1; - - /* Appended chunk? */ - if (i1 >= xe->xdf1.nrec) { - long i2 = xch->i2; - - /* - * We don't need additional context if - * a whole function was added. - */ - while (i2 < xe->xdf2.nrec) { - if (is_func_rec(&xe->xdf2, xecfg, i2)) - goto post_context_calculation; - i2++; - } - - /* - * Otherwise get more context from the - * pre-image. - */ - i1 = xe->xdf1.nrec - 1; - } - - fs1 = get_func_line(xe, xecfg, NULL, i1, -1); - while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) && - !is_func_rec(&xe->xdf1, xecfg, fs1 - 1)) - fs1--; - if (fs1 < 0) - fs1 = 0; - if (fs1 < s1) { - s2 -= s1 - fs1; - s1 = fs1; - } - } - - post_context_calculation: - lctx = xecfg->ctxlen; - lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1)); - lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2)); - - e1 = xche->i1 + xche->chg1 + lctx; - e2 = xche->i2 + xche->chg2 + lctx; - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fe1 = get_func_line(xe, xecfg, NULL, - xche->i1 + xche->chg1, - xe->xdf1.nrec); - while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1)) - fe1--; - if (fe1 < 0) - fe1 = xe->xdf1.nrec; - if (fe1 > e1) { - e2 += fe1 - e1; - e1 = fe1; - } - - /* - * Overlap with next change? Then include it - * in the current hunk and start over to find - * its new end. - */ - if (xche->next) { - long l = XDL_MIN(xche->next->i1, - xe->xdf1.nrec - 1); - if (l - xecfg->ctxlen <= e1 || - get_func_line(xe, xecfg, NULL, l, e1) < 0) { - xche = xche->next; - goto post_context_calculation; - } - } - } - - /* - * Emit current hunk header. - */ - - if (xecfg->flags & XDL_EMIT_FUNCNAMES) { - get_func_line(xe, xecfg, &func_line, - s1 - 1, funclineprev); - funclineprev = s1 - 1; - } - if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, - func_line.buf, func_line.len, ecb) < 0) - return -1; - - /* - * Emit pre-context. - */ - for (; s2 < xch->i2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) { - /* - * Merge previous with current change atom. - */ - for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - /* - * Removes lines from the first file. - */ - for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) - if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) - return -1; - - /* - * Adds lines from the second file. - */ - for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) - return -1; - - if (xch == xche) - break; - s1 = xch->i1 + xch->chg1; - s2 = xch->i2 + xch->chg2; - } - - /* - * Emit post-context. - */ - for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - } - - return 0; -}
--- a/mercurial/thirdparty/xdiff/xemit.h Sun Mar 04 00:17:49 2018 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XEMIT_H) -#define XEMIT_H - - -typedef int (*emit_func_t)(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); - -xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg); -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); - - - -#endif /* #if !defined(XEMIT_H) */
--- a/mercurial/thirdparty/xdiff/xinclude.h Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xinclude.h Tue Mar 06 18:41:08 2018 -0800 @@ -35,7 +35,6 @@ #include "xutils.h" #include "xprepare.h" #include "xdiffi.h" -#include "xemit.h" #endif /* #if !defined(XINCLUDE_H) */
--- a/mercurial/thirdparty/xdiff/xutils.c Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xutils.c Tue Mar 06 18:41:08 2018 -0800 @@ -40,28 +40,6 @@ } -int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, - xdemitcb_t *ecb) { - int i = 2; - mmbuffer_t mb[3]; - - mb[0].ptr = (char *) pre; - mb[0].size = psize; - mb[1].ptr = (char *) rec; - mb[1].size = size; - if (size > 0 && rec[size - 1] != '\n') { - mb[2].ptr = (char *) "\n\\ No newline at end of file\n"; - mb[2].size = strlen(mb[2].ptr); - i++; - } - if (ecb->outf(ecb->priv, mb, i) < 0) { - - return -1; - } - - return 0; -} - void *xdl_mmfile_first(mmfile_t *mmf, long *size) { *size = mmf->size; @@ -169,75 +147,3 @@ for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++); return bits ? bits: 1; } - - -int xdl_num_out(char *out, long val) { - char *ptr, *str = out; - char buf[32]; - - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; - if (val < 0) { - *--ptr = '-'; - val = -val; - } - for (; val && ptr > buf; val /= 10) - *--ptr = "0123456789"[val % 10]; - if (*ptr) - for (; *ptr; ptr++, str++) - *str = *ptr; - else - *str++ = '0'; - *str = '\0'; - - return str - out; -} - -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, xdemitcb_t *ecb) { - int nb = 0; - mmbuffer_t mb; - char buf[128]; - - memcpy(buf, "@@ -", 4); - nb += 4; - - nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1); - - if (c1 != 1) { - memcpy(buf + nb, ",", 1); - nb += 1; - - nb += xdl_num_out(buf + nb, c1); - } - - memcpy(buf + nb, " +", 2); - nb += 2; - - nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1); - - if (c2 != 1) { - memcpy(buf + nb, ",", 1); - nb += 1; - - nb += xdl_num_out(buf + nb, c2); - } - - memcpy(buf + nb, " @@", 3); - nb += 3; - if (func && funclen) { - buf[nb++] = ' '; - if (funclen > sizeof(buf) - nb - 1) - funclen = sizeof(buf) - nb - 1; - memcpy(buf + nb, func, funclen); - nb += funclen; - } - buf[nb++] = '\n'; - - mb.ptr = buf; - mb.size = nb; - if (ecb->outf(ecb->priv, &mb, 1) < 0) - return -1; - - return 0; -}
--- a/mercurial/thirdparty/xdiff/xutils.h Sun Mar 04 00:17:49 2018 -0800 +++ b/mercurial/thirdparty/xdiff/xutils.h Tue Mar 06 18:41:08 2018 -0800 @@ -26,8 +26,6 @@ long xdl_bogosqrt(long n); -int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, - xdemitcb_t *ecb); int xdl_cha_init(chastore_t *cha, long isize, long icount); void xdl_cha_free(chastore_t *cha); void *xdl_cha_alloc(chastore_t *cha); @@ -35,9 +33,6 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags); unsigned long xdl_hash_record(char const **data, char const *top, long flags); unsigned int xdl_hashbits(unsigned int size); -int xdl_num_out(char *out, long val); -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, xdemitcb_t *ecb);
--- a/setup.py Sun Mar 04 00:17:49 2018 -0800 +++ b/setup.py Tue Mar 06 18:41:08 2018 -0800 @@ -850,7 +850,6 @@ xdiff_srcs = [ 'mercurial/thirdparty/xdiff/xdiffi.c', - 'mercurial/thirdparty/xdiff/xemit.c', 'mercurial/thirdparty/xdiff/xmerge.c', 'mercurial/thirdparty/xdiff/xprepare.c', 'mercurial/thirdparty/xdiff/xutils.c', @@ -859,7 +858,6 @@ xdiff_headers = [ 'mercurial/thirdparty/xdiff/xdiff.h', 'mercurial/thirdparty/xdiff/xdiffi.h', - 'mercurial/thirdparty/xdiff/xemit.h', 'mercurial/thirdparty/xdiff/xinclude.h', 'mercurial/thirdparty/xdiff/xmacros.h', 'mercurial/thirdparty/xdiff/xprepare.h',