Skip to content

Commit 162c3bc

Browse files
committed
Repository.mergeBranches merges HEAD with a dirty index
1 parent d847093 commit 162c3bc

File tree

2 files changed

+132
-16
lines changed

2 files changed

+132
-16
lines changed

lib/repository.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ Repository.prototype.mergeBranches = function(
15591559
})
15601560
.then(function(tree) {
15611561
var opts = {
1562-
checkoutStrategy: NodeGit.Checkout.STRATEGY.SAFE |
1562+
checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE |
15631563
NodeGit.Checkout.STRATEGY.RECREATE_MISSING
15641564
};
15651565
return NodeGit.Checkout.tree(repo, tree, opts);

test/tests/merge.js

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ describe("Merge", function() {
364364
});
365365
});
366366

367-
it("can merge --no-ff a non-fast-forward using the convenience method",
368-
function() {
367+
function mergeUnrelatedChanges(repository, head) {
369368
var initialFileName = "initialFile.txt";
370369
var ourFileName = "ourNewFile.txt";
371370
var theirFileName = "theirNewFile.txt";
@@ -379,7 +378,6 @@ describe("Merge", function() {
379378
var theirSignature = NodeGit.Signature.create
380379
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
381380

382-
var repository = this.repository;
383381
var initialCommit;
384382
var ourCommit;
385383
var theirCommit;
@@ -416,16 +414,18 @@ describe("Merge", function() {
416414
return repository.getCommit(commitOid).then(function(commit) {
417415
initialCommit = commit;
418416
}).then(function() {
419-
return repository.createBranch(ourBranchName, commitOid)
417+
return repository.createBranch(theirBranchName, commitOid)
420418
.then(function(branch) {
421-
ourBranch = branch;
422-
return repository.createBranch(theirBranchName, commitOid);
419+
theirBranch = branch;
420+
if (!head) {
421+
return repository.createBranch(ourBranchName, commitOid)
422+
.then(function(branch) {
423+
ourBranch = branch;
424+
});
425+
}
423426
});
424427
});
425428
})
426-
.then(function(branch) {
427-
theirBranch = branch;
428-
})
429429
.then(function() {
430430
return fse.writeFile(path.join(repository.workdir(), ourFileName),
431431
ourFileContent);
@@ -445,8 +445,8 @@ describe("Merge", function() {
445445
.then(function(oid) {
446446
assert.equal(oid.toString(),
447447
"af60aa06b3537f75b427f6268a130c842c84a137");
448-
449-
return repository.createCommit(ourBranch.name(), ourSignature,
448+
var branch = ourBranch ? "refs/heads/" + ourBranchName : "HEAD";
449+
return repository.createCommit(branch, ourSignature,
450450
ourSignature, "we made a commit", oid, [initialCommit]);
451451
})
452452
.then(function(commitOid) {
@@ -489,19 +489,26 @@ describe("Merge", function() {
489489
});
490490
})
491491
.then(function() {
492+
var branch = ourBranch ? ourBranchName : "HEAD";
492493
var opts = {checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE};
493-
return repository.checkoutBranch(ourBranchName, opts);
494+
return repository.checkoutBranch(branch, opts);
494495
})
495496
.then(function() {
497+
var branch = ourBranch ? ourBranchName : "HEAD";
496498
return repository.mergeBranches(
497-
ourBranchName,
499+
branch,
498500
theirBranchName,
499501
ourSignature,
500502
NodeGit.Merge.PREFERENCE.NO_FASTFORWARD);
501503
})
502504
.then(function(commitId) {
503-
assert.equal(commitId.toString(),
504-
"96d6f1d0704eb3ef9121a13348d17c1d672c28aa");
505+
if (head) {
506+
assert.equal(commitId.toString(),
507+
"4a21e4f93ec1900bc3446a9867906d5982f18172");
508+
} else {
509+
assert.equal(commitId.toString(),
510+
"96d6f1d0704eb3ef9121a13348d17c1d672c28aa");
511+
}
505512
})
506513
.then(function() {
507514
return repository.getStatus();
@@ -510,6 +517,18 @@ describe("Merge", function() {
510517
// make sure we didn't change the index
511518
assert.equal(statuses.length, 0);
512519
});
520+
}
521+
522+
it("can merge --no-ff a non-fast-forward " +
523+
"using the convenience method updating HEAD",
524+
function() {
525+
return mergeUnrelatedChanges(this.repository, true);
526+
});
527+
528+
it("can merge --no-ff a non-fast-forward " +
529+
"using the convenience method without updating HEAD",
530+
function() {
531+
return mergeUnrelatedChanges(this.repository, false);
513532
});
514533

515534
it(
@@ -1613,6 +1632,103 @@ describe("Merge", function() {
16131632
});
16141633
});
16151634

1635+
it("merging to HEAD with convenience method leaves clean index", function() {
1636+
var repository = this.repository;
1637+
var name = "test.txt";
1638+
var name2 = "test2.txt";
1639+
var fileName = path.resolve(repository.workdir(), name);
1640+
var fileName2 = path.resolve(repository.workdir(), name2);
1641+
var index;
1642+
var head;
1643+
var left;
1644+
var leftBranch;
1645+
return fse.writeFile(fileName, "A")
1646+
.then(function() {
1647+
return repository.refreshIndex();
1648+
})
1649+
.then(function(idx) {
1650+
index = idx;
1651+
return index.addByPath(name);
1652+
})
1653+
.then(function() {
1654+
return index.writeTree();
1655+
})
1656+
.then(function(oid) {
1657+
return repository.createCommit("HEAD",
1658+
NodeGit.Signature.default(repository),
1659+
NodeGit.Signature.default(repository),
1660+
"message", oid, []);
1661+
})
1662+
.then(function(oid) {
1663+
head = oid;
1664+
return fse.writeFile(fileName, "B");
1665+
})
1666+
.then(function() {
1667+
return repository.refreshIndex();
1668+
})
1669+
.then(function(idx) {
1670+
index = idx;
1671+
return index.addByPath(name);
1672+
})
1673+
.then(function() {
1674+
return index.writeTree();
1675+
})
1676+
.then(function(oid) {
1677+
return repository.createCommit("HEAD",
1678+
NodeGit.Signature.default(repository),
1679+
NodeGit.Signature.default(repository),
1680+
"left", oid, [ head ]);
1681+
})
1682+
.then(function(_left) {
1683+
left = _left;
1684+
return repository.getCommit(left);
1685+
})
1686+
.then(function(commit) {
1687+
return NodeGit.Branch.create(repository, "leftBranch", commit, false);
1688+
})
1689+
.then(function(branch) {
1690+
leftBranch = branch;
1691+
return repository.getCommit(head);
1692+
})
1693+
.then(function(commit) {
1694+
return NodeGit.Reset.reset(
1695+
repository, commit, NodeGit.Reset.TYPE.HARD, {});
1696+
})
1697+
.then(function() {
1698+
return fse.writeFile(fileName2, "C");
1699+
})
1700+
.then(function() {
1701+
return repository.refreshIndex();
1702+
})
1703+
.then(function(idx) {
1704+
index = idx;
1705+
return index.addByPath(name2);
1706+
})
1707+
.then(function() {
1708+
return index.write();
1709+
})
1710+
.then(function() {
1711+
return index.writeTree();
1712+
})
1713+
.then(function(oid) {
1714+
return repository.createCommit("HEAD",
1715+
NodeGit.Signature.default(repository),
1716+
NodeGit.Signature.default(repository),
1717+
"right", oid, [ head ]);
1718+
})
1719+
.then(function(commit) {
1720+
return repository.mergeBranches(
1721+
"master", leftBranch,
1722+
NodeGit.Signature.default(repository), null, null);
1723+
})
1724+
.then(function() {
1725+
return repository.getStatus();
1726+
})
1727+
.then(function(statuses) {
1728+
assert.equal(statuses.length, 0);
1729+
});
1730+
});
1731+
16161732
it("can retrieve error code on if common merge base not found", function() {
16171733
var repo;
16181734
return NodeGit.Repository.open(local("../repos/workdir"))

0 commit comments

Comments
 (0)