forked from toonote/desktop-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.js
More file actions
79 lines (74 loc) · 1.94 KB
/
Copy pathgit.js
File metadata and controls
79 lines (74 loc) · 1.94 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
import path from 'path';
import fs from 'fs';
import logger from './logger';
import util from './util';
export default class Git{
constructor(options){
if(!options) options = {};
// git二进制程序路径
this._git = path.join(require('electron').remote.app.getAppPath(), `lib/${util.os}/git`);
// git仓库根目录
let gitFolder = 'git';
if(DEBUG) gitFolder = 'devgit';
if(TEST) gitFolder = 'testgit';
this._root = options.path || path.join(require('electron').remote.app.getPath('userData'), gitFolder);
// 新建仓库根目录
if(!fs.existsSync(this._root)){
fs.mkdirSync(this._root);
}
}
// 获取git根目录
getPath(){
return this._root;
}
runCommand(command){
let execSync = require('child_process').execSync;
try{
logger.debug('[Git runCommand] ' + command);
return execSync(`${this._git} ${command}`, {
cwd: this._root
}).toString();
}catch(e){
logger.error('[Git runCommand Error]', e);
return false;
}
}
hasInited(){
return fs.existsSync(path.join(this._root, '.git'));
}
init(){
let ret = this.runCommand('init');
ret += ';' + this.runCommand('config user.name "TooNote"');
ret += ';' + this.runCommand('config user.email "toonote@local.git"');
return ret;
}
status(){
return this.runCommand('status');
}
checkout(commitOrPath){
return this.runCommand(`checkout ${commitOrPath}`);
}
log(folderPath){
let log = this.runCommand(`log --pretty=format:"%H %ct" ${folderPath}`);
if(!log){
return [];
}
let logArray = log.trim().split('\n').map((line) => {
let linePart = line.split(' ');
return {
id: linePart[0],
date: new Date(linePart[1] * 1000)
};
}).filter((logItem) => {
return logItem.date.getTime() >= Date.now() - 30 * 24 * 3600 * 1000;
});
return logArray;
}
commit(msg){
this.runCommand('add .');
return this.runCommand(`commit -am "${msg}"`);
}
show(version, filePath){
return this.runCommand(`show ${version}:${filePath}`);
}
}