Skip to content

Commit 1c334c6

Browse files
author
Carson Howard
committed
Added a unit test and better comments
1 parent 72644a6 commit 1c334c6

2 files changed

Lines changed: 172 additions & 10 deletions

File tree

lib/repository.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,8 @@ function performRebase(
204204
) {
205205
var beforeNextFnResult;
206206

207-
/* In the case of FF merges, this will fail
208-
* when looking for 'rewritten' so we need
209-
* to handle that case.
207+
/* In the case of FF merges and a beforeFinishFn, this will fail
208+
* when looking for 'rewritten' so we need to handle that case.
210209
*/
211210
function readRebaseMetadataFile(fileName, determineExistance) {
212211
const fullPath = path.join(repository.path(), "rebase-merge", fileName);
@@ -249,13 +248,12 @@ function performRebase(
249248
})
250249
.catch(function(error) {
251250
if (error && error.errno === NodeGit.Error.CODE.ITEROVER) {
252-
const calcRewritten = fp.flow([
253-
fp.cond([
254-
[fp.isEmpty, fp.constant("")],
255-
[fp.stubTrue, fp.identity]
256-
]),
257-
fp.split("\n"),
258-
fp.map(fp.split(" "))
251+
const calcRewritten = fp.cond([
252+
[fp.isEmpty, fp.constant(null)],
253+
[fp.stubTrue, fp.flow([
254+
fp.split("\n"),
255+
fp.map(fp.split(" "))
256+
])]
259257
]);
260258

261259
const beforeFinishFnPromise = !beforeFinishFn ?

test/tests/rebase.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var fse = promisify(require("fs-extra"));
66

77
describe("Rebase", function() {
88
var NodeGit = require("../../");
9+
var Checkout = NodeGit.Checkout;
10+
var Merge = NodeGit.Merge;
911
var RepoUtils = require("../utils/repository_setup");
1012

1113
var repoPath = local("../repos/rebase");
@@ -790,6 +792,168 @@ describe("Rebase", function() {
790792
});
791793
});
792794

795+
it(
796+
"can fast-forward a merge commit via rebase using the " +
797+
"convenience methods that has a beforeFinishFn",
798+
function() {
799+
var ourFileName = "ourNewFile.txt";
800+
var theirFileName = "theirNewFile.txt";
801+
var theirOtherFileName = "antherNewFile.txt";
802+
803+
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!";
804+
var theirFileContent = "I'm skeptical about Toll Roads";
805+
var theirOtherFileContent = "This is some more content, guys!";
806+
807+
var ourSignature = NodeGit.Signature.create
808+
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
809+
var theirSignature = NodeGit.Signature.create
810+
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
811+
var theirOtherSignature = NodeGit.Signature.create
812+
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456999, 60);
813+
var ourMergeSignature = NodeGit.Signature.create
814+
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456889, 60);
815+
816+
var repository = this.repository;
817+
var ourCommit;
818+
var theirCommit;
819+
var theirBranch;
820+
var ourBranch;
821+
822+
return fse.writeFile(path.join(repository.workdir(), ourFileName),
823+
ourFileContent)
824+
// Load up the repository index and make our initial commit to HEAD
825+
.then(function() {
826+
return RepoUtils.addFileToIndex(repository, ourFileName);
827+
})
828+
.then(function(oid) {
829+
assert.equal(oid.toString(),
830+
"11ead82b1135b8e240fb5d61e703312fb9cc3d6a");
831+
832+
return repository.createCommit("HEAD", ourSignature,
833+
ourSignature, "we made a commit", oid, []);
834+
})
835+
.then(function(commitOid) {
836+
assert.equal(commitOid.toString(),
837+
"91a183f87842ebb7a9b08dad8bc2473985796844");
838+
839+
return repository.getCommit(commitOid).then(function(commit) {
840+
ourCommit = commit;
841+
}).then(function() {
842+
return repository.createBranch(ourBranchName, commitOid)
843+
.then(function(branch) {
844+
ourBranch = branch;
845+
return repository.createBranch(theirBranchName, commitOid);
846+
});
847+
});
848+
})
849+
.then(function(branch) {
850+
theirBranch = branch;
851+
return fse.writeFile(path.join(repository.workdir(), theirFileName),
852+
theirFileContent);
853+
})
854+
.then(function() {
855+
return RepoUtils.addFileToIndex(repository, theirFileName);
856+
})
857+
.then(function(oid) {
858+
assert.equal(oid.toString(),
859+
"76631cb5a290dafe2959152626bb90f2a6d8ec94");
860+
861+
return repository.createCommit(theirBranch.name(), theirSignature,
862+
theirSignature, "they made a commit", oid, [ourCommit]);
863+
})
864+
.then(function(commitOid) {
865+
theirCommit = commitOid;
866+
assert.equal(commitOid.toString(),
867+
"0e9231d489b3f4303635fc4b0397830da095e7e7");
868+
})
869+
.then(function() {
870+
return repository.checkoutBranch(
871+
ourBranch,
872+
{ checkoutStrategy: Checkout.STRATEGY.FORCE }
873+
);
874+
})
875+
.then(function() {
876+
return repository.mergeBranches(
877+
ourBranchName,
878+
theirBranchName,
879+
ourMergeSignature,
880+
Merge.PREFERENCE.NO_FASTFORWARD
881+
);
882+
})
883+
.then(function() {
884+
return repository.getHeadCommit();
885+
})
886+
.then(function(headCommit) {
887+
assert.notEqual(ourCommit.id().toString(), headCommit.id().toString());
888+
})
889+
.then(function() {
890+
return repository.checkoutBranch(
891+
theirBranch,
892+
{ checkoutStrategy: Checkout.STRATEGY.FORCE }
893+
);
894+
})
895+
.then(function() {
896+
return fse.writeFile(
897+
path.join(repository.workdir(), theirOtherFileName),
898+
theirOtherFileContent
899+
);
900+
})
901+
.then(function() {
902+
return RepoUtils.addFileToIndex(repository, theirOtherFileName);
903+
})
904+
.then(function(oid) {
905+
assert.equal(oid.toString(),
906+
"c242b53f2c9446544cf9bdac7e8ed6ce583226cb");
907+
908+
return repository.createCommit(theirBranch.name(), theirOtherSignature,
909+
theirOtherSignature, "they made another commit", oid, [theirCommit]);
910+
})
911+
.then(function(commitOid) {
912+
assert.equal(commitOid.toString(),
913+
"8fa0ce25a2accf464b004ddeeb63add7b816b627");
914+
})
915+
.then(function() {
916+
return Promise.all([
917+
repository.getReference(ourBranchName),
918+
repository.getReference(theirBranchName)
919+
]);
920+
})
921+
.then(function(refs) {
922+
assert.equal(refs.length, 2);
923+
924+
return Promise.all([
925+
NodeGit.AnnotatedCommit.fromRef(repository, refs[0]),
926+
NodeGit.AnnotatedCommit.fromRef(repository, refs[1])
927+
]);
928+
})
929+
.then(function(annotatedCommits) {
930+
assert.equal(annotatedCommits.length, 2);
931+
932+
var ourAnnotatedCommit = annotatedCommits[0];
933+
var theirAnnotatedCommit = annotatedCommits[1];
934+
935+
assert.equal(ourAnnotatedCommit.id().toString(),
936+
"0d1d322b59df68bac6eea6a2a189f974cb590368");
937+
assert.equal(theirAnnotatedCommit.id().toString(),
938+
"8fa0ce25a2accf464b004ddeeb63add7b816b627");
939+
940+
return repository.rebaseBranches(
941+
ourBranchName,
942+
theirBranchName,
943+
null,
944+
ourSignature,
945+
null,
946+
function(rebaseData) {
947+
assert.equal(rebaseData.rewritten, null);
948+
}
949+
);
950+
})
951+
.then(function(commit) {
952+
assert.equal(commit.id().toString(),
953+
"8fa0ce25a2accf464b004ddeeb63add7b816b627");
954+
});
955+
});
956+
793957
it("can rebase using the convenience method", function() {
794958
var baseFileName = "baseNewFile.txt";
795959
var ourFileName = "ourNewFile.txt";

0 commit comments

Comments
 (0)