-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgit.js
199 lines (157 loc) · 4.36 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { join, normalize } from 'path'
import { spawner } from './spawner.js'
import { toArray } from './utils.js'
const ADDED = 'A'.charCodeAt(0)
const COPIED = 'C'.charCodeAt(0)
const DELETED = 'D'.charCodeAt(0)
const MODIFIED = 'M'.charCodeAt(0)
const RENAMED = 'R'.charCodeAt(0)
const SPACE = ' '.charCodeAt(0)
export const STAGED_CODE = 1 << 0
export const CHANGED_CODE = 1 << 1
export const DELETED_CODE = 1 << 2
const APPLY_ARGS = ['-v', '--whitespace=nowarn', '--recount', '--unidiff-zero']
const DIFF_ARGS = [
'--binary',
'--unified=0',
'--no-color',
'--no-ext-diff',
'--src-prefix=a/',
'--dst-prefix=b/',
'--patch',
'--submodule=short',
]
export function gitWorker(cwd = process.cwd()) {
let git = {
async exec(args = [], opts = {}) {
try {
return await spawner('git', args, {
...opts,
cwd: opts.cwd || cwd,
})
} catch (err) {
throw err
}
},
async diff(fileName, files = [], opts = {}) {
const args = ['diff', ...DIFF_ARGS, '--output', fileName]
if (files.length) {
args.push('--')
args.push(...files)
}
await git.exec(args, opts)
},
async apply(patch, allowConflicts = false, opts = {}) {
const args = ['apply', ...APPLY_ARGS]
if (allowConflicts) {
args.push('-3')
}
if (patch) {
args.push(patch)
}
await git.exec(args, opts)
},
async getRepoAndDotGitPaths(opts = {}) {
try {
let result = await git.exec(['rev-parse', '--show-toplevel'], opts)
let repositoriyPath = result ? normalize(result.trimLeft().replace(/[\r\n]+$/, '')) : ''
return {
repoPath: repositoriyPath || null,
dotGitPath: repositoriyPath ? join(repositoriyPath, '.git') : null,
}
} catch (error) {
return {
repoPath: null,
dotGitPath: null,
}
}
},
async add(paths, opts = {}) {
paths = toArray(paths)
if (paths.length) {
const args = ['add', '-A', '--', ...paths]
await git.exec(args, opts)
}
},
async checkout(paths, opts = {}) {
paths = toArray(paths)
if (paths.length) {
const args = ['checkout', '-q', '--force', '--', ...paths]
await git.exec(args, opts)
}
},
async status(opts = {}) {
const env = { GIT_OPTIONAL_LOCKS: '0' }
const args = ['status', '-z', '-u']
const result = []
try {
let i = 0
let lastIndex
let raw = await git.exec(args, { env, ...opts })
while (i < raw.length) {
if (i + 4 >= raw.length) {
return []
}
let entry = {
x: raw.charCodeAt(i++),
y: raw.charCodeAt(i++),
path: '',
rename: undefined,
}
i++
if (entry.x === RENAMED || entry.x === COPIED) {
lastIndex = raw.indexOf('\0', i)
if (!~lastIndex) {
return []
}
entry.rename = raw.substring(i, lastIndex)
i = lastIndex + 1
}
lastIndex = raw.indexOf('\0', i)
if (!~lastIndex) {
return []
}
entry.path = raw.substring(i, lastIndex)
if (entry.path[entry.path.length - 1] !== '/') {
result.push(entry)
}
i = lastIndex + 1
}
return result
} catch (err) {
return []
}
},
async stagedFiles(opts = {}) {
let entries = await git.status(opts)
let result = []
for (let entry of entries) {
let { x, y } = entry
if (x === ADDED || x === MODIFIED || x === RENAMED || x === COPIED) {
if (y === ADDED || y === COPIED || y === MODIFIED || y === RENAMED) {
entry.type = CHANGED_CODE
} else if (y === DELETED) {
entry.type = DELETED_CODE
} else {
entry.type = STAGED_CODE
}
result.push(entry)
}
}
return result
},
async unstagedFiles(opts = {}) {
let entries = await git.status(opts)
let result = []
for (let entry of entries) {
let { y } = entry
if (y !== SPACE && y !== DELETED) {
entry.type = CHANGED_CODE
result.push(entry)
}
}
return result
},
}
return git
}