zutubi diff

Contents

Introduction

Zutubi Diff is a Java library for parsing and applying patch files in unified diff and other formats.

For the Impatient

Just want to get started? Refer to the quick start instructions on the project home page.

License

This code is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

Supported Formats

Zutubi Diff has full support for unified diffs, i.e. those used by the patch utility.

There is also support for parsing git diffs, an extension of unified diffs with extensions such as binary diffs. This is the format used by git and adopted by Mercurial. Currently, however, the library does not support application of diffs in git format.

Using the Library

Obtaining the Jar

First you will need to obtain a release jar file. You can build from source (see below), but it's probably easier to just grab the latest jar from the downloads page. You can also grab the latest release build directly from our Pulse build server (click on "jar" in the "featured artifacts" table on the right of the page.)

Adding to Your Project

Zutubi diff has no external dependencies, so all you need to do is drop the jar into your project classpath.

Library API

The primary entry points to the library are PatchFileParser and the implementations of PatchParser for each supported format. These can be used to parse a file into a PatchFile instance which may be examined and/or applied.

For example, to parse a unified diff named my.patch:

PatchFileParser parser = new PatchFileParser(new UnifiedPatchParser());
PatchFile patchFile = parser.parse(new FileReader("my.patch"));

Assuming the patch file parses successfully, you can apply it to a directory (the final argument is the number of elements to strip from paths in the patch):

File toDirectory = new File("directory/to/patch");
patchFile.apply(toDirectory, 3);

Full source for an example is available in the examples/patch directory. Full documentation for the API may be generated via the build, see below.

Building From Source

If you would like to modify the library, or build it yourself for any other reason, you will need:

  • A JDK, version 1.5 or later.
  • Gradle version 1.2 or later.

To compile and package as a jar run:

$ gradle jar
The jar will appear under build/libs.

To use the library you may want to refer to javadoc, which may be generated by running:

$ gradle javadoc
The documentation appears under build/docs/javadoc.

More gradle targets are available, for details run:

$ gradle -t

Feedback

If you have any thoughts, questions etc about the library, you can contact me at:

jason@zutubi.com

Or you can submit an issue or pull request via GitHub. All feedback is welcome.