|
| 1 | +var assert = require("assert"); |
| 2 | +var NodeGit = require("../../"); |
| 3 | +var path = require("path"); |
| 4 | +var promisify = require("promisify-node"); |
| 5 | +var fse = promisify(require("fs-extra")); |
| 6 | +var RepoUtils = require("../utils/repository_setup"); |
| 7 | + |
| 8 | +var IndexSetup = { |
| 9 | + createConflict: function createConflict( |
| 10 | + repository, |
| 11 | + _ourBranchName, |
| 12 | + _theirBranchName, |
| 13 | + _fileName |
| 14 | + ) { |
| 15 | + var fileName = _fileName || "everyonesFile.txt"; |
| 16 | + |
| 17 | + var ourBranchName = _ourBranchName || "ours"; |
| 18 | + var theirBranchName = _theirBranchName || "theirs"; |
| 19 | + |
| 20 | + var baseFileContent = "How do you feel about Toll Roads?\n"; |
| 21 | + var ourFileContent = "I like Toll Roads. I have an EZ-Pass!\n"; |
| 22 | + var theirFileContent = "I'm skeptical about Toll Roads\n"; |
| 23 | + |
| 24 | + var ourSignature = NodeGit.Signature.create |
| 25 | + ("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60); |
| 26 | + var theirSignature = NodeGit.Signature.create |
| 27 | + ("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60); |
| 28 | + |
| 29 | + var ourCommit; |
| 30 | + var ourBranch; |
| 31 | + var theirBranch; |
| 32 | + |
| 33 | + return fse.writeFile( |
| 34 | + path.join(repository.workdir(), fileName), |
| 35 | + baseFileContent |
| 36 | + ) |
| 37 | + .then(function() { |
| 38 | + return RepoUtils.addFileToIndex(repository, fileName); |
| 39 | + }) |
| 40 | + .then(function(oid) { |
| 41 | + return repository.createCommit("HEAD", ourSignature, |
| 42 | + ourSignature, "initial commit", oid, []); |
| 43 | + }) |
| 44 | + .then(function(commitOid) { |
| 45 | + return repository.getCommit(commitOid).then(function(commit) { |
| 46 | + ourCommit = commit; |
| 47 | + }).then(function() { |
| 48 | + console.log("after creating base commit"); |
| 49 | + return repository.createBranch(ourBranchName, commitOid) |
| 50 | + .then(function(branch) { |
| 51 | + console.log("after creating our branch"); |
| 52 | + ourBranch = branch; |
| 53 | + return repository.createBranch(theirBranchName, commitOid); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }) |
| 57 | + .then(function(branch) { |
| 58 | + console.log("after creating their commit"); |
| 59 | + |
| 60 | + theirBranch = branch; |
| 61 | + return fse.writeFile(path.join(repository.workdir(), fileName), |
| 62 | + baseFileContent + theirFileContent); |
| 63 | + }) |
| 64 | + .then(function() { |
| 65 | + return RepoUtils.addFileToIndex(repository, fileName); |
| 66 | + }) |
| 67 | + .then(function(oid) { |
| 68 | + return repository.createCommit(theirBranch.name(), theirSignature, |
| 69 | + theirSignature, "they made a commit", oid, [ourCommit]); |
| 70 | + }) |
| 71 | + .then(function(commitOid) { |
| 72 | + return fse.writeFile(path.join(repository.workdir(), fileName), |
| 73 | + baseFileContent + ourFileContent); |
| 74 | + }) |
| 75 | + .then(function() { |
| 76 | + return RepoUtils.addFileToIndex(repository, fileName); |
| 77 | + }) |
| 78 | + .then(function(oid) { |
| 79 | + return repository.createCommit(ourBranch.name(), ourSignature, |
| 80 | + ourSignature, "we made a commit", oid, [ourCommit]); |
| 81 | + }) |
| 82 | + .then(function(commitOid) { |
| 83 | + var opts = { |
| 84 | + checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE |
| 85 | + }; |
| 86 | + |
| 87 | + return NodeGit.Checkout.head(repository, opts); |
| 88 | + }) |
| 89 | + .then(function() { |
| 90 | + return repository.mergeBranches(ourBranchName, theirBranchName); |
| 91 | + }) |
| 92 | + .then(function(commit) { |
| 93 | + assert.fail(commit, undefined, |
| 94 | + "The index should have been thrown due to merge conflicts"); |
| 95 | + }) |
| 96 | + .catch(function(index) { |
| 97 | + assert.ok(index); |
| 98 | + assert.ok(index.hasConflicts()); |
| 99 | + |
| 100 | + return index.conflictGet(fileName); |
| 101 | + }); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +module.exports = IndexSetup; |
0 commit comments