Patchwork D2574: xdiff: add a binary utility that runs xdiff

login
register
mail settings
Submitter phabricator
Date March 3, 2018, 12:25 a.m.
Message ID <differential-rev-PHID-DREV-vchdiydyxaquij2byd2z-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/28755/
State Superseded
Headers show

Comments

phabricator - March 3, 2018, 12:25 a.m.
ryanmce created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Add a simple binary that runs xdiff in a minimal way. This is useful for
  tweaking xdiff related code. It's also potentially useful in .t tests.

TEST PLAN
  `cd contrib/xdiff; make` and run `xdiff file1 file2`, check it has a sane
  output.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2574

AFFECTED FILES
  contrib/xdiff/Makefile
  contrib/xdiff/xdiff.c

CHANGE DETAILS




To: ryanmce, #hg-reviewers
Cc: mercurial-devel
phabricator - March 3, 2018, 8:23 p.m.
quark commandeered this revision.
quark added a reviewer: ryanmce.
quark added a comment.


  Removing this to keep the stack shorter.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2574

To: quark, #hg-reviewers, ryanmce
Cc: quark, mercurial-devel

Patch

diff --git a/contrib/xdiff/xdiff.c b/contrib/xdiff/xdiff.c
new file mode 100644
--- /dev/null
+++ b/contrib/xdiff/xdiff.c
@@ -0,0 +1,81 @@ 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "lib/third-party/xdiff/xdiff.h"
+
+#define abort(...)                                                             \
+	{                                                                      \
+		fprintf(stderr, __VA_ARGS__);                                  \
+		exit(-1);                                                      \
+	}
+
+void readfile(const char *path, mmfile_t *file)
+{
+	memset(file, 0, sizeof(*file));
+	FILE *fp = fopen(path, "r");
+	if (!fp) {
+		abort("cannot open %s\n", path);
+	}
+	while (!feof(fp)) {
+		char buf[40960];
+		size_t size = fread(buf, 1, sizeof buf, fp);
+		if (size > 0) {
+			size_t new_size = file->size + size;
+			file->ptr = realloc(file->ptr, new_size);
+			if (!file->ptr) {
+				abort("cannot allocate\n");
+			}
+			memcpy(file->ptr + file->size, buf, size);
+			file->size = new_size;
+		}
+	}
+	fclose(fp);
+}
+
+static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
+{
+	int i;
+	for (i = 0; i < nbuf; i++) {
+		write(STDOUT_FILENO, mb[i].ptr, mb[i].size);
+	}
+	return 0;
+}
+
+int main(int argc, char const *argv[])
+{
+	if (argc < 3) {
+		abort("usage: %s FILE1 FILE2\n", argv[0]);
+	}
+
+	mmfile_t a, b;
+
+	readfile(argv[1], &a);
+	readfile(argv[2], &b);
+
+	xpparam_t xpp = {
+	    0,    /* flags */
+	    NULL, /* anchors */
+	    0,    /* anchors_nr */
+	};
+	xdemitconf_t xecfg = {
+	    3,    /* ctxlen */
+	    0,    /* interhunkctxlen */
+	    0,    /* flags */
+	    NULL, /* find_func */
+	    NULL, /* find_func_priv */
+	    NULL, /* hunk_consume_func */
+	};
+	xdemitcb_t ecb = {
+	    0,           /* priv */
+	    &xdiff_outf, /* outf */
+	};
+
+	xdl_diff(&a, &b, &xpp, &xecfg, &ecb);
+
+	free(a->ptr);
+	free(b->ptr);
+
+	return 0;
+}
diff --git a/contrib/xdiff/Makefile b/contrib/xdiff/Makefile
new file mode 100644
--- /dev/null
+++ b/contrib/xdiff/Makefile
@@ -0,0 +1,5 @@ 
+xdiff: ../../lib/third-party/xdiff/*.c xdiff.c
+	gcc -g -std=c99 -I../.. -I../../lib/third-party/xdiff -o $@ $^
+
+clean:
+	-rm -f xdiff