add xdi_diff_hunks() for callers that only need hunk lengths
Based on a patch by Brian Downing, this uses the xdiff emit_func feature to implement xdi_diff_hunks(). It's a function that calls a callback for each hunk of a diff, passing its lengths. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 25, 2008 at 15:31 UTC
86295bb6bac1482d29650d1f77f19d8e7a7cc2fe
2 files changed
+52
-1
xdiff-interface.c
+48
-1
@@ -1,6 +1,9 @@
1
#include "cache.h"
2
#include "xdiff-interface.h"
3
-#include "strbuf.h"
3
+#include "xdiff/xtypes.h"
4
+#include "xdiff/xdiffi.h"
5
+#include "xdiff/xemit.h"
6
+#include "xdiff/xmacros.h"
7
8
struct xdiff_emit_state {
9
xdiff_emit_consume_fn consume;
@@ -153,6 +156,50 @@ int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
156
return ret;
157
}
158
159
+struct xdiff_emit_hunk_state {
160
+ xdiff_emit_hunk_consume_fn consume;
161
+ void *consume_callback_data;
162
+};
163
+
164
+static int process_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
165
+ xdemitconf_t const *xecfg)
166
+{
167
+ long s1, s2, same, p_next, t_next;
168
+ xdchange_t *xch, *xche;
169
+ struct xdiff_emit_hunk_state *state = ecb->priv;
170
+ xdiff_emit_hunk_consume_fn fn = state->consume;
171
+ void *consume_callback_data = state->consume_callback_data;
172
+
173
+ for (xch = xscr; xch; xch = xche->next) {
174
+ xche = xdl_get_hunk(xch, xecfg);
175
+
176
+ s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0);
177
+ s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0);
178
+ same = s2 + XDL_MAX(xch->i1 - s1, 0);
179
+ p_next = xche->i1 + xche->chg1;
180
+ t_next = xche->i2 + xche->chg2;
181
+
182
+ fn(consume_callback_data, same, p_next, t_next);
183
+ }
184
+ return 0;
185
+}
186
+
187
+int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
188
+ xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
189
+ xpparam_t const *xpp, xdemitconf_t *xecfg)
190
+{
191
+ struct xdiff_emit_hunk_state state;
192
+ xdemitcb_t ecb;
193
+
194
+ memset(&state, 0, sizeof(state));
195
+ memset(&ecb, 0, sizeof(ecb));
196
+ state.consume = fn;
197
+ state.consume_callback_data = consume_callback_data;
198
+ xecfg->emit_func = (void (*)())process_diff;
199
+ ecb.priv = &state;
200
+ return xdi_diff(mf1, mf2, xpp, xecfg, &ecb);
201
+}
202
+
203
int read_mmfile(mmfile_t *ptr, const char *filename)
204
{
205
struct stat st;
xdiff-interface.h
+4
@@ -4,12 +4,16 @@
4
#include "xdiff/xdiff.h"
5
6
typedef void (*xdiff_emit_consume_fn)(void *, char *, unsigned long);
7
+typedef void (*xdiff_emit_hunk_consume_fn)(void *, long, long, long);
8
9
int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *ecb);
10
int xdi_diff_outf(mmfile_t *mf1, mmfile_t *mf2,
11
xdiff_emit_consume_fn fn, void *consume_callback_data,
12
xpparam_t const *xpp,
13
xdemitconf_t const *xecfg, xdemitcb_t *xecb);
14
+int xdi_diff_hunks(mmfile_t *mf1, mmfile_t *mf2,
15
+ xdiff_emit_hunk_consume_fn fn, void *consume_callback_data,
16
+ xpparam_t const *xpp, xdemitconf_t *xecfg);
17
int parse_hunk_header(char *line, int len,
18
int *ob, int *on,
19
int *nb, int *nn);