-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
120 lines (110 loc) · 2.99 KB
/
git.js
File metadata and controls
120 lines (110 loc) · 2.99 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
const { exec, execCommand } = require('./util');
// @BUG 新的没有改动可提交的时候会报错?
const GIT_TYPE_MAP = {
'??': 'add',
D: 'delete',
M: 'update',
};
const parseStatus = (data) => {
const result = [];
data.split('\n').forEach((changedItem) => {
const [type, path] = changedItem.trim().split(' ');
if (!type || !path) return;
result.push({
rawType: type,
type: GIT_TYPE_MAP[type],
path,
});
});
return result;
};
const LOG_SEPERATOR = '<seperator>';
const parseLog = (data) => {
const result = [];
data.split('\n').forEach((commit) => {
const [version, author, date, message] = commit.split(LOG_SEPERATOR);
result.push({
version,
author,
date: date.slice(0, 19), // 2019-03-31 13:36:28 +0800 => 2019-03-31 13:36:28
msg: message,
});
});
return result;
};
class Git {
static async status(codePath) {
// -s means short: show git status message in short-format
const command = 'git status -s';
let statusData = [];
await exec(command, codePath)
.then((data) => {
statusData = parseStatus(data);
})
.catch(() => {
statusData = false;
});
return statusData;
}
// @TODO 支持查询位置
static async log(codePath) {
const countLimit = 10;
const command = `git log -n ${countLimit} --pretty=format:'%h${LOG_SEPERATOR}%an${LOG_SEPERATOR}%ai${LOG_SEPERATOR}%s'`;
let logData = [];
await exec(command, codePath)
.then((data) => {
logData = parseLog(data);
})
.catch((error) => {
if (!error.message.includes('does not have any commits yet')) console.log(error);
return [];
});
return logData;
}
static async update(codePath) {
const command = 'git pull --rebase';
return await execCommand(command, codePath);
}
static async addAll(codePath) {
const command = 'git add --all';
return await execCommand(command, codePath);
}
static async push(codePath) {
const command = 'git push';
return await execCommand(command, codePath);
}
static async commit(codePath, commitMsg) {
const command = `git commit -m '${commitMsg}'`;
await this.addAll(codePath);
const result = { error: false, msg: '' };
await exec(command, codePath)
.then((data) => {
result.msg = data;
})
.catch((error) => {
// @BUG 新的没有改动可提交的时候会报错?
result.error = true;
result.msg = error;
});
return result;
}
static async revert(codePath, version) {
const result = { error: false, msg: '' };
const command = `git reset ${version} --hard`;
if (!version) {
result.error = true;
result.msg = 'version is required!';
return result;
}
await exec(command, codePath)
.then((data) => {
result.msg = data;
})
.catch((error) => {
result.error = true;
result.msg = error;
});
return result;
}
}
module.exports = Git;