All Projects → bluekeyes → go-gitdiff

bluekeyes / go-gitdiff

Licence: MIT license
Go library for parsing and applying patches created by Git

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-gitdiff

Diff Match Patch
Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
Stars: ✭ 4,910 (+11875.61%)
Mutual labels:  diff, patch
Python Patch
Library to parse and apply unified diffs
Stars: ✭ 65 (+58.54%)
Mutual labels:  diff, patch
Hdiffpatch
a C\C++ library and command-line tools for Diff & Patch between binary files or directories(folder); cross-platform; run fast; create small delta/differential; support large files and limit memory requires when diff & patch.
Stars: ✭ 459 (+1019.51%)
Mutual labels:  diff, patch
Jsondiffpatch
Diff & patch JavaScript objects
Stars: ✭ 3,951 (+9536.59%)
Mutual labels:  diff, patch
intellij-diff-plugin
Syntax highlighting for .diff files and .patch files in IntelliJ IDEs
Stars: ✭ 17 (-58.54%)
Mutual labels:  diff, patch
Gojsondiff
Go JSON Diff
Stars: ✭ 371 (+804.88%)
Mutual labels:  diff, patch
Git Follow
Follow lifetime changes of a pathspec in Git.
Stars: ✭ 25 (-39.02%)
Mutual labels:  diff, patch
tmux-eaw-fix
tmux 2.6 以降において East Asian Ambiguous Character を全角文字の幅で表示する
Stars: ✭ 16 (-60.98%)
Mutual labels:  diff, patch
diffy
Tools for finding and manipulating differences between files
Stars: ✭ 47 (+14.63%)
Mutual labels:  diff, patch
Ex audit
Ecto auditing library that transparently tracks changes and can revert them.
Stars: ✭ 214 (+421.95%)
Mutual labels:  diff, patch
Gsync
gSync is an rsync based library for sending delta updates of files to a remote server.
Stars: ✭ 344 (+739.02%)
Mutual labels:  diff, patch
duff
Pure OCaml implementation of libXdiff (Rabin's fingerprint)
Stars: ✭ 20 (-51.22%)
Mutual labels:  diff, patch
Editscript
A library designed to diff and patch Clojure data structures
Stars: ✭ 281 (+585.37%)
Mutual labels:  diff, patch
Similar
A high level diffing library for rust based on diffs
Stars: ✭ 386 (+841.46%)
Mutual labels:  diff, patch
Diffson
A scala diff/patch library for Json
Stars: ✭ 258 (+529.27%)
Mutual labels:  diff, patch
Patch Package
Fix broken node modules instantly 🏃🏽‍♀️💨
Stars: ✭ 6,062 (+14685.37%)
Mutual labels:  diff, patch
Apkdiffpatch
a C++ library and command-line tools for Zip(Jar,Apk) file Diff & Patch; create minimal delta/differential; support Jar sign(apk v1 sign) & apk v2,v3 sign .
Stars: ✭ 121 (+195.12%)
Mutual labels:  diff, patch
dipa
dipa makes it easy to efficiently delta encode large Rust data structures.
Stars: ✭ 243 (+492.68%)
Mutual labels:  diff, patch
deltaq
Fast and portable delta encoding for .NET in 100% safe, managed code.
Stars: ✭ 26 (-36.59%)
Mutual labels:  diff, patch
wd
Comparing strings on a word per word basis and generating a coloured diff. This repository has migrated to https://gitlab.com/opennota/wd
Stars: ✭ 16 (-60.98%)
Mutual labels:  diff

go-gitdiff

PkgGoDev Go Report Card

A Go library for parsing and applying patches generated by git diff, git show, and git format-patch. It can also parse and apply unified diffs generated by the standard diff tool.

It supports standard line-oriented text patches and Git binary patches, and aims to parse anything accepted by the git apply command.

patch, err := os.Open("changes.patch")
if err != nil {
    log.Fatal(err)
}

// files is a slice of *gitdiff.File describing the files changed in the patch
// preamble is a string of the content of the patch before the first file
files, preamble, err := gitdiff.Parse(patch)
if err != nil {
    log.Fatal(err)
}

code, err := os.Open("code.go")
if err != nil {
    log.Fatal(err)
}

// apply the changes in the patch to a source file
var output bytes.Buffer
if err := gitdiff.Apply(&output, code, files[0]); err != nil {
    log.Fatal(err)
}

Development Status

The parsing API and types are complete and I expect will remain stable. As of March 2022, I am refactoring the apply API to address existing issues and to support non-strict application. Version 0.6.1 is the last release using the old API and the master branch may contain breaking changes. Please use a release version to avoid surprises.

Parsing and strict application are well-covered by unit tests and the library is used in a production application that parses and applies thousands of patches every day. However, the space of all possible patches is large, so there are likely undiscovered bugs.

The parsing code has also had a modest amount of fuzz testing.

Why another git/unified diff parser?

Several packages with similar functionality exist, so why did I write another?

  1. No other packages I found support binary diffs, as generated with the --binary flag. This is the main reason for writing a new package, as the format is pretty different from line-oriented diffs and is unique to Git.

  2. Most other packages only parse patches, so you need additional code to apply them (and if applies are supported, it is only for text files.)

  3. This package aims to accept anything that git apply accepts, and closely follows the logic in apply.c.

  4. It seemed like a fun project and a way to learn more about Git.

Differences From Git

  1. Certain types of invalid input that are accepted by git apply generate errors. These include:

    • Numbers immediately followed by non-numeric characters
    • Trailing characters on a line after valid or expected content
    • Malformed file header lines (lines that start with diff --git)
  2. Errors for invalid input are generally more verbose and specific than those from git apply.

  3. The translation from C to Go may have introduced inconsistencies in the way Unicode file names are handled; these are bugs, so please report any issues of this type.

  4. When reading headers, there is no validation that OIDs present on an index line are shorter than or equal to the maximum hash length, as this requires knowing if the repository used SHA1 or SHA256 hashes.

  5. When reading "traditional" patches (those not produced by git), prefixes are not stripped from file names; git apply attempts to remove prefixes that match the current repository directory/prefix.

  6. Patches can only be applied in "strict" mode, where the line numbers and context of each fragment must exactly match the source file; git apply implements a search algorithm that tries different lines and amounts of context, with further options to normalize or ignore whitespace changes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].