-
Notifications
You must be signed in to change notification settings - Fork 30
/
git.js
123 lines (91 loc) · 3.09 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
import fsApi from 'fs';
import path from 'path';
import simpleGit from 'simple-git';
process.env.LC_ALL = 'en_GB'; // Ensure git messages will be in English as some errors are handled by analysing the message content
const fs = fsApi.promises;
export default class Git {
constructor({ path: repositoryPath, author }) {
this.path = repositoryPath;
this.author = author;
}
async initialize() {
if (!fsApi.existsSync(this.path)) {
await fs.mkdir(this.path, { recursive: true });
}
this.git = simpleGit(this.path, { maxConcurrentProcesses: 1 });
await this.git.init();
return this.git
.addConfig('core.autocrlf', false)
.addConfig('push.default', 'current')
.addConfig('user.name', this.author.name)
.addConfig('user.email', this.author.email)
.addConfig('core.quotePath', false); // disable Git's encoding of special characters in pathnames. For example, `service·A` will be encoded as `service\302\267A` without this setting, leading to issues. See https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
}
add(filePath) {
return this.git.add(this.relativePath(filePath));
}
async commit({ filePath, message, date = new Date() }) {
const commitDate = new Date(date).toISOString();
let summary;
try {
process.env.GIT_AUTHOR_DATE = commitDate;
process.env.GIT_COMMITTER_DATE = commitDate;
summary = await this.git.commit(message, filePath);
} finally {
process.env.GIT_AUTHOR_DATE = '';
process.env.GIT_COMMITTER_DATE = '';
}
if (!summary.commit) { // Nothing committed, no hash to return
return;
}
return summary.commit;
}
pushChanges() {
return this.git.push();
}
listCommits(options = []) {
return this.log([ '--reverse', '--no-merges', '--name-only', ...options ]);
}
async getCommit(options) {
const [commit] = await this.listCommits([ '-1', ...options ]);
return commit;
}
async log(options = []) {
try {
const logSummary = await this.git.log(options);
return logSummary.all;
} catch (error) {
if (/unknown revision or path not in the working tree|does not have any commits yet/.test(error.message)) {
return [];
}
throw error;
}
}
async isTracked(filePath) {
const result = await this.git.raw('ls-files', this.relativePath(filePath));
return Boolean(result);
}
checkout(options) {
return this.git.checkout(options);
}
show(options) {
return this.git.show(options);
}
async cleanUp() {
await this.git.reset('hard');
return this.git.clean('f', '-d');
}
async getFullHash(shortHash) {
return (await this.git.show([ shortHash, '--pretty=%H', '-s' ])).trim();
}
restore(path, commit) {
return this.git.raw([ 'restore', '-s', commit, '--', path ]);
}
async destroyHistory() {
await fs.rm(this.path, { recursive: true });
return this.initialize();
}
relativePath(absolutePath) {
return path.relative(this.path, absolutePath); // Git needs a path relative to the .git directory, not an absolute one
}
}