All Projects → ptlis → diff-parser

ptlis / diff-parser

Licence: MIT license
A parser for unified diffs

Programming Languages

PHP
23972 projects - #3 most used programming language
Roff
2310 projects

Projects that are alternatives of or similar to diff-parser

Diff2html
Pretty diff to html javascript library (diff2html)
Stars: ✭ 1,867 (+8386.36%)
Mutual labels:  diff, unified-diffs
Diff2html Cli
Pretty diff to html javascript cli (diff2html-cli)
Stars: ✭ 287 (+1204.55%)
Mutual labels:  diff, unified
ecomparatio
eComparatio: text diff and support for digital edition
Stars: ✭ 21 (-4.55%)
Mutual labels:  diff
got
An enjoyable golang test framework.
Stars: ✭ 234 (+963.64%)
Mutual labels:  diff
atom-hg
Mercurial support for Atom text editor. Works on Linux, Mac OS X and Windows.
Stars: ✭ 27 (+22.73%)
Mutual labels:  diff
TreeWalker
PHP JSON diff
Stars: ✭ 58 (+163.64%)
Mutual labels:  diff
go-patchutils
go-patchutils is a library written in Go to show differences in source and diff files.
Stars: ✭ 19 (-13.64%)
Mutual labels:  diff
apibone
[Deprecated] Abstracts requests and responses to make them platform agnostic
Stars: ✭ 13 (-40.91%)
Mutual labels:  unified
remark-slate-transformer
remark plugin to transform remark syntax tree (mdast) to Slate document tree, and vice versa. Made for WYSIWYG markdown editor.
Stars: ✭ 62 (+181.82%)
Mutual labels:  unified
deltaq
Fast and portable delta encoding for .NET in 100% safe, managed code.
Stars: ✭ 26 (+18.18%)
Mutual labels:  diff
devops-ninja
This is a collection of some very useful command-line commands that eases the life of a DevOps Engineer.
Stars: ✭ 27 (+22.73%)
Mutual labels:  diff
Micro
🏎Fast diffing and type safe SwiftUI style data source for UICollectionView
Stars: ✭ 77 (+250%)
Mutual labels:  diff
visual-differ
A Node-based diffing tool to compare an array of URLs and flag differences between them
Stars: ✭ 18 (-18.18%)
Mutual labels:  diff
AnyDiff
A CSharp (C#) diff library that allows you to diff two objects and get a list of the differences back.
Stars: ✭ 80 (+263.64%)
Mutual labels:  diff
lcs-image-diff-rs
🖼 Image diff tool with LCS algorithm
Stars: ✭ 67 (+204.55%)
Mutual labels:  diff
ncdu-diff
ncdu fork that can compare and diff results
Stars: ✭ 21 (-4.55%)
Mutual labels:  diff
eslint-config-typescript-unified
🛠️ A unified ESLint configuration with sensible defaults for TypeScript projects.
Stars: ✭ 15 (-31.82%)
Mutual labels:  unified
diffview.nvim
Single tabpage interface for easily cycling through diffs for all modified files for any git rev.
Stars: ✭ 1,472 (+6590.91%)
Mutual labels:  diff
open-md-gateway
Diff协议行情网关, 支持实时行情和历史行情
Stars: ✭ 18 (-18.18%)
Mutual labels:  diff
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 (-27.27%)
Mutual labels:  diff

ptlis/diff-parser

A parser for unified diff files, returning a hydrated object graph.

Uses __toString() to serialize back into unified diff format.

Build Status codecov Latest Stable Version

Install

Install with composer:

$ composer require ptlis/diff-parser

Usage

Build a Changeset

Get a changeset from a file:

<?php

use ptlis\DiffParser\Parser;

$parser = new Parser();

$changeset = $parser->parseFile('path/to/svn/diff', Parser::VCS_SVN);

Get a changeset from a variable containg the contents of a patch file:

<?php

use ptlis\DiffParser\Parser;

$parser = new Parser();

$patchData = \file_get_contents('/path/to/patchfile');

$changeset = $parser->parse($patchData, Parser::VCS_SVN);

Serialization

All of the value classes implement the __toString() method to support direct serialization of that component back to unified diff format.

For example, serialization of a changeset back to a file is as simple as:

\file_put_contents('my.patch', $changeset);

The Object Graph

The tree built to store changesets is very simple, in essence:

  • A Changeset is the root node & contains Files
  • A File contain Hunks
  • A Hunk contain Lines
  • Lines are the leaf nodes.

Changeset

From a Changeset you get an array of files that have changed:

$files = $changeset->files;  // Array of ptlis\DiffParser\File instances.

File

$file = $files[0];   // Get the first changed file

Get the original and new filenames:

$file->originalFilename;    // Eg 'readme.md' or '' (empty) on create
$file->newFilename;         // EG 'README.md' or '' (empty) on delete

Get the operation that was performed (create, delete or change):

$file->operation;   // One of File::CREATED, File::CHANGED, File::DELETED  

Get the changed hunks for the file:

$hunks = $file->hunks;  // Array of ptlis\DiffParser\Hunk instances.  

Hunk

$hunk = $hunks[0];  // Get the first hunk for this file

Get the hunk metadata:

$hunk->originalStart;   // Eg '0'
$hunk->originalCount;   // Eg '5'
$hunk->newStart;        // Eg '0'
$hunk->newCount;        // Eg '7'

Get the changed lines:

$lines = $hunk->lines;  // Array of ptlis\DiffParser\Line instances.  

Line

$line = $lines[0];  // Get the first line for this hunk

Get the original and new line numbers:

$line->originalLineNo;  // Eg '7' or '-1' on create
$line->newLineNo;       // Eg '7' or '-1' on delete

Get the operation:

$line->operation;   // One of Line::ADDED, Line::REMOVED, Line::UNCHANGED

Get the value of the line:

$line->content; // Eg ' $foo = bar;'

Contributing

You can contribute by submitting an Issue to the issue tracker, improving the documentation or submitting a pull request. For pull requests i'd prefer that the code style and test coverage is maintained, but I am happy to work through any minor issues that may arise so that the request can be merged.

TODO

  • Add more tests for robustness - being generated, in theory diffs should be reliable, but we still need to gracefully fail when this assumption is false.
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].