-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.js
164 lines (145 loc) · 3.97 KB
/
repository.js
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
class Repository {
constructor(apiHandle, name) {
this.handle = apiHandle.repos('loot', name);
this.name = name;
}
getDefaultBranch() {
return this.handle.fetch().then((response) => {
return response.defaultBranch;
});
}
setDefaultBranch(newDefaultBranch) {
return this.handle.update({
name: this.name,
default_branch: newDefaultBranch,
});
}
getBranchHeadHash(branch) {
return this.handle.git.refs(`heads/${branch}`).fetch().then((response) => {
return response.object.sha;
});
}
createNewBranch(sourceBranch, newBranch) {
return this.getBranchHeadHash(sourceBranch).then((sha) => {
return this.handle.git.refs.create({
ref: `refs/heads/${newBranch}`,
sha,
}).catch((error) => {
console.log(`Error creating new branch "${newBranch}":`);
console.log(error);
});
});
}
createBranchFromDefault(newBranch) {
return this.getDefaultBranch().then((defaultBranch) => {
return this.createNewBranch(defaultBranch, newBranch);
}).catch((error) => {
console.log(error);
});
}
getTree(commitHash) {
return this.handle.git.trees(commitHash).fetch();
}
getFileBlobHash(tree, filename) {
const blob = tree.find((element) => {
return element.path === filename;
});
if (!blob) {
throw new Error(`${filename} not found in tree`);
}
return blob.sha;
}
getTextFileBlobContent(blobHash) {
return this.handle.git.blobs(blobHash).fetch().then((response) => {
return (new Buffer(response.content, 'base64')).toString('utf8');
});
}
createTreeWithBlob(parentTreeHash, blobHash, path) {
return this.handle.git.trees.create({
base_tree: parentTreeHash,
tree: [{
path,
mode: '100644',
type: 'blob',
sha: blobHash,
}],
});
}
commitTree(parentCommitHash, treeHash, message) {
return this.handle.git.commits.create({
message: message,
tree: treeHash,
parents: [
parentCommitHash,
],
});
}
updateBranchHead(branch, commitHash) {
return this.handle.git.refs(`heads/${branch}`).update({
sha: commitHash,
});
}
commitFileChange(parentCommitHash, filename, content, message) {
let parentTreeHash;
return this.getTree(parentCommitHash)
.then((response) => {
parentTreeHash = response.sha;
return this.handle.git.blobs.create({content});
})
.then((response) => {
return this.createTreeWithBlob(parentTreeHash, response.sha, filename);
})
.then((response) => {
return this.commitTree(parentCommitHash, response.sha, message);
});
}
getFile(filename) {
let defaultBranch;
let commitHash;
return this.getDefaultBranch()
.then((branch) => {
defaultBranch = branch;
return this.getBranchHeadHash(branch);
})
.then((hash) => {
commitHash = hash;
return this.getTree(hash);
})
.then((response) => {
return this.getFileBlobHash(response.tree, filename);
})
.then((hash) => {
return this.getTextFileBlobContent(hash);
})
.then((content) => {
return {
branch: defaultBranch,
commit: commitHash,
content,
}
});
}
updateFile(filename, commitMessage, editContentFunction, newContent) {
let defaultBranch;
let commitHash;
return this.getFile(filename)
.then((file) => {
defaultBranch = file.branch;
commitHash = file.commit;
return editContentFunction(file.content, newContent);
})
.then((content) => {
return this.commitFileChange(commitHash,
filename,
content,
commitMessage);
})
.then((response) => {
return this.updateBranchHead(defaultBranch, response.sha);
})
.catch((error) => {
console.log(error);
});
}
}
module.exports.Repository = Repository;