- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
Latest commit
120 lines (110 loc) · 2.99 KB
/
Copy pathgit.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 新的没有改动可提交的时候会报错?
constGIT_TYPE_MAP={
'??': 'add',
D: 'delete',
M: 'update',
};
constparseStatus=(data)=>{
constresult=[];
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,
});
});
returnresult;
};
constLOG_SEPERATOR='<seperator>';
constparseLog=(data)=>{
constresult=[];
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,
});
});
returnresult;
};
classGit{
staticasyncstatus(codePath){
// -s means short: show git status message in short-format
constcommand='git status -s';
letstatusData=[];
awaitexec(command,codePath)
.then((data)=>{
statusData=parseStatus(data);
})
.catch(()=>{
statusData=false;
});
returnstatusData;
}
// @TODO 支持查询位置
staticasynclog(codePath){
constcountLimit=10;
constcommand=`git log -n ${countLimit} --pretty=format:'%h${LOG_SEPERATOR}%an${LOG_SEPERATOR}%ai${LOG_SEPERATOR}%s'`;
letlogData=[];
awaitexec(command,codePath)
.then((data)=>{
logData=parseLog(data);
})
.catch((error)=>{
if(!error.message.includes('does not have any commits yet'))console.log(error);
return[];
});
returnlogData;
}
staticasyncupdate(codePath){
constcommand='git pull --rebase';
returnawaitexecCommand(command,codePath);
}
staticasyncaddAll(codePath){
constcommand='git add --all';
returnawaitexecCommand(command,codePath);
}
staticasyncpush(codePath){
constcommand='git push';
returnawaitexecCommand(command,codePath);
}
staticasynccommit(codePath,commitMsg){
constcommand=`git commit -m '${commitMsg}'`;
awaitthis.addAll(codePath);
constresult={error: false,msg: ''};
awaitexec(command,codePath)
.then((data)=>{
result.msg=data;
})
.catch((error)=>{
// @BUG 新的没有改动可提交的时候会报错?
result.error=true;
result.msg=error;
});
returnresult;
}
staticasyncrevert(codePath,version){
constresult={error: false,msg: ''};
constcommand=`git reset ${version} --hard`;
if(!version){
result.error=true;
result.msg='version is required!';
returnresult;
}
awaitexec(command,codePath)
.then((data)=>{
result.msg=data;
})
.catch((error)=>{
result.error=true;
result.msg=error;
});
returnresult;
}
}
module.exports=Git;