Skip to content

Commit 92b7de9

Browse files
dschogitster
authored andcommitted
Implement the patience diff algorithm
The patience diff algorithm produces slightly more intuitive output than the classic Myers algorithm, as it does not try to minimize the number of +/- lines first, but tries to preserve the lines that are unique. To this end, it first determines lines that are unique in both files, then the maximal sequence which preserves the order (relative to both files) is extracted. Starting from this initial set of common lines, the rest of the lines is handled recursively, with Myers' algorithm as a fallback when the patience algorithm fails (due to no common unique lines). This patch includes memory leak fixes by Pierre Habouzit. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8104ebf commit 92b7de9

File tree

5 files changed

+389
-1
lines changed

5 files changed

+389
-1
lines changed

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
#define XDF_IGNORE_WHITESPACE (1 << 2)
3333
#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 3)
3434
#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 4)
35+
#define XDF_PATIENCE_DIFF (1 << 5)
3536
#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE | XDF_IGNORE_WHITESPACE_AT_EOL)
3637

3738
#define XDL_PATCH_NORMAL '-'

xdiff/xdiffi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
329329
xdalgoenv_t xenv;
330330
diffdata_t dd1, dd2;
331331

332+
if (xpp->flags & XDF_PATIENCE_DIFF)
333+
return xdl_do_patience_diff(mf1, mf2, xpp, xe);
334+
332335
if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) {
333336

334337
return -1;

xdiff/xdiffi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr);
5555
void xdl_free_script(xdchange_t *xscr);
5656
int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
5757
xdemitconf_t const *xecfg);
58+
int xdl_do_patience_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
59+
xdfenv_t *env);
5860

5961
#endif /* #if !defined(XDIFFI_H) */

0 commit comments

Comments
 (0)