Download the PHP package ptlis/diff-parser without Composer
On this page you can find all versions of the php package ptlis/diff-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ptlis/diff-parser
More information about ptlis/diff-parser
Files in ptlis/diff-parser
Package diff-parser
Short Description A parser for unified diff files, returning a hydrated object graph.
License MIT
Informations about the package diff-parser
ptlis/diff-parser
A parser for unified diff files, returning a hydrated object graph.
Uses __toString() to serialize back into unified diff format.
Install
Install with composer:
Usage
Parsing a diff file
Create a parser:
And then get a changeset from a file path:
or parse the patch data stored from a variable:
Serialization
All the value classes implement the method to support direct serialization of that component back to unified diff format.
For example this serializes the data in $changeset
into the file my.patch
.
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:
- 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:
File
Get the original and new filenames:
Get the operation that was performed (create, delete or change):
From a file you may iterate over the change hunks:
Hunk
Get the start line number of the hunk:
Get the number of lines affected in the hunk:
From a hunk you may iterate over the changed lines:
Line
Get the original and new line numbers:
Get the operation:
Get the value of the line:
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.