{"id":16012447,"url":"https://github.com/bircher/php-merge","last_synced_at":"2025-07-24T12:39:27.174Z","repository":{"id":56950891,"uuid":"47032556","full_name":"bircher/php-merge","owner":"bircher","description":"Php library to merge text. 3 way merge like git in php.","archived":false,"fork":false,"pushed_at":"2022-01-04T21:09:51.000Z","size":71,"stargazers_count":32,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"4.x","last_synced_at":"2025-02-27T05:50:57.657Z","etag":null,"topics":["3-way-merge","composer","git","merge","merge-text","php","php-library"],"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/bircher.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-28T17:20:20.000Z","updated_at":"2024-12-16T15:15:13.000Z","dependencies_parsed_at":"2022-08-21T08:21:01.742Z","dependency_job_id":null,"html_url":"https://github.com/bircher/php-merge","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bircher%2Fphp-merge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bircher%2Fphp-merge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bircher%2Fphp-merge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bircher%2Fphp-merge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bircher","download_url":"https://codeload.github.com/bircher/php-merge/tar.gz/refs/heads/4.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243806046,"owners_count":20350775,"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","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":["3-way-merge","composer","git","merge","merge-text","php","php-library"],"created_at":"2024-10-08T14:03:27.240Z","updated_at":"2025-03-16T07:32:11.852Z","avatar_url":"https://github.com/bircher.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-merge\n\n[![Build Status](https://travis-ci.org/bircher/php-merge.svg?branch=master)](https://travis-ci.org/bircher/php-merge)\n[![Coverage Status](https://coveralls.io/repos/github/bircher/php-merge/badge.svg?branch=master)](https://coveralls.io/github/bircher/php-merge?branch=master)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/bircher/php-merge/master/LICENSE.txt)\n\n## Introduction\n\nWhen working with revisions of text one sometimes faces the problem that there\nare several revisions based off the same original text. Rather than choosing\none and discarding the other we want to merge the two revisions.\n\nGit does that already wonderfully. In a php application we want a simple tool\nthat does the same. There is the [xdiff PECL extension](http://php.net/manual/en/book.xdiff.php)\nwhich has the [xdiff_string_merge3](http://php.net/manual/en/function.xdiff-string-merge3.php)\nfunction. But `xdiff_string_merge3` does not behave the same way as git and\nxdiff may not be available on your system.\n\nPhpMerge is a small library that solves this problem. There are two classes:\n`\\PhpMerge\\PhpMerge` and `\\PhpMerge\\GitMerge` that implement the\n`\\PhpMerge\\PhpMergeInterface` which has just a `merge` method.\n\n`PhpMerge` uses `SebastianBergmann\\Diff\\Differ` to get the differences between\nthe different versions and calculates the merged text from it.\n`GitMerge` uses `Symplify\\GitWrapper\\GitWrapper`, writes the text to a temporary file\nand uses the command line git to merge the text.\n\n## Usage\n\nSimple example:\n\n```php\nuse PhpMerge\\PhpMerge;\n\n// Create a merger instance.\n$merger = new PhpMerge();\n\n// Get the texts to merge.\n$original = \u003c\u003c\u003c'EOD'\nunchanged\nreplaced\nunchanged\nnormal\nunchanged\nunchanged\nremoved\n\nEOD;\n\n$version1= \u003c\u003c\u003c'EOD'\nadded\nunchanged\nreplacement\nunchanged\nnormal\nunchanged\nunchanged\n\nEOD;\n\n$version2 = \u003c\u003c\u003c'EOD'\nunchanged\nreplaced\nunchanged\nnormal??\nunchanged\nunchanged\n\nEOD;\n\n$expected = \u003c\u003c\u003c'EOD'\nadded\nunchanged\nreplacement\nunchanged\nnormal??\nunchanged\nunchanged\n\nEOD;\n\n$result = $merger-\u003emerge($original, $version1, $version2);\n// $result === $expected;\n\n```\n\nWith merge conflicts:\n\n```php\n// Continuing from before with:\nuse Phpmerge\\MergeException;\nuse PhpMerge\\MergeConflict;\n\n\n$conflicting = \u003c\u003c\u003c'EOD'\nunchanged\nreplaced\nunchanged\nnormal!!\nunchanged\nunchanged\n\nEOD;\n\ntry {\n    $merger-\u003emerge($original, $version2, $conflicting);\n} catch (MergeException $exception) {\n    /** @var MergeConflict[] $conflicts */\n    $conflicts = $exception-\u003egetConflicts();\n\n    $original_lines = $conflicts[0]-\u003egetBase();\n    // $original_lines === [\"normal\\n\"];\n    \n    $version2_lines = $conflicts[0]-\u003egetRemote();\n    // $version2_lines === [\"normal??\\n\"];\n    \n    $conflicting_lines = $conflicts[0]-\u003egetLocal();\n    // $conflicting_lines === [\"normal!!\\n\"];\n    \n    $line_numer_of_conflict = $conflicts[0]-\u003egetBaseLine();\n    // $line_numer_of_conflict === 3; // Count starts with 0.\n    \n    // It is also possible to get the merged version using the first version\n    // to resolve conflicts.\n    $merged = $exception-\u003egetMerged();\n    // $merged === $version2;\n    // In this case, but in general there could be non-conflicting changes.\n    \n    $line_in_merged = $conflicts[0]-\u003egetMergedLine();\n    // $line_in_merged === 3; // Count starts with 0.\n}\n\n```\n\nUsing the command line git to perform the merge:\n\n```php\nuse PhpMerge\\GitMerge;\n\n$merger = new GitMerge();\n\n// Use as the previous example.\n```\n\n\n## Installation\n\nPhpMerge can be installed with [Composer](http://getcomposer.org) by adding\nthe library as a dependency to your composer.json file.\n\n```json\n{\n    \"require\": {\n        \"bircher/php-merge\": \"~4.0\"\n    }\n}\n```\n\nTo use the command line git with `GitMerge`:\n\n```json\n{\n    \"require\": {\n        \"bircher/php-merge\": \"~4.0\",\n        \"symplify/git-wrapper\": \"^9.1|^10.0\"\n    }\n}\n```\n\nPlease refer to [Composer's documentation](https://github.com/composer/composer/blob/master/doc/00-intro.md#introduction)\nfor installation and usage instructions.\n\n\n## Difference to ~3.0\n\nIn the ~4.0 version we switch from `cpliakas/git-wrapper` to `symplify/git-wrapper` since the former is deprecated.\nThis update means that there is no change when only using `PhpMerge\\PhpMerge`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbircher%2Fphp-merge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbircher%2Fphp-merge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbircher%2Fphp-merge/lists"}