|
| 1 | +var nodegit = require('../'); |
| 2 | +var path = require('path'); |
| 3 | +var Promise = require('nodegit-promise'); |
| 4 | +var promisify = require('promisify-node'); |
| 5 | +var fse = promisify(require('fs-extra')); |
| 6 | +fse.ensureDir = promisify(fse.ensureDir); |
| 7 | + |
| 8 | +var normalizeOptions = require('../lib/util/normalize_options'); |
| 9 | +var ourFileName = 'ourNewFile.txt'; |
| 10 | +var ourFileContent = 'I like Toll Roads. I have an EZ-Pass!'; |
| 11 | +var ourBranchName = "ours"; |
| 12 | + |
| 13 | +var theirFileName = 'theirNewFile.txt'; |
| 14 | +var theirFileContent = "I'm skeptical about Toll Roads"; |
| 15 | +var theirBranchName = "theirs"; |
| 16 | + |
| 17 | +var repoDir = '../../newRepo'; |
| 18 | + |
| 19 | +var repository; |
| 20 | +var ourCommit; |
| 21 | +var theirCommit; |
| 22 | +var ourBranch; |
| 23 | +var theirBranch; |
| 24 | + |
| 25 | +var ourSignature = nodegit.Signature.create("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60); |
| 26 | +var theirSignature = nodegit.Signature.create("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60); |
| 27 | + |
| 28 | +fse.remove(path.resolve(__dirname, repoDir)) |
| 29 | +.then(function() { |
| 30 | + return fse.ensureDir(path.resolve(__dirname, repoDir)); |
| 31 | +}) |
| 32 | +.then(function() { |
| 33 | + return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0); |
| 34 | +}) |
| 35 | +.then(function(repo) { |
| 36 | + repository = repo; |
| 37 | + return fse.writeFile(path.join(repository.workdir(), ourFileName), ourFileContent); |
| 38 | +}) |
| 39 | +.then(function() { |
| 40 | + return repository.openIndex(); |
| 41 | +}) |
| 42 | +.then(function(index) { |
| 43 | + index.read(1); |
| 44 | + index.addByPath(ourFileName); |
| 45 | + index.write() |
| 46 | + |
| 47 | + return index.writeTree(); |
| 48 | +}) |
| 49 | +.then(function(oid) { |
| 50 | + return repository.createCommit('HEAD', ourSignature, ourSignature, 'we made a commit', oid, []); |
| 51 | +}).then(function(commitOid) { |
| 52 | + |
| 53 | + return repository.getCommit(commitOid).then(function(commit) { |
| 54 | + ourCommit = commit; |
| 55 | + }).then(function() { |
| 56 | + return repository.createBranch(ourBranchName, commitOid).then(function(branch) { |
| 57 | + ourBranch = branch; |
| 58 | + return repository.createBranch(theirBranchName, commitOid); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}).then(function(branch) { |
| 62 | + theirBranch = branch; |
| 63 | + |
| 64 | + return nodegit.Reference.lookup(repository, 'HEAD').then(function(head) { |
| 65 | + return head.symbolicSetTarget(theirBranch.name(), theirSignature, ""); |
| 66 | + }); |
| 67 | +}).then(function() { |
| 68 | + return fse.writeFile(path.join(repository.workdir(), theirFileName), theirFileContent); |
| 69 | +}) |
| 70 | +.then(function() { |
| 71 | + return repository.openIndex(); |
| 72 | +}) |
| 73 | +.then(function(index) { |
| 74 | + index.read(1); |
| 75 | + index.addByPath(theirFileName); |
| 76 | + index.write() |
| 77 | + |
| 78 | + return index.writeTree(); |
| 79 | +}) |
| 80 | +.then(function(oid) { |
| 81 | + return repository.createCommit('HEAD', theirSignature, theirSignature, 'they made a commit', oid, [ourCommit]); |
| 82 | +}) |
| 83 | +.then(function(commitOid) { |
| 84 | + return repository.getCommit(commitOid).then(function(commit) { |
| 85 | + theirCommit = commit; |
| 86 | + }).then(function() { |
| 87 | + return nodegit.Reference.lookup(repository, 'HEAD').then(function(head) { |
| 88 | + return head.symbolicSetTarget(ourBranch.name(), ourSignature, ""); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}) |
| 92 | +.then(function() { |
| 93 | + return nodegit.Merge.commits(repository, ourCommit, theirCommit, new nodegit.MergeOptions()); |
| 94 | +}) |
| 95 | +.then(function(index) { |
| 96 | + index.write() |
| 97 | + return index.writeTreeTo(repository); |
| 98 | +}) |
| 99 | +.then(function(oid) { |
| 100 | + return repository.createCommit(ourBranch.name(), ourSignature, ourSignature, 'we merged their commit', oid, [ourCommit, theirCommit]); |
| 101 | +}) |
| 102 | +.done(function(commitId) { |
| 103 | + console.log('New Commit: ', commitId); |
| 104 | +}); |
0 commit comments