Реверснутый zcode с своими фишками и плагинами · VibeGit
Реверснутый zcode с своими фишками и плагинами Реверснутая ade zcode без z ai, с массовой загрузкой ключей, умным апи роутингом, плагином для командной разработки
reversnutyy-zcode-s-svoimi-fishkami-i-plaginami / fork / team / lib / git.mjs 413 строк · изменён 13 нояб., 21:19
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { execFile } from 'node:child_process' ;
import fs from 'node:fs' ;
import path from 'node:path' ;
import { HttpError } from './http.mjs' ;
const MAX_BUFFER = 8 * 1024 * 1024 ;
const PATCH_LIMIT = 400 * 1024 ;
export function runGit ( cwd, args, { timeout = 30_000 , allowExit = [ ]
args
cwd
timeout
process
err
code err err
code allowExit code stdout stderr code
msg stderr err
e err msg stderr
e code
e
stdout stderr
cwd dir dir fallback
cwd
abs path cwd
st
st fs abs
st
abs
files files
f files
f f
f
f
files
branch branch branch
cwd branch
branch
stdout cwd
stdout
stdout cwd
path stdout
res
tokens out
i i tokens i
line tokens i
line
line
key rest line
val rest
key res val val
key
val res
res val
key res val
key
m val
m res m res m
type line
type res line
type
type type
parts line
xy parts
p parts type
orig
type orig tokens i
x xy
y xy
x res p x x x orig orig
y res p y y y orig orig
type
parts line
res parts
res
cwd
cwd
stdout cwd
s stdout
remoteUrl cwd
counts
s
s
s
s
counts counts counts counts counts
cwd s s s s
s s remoteUrl
s s s s counts
stdout cwd
stdout
stdout cwd
first stdout
first
r cwd first
r
stdout cwd
stdout
n limit
s skip
args
file
file
args file
stdout
stdout cwd args
err
err
err
stdout r
hash short parents author email date subject refs rec
hash short
parents parents
author email date subject
refs refs x
hash hash
hash
hash
meta cwd hash
full author email date parents subject bodyParts meta
numstat cwd hash
nameStatus cwd hash
statuses
t nameStatus x
i i t
code t i
letter code
letter letter
t i
to t i
statuses to letter letter
statuses t i letter letter
files
t numstat
i i t i
rec t i
rec
a d p rec
filePath p
p p
i
filePath t i
st statuses filePath
files
filePath
st
st st
a a
d d
a
patchRes cwd hash
patch patchRes
truncated
patch
patch patch
truncated
full author email date
subject bodyParts
parents parents
files patch truncated
file file
file staged
ls cwd file
ls
r cwd file
r
args
staged args
file args file
stdout cwd args
out stdout
truncated
out
out out
truncated
out truncated truncated
cwd files
cwd
files
cwd files
err
err
cwd files
err
files
root cwd
base path cwd
ls cwd files
untrackedRel ls path cwd p
tracked
removed
f files
abs path cwd f
inside abs path base path abs base
inside abs path root path
untrackedRel abs
fs abs
removed f
tracked f
tracked cwd tracked
tracked removed
message message
amend
cached cwd
cached amend
args
amend args
message message args
out
child args
cwd
process
err stderr stdout err
stdout
child message message message
hash cwd
hash out
s cwd
s
args
s setUpstream
s
cwd s
args s
r cwd args
r r
r cwd
r r
r cwd
r r
cwd
fmt
stdout cwd
stdout
name head upstream track hash subject date line
ahead track
behind track
name head upstream ahead behind
track
hash hash subject date
cwd branch
args create branch branch
cwd args
branch
}
=
{
}
)
{
return
new
Promise
(
(
resolve, reject
)
=>
{
execFile
(
'git'
,
,
{
,
,
windowsHide
:
true
,
maxBuffer
:
MAX_BUFFER
,
encoding
:
'utf8'
,
env
:
{
...
.
env
,
GIT_TERMINAL_PROMPT
:
'0'
,
GIT_OPTIONAL_LOCKS
:
'0'
,
LC_ALL
:
'C'
}
,
}
,
(
err, stdout, stderr
)
=>
{
if
(
)
{
const
=
typeof
.
code
===
'number'
?
.
code
:
null
;
if
(
!==
null
&&
.
includes
(
)
)
return
resolve
(
{
,
,
}
)
;
const
=
(
||
.
message
||
''
)
.
trim
(
)
.
split
(
'\n'
)
.
slice
(
-
6
)
.
join
(
'\n'
)
;
const
=
new
HttpError
(
.
code
===
'ENOENT'
?
500
:
400
,
||
` git ${ args[ 0 ] } failed `
,
{
stderr
:
?.
slice
(
0
,
4000
)
??
''
}
)
;
.
exitCode
=
;
return
reject
(
)
;
}
resolve
(
{
,
,
code
:
0
}
)
;
}
)
;
}
)
;
}
export
function
resolveDir
(
dir, fallback
)
{
const
=
(
typeof
===
'string'
&&
.
trim
(
)
)
||
;
if
(
!
)
throw
new
HttpError
(
400
,
'no workspace selected (set workspace or pass dir)'
)
;
const
=
.
resolve
(
)
;
let
;
try
{
=
.
statSync
(
)
;
}
catch
{
throw
new
HttpError
(
400
,
` directory not found: ${ abs} `
)
;
}
if
(
!
.
isDirectory
(
)
)
throw
new
HttpError
(
400
,
` not a directory: ${ abs} `
)
;
return
;
}
function
checkFiles
(
files
)
{
if
(
!
Array
.
isArray
(
)
||
.
length
===
0
)
throw
new
HttpError
(
400
,
'files must be a non-empty array'
)
;
for
(
const
of
)
{
if
(
typeof
!==
'string'
||
!
.
trim
(
)
)
throw
new
HttpError
(
400
,
'file path must be a non-empty string'
)
;
if
(
.
startsWith
(
'-'
)
)
throw
new
HttpError
(
400
,
` invalid file argument: ${ f} `
)
;
if
(
.
includes
(
'\0'
)
)
throw
new
HttpError
(
400
,
'invalid file argument'
)
;
}
return
;
}
async
function
checkBranch
(
cwd, branch
)
{
if
(
typeof
!==
'string'
||
!
.
trim
(
)
||
.
startsWith
(
'-'
)
)
throw
new
HttpError
(
400
,
'invalid branch name'
)
;
try
{
await
runGit
(
,
[
'check-ref-format'
,
'--branch'
,
]
,
{
timeout
:
10_000
}
)
;
}
catch
{
throw
new
HttpError
(
400
,
` invalid branch name: ${ branch} `
)
;
}
return
;
}
async
function
isRepo
(
cwd
)
{
try
{
const
{
}
=
await
runGit
(
,
[
'rev-parse'
,
'--is-inside-work-tree'
]
,
{
timeout
:
10_000
}
)
;
return
.
trim
(
)
===
'true'
;
}
catch
{
return
false
;
}
}
async
function
topLevel
(
cwd
)
{
const
{
}
=
await
runGit
(
,
[
'rev-parse'
,
'--show-toplevel'
]
,
{
timeout
:
10_000
}
)
;
return
.
resolve
(
.
trim
(
)
)
;
}
const
STATUS_NAMES
=
{
M
:
'modified'
,
A
:
'added'
,
D
:
'deleted'
,
R
:
'renamed'
,
C
:
'copied'
,
T
:
'typechange'
,
U
:
'unmerged'
}
;
export
function
parseStatusV2
(
out
)
{
const
=
{
branch
:
null
,
detached
:
false
,
oid
:
null
,
upstream
:
null
,
ahead
:
0
,
behind
:
0
,
staged
:
[
]
,
unstaged
:
[
]
,
untracked
:
[
]
,
conflicts
:
[
]
,
}
;
const
=
.
split
(
'\0'
)
;
for
(
let
=
0
;
<
.
length
;
++
)
{
const
=
[
]
;
if
(
!
)
continue
;
if
(
.
startsWith
(
'# '
)
)
{
const
[
,
,
...
]
=
.
split
(
' '
)
;
const
=
.
join
(
' '
)
;
if
(
===
'branch.oid'
)
.
oid
=
===
'(initial)'
?
null
:
;
else
if
(
===
'branch.head'
)
{
if
(
===
'(detached)'
)
.
detached
=
true
;
else
.
branch
=
;
}
else
if
(
===
'branch.upstream'
)
.
upstream
=
;
else
if
(
===
'branch.ab'
)
{
const
=
/ \+ ( \d + ) -( \d + ) /
.
exec
(
)
;
if
(
)
{
.
ahead
=
Number
(
[
1
]
)
;
.
behind
=
Number
(
[
2
]
)
;
}
}
continue
;
}
const
=
[
0
]
;
if
(
===
'?'
)
{
.
untracked
.
push
(
.
slice
(
2
)
)
;
continue
;
}
if
(
===
'!'
)
continue
;
if
(
===
'1'
||
===
'2'
)
{
const
=
.
split
(
' '
)
;
const
=
[
1
]
;
const
=
.
slice
(
===
'1'
?
8
:
9
)
.
join
(
' '
)
;
let
;
if
(
===
'2'
)
=
[
++
]
;
const
=
[
0
]
;
const
=
[
1
]
;
if
(
!==
'.'
)
.
staged
.
push
(
{
path
:
,
status
:
STATUS_NAMES
[
]
??
,
code
:
,
...
(
?
{
}
:
{
}
)
}
)
;
if
(
!==
'.'
)
.
unstaged
.
push
(
{
path
:
,
status
:
STATUS_NAMES
[
]
??
,
code
:
,
...
(
?
{
}
:
{
}
)
}
)
;
continue
;
}
if
(
===
'u'
)
{
const
=
.
split
(
' '
)
;
.
conflicts
.
push
(
.
slice
(
10
)
.
join
(
' '
)
)
;
}
}
return
;
}
export
async
function
status
(
cwd
)
{
if
(
!
(
await
isRepo
(
)
)
)
{
return
{
,
isRepo
:
false
,
branch
:
null
,
detached
:
false
,
upstream
:
null
,
ahead
:
0
,
behind
:
0
,
remoteUrl
:
null
,
staged
:
[
]
,
unstaged
:
[
]
,
untracked
:
[
]
,
conflicts
:
[
]
,
counts
:
{
staged
:
0
,
unstaged
:
0
,
untracked
:
0
,
conflicts
:
0
,
total
:
0
}
}
;
}
const
{
}
=
await
runGit
(
,
[
'status'
,
'--porcelain=v2'
,
'--branch'
,
'-z'
,
'--untracked-files=all'
]
)
;
const
=
parseStatusV2
(
)
;
const
=
await
getRemoteUrl
(
)
;
const
=
{
staged
:
.
staged
.
length
,
unstaged
:
.
unstaged
.
length
,
untracked
:
.
untracked
.
length
,
conflicts
:
.
conflicts
.
length
,
}
;
.
total
=
.
staged
+
.
unstaged
+
.
untracked
+
.
conflicts
;
return
{
,
isRepo
:
true
,
branch
:
.
branch
,
detached
:
.
detached
,
head
:
.
oid
,
upstream
:
.
upstream
,
ahead
:
.
ahead
,
behind
:
.
behind
,
,
staged
:
.
staged
,
unstaged
:
.
unstaged
,
untracked
:
.
untracked
,
conflicts
:
.
conflicts
,
,
}
;
}
export
async
function
getRemoteUrl
(
cwd
)
{
try
{
const
{
}
=
await
runGit
(
,
[
'remote'
,
'get-url'
,
'origin'
]
,
{
timeout
:
10_000
}
)
;
return
.
trim
(
)
||
null
;
}
catch
{
try
{
const
{
}
=
await
runGit
(
,
[
'remote'
]
,
{
timeout
:
10_000
}
)
;
const
=
.
split
(
/ \r ? \n /
)
.
find
(
Boolean
)
;
if
(
!
)
return
null
;
const
=
await
runGit
(
,
[
'remote'
,
'get-url'
,
]
,
{
timeout
:
10_000
}
)
;
return
.
stdout
.
trim
(
)
||
null
;
}
catch
{
return
null
;
}
}
}
export
async
function
currentBranch
(
cwd
)
{
try
{
const
{
}
=
await
runGit
(
,
[
'symbolic-ref'
,
'--short'
,
'-q'
,
'HEAD'
]
,
{
timeout
:
10_000
}
)
;
return
.
trim
(
)
||
null
;
}
catch
{
return
null
;
}
}
const
FS
=
'\x1f'
;
const
RS
=
'\x1e'
;
export
async
function
log
(
cwd, { limit = 50 , skip = 0 , file } = { }
)
{
const
=
Math
.
min
(
Math
.
max
(
Number
(
)
||
50
,
1
)
,
500
)
;
const
=
Math
.
max
(
Number
(
)
||
0
,
0
)
;
const
=
[
'log'
,
` --max-count= ${ n} `
,
` --skip= ${ s} `
,
'--decorate=short'
,
'--date=iso-strict'
,
` --format=%H ${ FS } %h ${ FS } %P ${ FS } %an ${ FS } %ae ${ FS } %aI ${ FS } %s ${ FS } %D ${ RS } `
]
;
if
(
)
{
checkFiles
(
[
]
)
;
.
push
(
'--'
,
)
;
}
let
;
try
{
(
{
}
=
await
runGit
(
,
)
)
;
}
catch
(
)
{
if
(
/ does not have any commits| unknown revision| bad default revision / i
.
test
(
.
message
)
)
return
[
]
;
throw
;
}
return
.
split
(
RS
)
.
map
(
(
r
)
=>
.
replace
(
/ ^ \s + /
,
''
)
)
.
filter
(
Boolean
)
.
map
(
(
rec
)
=>
{
const
[
,
,
,
,
,
,
,
]
=
.
split
(
FS
)
;
return
{
,
,
parents
:
?
.
split
(
' '
)
.
filter
(
Boolean
)
:
[
]
,
,
,
,
,
refs
:
?
.
split
(
','
)
.
map
(
(
x
)
=>
.
trim
(
)
)
.
filter
(
Boolean
)
:
[
]
,
}
;
}
)
;
}
function
checkHash
(
hash
)
{
if
(
typeof
!==
'string'
||
!
/ ^ [ 0- 9 a- f A- F ] {4,64} $ | ^ HEAD( ~\d + | \^ \d * ) * $ /
.
test
(
)
)
throw
new
HttpError
(
400
,
'invalid commit hash'
)
;
return
;
}
export
async
function
show
(
cwd, hash
)
{
checkHash
(
)
;
const
=
await
runGit
(
,
[
'show'
,
'-s'
,
` --format=%H ${ FS } %an ${ FS } %ae ${ FS } %aI ${ FS } %P ${ FS } %s ${ FS } %b `
,
,
'--'
]
)
;
const
[
,
,
,
,
,
,
...
]
=
.
stdout
.
split
(
FS
)
;
const
=
await
runGit
(
,
[
'show'
,
'--format='
,
'--numstat'
,
'-z'
,
'-M'
,
,
'--'
]
)
;
const
=
await
runGit
(
,
[
'show'
,
'--format='
,
'--name-status'
,
'-z'
,
'-M'
,
,
'--'
]
)
;
const
=
new
Map
(
)
;
{
const
=
.
stdout
.
split
(
'\0'
)
.
filter
(
(
x
)
=>
!==
''
)
;
for
(
let
=
0
;
<
.
length
;
)
{
const
=
[
++
]
;
const
=
[
0
]
;
if
(
===
'R'
||
===
'C'
)
{
const
from
=
[
++
]
;
const
=
[
++
]
;
.
set
(
,
{
status
:
STATUS_NAMES
[
]
??
,
orig
:
from
}
)
;
}
else
{
.
set
(
[
++
]
,
{
status
:
STATUS_NAMES
[
]
??
}
)
;
}
}
}
const
=
[
]
;
{
const
=
.
stdout
.
split
(
'\0'
)
;
for
(
let
=
0
;
<
.
length
;
++
)
{
const
=
[
]
;
if
(
!
)
continue
;
const
[
,
,
]
=
.
split
(
'\t'
)
;
let
=
;
if
(
===
''
||
===
undefined
)
{
+=
2
;
=
[
]
;
}
const
=
.
get
(
)
??
{
status
:
'modified'
}
;
.
push
(
{
path
:
,
status
:
.
status
,
...
(
.
orig
?
{
orig
:
.
orig
}
:
{
}
)
,
additions
:
===
'-'
?
null
:
Number
(
)
,
deletions
:
===
'-'
?
null
:
Number
(
)
,
binary
:
===
'-'
,
}
)
;
}
}
const
=
await
runGit
(
,
[
'show'
,
'--format='
,
'--patch'
,
'--no-color'
,
'-M'
,
,
'--'
]
)
;
let
=
.
stdout
;
let
=
false
;
if
(
Buffer
.
byteLength
(
)
>
PATCH_LIMIT
)
{
=
Buffer
.
from
(
)
.
subarray
(
0
,
PATCH_LIMIT
)
.
toString
(
'utf8'
)
;
=
true
;
}
return
{
hash
:
,
,
,
,
,
body
:
.
join
(
FS
)
.
replace
(
/ \s + $ /
,
''
)
,
parents
:
?
.
split
(
' '
)
.
filter
(
Boolean
)
:
[
]
,
,
,
,
}
;
}
export
async
function
diff
(
cwd, { file, staged } = { }
)
{
if
(
)
checkFiles
(
[
]
)
;
if
(
&&
!
)
{
const
=
await
runGit
(
,
[
'ls-files'
,
'--others'
,
'--exclude-standard'
,
'--'
,
]
)
;
if
(
.
stdout
.
trim
(
)
)
{
const
=
await
runGit
(
,
[
'diff'
,
'--no-color'
,
'--no-index'
,
'--'
,
'/dev/null'
,
]
,
{
allowExit
:
[
1
]
}
)
;
return
{
diff
:
.
stdout
,
untracked
:
true
}
;
}
}
const
=
[
'diff'
,
'--no-color'
,
'-M'
]
;
if
(
)
.
push
(
'--cached'
)
;
if
(
)
.
push
(
'--'
,
)
;
const
{
}
=
await
runGit
(
,
)
;
let
=
;
let
=
false
;
if
(
Buffer
.
byteLength
(
)
>
PATCH_LIMIT
)
{
=
Buffer
.
from
(
)
.
subarray
(
0
,
PATCH_LIMIT
)
.
toString
(
'utf8'
)
;
=
true
;
}
return
{
diff
:
,
...
(
?
{
}
:
{
}
)
}
;
}
export
async
function
stage
(
cwd, files
)
{
await
runGit
(
,
[
'add'
,
'--'
,
...
checkFiles
(
)
]
)
;
return
{
ok
:
true
}
;
}
export
async
function
stageAll
(
cwd
)
{
await
runGit
(
,
[
'add'
,
'-A'
]
)
;
return
{
ok
:
true
}
;
}
export
async
function
unstage
(
cwd, files
)
{
checkFiles
(
)
;
try
{
await
runGit
(
,
[
'restore'
,
'--staged'
,
'--'
,
...
]
)
;
}
catch
(
)
{
if
(
/ could not resolve HEAD| HEAD. * invalid| bad revision / i
.
test
(
.
message
)
)
{
await
runGit
(
,
[
'rm'
,
'--cached'
,
'-q'
,
'--'
,
...
]
)
;
}
else
{
throw
;
}
}
return
{
ok
:
true
}
;
}
export
async
function
discard
(
cwd, files
)
{
checkFiles
(
)
;
const
=
await
topLevel
(
)
;
const
=
.
resolve
(
)
;
const
=
await
runGit
(
,
[
'ls-files'
,
'--others'
,
'--exclude-standard'
,
'-z'
,
'--'
,
...
]
)
;
const
=
new
Set
(
.
stdout
.
split
(
'\0'
)
.
filter
(
Boolean
)
.
map
(
(
p
)
=>
.
resolve
(
,
)
)
)
;
const
=
[
]
;
const
=
[
]
;
for
(
const
of
)
{
const
=
.
resolve
(
,
)
;
const
=
(
+
.
sep
)
.
toLowerCase
(
)
.
startsWith
(
.
toLowerCase
(
)
+
.
sep
)
&&
.
toLowerCase
(
)
!==
.
toLowerCase
(
)
;
if
(
!
||
!
(
.
toLowerCase
(
)
+
.
sep
)
.
startsWith
(
.
toLowerCase
(
)
+
.
sep
)
)
{
throw
new
HttpError
(
400
,
` path outside working directory: ${ f} `
)
;
}
if
(
.
has
(
)
)
{
.
rmSync
(
,
{
force
:
true
,
recursive
:
false
}
)
;
.
push
(
)
;
}
else
{
.
push
(
)
;
}
}
if
(
.
length
)
await
runGit
(
,
[
'restore'
,
'--'
,
...
]
)
;
return
{
ok
:
true
,
restored
:
,
}
;
}
export
async
function
commit
(
cwd, { message, amend = false }
)
{
if
(
typeof
!==
'string'
||
!
.
trim
(
)
)
{
if
(
!
)
throw
new
HttpError
(
400
,
'commit message is required'
)
;
}
const
=
await
runGit
(
,
[
'diff'
,
'--cached'
,
'--quiet'
]
,
{
allowExit
:
[
1
]
}
)
;
if
(
.
code
===
0
&&
!
)
throw
new
HttpError
(
400
,
'Nothing staged'
)
;
const
=
[
'commit'
,
'--no-edit'
]
;
if
(
)
.
push
(
'--amend'
)
;
if
(
&&
.
trim
(
)
)
.
push
(
'-F'
,
'-'
)
;
const
=
await
new
Promise
(
(
resolve, reject
)
=>
{
const
=
execFile
(
'git'
,
,
{
,
timeout
:
60_000
,
windowsHide
:
true
,
maxBuffer
:
MAX_BUFFER
,
encoding
:
'utf8'
,
env
:
{
...
.
env
,
GIT_TERMINAL_PROMPT
:
'0'
}
,
}
,
(
err, stdout, stderr
)
=>
{
if
(
)
return
reject
(
new
HttpError
(
400
,
(
||
||
.
message
)
.
trim
(
)
.
slice
(
-
2000
)
)
)
;
resolve
(
)
;
}
)
;
.
stdin
.
end
(
&&
.
trim
(
)
?
:
''
)
;
}
)
;
const
{
stdout
:
}
=
await
runGit
(
,
[
'rev-parse'
,
'HEAD'
]
)
;
return
{
hash
:
.
trim
(
)
,
summary
:
.
trim
(
)
.
split
(
/ \r ? \n /
)
[
0
]
??
''
}
;
}
export
async
function
push
(
cwd, { setUpstream = false } = { }
)
{
const
=
await
status
(
)
;
if
(
!
.
isRepo
)
throw
new
HttpError
(
400
,
'not a git repository'
)
;
let
=
[
'push'
]
;
if
(
!
.
upstream
||
)
{
if
(
!
.
branch
)
throw
new
HttpError
(
400
,
'cannot push a detached HEAD'
)
;
await
checkBranch
(
,
.
branch
)
;
=
[
'push'
,
'-u'
,
'origin'
,
.
branch
]
;
}
const
=
await
runGit
(
,
,
{
timeout
:
120_000
}
)
;
return
{
ok
:
true
,
output
:
(
.
stdout
+
.
stderr
)
.
trim
(
)
}
;
}
export
async
function
pull
(
cwd
)
{
const
=
await
runGit
(
,
[
'pull'
,
'--ff-only'
]
,
{
timeout
:
120_000
}
)
;
return
{
ok
:
true
,
output
:
(
.
stdout
+
.
stderr
)
.
trim
(
)
}
;
}
export
async
function
fetchAll
(
cwd
)
{
const
=
await
runGit
(
,
[
'fetch'
,
'--prune'
]
,
{
timeout
:
120_000
}
)
;
return
{
ok
:
true
,
output
:
(
.
stdout
+
.
stderr
)
.
trim
(
)
}
;
}
export
async
function
branches
(
cwd
)
{
if
(
!
(
await
isRepo
(
)
)
)
return
[
]
;
const
=
` %(refname:short) ${ FS } %(HEAD) ${ FS } %(upstream:short) ${ FS } %(upstream:track,nobracket) ${ FS } %(objectname:short) ${ FS } %(subject) ${ FS } %(committerdate:iso-strict) `
;
const
{
}
=
await
runGit
(
,
[
'for-each-ref'
,
` --format= ${ fmt} `
,
'refs/heads'
]
)
;
return
.
split
(
/ \r ? \n /
)
.
filter
(
Boolean
)
.
map
(
(
line
)
=>
{
const
[
,
,
,
,
,
,
]
=
.
split
(
FS
)
;
const
=
Number
(
/ ahead ( \d + ) /
.
exec
(
)
?.
[
1
]
??
0
)
;
const
=
Number
(
/ behind ( \d + ) /
.
exec
(
)
?.
[
1
]
??
0
)
;
return
{
,
current
:
===
'*'
,
upstream
:
||
null
,
,
,
gone
:
/ gone /
.
test
(
)
,
lastCommit
:
?
{
,
,
}
:
null
,
}
;
}
)
;
}
export
async
function
checkout
(
cwd, { branch, create = false }
)
{
await
checkBranch
(
,
)
;
const
=
?
[
'checkout'
,
'-b'
,
]
:
[
'checkout'
,
,
'--'
]
;
await
runGit
(
,
)
;
return
{
ok
:
true
,
}
;
}