Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git never (auto) merge composer.json

I would like to avoid git auto merging my composer.json. This way I can have a composer.json in develop branch that uses dev-develop packages, while the one in master only uses dev-master packages.

I could use two composer.json files as suggested here, but I am afraid that I will end up forgetting to add a bundle in my composer.json for master. Especially when automating tasks, this can end up in a disaster.

So actually I would like git to either:

  • Always conflict when merging composer.json, so I can manually edit it before comitting to the branch.
  • Only merge based on the number of lines .. so if a bundle is added / removed, the number of lines changes, then it should merge, conflict, whatever. If the contents of a line change, it may ignore that ..
like image 311
rolandow Avatar asked Feb 17 '14 10:02

rolandow


1 Answers

While I do tend to agree that the particular scenario you describe might indicate that you have deeper issues with your development process, git can be made to do what you ask pretty easily by setting the merge attribute on the file in question, to one of two values. From the "Performing a three-way merge" section of git help attributes, they are:

merge unset

Take the version from the current branch as the tentative merge result, and declare that the merge has conflicts. This is suitable for binary files that do not have a well-defined merge semantics.

To establish this, do:

echo "composer.json -merge" >> .gitattributes

merge=binary

Keep the version from your branch in the work tree, but leave the path in the conflicted state for the user to sort out.

To establish this, do:

echo "composer.json merge=binary" >> .gitattributes

Both behave the same

These descriptions don't really make clear how the behaviour would differ, and indeed, upon testing, both of them produce this output:

$ git merge develop
warning: Cannot merge binary files: composer.json (HEAD vs. develop)
Auto-merging composer.json
CONFLICT (content): Merge conflict in composer.json
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch attr-test-2
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:      composer.json

no changes added to commit (use "git add" and/or "git commit -a")

Since the semantics you want are not necessarily to declare that this file is binary, but just to tell git not to merge it automatically, -merge seems ever so slightly preferable.

like image 125
Matt McHenry Avatar answered Oct 23 '22 16:10

Matt McHenry