├── test ├── mocha.opts └── git-json-merge.spec.js ├── bin └── git-json-merge ├── .circleci └── config.yml ├── .gitignore ├── package.json ├── LICENSE ├── lib └── git-json-merge.js └── README.md /test/mocha.opts: -------------------------------------------------------------------------------- 1 | test/git-json-merge.spec.js 2 | -------------------------------------------------------------------------------- /bin/git-json-merge: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var gitJsonMerge = require('./../lib/git-json-merge.js'); 3 | 4 | var oursFileName = process.argv[2]; 5 | var baseFileName = process.argv[3]; 6 | var theirsFileName = process.argv[4]; 7 | 8 | gitJsonMerge.mergeJsonFiles(oursFileName, baseFileName, theirsFileName); 9 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | workflows: 4 | main: 5 | jobs: 6 | - test: 7 | version: "14.21" 8 | - test: 9 | version: "16.19" 10 | - test: 11 | version: "18.13" 12 | - test: 13 | version: "19.5" 14 | jobs: 15 | test: 16 | parameters: 17 | version: 18 | type: string 19 | docker: 20 | - image: cimg/node:<< parameters.version >> 21 | steps: 22 | - checkout 23 | - run: 24 | name: "NPM Install" 25 | command: npm install 26 | - run: 27 | name: "NPM Test" 28 | command: npm test 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "git-json-merge", 3 | "version": "1.0.0", 4 | "description": "a git merge driver for json files", 5 | "scripts": { 6 | "postversion": "git push && git push --tags", 7 | "preversion": "npm test", 8 | "test": "mocha" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/jonatanpedersen/git-json-merge.git" 13 | }, 14 | "author": "", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/jonatanpedersen/git-json-merge/issues" 18 | }, 19 | "main": "lib/git-json-merge.js", 20 | "homepage": "https://github.com/jonatanpedersen/git-json-merge#readme", 21 | "bin": { 22 | "git-json-merge": "bin/git-json-merge" 23 | }, 24 | "dependencies": { 25 | "detect-indent": "^6.0.0", 26 | "xdiff": "^0.2.11" 27 | }, 28 | "devDependencies": { 29 | "chai": "^4.3.7", 30 | "mocha": "^10.2.0" 31 | }, 32 | "engines": { 33 | "node": ">=6" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jonatan Pedersen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /lib/git-json-merge.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var xdiff = require('xdiff'); 3 | var detectIndent = require('detect-indent'); 4 | 5 | var encoding = 'utf-8'; 6 | 7 | function mergeJsonFiles (oursFileName, baseFileName, theirsFileName) { 8 | var oursJson = stripBom(fs.readFileSync(oursFileName, encoding)); 9 | var baseJson = stripBom(fs.readFileSync(baseFileName, encoding)); 10 | var theirsJson = stripBom(fs.readFileSync(theirsFileName, encoding)); 11 | var newOursJson = mergeJson(oursJson, baseJson, theirsJson); 12 | fs.writeFileSync(oursFileName, newOursJson, encoding); 13 | } 14 | 15 | function mergeJson (oursJson, baseJson, theirsJson) { 16 | var oursIndent = detectIndent(oursJson).indent; 17 | var baseIndent = detectIndent(baseJson).indent; 18 | var theirsIndent = detectIndent(theirsJson).indent; 19 | var newOursIndent = selectIndent(oursIndent, baseIndent, theirsIndent); 20 | var ours = JSON.parse(oursJson); 21 | var base = JSON.parse(baseJson); 22 | var theirs = JSON.parse(theirsJson); 23 | var newOurs = merge(ours, base, theirs); 24 | var newOursJson = JSON.stringify(newOurs, null, newOursIndent); 25 | 26 | return newOursJson; 27 | } 28 | 29 | function merge (ours, base, theirs) { 30 | var diff = xdiff.diff3(ours, base, theirs); 31 | 32 | if (diff) { 33 | return xdiff.patch(base, diff); 34 | } 35 | 36 | return base; 37 | } 38 | 39 | function selectIndent (oursIndent, baseIndent, theirsIndent) { 40 | return oursIndent !== baseIndent ? oursIndent : theirsIndent !== baseIndent ? theirsIndent : baseIndent; 41 | } 42 | 43 | function stripBom (str) { 44 | return str[0] === '\uFEFF' ? str.slice(1) : str; 45 | } 46 | 47 | module.exports = { 48 | mergeJsonFiles: mergeJsonFiles, 49 | mergeJson: mergeJson, 50 | merge: merge, 51 | selectIndent: selectIndent, 52 | stripBom: stripBom 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-json-merge 2 | 3 | A git merge driver that use [xdiff](https://github.com/dominictarr/xdiff) to automatically resolve merge conflicts in json files. It also detects indentation automatically. This project was inspired by [git-po-merge](https://github.com/beck/git-po-merge). 4 | 5 | [![@git-json-merge](https://circleci.com/gh/jonatanpedersen/git-json-merge.svg?style=shield)](https://app.circleci.com/pipelines/github/jonatanpedersen/git-json-merge) 6 | [![NPM Version](https://img.shields.io/npm/v/git-json-merge.svg)](https://www.npmjs.com/package/git-json-merge) 7 | 8 | ## Install 9 | 10 | This can be done one of two ways, globally or per-project/directory: 11 | 12 | ### Globally 13 | 14 | Install: 15 | 16 | ```sh 17 | npm install --global git-json-merge 18 | ``` 19 | 20 | Add to `~/.gitconfig`: 21 | 22 | ```ini 23 | [core] 24 | attributesfile = ~/.gitattributes 25 | [merge "json"] 26 | name = custom merge driver for json files 27 | driver = git-json-merge %A %O %B 28 | ``` 29 | 30 | Create `~/.gitattributes`: 31 | 32 | ```ini 33 | *.json merge=json 34 | ``` 35 | 36 | ### Single project / directory 37 | 38 | Install: 39 | 40 | ```sh 41 | npm install git-json-merge --save-dev 42 | ``` 43 | 44 | Update git config: 45 | 46 | ```sh 47 | git config merge.json.driver "$(npm bin)/git-json-merge %A %O %B" 48 | git config merge.json.name "custom merge driver for json files" 49 | ``` 50 | 51 | Add the same `.gitattributes` where desired and commit. 52 | Note `.gitattributes` is only used after committed. 53 | 54 | Helpful docs: 55 | 56 | - http://git-scm.com/docs/gitattributes#_defining_a_custom_merge_driver 57 | - http://stackoverflow.com/questions/28026767/where-should-i-place-my-global-gitattributes-file 58 | 59 | Thanks: 60 | 61 | - https://gist.github.com/mezis/1605647 62 | - http://stackoverflow.com/questions/16214067/wheres-the-3-way-git-merge-driver-for-po-gettext-files 63 | -------------------------------------------------------------------------------- /test/git-json-merge.spec.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var expect = chai.expect; 3 | var gitJsonMerge = require('../lib/git-json-merge'); 4 | 5 | describe('gitJsonMerge', function () { 6 | var foo = { foo: 'foo' }; 7 | var bar = { bar: 'bar' }; 8 | var fooBar = { foo: 'foo', bar: 'bar' }; 9 | 10 | describe('mergeJson', function () { 11 | describeMergeJsonTest(foo, foo, foo, foo); 12 | describeMergeJsonTest(foo, foo, bar, bar); 13 | describeMergeJsonTest(fooBar, foo, foo, fooBar); 14 | describeMergeJsonTest(fooBar, foo, bar, bar); 15 | describeMergeJsonTest(bar, fooBar, bar, bar); 16 | describeMergeJsonTest(bar, fooBar, fooBar, bar); 17 | }); 18 | 19 | describe('selectIndent', function () { 20 | describeSelectIndentTest(4, 2, 2, 4); 21 | describeSelectIndentTest(4, 4, 2, 2); 22 | describeSelectIndentTest(4, 4, 4, 4); 23 | describeSelectIndentTest(2, 4, 2, 2); 24 | describeSelectIndentTest(2, 2, 4, 4); 25 | describeSelectIndentTest(2, 4, 4, 2); 26 | }); 27 | 28 | describe('stripBom', function () { 29 | describeStripBomTest('[{"id":1,"field":"Foo"}]', '[{"id":1,"field":"Foo"}]'); 30 | describeStripBomTest('\uFEFF[{"id":1,"field":"Foo"}]', '[{"id":1,"field":"Foo"}]'); 31 | describeStripBomTest('[{"id":1,"field":"Foo"}]\uFEFF', '[{"id":1,"field":"Foo"}]\uFEFF'); 32 | describeStripBomTest('[{"id":1,\uFEFF"field":"Foo"}]', '[{"id":1,\uFEFF"field":"Foo"}]'); 33 | describeStripBomTest('\uFEFF[{"id":1,"field":"Foo"}]\uFEFF', '[{"id":1,"field":"Foo"}]\uFEFF'); 34 | }); 35 | }); 36 | 37 | function toString (object) { 38 | return JSON.stringify(object); 39 | } 40 | 41 | function clone (object) { 42 | return JSON.parse(JSON.stringify(object)); 43 | } 44 | 45 | function repeatCharacter (character, count) { 46 | return new Array(count + 1).join(character); 47 | } 48 | 49 | function describeMergeJsonTest (ours, base, theirs, expected) { 50 | ours = toString(clone(ours)); 51 | base = toString(clone(base)); 52 | theirs = toString(clone(theirs)); 53 | expected = toString(clone(expected)); 54 | 55 | describe('given arguments of ' + toString(ours) + ' as ours, ' + toString(base) + ' as base and ' + toString(theirs) + ' as theirs', function () { 56 | var actual = gitJsonMerge.mergeJson(ours, base, theirs); 57 | 58 | it('should return ' + toString(expected), function () { 59 | expect(actual).to.deep.equal(expected); 60 | }) 61 | }); 62 | } 63 | 64 | function describeSelectIndentTest (ours, base, theirs, expected) { 65 | var character = ' '; 66 | ours = repeatCharacter(character, ours); 67 | base = repeatCharacter(character, base); 68 | theirs = repeatCharacter(character, theirs); 69 | expected = repeatCharacter(character, expected); 70 | 71 | describe('given arguments of ' + ours.length + ' as ours, ' + base.length + ' as base and ' + theirs.length + ' as theirs', function () { 72 | var actual = gitJsonMerge.selectIndent(ours, base, theirs); 73 | it('should return ' + expected.length, function () { 74 | expect(actual).to.equal(expected); 75 | }) 76 | }); 77 | } 78 | 79 | function describeStripBomTest (str, expected) { 80 | describe('given arguments of ' + str.replace('\uFEFF', '') + ' as str', function () { 81 | var actual = gitJsonMerge.stripBom(str); 82 | 83 | it('should return ' + expected.replace('\uFEFF', ''), function () { 84 | expect(actual).to.equal(expected); 85 | }) 86 | }); 87 | } 88 | --------------------------------------------------------------------------------