GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ptlis/diff-parser: A unified diff parser for PHP

BranchesTags
Open more actions menu

Latest commit

 

History

82 Commits
82 Commits

Folders and files

NameName
Last commit message
Last commit date
.circleci
.circleci
 
 
src
src
 
 
tests
tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
CHANGES.md
CHANGES.md
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
licence.txt
licence.txt
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

ptlis/diff-parser

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

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

CircleCI codecov Latest Stable Version

Install

Install with composer:

$ composer require ptlis/diff-parser

Usage

Parsing a diff file

Create a parser:

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

And then get a changeset from a file path:

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

or parse the patch data stored from a variable:

$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.

For example this serializes the data in $changeset into the file my.patch.

\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:

  • 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:

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

File

Get the original and new filenames:

$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):

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

From a file you may iterate over the change hunks:

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

Hunk

Get the start line number of the hunk:

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

Get the number of lines affected in the hunk:

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

From a hunk you may iterate over the changed lines:

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

Line

Get the original and new line numbers:

$line->number->original;    // Eg '7' or '-1' on create
$line->number->new;         // 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.

About

A unified diff parser for PHP

Topics

diffdiff-parserphpunifiedunified-diffs

Resources

Readme
MIT license
Activity

Stars

29 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.