2020-11-13 14:38:28 +00:00
|
|
|
<!--
|
|
|
|
tagline: On gracefully resolving conflicts while merging
|
|
|
|
-->
|
|
|
|
|
|
|
|
# Resolving merge conflicts
|
|
|
|
|
|
|
|
When working as a team on the same Composer project, you will eventually run into a scenario
|
|
|
|
where multiple people added, updated or removed something in the `composer.json` and
|
|
|
|
`composer.lock` files in multiple branches. When those branches are eventually merged
|
|
|
|
together, you will get merge conflicts. Resolving these merge conflicts is not as straight
|
|
|
|
forward as on other files, especially not regarding the `composer.lock` file.
|
|
|
|
|
|
|
|
> **Note:** It might not immediately be obvious why text based merging is not possible for
|
|
|
|
> lock files, so let's imagine the following example where we want to merge two branches;
|
|
|
|
>
|
|
|
|
> - Branch 1 has added package A which requires package B. Package B is locked at version `1.0.0`.
|
|
|
|
> - Branch 2 has added package C which conflicts with all versions below `1.2.0` of package B.
|
|
|
|
>
|
|
|
|
> A text based merge would result in package A version `1.0.0`, package B version `1.0.0`
|
|
|
|
> and package C version `1.0.0`. This is an invalid result, as the conflict of package C
|
|
|
|
> was not considered and would require an upgrade of package B.
|
|
|
|
|
|
|
|
## 1. Reapplying changes
|
|
|
|
|
|
|
|
The safest method to merge Composer files is to accept the version from one branch and apply
|
|
|
|
the changes from the other branch.
|
|
|
|
|
|
|
|
An example where we have two branches:
|
|
|
|
|
|
|
|
1. Package 'A' has been added
|
|
|
|
2. Package 'B' has been removed and package 'C' is added.
|
|
|
|
|
|
|
|
To resolve the conflict when we merge these two branches:
|
|
|
|
|
|
|
|
- We choose the branch that has the most changes, and accept the `composer.json` and `composer.lock`
|
2021-10-25 11:11:56 +00:00
|
|
|
files from that branch. In this case, we choose the Composer files from branch 2.
|
2020-11-13 14:38:28 +00:00
|
|
|
- We reapply the changes from the other branch (branch 1). In this case we have to run
|
2021-10-25 11:11:56 +00:00
|
|
|
`composer require package/A` again.
|
2020-11-13 14:38:28 +00:00
|
|
|
|
|
|
|
## 2. Validating your merged files
|
|
|
|
|
|
|
|
Before committing, make sure the resulting `composer.json` and `composer.lock` files are valid.
|
|
|
|
To do this, run the following commands:
|
|
|
|
|
2022-08-20 10:23:00 +00:00
|
|
|
```shell
|
2021-10-25 11:11:35 +00:00
|
|
|
php composer.phar validate
|
|
|
|
php composer.phar install [--dry-run]
|
2020-11-13 14:38:28 +00:00
|
|
|
```
|
|
|
|
|
2022-06-22 11:47:15 +00:00
|
|
|
## Automating merge conflict resolving with git
|
|
|
|
|
|
|
|
Some improvement _could_ be made to git's conflict resolving by using a custom git merge driver.
|
|
|
|
|
|
|
|
An example of this can be found at [balbuf's composer git merge driver](https://github.com/balbuf/composer-git-merge-driver).
|
|
|
|
|
2020-11-13 14:38:28 +00:00
|
|
|
## Important considerations
|
|
|
|
|
2021-12-19 14:15:21 +00:00
|
|
|
Keep in mind that whenever merge conflicts occur on the lock file, the information, about the exact version
|
|
|
|
new packages were locked on for one of the branches, is lost. When package A in branch 1 is constrained
|
2020-11-13 14:38:28 +00:00
|
|
|
as `^1.2.0` and locked as `1.2.0`, it might get updated when branch 2 is used as baseline and a new
|
|
|
|
`composer require package/A:^1.2.0` is executed, as that will use the most recent version that the
|
|
|
|
constraint allows when possible. There might be a version 1.3.0 for that package available by now, which
|
|
|
|
will now be used instead.
|
|
|
|
|
|
|
|
Choosing the correct [version constraints](../articles/versions.md) and making sure the packages adhere
|
|
|
|
to [semantic versioning](https://semver.org/) when using
|
|
|
|
[next significant release operators](versions.md#next-significant-release-operators) should make sure
|
|
|
|
that merging branches does not break anything by accidentally updating a dependency.
|