1
1
import fs from 'fs' ;
2
2
import { Context } from '@actions/github/lib/context' ;
3
3
import { Command , Logger } from './index' ;
4
- import { getBranch , isBranch , isPrRef , isCloned , split , generateNewPatchVersion , arrayChunk , versionCompare , getAccessToken } from './utils' ;
4
+ import { getBranch , isBranch , isPrRef , getRefspec , isCloned , split , generateNewPatchVersion , arrayChunk , versionCompare , getAccessToken } from './utils' ;
5
5
import { getGitUrlWithToken } from './context-helper' ;
6
6
7
7
type CommandType = string | {
@@ -82,17 +82,34 @@ export default class GitHelper {
82
82
}
83
83
} ;
84
84
85
+ /**
86
+ * @param {string } workDir work dir
87
+ * @return {Promise<void> } void
88
+ */
89
+ private initialize = async ( workDir : string ) : Promise < void > => {
90
+ if ( fs . existsSync ( workDir ) ) {
91
+ await this . runCommand ( workDir , { command : 'rm' , args : [ '-rdf' , workDir ] } ) ;
92
+ }
93
+ fs . mkdirSync ( workDir , { recursive : true } ) ;
94
+ await this . runCommand ( workDir , { command : 'git init' , args : [ '.' ] } ) ;
95
+ } ;
96
+
97
+ /**
98
+ * @param {Context } context context
99
+ * @return {string } origin
100
+ */
101
+ private getOrigin = ( context : Context ) : string => this . origin ?? getGitUrlWithToken ( context , this . token ) ;
102
+
85
103
/**
86
104
* @param {string } workDir work dir
87
105
* @param {Context } context context
88
106
* @return {Promise<void> } void
89
107
*/
90
108
public addOrigin = async ( workDir : string , context : Context ) : Promise < void > => {
91
- const url = this . getOrigin ( context ) ;
92
109
await this . initialize ( workDir ) ;
93
110
await this . runCommand ( workDir , {
94
111
command : 'git remote add' ,
95
- args : [ 'origin' , url ] ,
112
+ args : [ 'origin' , getGitUrlWithToken ( context , this . token ) ] ,
96
113
quiet : this . isQuiet ( ) ,
97
114
altCommand : 'git remote add origin' ,
98
115
suppressError : true ,
@@ -115,12 +132,6 @@ export default class GitHelper {
115
132
*/
116
133
private isQuiet = ( ) : boolean => ! this . origin || this . quietIfNotOrigin ;
117
134
118
- /**
119
- * @param {Context } context context
120
- * @return {string } origin
121
- */
122
- private getOrigin = ( context : Context ) : string => this . origin ?? getGitUrlWithToken ( context , this . token ) ;
123
-
124
135
/**
125
136
* @param {string } workDir work dir
126
137
* @return {Promise<string> } branch name
@@ -199,64 +210,6 @@ export default class GitHelper {
199
210
}
200
211
} ;
201
212
202
- /**
203
- * @param {string } workDir work dir
204
- * @param {Context } context context
205
- * @return {Promise<void> } void
206
- */
207
- public checkout = async ( workDir : string , context : Context ) : Promise < void > => {
208
- const url = this . getOrigin ( context ) ;
209
- if ( this . cloneDepth && context . sha ) {
210
- await this . runCommand ( workDir , [
211
- {
212
- command : 'git clone' ,
213
- args : [ this . cloneDepth , url , '.' ] ,
214
- quiet : this . isQuiet ( ) ,
215
- altCommand : 'git clone' ,
216
- } ,
217
- {
218
- command : 'git fetch' ,
219
- args : [ url , context . ref ] ,
220
- quiet : this . isQuiet ( ) ,
221
- altCommand : `git fetch origin ${ context . ref } ` ,
222
- } ,
223
- {
224
- command : 'git checkout' ,
225
- args : [ '-qf' , context . sha ] ,
226
- } ,
227
- ] ) ;
228
- } else {
229
- const checkout = context . sha || getBranch ( context ) || context . ref ;
230
- if ( ! checkout ) {
231
- throw new Error ( 'Invalid context.' ) ;
232
- }
233
- await this . runCommand ( workDir , [
234
- {
235
- command : 'git clone' ,
236
- args : [ url , '.' ] ,
237
- quiet : this . isQuiet ( ) ,
238
- altCommand : 'git clone' ,
239
- } ,
240
- {
241
- command : 'git checkout' ,
242
- args : [ '-qf' , checkout ] ,
243
- } ,
244
- ] ) ;
245
- }
246
- } ;
247
-
248
- /**
249
- * @param {string } workDir work dir
250
- * @return {Promise<void> } void
251
- */
252
- private initialize = async ( workDir : string ) : Promise < void > => {
253
- if ( fs . existsSync ( workDir ) ) {
254
- await this . runCommand ( workDir , { command : 'rm' , args : [ '-rdf' , workDir ] } ) ;
255
- }
256
- fs . mkdirSync ( workDir , { recursive : true } ) ;
257
- await this . runCommand ( workDir , { command : 'git init' , args : [ '.' ] } ) ;
258
- } ;
259
-
260
213
/**
261
214
* @param {string } workDir work dir
262
215
* @param {string } branch branch
@@ -288,6 +241,22 @@ export default class GitHelper {
288
241
} ) ;
289
242
} ;
290
243
244
+ /**
245
+ * @param {string } workDir work dir
246
+ * @param {Context } context context
247
+ * @return {Promise<void> } void
248
+ */
249
+ public checkout = async ( workDir : string , context : Context ) : Promise < void > => {
250
+ await this . fetchOrigin ( workDir , context , [ '--no-tags' ] , [ getRefspec ( context ) ] ) ;
251
+ await this . runCommand ( workDir , [
252
+ {
253
+ command : 'git checkout' ,
254
+ args : [ '-qf' , context . sha ] ,
255
+ stderrToStdout : true ,
256
+ } ,
257
+ ] ) ;
258
+ } ;
259
+
291
260
/**
292
261
* @param {string } workDir work dir
293
262
* @param {string } branch branch
@@ -387,29 +356,34 @@ export default class GitHelper {
387
356
/**
388
357
* @param {string } workDir work dir
389
358
* @param {string } message message
359
+ * @param {object } options options
390
360
*/
391
- public commit = async ( workDir : string , message : string ) : Promise < boolean > => {
361
+ public commit = async ( workDir : string , message : string , options ?: { count ?: number ; allowEmpty ?: boolean ; args ?: Array < string > } ) : Promise < boolean > => {
392
362
await this . runCommand ( workDir , { command : 'git add' , args : [ '--all' ] } ) ;
393
363
394
364
if ( ! await this . checkDiff ( workDir ) ) {
395
365
this . logger . info ( 'There is no diff.' ) ;
396
366
return false ;
397
367
}
398
368
399
- await this . makeCommit ( workDir , message ) ;
369
+ await this . makeCommit ( workDir , message , options ) ;
400
370
return true ;
401
371
} ;
402
372
403
373
/**
404
374
* @param {string } workDir work dir
405
375
* @param {string } message message
406
- * @param {number } count stat count
376
+ * @param {object } options options
407
377
*/
408
- public makeCommit = async ( workDir : string , message : string , count = 10 ) : Promise < void > => { // eslint-disable-line no-magic-numbers
378
+ public makeCommit = async ( workDir : string , message : string , options ?: { count ?: number ; allowEmpty ?: boolean ; args ?: Array < string > } ) : Promise < void > => {
379
+ const count = options ?. count ?? 10 ; // eslint-disable-line no-magic-numbers
380
+ const allowEmpty = options ?. allowEmpty ?? false ;
381
+ const args = options ?. args ?? [ ] ;
382
+
409
383
await this . runCommand ( workDir , [
410
384
{
411
385
command : 'git commit' ,
412
- args : [ '-qm' , message ] ,
386
+ args : [ allowEmpty ? '--allow-empty' : '' , ... args , '-qm' , message ] ,
413
387
} ,
414
388
{
415
389
command : 'git show' ,
0 commit comments