2017 © Pedro Peláez
 

library diff-parser

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

image

ptlis/diff-parser

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

  • Saturday, February 25, 2017
  • by ptlis
  • Repository
  • 1 Watchers
  • 14 Stars
  • 2,782 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 44 % Grown

The README.md

ptlis/diff-parser

A parser for unified diff files, returning a hydrated object graph., (*1)

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

CircleCI codecov Latest Stable Version, (*3)

Install

Install with composer:, (*4)

$ composer require ptlis/diff-parser

Usage

Parsing a diff file

Create a parser:, (*5)

$parser = new \ptlis\DiffParser\Parser();

And then get a changeset from a file path:, (*6)

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

or parse the patch data stored from a variable:, (*7)

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

Serialization

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

For example this serializes the data in $changeset into the file my.patch., (*9)

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

The Object Graph

The tree built to store changesets is very simple, mapping one-to-one to the components of a diff file. In essence:, (*10)

  • 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 may iterate over the array of files that have changed:, (*11)

foreach ($changeset->files as $file) {
    // $file is an instance of ptlis\DiffParser\File
}

File

Get the original and new filenames:, (*12)

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

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

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

From a file you may iterate over the change hunks:, (*14)

foreach ($file->hunks as $hunk) {
    // $hunk is an instance of ptlis\DiffParser\Hunk
}  

Hunk

Get the start line number of the hunk:, (*15)

$hunk->startLine->original; // Eg '0'
$hunk->startLine->new;      // Eg '0'

Get the number of lines affected in the hunk:, (*16)

$hunk->affectedLines->original; // Eg '5'
$hunk->affectedLines->new;      // Eg '7'

From a hunk you may iterate over the changed lines:, (*17)

foreach ($hunk->lines as $line) {
    // $line is an instance of ptlis\DiffParser\Line
}

Line

Get the original and new line numbers:, (*18)

$line->number->original;    // Eg '7' or '-1' on create
$line->number->new;         // Eg '7' or '-1' on delete

Get the operation:, (*19)

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

Get the value of the line:, (*20)

$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., (*21)

The Versions

25/02 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

24/02 2017

v0.6.0

0.6.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

21/12 2015

v0.5.0

0.5.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

16/03 2015

v0.4.0

0.4.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

03/03 2015

v0.3.0

0.3.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

21/01 2015

v0.2.0

0.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff

20/01 2015

v0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

parser diff unified-diff