-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathmerge-cleanly.js
More file actions
133 lines (118 loc) · 3.67 KB
/
merge-cleanly.js
File metadata and controls
133 lines (118 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var nodegit = require("../");
var path = require("path");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
fse.ensureDir = promisify(fse.ensureDir);
var ourFileName = "ourNewFile.txt";
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!";
var ourBranchName = "ours";
var theirFileName = "theirNewFile.txt";
var theirFileContent = "I'm skeptical about Toll Roads";
var theirBranchName = "theirs";
var repoDir = "../../newRepo";
var repository;
var ourCommit;
var theirCommit;
var ourBranch;
var theirBranch;
var ourSignature = nodegit.Signature.create("Ron Paul",
"RonPaul@TollRoadsRBest.info", 123456789, 60);
var theirSignature = nodegit.Signature.create("Greg Abbott",
"Gregggg@IllTollYourFace.us", 123456789, 60);
// Create a new repository in a clean directory, and add our first file
fse.remove(path.resolve(__dirname, repoDir))
.then(function() {
return fse.ensureDir(path.resolve(__dirname, repoDir));
})
.then(function() {
return nodegit.Repository.init(path.resolve(__dirname, repoDir), 0);
})
.then(function(repo) {
repository = repo;
return fse.writeFile(
path.join(repository.workdir(), ourFileName),
ourFileContent
);
})
// Load up the repository index and make our initial commit to HEAD
.then(function() {
return repository.refreshIndex();
})
.then(function(index) {
return index.addByPath(ourFileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
})
.then(function(oid) {
return repository.createCommit("HEAD", ourSignature,
ourSignature, "we made a commit", oid, []);
})
// Get commit object from the oid, and create our new branches at that position
.then(function(commitOid) {
return repository.getCommit(commitOid).then(function(commit) {
ourCommit = commit;
}).then(function() {
return repository.createBranch(ourBranchName, commitOid)
.then(function(branch) {
ourBranch = branch;
return repository.createBranch(theirBranchName, commitOid);
});
});
})
// Create a new file, stage it and commit it to our second branch
.then(function(branch) {
theirBranch = branch;
return fse.writeFile(
path.join(repository.workdir(), theirFileName),
theirFileContent
);
})
.then(function() {
return repository.refreshIndex();
})
.then(function(index) {
return index.addByPath(theirFileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
})
.then(function(oid) {
// You don"t have to change head to make a commit to a different branch.
return repository.createCommit(theirBranch.name(), theirSignature,
theirSignature, "they made a commit", oid, [ourCommit]);
})
.then(function(commitOid) {
return repository.getCommit(commitOid).then(function(commit) {
theirCommit = commit;
});
})
// Merge the two commits
.then(function() {
return nodegit.Merge.commits(repository, ourCommit, theirCommit);
})
// Merging returns an index that isn't backed by the repository.
// You have to manually check for merge conflicts. If there are none
// you just have to write the index. You do have to write it to
// the repository instead of just writing it.
.then(function(index) {
if (!index.hasConflicts()) {
return index.writeTreeTo(repository);
}
})
// Create our merge commit back on our branch
.then(function(oid) {
return repository.createCommit(ourBranch.name(), ourSignature,
ourSignature, "we merged their commit", oid, [ourCommit, theirCommit]);
})
.done(function(commitId) {
// We never changed the HEAD after the initial commit;
// it should still be the same as master.
console.log("New Commit: ", commitId);
});