-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathrepository_setup.js
More file actions
200 lines (175 loc) · 5.35 KB
/
repository_setup.js
File metadata and controls
200 lines (175 loc) · 5.35 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var assert = require("assert");
var NodeGit = require("../../");
var path = require("path");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
var RepositorySetup = {
addFileToIndex:
function addFileToIndex(repository, fileName) {
return repository.refreshIndex()
.then(function(index) {
return index.addByPath(fileName)
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
});
},
commitFileToRepo:
function commitFileToRepo(repository, fileName, fileContent, parentCommit) {
var repoWorkDir = repository.workdir();
var signature = NodeGit.Signature.create("Foo bar",
"foo@bar.com", 123456789, 60);
var filePath = path.join(repoWorkDir, fileName);
var parents = [];
if (parentCommit) {
parents.push(parentCommit);
}
// fse.ensure allows us to write files inside new folders
return fse.ensureFile(filePath)
.then(function() {
return fse.writeFile(filePath, fileContent);
})
.then(function() {
return RepositorySetup.addFileToIndex(repository, fileName);
})
.then(function(oid) {
return repository.createCommit("HEAD", signature, signature,
"initial commit", oid, parents);
})
.then(function(commitOid) {
return repository.getCommit(commitOid);
});
},
createRepository:
function createRepository(repoPath){
// Create a new repository in a clean directory
return fse.remove(repoPath)
.then(function() {
return fse.ensureDir(repoPath);
})
.then(function() {
return NodeGit.Repository.init(repoPath, 0);
});
},
// Expects empty repo
setupBranches:
function setupBranches(repository, checkoutOurs) {
var repoWorkDir = repository.workdir();
var ourBranchName = "ours";
var theirBranchName = "theirs";
var baseFileName = "baseNewFile.txt";
var ourFileName = "ourNewFile.txt";
var theirFileName = "theirNewFile.txt";
var baseFileContent = "How do you feel about Toll Roads?";
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!";
var theirFileContent = "I'm skeptical about Toll Roads";
var ourSignature = NodeGit.Signature.create
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
var theirSignature = NodeGit.Signature.create
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
var initialCommit;
var ourBranch;
var theirBranch;
var ret = {
ourBranchName: ourBranchName,
theirBranchName: theirBranchName,
ourSignature: ourSignature,
theirSignature: theirSignature,
ourFileName: ourFileName,
theirFileName: theirFileName,
ourFileContent: ourFileContent,
theirFileContent: theirFileContent
};
return Promise.all([
fse.writeFile(path.join(repoWorkDir, baseFileName),
baseFileContent),
fse.writeFile(path.join(repoWorkDir, ourFileName),
ourFileContent),
fse.writeFile(path.join(repoWorkDir, theirFileName),
theirFileContent)
])
.then(function() {
return RepositorySetup.addFileToIndex(repository, baseFileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"b5cdc109d437c4541a13fb7509116b5f03d5039a");
return repository.createCommit(
"HEAD", ourSignature, ourSignature,
"initial commit", oid, []);
})
.then(function(commitOid) {
assert.equal(commitOid.toString(),
"be03abdf0353d05924c53bebeb0e5bb129cda44a");
return repository.getCommit(commitOid);
})
.then(function(commit) {
ret.initialCommit = initialCommit = commit;
return Promise.all([
repository.createBranch(ourBranchName, initialCommit),
repository.createBranch(theirBranchName, initialCommit)
]);
})
.then(function(branches) {
assert(branches[0]);
assert(branches[1]);
ret.ourBranch = ourBranch = branches[0];
ret.theirBranch = theirBranch = branches[1];
return RepositorySetup.addFileToIndex(repository, ourFileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"77867fc0bfeb3f80ab18a78c8d53aa3a06207047");
return repository.createCommit(ourBranch.name(), ourSignature,
ourSignature, "we made a commit", oid, [initialCommit]);
})
.then(function(commitOid) {
return repository.getCommit(commitOid);
})
.then(function(commit) {
ret.ourCommit = commit;
return NodeGit.Reset.default(
repository, initialCommit, ourFileName);
})
.then(function() {
return RepositorySetup.addFileToIndex(
repository, theirFileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"be5f0fd38a39a67135ad68921c93cd5c17fefb3d");
return repository.createCommit(
theirBranch.name(), theirSignature,
theirSignature, "they made a commit", oid, [initialCommit]);
})
.then(function(commitOid) {
return repository.getCommit(commitOid);
})
.then(function(commit) {
ret.theirCommit = commit;
return NodeGit.Reset.default(
repository, initialCommit, theirFileName);
})
.then(function() {
return Promise.all([
fse.remove(path.join(repoWorkDir, ourFileName)),
fse.remove(path.join(repoWorkDir, theirFileName))
]);
})
.then(function() {
if (checkoutOurs) {
var opts = {
checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE
};
return repository.checkoutBranch(ourBranchName, opts);
}
})
.then(function() {
return ret;
});
}
};
module.exports = RepositorySetup;