All Projects → balbuf → Composer Git Merge Driver

balbuf / Composer Git Merge Driver

Custom git merge driver to minimize merge conflicts in composer.json and composer.lock files

Projects that are alternatives of or similar to Composer Git Merge Driver

Codice Fiscale
A PHP library to calculate and check the italian tax code (codice fiscale).
Stars: ✭ 57 (-30.49%)
Mutual labels:  composer
Formula Parser
Parsing and evaluating mathematical formulas given as strings.
Stars: ✭ 62 (-24.39%)
Mutual labels:  composer
Composer Tools
⚠️ ⚠️ ⚠️ Hyperledger Composer has been deprecated ⚠️ ⚠️ ⚠️
Stars: ✭ 75 (-8.54%)
Mutual labels:  composer
Mailchimp Api 3.0 Php
A feature rich object-oriented PHP library for interacting with MailChimp's API v3 💌🐵
Stars: ✭ 61 (-25.61%)
Mutual labels:  composer
Deb Dev Machine
Quickly install common Developer tools, IDE's & Services on Debian 9
Stars: ✭ 63 (-23.17%)
Mutual labels:  composer
Pusher Http Php
PHP library for interacting with the Pusher Channels HTTP API
Stars: ✭ 1,136 (+1285.37%)
Mutual labels:  composer
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-30.49%)
Mutual labels:  composer
Laravel Sitemap
Laravelium Sitemap generator for Laravel.
Stars: ✭ 1,231 (+1401.22%)
Mutual labels:  composer
Scriptsdev
Scripts-dev directive for composer
Stars: ✭ 63 (-23.17%)
Mutual labels:  composer
Html Compress Twig
Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress
Stars: ✭ 72 (-12.2%)
Mutual labels:  composer
Laravel Restify
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.
Stars: ✭ 62 (-24.39%)
Mutual labels:  composer
Packagist Mirror
Alibaba Cloud Packagist Mirror
Stars: ✭ 63 (-23.17%)
Mutual labels:  composer
Designpattern
设计模式
Stars: ✭ 66 (-19.51%)
Mutual labels:  composer
Php
PHP 文章集锦,浮点数搞定度运算、签名验证、单点登录、安全防御、缓存技术、RPC、Composer ...
Stars: ✭ 61 (-25.61%)
Mutual labels:  composer
Array group by
A PHP function that groups an array by a key or set of keys shared between all array members.
Stars: ✭ 77 (-6.1%)
Mutual labels:  composer
Spinupwp Composer Site
A WordPress site setup using Composer that is primed and ready to be hosted using SpinupWP.
Stars: ✭ 58 (-29.27%)
Mutual labels:  composer
Conductor
Conductor makes it easy to mange multiple composer packages within a single source repository
Stars: ✭ 64 (-21.95%)
Mutual labels:  composer
Phpari
A Class Library enabling Asterisk ARI functionality for PHP
Stars: ✭ 80 (-2.44%)
Mutual labels:  composer
Chinese Typesetting
📄 中文排版 Composer 包
Stars: ✭ 79 (-3.66%)
Mutual labels:  composer
Easy Short Url
ESU 短网址,可在 Laravel、Yii、ThinkPHP 等框架 Composer 包引入,也可以独立搭建短网址站点
Stars: ✭ 71 (-13.41%)
Mutual labels:  composer

Composer JSON Git Merge Driver

The Composer JSON Git Merge Driver provides a mechanism to more effectively merge composer.json and composer.lock files that have been modified simultaneously in separate branches / development histories. The custom git merge driver is invoked when the composer JSON files require a merge more complex than a simple "fast forward."

How it Works

The merge driver replaces git's standard merge algorithm only for composer's JSON files: instead of analyzing the files for changed lines, the JSON is parsed and the actual properties and values are compared for changes. As such, the merge driver is able to more gracefully handle most new, updated, and removed dependencies in your composer files. A merge conflict is only triggered when the version constraint, locked version number, or presence / absence of the same dependency has been modified in multiple development histories involved in the merge.

For instance, if a certain dependency is updated in one branch and removed in another, a merge conflict is triggered because it is unclear which change for that dependency is desired following the merge. However, if a new, different dependency is appended to the require section in both branches, the merge driver will understand that both should be kept, whereas the standard git merge driver would trigger a merge conflict because the same line has been edited in both branches.

More generally, all object data structures are merged gracefully and recursively, meaning other composer configuration properties in the composer JSON files (e.g. extra and config) will be merged properly as well—whenever changes are not ambiguous. In fact, the merge driver will work on any JSON file whose outermost data structure is an object.

Composer Lock Handling

For the the lock file in particular, special handling is performed to minimize the potential for merge conflicts. For example, the locked dependency data is converted into an object for the purposes of merging and converted back to the proper data structure when the merge is complete. As well, the content-hash property is excluded from the merge, as the very nature of this property guarantees that two differing branches would have conflicting values. The content hash is used by composer to efficiently recognize that the composer.json file has changed, so the actual value is not necessarily important after a merge has occured. As a workaround for not being able to generate an accurate hash, the merge driver sets its own unique value for the hash to signal to composer that a change has occurred; the value is a simple message instructing you to run composer update --lock so composer can regenerate the real hash!

Installation

The merge driver can be installed globally or per repo and configured and activated globally or per repo, i.e. you can install the driver globally but only activate it on certain projects.

1. Install the merge driver on your system.

Use composer to install the driver globally:

$ composer global require balbuf/composer-git-merge-driver

or just in a particular repo:

$ composer require --dev balbuf/composer-git-merge-driver

Note that if you are installing the driver via the latter (per repo) method, this will be added to the repo's composer.json and thus will be installed as a dependency for all users of the repo.

2. Configure the merge driver with git.

The driver is made available for use by informing git of its existence via a git config file:

[merge "composer_json"]
    name = composer JSON file merge driver
    driver = composer-git-merge-driver %O %A %B %L %P
    recursive = binary

To open the git config file for editing in your default command line text editor:

$ git config -e

By default, this allows you to edit the config file for the current repo, meaning the driver is only installed locally to the repo. To edit the global config file for your user, append the --global flag; to edit the system-wide config file, append the --system flag.

Copy and paste the block above into the config file and save it. This example assumes that your $PATH includes Composer's vendor bin path (by default, ~/.composer/vendor/bin for global installation or ./vendor/bin for repo installation). If not, be sure to update your $PATH or add the appropriate path to the binary on the driver line.

A sample .gitconfig file is included in this repo for convenience. If composer-git-merge-driver is installed as a project dependency, the sample .gitconfig file can be installed like so:

$ git config --add include.path $(git rev-parse --show-toplevel)/vendor/balbuf/composer-git-merge-driver/.gitconfig

For more information about git config files, refer to the git documentation.

3. Activate the merge driver.

Finally, the merge driver must be activated for composer.json and composer.lock files via a git attributes file:

composer.json merge=composer_json
composer.lock merge=composer_json

To activate only for a specific repo, edit the .git/info/attributes file inside of the repo. To activate globally for your user, edit the ~/.gitattributes file; to activate system-wide, edit the $(prefix)/etc/gitattributes file (e.g. /usr/local/etc/gitattributes). Copy and paste the block above into the config file and save it. In some cases the file may not yet exist, in which case you can simply create the file at the aforementioned path.

For more information about git attributes files, refer to the git documentation.

Usage

The merge driver is automatically invoked any time a composer.json and/or composer.lock file must be merged in a repo where the driver is activated. If there are any merge conflicts within the files, the standard git merge conflict procedure kicks in: you will be alerted of which file(s) contain conflicts, and conflicts are denoted via the standard conflict markers (e.g. <<<<<<<).

If there are no conflicts and the merge can be applied cleanly, the merge completes as normal. However, a changed lock file results in a non-standard content-hash value (whether there are conflicts or not). While there should be no harm in leaving the hash value as-is, it's best to let composer regenerate the hash. To do so, simply run:

$ composer update --lock

The value of the hash actually informs you to take this action, but if the merge completes with no conflicts, you may not even notice the message. When there are conflicts, this step should be completed after fixing the conflicts but before finishing the merge. If there were no conflicts and the merge completed, you can avoid creating an additional commit by amending the merge commit:

$ composer update --lock
$ git add composer.lock
$ git commit --amend --no-edit

Additional Information

Requirements

The merge driver is written in PHP and requires at least version 5.4.

Known Limitations

  • The merge driver parses and regenerates the JSON to complete the merge. As such, some formatting may be lost or altered in the process. For the composer.json file, the driver attempts to detect the indentation preference of the file in the working tree and replicate that indentation when the JSON is regenerated, which should minimize formatting changes. However, some whitespace style cannot be preserved, e.g. fewer line breaks or more whitespace. While the interpreted contents of the file will not be compromised, the formatting could result in extraneous changes to the file as a result of the merge. (In keeping with the behavior of composer, the composer.lock file is regenerated by the default behavior of PHP's json_encode function and is consistently indented.)

  • When a merge conflict occurs where the last property of an object is removed, accepting the removed version will result in invalid syntax due to a trailing comma on the preceding property. Care should be taken to verify the syntax of the overall file and manually update accordingly.

Acknowledgements

Thanks to @christian-blades-cb and @jphass for the inspiration and examples.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].