{"id":13828192,"url":"https://github.com/ptlis/diff-parser","last_synced_at":"2025-10-11T19:20:26.040Z","repository":{"id":26105432,"uuid":"29549650","full_name":"ptlis/diff-parser","owner":"ptlis","description":"A unified diff parser for PHP","archived":false,"fork":false,"pushed_at":"2023-01-12T19:36:46.000Z","size":211,"stargazers_count":28,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-04T17:18:48.877Z","etag":null,"topics":["diff","diff-parser","php","unified","unified-diffs"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ptlis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-20T20:09:43.000Z","updated_at":"2024-06-11T14:22:16.000Z","dependencies_parsed_at":"2023-01-14T04:45:17.546Z","dependency_job_id":null,"html_url":"https://github.com/ptlis/diff-parser","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/ptlis/diff-parser","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptlis%2Fdiff-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptlis%2Fdiff-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptlis%2Fdiff-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptlis%2Fdiff-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ptlis","download_url":"https://codeload.github.com/ptlis/diff-parser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ptlis%2Fdiff-parser/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279005274,"owners_count":26083863,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["diff","diff-parser","php","unified","unified-diffs"],"created_at":"2024-08-04T09:02:35.995Z","updated_at":"2025-10-11T19:20:26.020Z","avatar_url":"https://github.com/ptlis.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# ptlis/diff-parser\n\nA parser for unified diff files, returning a hydrated object graph.\n\nUses __toString() to serialize back into unified diff format.\n\n[![CircleCI](https://dl.circleci.com/status-badge/img/gh/ptlis/diff-parser/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/ptlis/diff-parser/tree/main) [![codecov](https://codecov.io/gh/ptlis/diff-parser/branch/master/graph/badge.svg?token=r8NgjZyVVL)](https://codecov.io/gh/ptlis/diff-parser) [![Latest Stable Version](https://poser.pugx.org/ptlis/diff-parser/v/stable.png)](https://packagist.org/packages/ptlis/diff-parser)\n\n\n## Install\n\nInstall with composer:\n\n```shell\n$ composer require ptlis/diff-parser\n```\n\n\n## Usage\n\n\n### Parsing a diff file\n\nCreate a parser:\n\n```php\n$parser = new \\ptlis\\DiffParser\\Parser();\n```\n\nAnd then get a changeset from a file path:\n\n```php\n$changeset = $parser-\u003eparseFile('path/to/git/diff', Parser::VCS_GIT);\n```\n\nor parse the patch data stored from a variable:\n\n```php\n$changeset = $parser-\u003eparse($patchData, Parser::VCS_SVN);\n```\n\n\n### Serialization\n\nAll the value classes implement the ```__toString()``` method to support direct serialization of that component back to unified diff format.\n\nFor example this serializes the data in `$changeset` into the file `my.patch`.\n\n```php\n\\file_put_contents('my.patch', $changeset);\n```\n\n\n### The Object Graph\n\nThe tree built to store changesets is very simple, mapping one-to-one to the components of a diff file. In essence:\n\n* A Changeset is the root node \u0026 contains Files\n* A File contain Hunks\n* A Hunk contain Lines\n* Lines are the leaf nodes.\n\n#### Changeset\n\nFrom a Changeset you may iterate over the array of files that have changed:\n\n```php\nforeach ($changeset-\u003efiles as $file) {\n    // $file is an instance of ptlis\\DiffParser\\File\n}\n```\n\n#### File\n\nGet the original and new filenames:\n\n```php    \n$file-\u003efilename-\u003eoriginal;  // Eg 'readme.md' or '' (empty) on create\n$file-\u003efilename-\u003enew;       // EG 'README.md' or '' (empty) on delete\n```\n\nGet the operation that was performed (create, delete or change):\n\n```php\n$file-\u003eoperation;   // One of File::CREATED, File::CHANGED, File::DELETED  \n```\n\nFrom a file you may iterate over the change hunks:\n\n```php\nforeach ($file-\u003ehunks as $hunk) {\n    // $hunk is an instance of ptlis\\DiffParser\\Hunk\n}  \n```\n\n#### Hunk\n\nGet the start line number of the hunk:\n\n```php\n$hunk-\u003estartLine-\u003eoriginal; // Eg '0'\n$hunk-\u003estartLine-\u003enew;      // Eg '0'\n```\n\nGet the number of lines affected in the hunk:\n\n```php\n$hunk-\u003eaffectedLines-\u003eoriginal; // Eg '5'\n$hunk-\u003eaffectedLines-\u003enew;      // Eg '7'\n```\n\nFrom a hunk you may iterate over the changed lines:\n\n```php\nforeach ($hunk-\u003elines as $line) {\n    // $line is an instance of ptlis\\DiffParser\\Line\n}\n```\n\n\n#### Line\n\nGet the original and new line numbers:\n\n```php\n$line-\u003enumber-\u003eoriginal;    // Eg '7' or '-1' on create\n$line-\u003enumber-\u003enew;         // Eg '7' or '-1' on delete\n```\n\nGet the operation:\n\n```php\n$line-\u003eoperation;   // One of Line::ADDED, Line::REMOVED, Line::UNCHANGED\n```\n\nGet the value of the line:\n\n```php\n$line-\u003econtent; // Eg ' $foo = bar;'\n```\n\n\n## Contributing\n\nYou can contribute by submitting an Issue to the [issue tracker](https://github.com/ptlis/vcs/issues), 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fptlis%2Fdiff-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fptlis%2Fdiff-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fptlis%2Fdiff-parser/lists"}