-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGit.js
140 lines (113 loc) · 3.13 KB
/
Git.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
'use strict';
require('shelljs/global');
const TEMP_FOLDER = require('os').tmpdir() ;
class Git {
constructor() {
this.messageHashes = {};
this.lock = false;
}
cloneRepo(repository) {
return this.runCommand('git clone --depth=1 ' + repository + ' ' + this.getRepoFolderName());
}
getCurrentBranch() {
return this.runGitCommand('rev-parse --abbrev-ref HEAD')
.then(branch => {
return branch.trim();
});
}
getBranches() {
return this.runGitCommand('ls-remote --heads origin')
.then(branchesStr => {
return branchesStr
.split('\n')
.filter(branch => {
return branch.length > 0;
})
.map(branch => {
return branch.match(/refs\/heads\/(.+)/)[1];
})
});
}
getCommits() {
if(this.lock) {
return Promise.resolve([]);
}
return this.runGitCommand('pull')
.then(this.runGitCommand.bind(this, 'log --pretty=format:"%h;%cn;%s"'))
.then(commits => {
return commits.split('\n').map(commit => {
let parts = commit.split(';');
return {
hash: parts[0],
sender: parts[1],
message: parts[2]
}
})
})
.then(commits => {
return commits.filter(commit => {
return !(commit.hash in this.messageHashes);
});
})
.then(commits => {
commits.forEach(commit => {
this.messageHashes[commit.hash] = true;
});
return commits;
});
}
sendMessage(message) {
if(this.lock) {
return new Promise((resolve) => {
setTimeout(() => {
this.sendMessage(message)
.then(resolve)
}, 500)
})
}
this.lock = true;
return this.getCurrentBranch()
.then(branch => {
return this.runGitCommand('commit -am "' + message.replace(/"/g, "\\\"") + '" --allow-empty')
.then(this.runGitCommand.bind(this,'push -u origin ' + branch))
.then(() => {
this.lock = false;
});
})
}
switchBranch(branchName) {
return this.runGitCommand('checkout ' + branchName)
.catch(() => {
return this.runGitCommand('fetch origin ' + branchName + ':' + branchName)
.then(this.switchBranch.bind(this, branchName));
})
}
getRepoFolderName() {
return this.folderName = this.folderName || 'gic_' + new Date().getTime();
}
runCommand(command) {
cd(TEMP_FOLDER);
return new Promise((resolve, reject) => {
exec(command, {silent: true}, function(code, stdout, stderr) {
if(code === 0) {
resolve(stdout);
} else {
reject(stderr);
}
});
});
}
runGitCommand(command) {
cd(TEMP_FOLDER + '/' + this.getRepoFolderName());
return new Promise((resolve, reject) => {
exec('git ' + command, {silent: true}, function(code, stdout, stderr) {
if(code === 0) {
resolve(stdout);
} else {
reject(stderr);
}
});
});
}
}
module.exports = Git;