|
| 1 | +var assert = require("assert"); |
| 2 | +var path = require("path"); |
| 3 | +var local = path.join.bind(path, __dirname); |
| 4 | +var Promise = require("nodegit-promise"); |
| 5 | +var promisify = require("promisify-node"); |
| 6 | +var fse = promisify(require("fs-extra")); |
| 7 | + |
| 8 | +describe("Rebase", function() { |
| 9 | + var NodeGit = require("../../"); |
| 10 | + |
| 11 | + var repoPath = local("../repos/rebase"); |
| 12 | + var ourBranchName = "ours"; |
| 13 | + var theirBranchName = "theirs"; |
| 14 | + |
| 15 | + beforeEach(function() { |
| 16 | + var test = this; |
| 17 | + return fse.remove(repoPath) |
| 18 | + .then(function() { |
| 19 | + return fse.ensureDir(repoPath); |
| 20 | + }) |
| 21 | + .then(function() { |
| 22 | + return NodeGit.Repository.init(repoPath, 0); |
| 23 | + }) |
| 24 | + .then(function(repo) { |
| 25 | + test.repository = repo; |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + after(function() { |
| 30 | + return fse.remove(repoPath); |
| 31 | + }); |
| 32 | + |
| 33 | + it("can cleanly fast-forward via rebase", function() { |
| 34 | + var ourFileName = "ourNewFile.txt"; |
| 35 | + var theirFileName = "theirNewFile.txt"; |
| 36 | + |
| 37 | + var ourFileContent = "I like Toll Roads. I have an EZ-Pass!"; |
| 38 | + var theirFileContent = "I'm skeptical about Toll Roads"; |
| 39 | + |
| 40 | + var ourSignature = NodeGit.Signature.create |
| 41 | + ("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60); |
| 42 | + var theirSignature = NodeGit.Signature.create |
| 43 | + ("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60); |
| 44 | + |
| 45 | + var repository = this.repository; |
| 46 | + var ourCommit; |
| 47 | + var theirBranch; |
| 48 | + |
| 49 | + return fse.writeFile(path.join(repository.workdir(), ourFileName), |
| 50 | + ourFileContent) |
| 51 | + // Load up the repository index and make our initial commit to HEAD |
| 52 | + .then(function() { |
| 53 | + return repository.openIndex() |
| 54 | + .then(function(index) { |
| 55 | + index.read(1); |
| 56 | + index.addByPath(ourFileName); |
| 57 | + index.write(); |
| 58 | + |
| 59 | + return index.writeTree(); |
| 60 | + }); |
| 61 | + }) |
| 62 | + .then(function(oid) { |
| 63 | + assert.equal(oid.toString(), |
| 64 | + "11ead82b1135b8e240fb5d61e703312fb9cc3d6a"); |
| 65 | + |
| 66 | + return repository.createCommit("HEAD", ourSignature, |
| 67 | + ourSignature, "we made a commit", oid, []); |
| 68 | + }) |
| 69 | + .then(function(commitOid) { |
| 70 | + assert.equal(commitOid.toString(), |
| 71 | + "91a183f87842ebb7a9b08dad8bc2473985796844"); |
| 72 | + |
| 73 | + return repository.getCommit(commitOid).then(function(commit) { |
| 74 | + ourCommit = commit; |
| 75 | + }).then(function() { |
| 76 | + return repository.createBranch(ourBranchName, commitOid) |
| 77 | + .then(function(branch) { |
| 78 | + return repository.createBranch(theirBranchName, commitOid); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }) |
| 82 | + .then(function(branch) { |
| 83 | + theirBranch = branch; |
| 84 | + return fse.writeFile(path.join(repository.workdir(), theirFileName), |
| 85 | + theirFileContent); |
| 86 | + }) |
| 87 | + .then(function() { |
| 88 | + return repository.openIndex() |
| 89 | + .then(function(index) { |
| 90 | + index.read(1); |
| 91 | + index.addByPath(theirFileName); |
| 92 | + index.write(); |
| 93 | + |
| 94 | + return index.writeTree(); |
| 95 | + }); |
| 96 | + }) |
| 97 | + .then(function(oid) { |
| 98 | + assert.equal(oid.toString(), |
| 99 | + "76631cb5a290dafe2959152626bb90f2a6d8ec94"); |
| 100 | + |
| 101 | + return repository.createCommit(theirBranch.name(), theirSignature, |
| 102 | + theirSignature, "they made a commit", oid, [ourCommit]); |
| 103 | + }) |
| 104 | + .then(function(commitOid) { |
| 105 | + assert.equal(commitOid.toString(), |
| 106 | + "0e9231d489b3f4303635fc4b0397830da095e7e7"); |
| 107 | + }) |
| 108 | + .then(function() { |
| 109 | + // unstage changes so that we can begin a rebase |
| 110 | + return repository.openIndex() |
| 111 | + .then(function(index) { |
| 112 | + index.read(1); |
| 113 | + index.removeByPath(theirFileName); |
| 114 | + index.write(); |
| 115 | + |
| 116 | + return index.writeTree(); |
| 117 | + }); |
| 118 | + }) |
| 119 | + .then(function() { |
| 120 | + return Promise.all([ |
| 121 | + repository.getReference(ourBranchName), |
| 122 | + repository.getReference(theirBranchName) |
| 123 | + ]); |
| 124 | + }) |
| 125 | + .then(function(refs) { |
| 126 | + assert.equal(refs.length, 2); |
| 127 | + |
| 128 | + return Promise.all([ |
| 129 | + NodeGit.AnnotatedCommit.fromRef(repository, refs[0]), |
| 130 | + NodeGit.AnnotatedCommit.fromRef(repository, refs[1]) |
| 131 | + ]); |
| 132 | + }) |
| 133 | + .then(function(annotatedCommits) { |
| 134 | + assert.equal(annotatedCommits.length, 2); |
| 135 | + var ourAnnotatedCommit = annotatedCommits[0]; |
| 136 | + var theirAnnotatedCommit = annotatedCommits[1]; |
| 137 | + |
| 138 | + assert.equal(ourAnnotatedCommit.id().toString(), |
| 139 | + "91a183f87842ebb7a9b08dad8bc2473985796844"); |
| 140 | + assert.equal(theirAnnotatedCommit.id().toString(), |
| 141 | + "0e9231d489b3f4303635fc4b0397830da095e7e7"); |
| 142 | + |
| 143 | + return NodeGit.Rebase.init(repository, ourAnnotatedCommit, |
| 144 | + theirAnnotatedCommit, theirAnnotatedCommit, ourSignature, |
| 145 | + new NodeGit.RebaseOptions()); |
| 146 | + }) |
| 147 | + .then(function(rebase) { |
| 148 | + assert.equal(rebase.operationEntrycount(), 0); |
| 149 | + assert.equal(rebase.operationCurrent(), 0); |
| 150 | + |
| 151 | + return rebase.finish(ourSignature, new NodeGit.RebaseOptions()); |
| 152 | + }) |
| 153 | + .then(function() { |
| 154 | + return repository.getBranchCommit(ourBranchName); |
| 155 | + }) |
| 156 | + .then(function(commit) { |
| 157 | + assert.equal(commit.id().toString(), |
| 158 | + "0e9231d489b3f4303635fc4b0397830da095e7e7"); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments