@@ -99,11 +99,27 @@ export default class GitHelper {
99
99
await this . runCommand ( workDir , { command : 'git init' , args : [ '.' ] } ) ;
100
100
} ;
101
101
102
+ /**
103
+ * @param {string|boolean } origin origin
104
+ * @param {boolean } quiet quiet?
105
+ */
106
+ public useOrigin = ( origin : string | boolean , quiet ?: boolean ) : void => {
107
+ this . origin = typeof origin === 'boolean' ? ( origin ? 'origin' : undefined ) : origin ;
108
+ if ( quiet !== undefined ) {
109
+ this . quietIfNotOrigin = quiet ;
110
+ }
111
+ } ;
112
+
113
+ /**
114
+ * @return {string } origin name
115
+ */
116
+ public getRemoteName = ( ) : string | never => this . origin ?? 'origin' ;
117
+
102
118
/**
103
119
* @param {Context } context context
104
120
* @return {string } origin
105
121
*/
106
- private getOrigin = ( context : Context ) : string => this . origin ?? getGitUrlWithToken ( context , this . token ) ;
122
+ private getRemote = ( context : Context ) : string => this . origin ?? getGitUrlWithToken ( context , this . token ) ;
107
123
108
124
/**
109
125
* @param {string } workDir work dir
@@ -114,24 +130,13 @@ export default class GitHelper {
114
130
await this . initialize ( workDir , false ) ;
115
131
await this . runCommand ( workDir , {
116
132
command : 'git remote add' ,
117
- args : [ 'origin' , getGitUrlWithToken ( context , this . token ) ] ,
133
+ args : [ this . getRemoteName ( ) , getGitUrlWithToken ( context , this . token ) ] ,
118
134
quiet : this . isQuiet ( ) ,
119
- altCommand : ' git remote add origin' ,
135
+ altCommand : ` git remote add ${ this . getRemoteName ( ) } ` ,
120
136
suppressError : true ,
121
137
} ) ;
122
138
} ;
123
139
124
- /**
125
- * @param {string|boolean } origin origin
126
- * @param {boolean } quiet quiet?
127
- */
128
- public useOrigin = ( origin : string | boolean , quiet ?: boolean ) : void => {
129
- this . origin = typeof origin === 'boolean' ? ( origin ? 'origin' : undefined ) : origin ;
130
- if ( quiet !== undefined ) {
131
- this . quietIfNotOrigin = quiet ;
132
- }
133
- } ;
134
-
135
140
/**
136
141
* @return {boolean } is quiet?
137
142
*/
@@ -160,10 +165,9 @@ export default class GitHelper {
160
165
* @return {Promise<void> } void
161
166
*/
162
167
public cloneBranch = async ( workDir : string , branch : string , context : Context ) : Promise < void > => {
163
- const url = this . getOrigin ( context ) ;
164
168
await this . runCommand ( workDir , {
165
169
command : 'git clone' ,
166
- args : [ `--branch=${ branch } ` , this . cloneDepth , url , '.' ] ,
170
+ args : [ `--branch=${ branch } ` , this . cloneDepth , this . getRemote ( context ) , '.' ] ,
167
171
quiet : this . isQuiet ( ) ,
168
172
altCommand : `git clone --branch=${ branch } ` ,
169
173
suppressError : true ,
@@ -176,20 +180,19 @@ export default class GitHelper {
176
180
* @return {Promise<void> } void
177
181
*/
178
182
private clonePR = async ( workDir : string , context : Context ) : Promise < void > => {
179
- const url = this . getOrigin ( context ) ;
180
183
await this . runCommand ( workDir , [
181
184
{
182
185
command : 'git clone' ,
183
- args : [ this . cloneDepth , url , '.' ] ,
186
+ args : [ this . cloneDepth , this . getRemote ( context ) , '.' ] ,
184
187
quiet : this . isQuiet ( ) ,
185
188
altCommand : 'git clone' ,
186
189
suppressError : true ,
187
190
} ,
188
191
{
189
192
command : 'git fetch' ,
190
- args : [ url , `+${ context . ref } ` ] ,
193
+ args : [ this . getRemote ( context ) , `+${ context . ref } ` ] ,
191
194
quiet : this . isQuiet ( ) ,
192
- altCommand : `git fetch origin ${ context . ref } ` ,
195
+ altCommand : `git fetch ${ this . getRemoteName ( ) } ${ context . ref } ` ,
193
196
stderrToStdout : true ,
194
197
} ,
195
198
{
@@ -241,7 +244,7 @@ export default class GitHelper {
241
244
command : 'git fetch' ,
242
245
args : [
243
246
...( options ?? [ ] ) ,
244
- 'origin' ,
247
+ this . getRemoteName ( ) ,
245
248
...( refspec ?? [ ] ) ,
246
249
] ,
247
250
suppressError : true ,
@@ -272,13 +275,12 @@ export default class GitHelper {
272
275
* @return {Promise<void> } void
273
276
*/
274
277
public fetchBranch = async ( workDir : string , branch : string , context : Context ) : Promise < void > => {
275
- const url = this . getOrigin ( context ) ;
276
278
const branchName = getBranch ( branch , false ) ;
277
279
await this . runCommand ( workDir , {
278
280
command : 'git fetch' ,
279
- args : [ '--prune' , '--no-recurse-submodules' , this . cloneDepth , url , `+refs/heads/${ branchName } :refs/remotes/origin /${ branchName } ` ] ,
281
+ args : [ '--prune' , '--no-recurse-submodules' , this . cloneDepth , this . getRemote ( context ) , `+refs/heads/${ branchName } :refs/remotes/${ this . getRemoteName ( ) } /${ branchName } ` ] ,
280
282
quiet : this . isQuiet ( ) ,
281
- altCommand : `git fetch --prune --no-recurse-submodules${ this . cloneDepth } origin +refs/heads/${ branchName } :refs/remotes/origin /${ branchName } ` ,
283
+ altCommand : `git fetch --prune --no-recurse-submodules${ this . cloneDepth } ${ this . getRemoteName ( ) } +refs/heads/${ branchName } :refs/remotes/${ this . getRemoteName ( ) } /${ branchName } ` ,
282
284
suppressError : true ,
283
285
} ) ;
284
286
} ;
@@ -300,7 +302,7 @@ export default class GitHelper {
300
302
public switchBranch = async ( workDir : string , branch : string ) : Promise < void > => {
301
303
await this . runCommand ( workDir , {
302
304
command : 'git checkout' ,
303
- args : [ '-b' , branch , `origin /${ branch } ` ] ,
305
+ args : [ '-b' , branch , `${ this . getRemoteName ( ) } /${ branch } ` ] ,
304
306
suppressError : true ,
305
307
stderrToStdout : true ,
306
308
} ) ;
@@ -346,7 +348,7 @@ export default class GitHelper {
346
348
public getRefDiff = async ( workDir : string , baseRef : string , compareRef : string , diffFilter ?: string , dot ?: '..' | '...' ) : Promise < string [ ] > => {
347
349
const toDiffRef = ( ref : string ) : string =>
348
350
'HEAD' === ref ? 'HEAD' : (
349
- isPrRef ( ref ) ? ref . replace ( / ^ r e f s \/ / , '' ) : `origin /${ getBranch ( ref , false ) } `
351
+ isPrRef ( ref ) ? ref . replace ( / ^ r e f s \/ / , '' ) : `${ this . getRemoteName ( ) } /${ getBranch ( ref , false ) } `
350
352
) ;
351
353
return ( await this . runCommand ( workDir , {
352
354
command : 'git diff' ,
@@ -416,7 +418,6 @@ export default class GitHelper {
416
418
* @see https://qiita.com/ngyuki/items/ca7bed067d7e538fd0cd
417
419
*/
418
420
public fetchTags = async ( workDir : string , context : Context , splitSize = 20 ) : Promise < void > => { // eslint-disable-line no-magic-numbers
419
- const url = this . getOrigin ( context ) ;
420
421
await this . runCommand ( workDir , [
421
422
...arrayChunk ( await this . getTags ( workDir ) , splitSize ) . map ( tags => ( {
422
423
command : 'git tag' ,
@@ -426,9 +427,9 @@ export default class GitHelper {
426
427
} ) ) ,
427
428
{
428
429
command : 'git fetch' ,
429
- args : [ url , '--tags' ] ,
430
+ args : [ this . getRemote ( context ) , '--tags' ] ,
430
431
quiet : this . isQuiet ( ) ,
431
- altCommand : ' git fetch origin --tags' ,
432
+ altCommand : ` git fetch ${ this . getRemoteName ( ) } --tags` ,
432
433
} ,
433
434
] ) ;
434
435
} ;
@@ -441,16 +442,15 @@ export default class GitHelper {
441
442
* @return {Promise<void> } void
442
443
*/
443
444
public deleteTag = async ( workDir : string , tags : string | string [ ] , context : Context , splitSize = 20 ) : Promise < void > => { // eslint-disable-line no-magic-numbers
444
- const url = this . getOrigin ( context ) ;
445
445
const regexp = / ^ ( r e f s \/ ) ? t a g s \/ / ;
446
446
const getTagRef = ( tag : string ) : string => regexp . test ( tag ) ? tag : `tags/${ tag } ` ;
447
447
const getTag = ( tag : string ) : string => tag . replace ( regexp , '' ) ;
448
448
await this . runCommand ( workDir , [
449
449
...arrayChunk ( ( typeof tags === 'string' ? [ tags ] : tags ) . map ( getTagRef ) , splitSize ) . map ( tags => ( {
450
450
command : 'git push' ,
451
- args : [ url , '--delete' , ...tags ] ,
451
+ args : [ this . getRemote ( context ) , '--delete' , ...tags ] ,
452
452
quiet : this . isQuiet ( ) ,
453
- altCommand : `git push origin --delete ${ tags . join ( ' ' ) } ` ,
453
+ altCommand : `git push ${ this . getRemoteName ( ) } --delete ${ tags . join ( ' ' ) } ` ,
454
454
suppressError : true ,
455
455
} ) ) ,
456
456
...arrayChunk ( ( typeof tags === 'string' ? [ tags ] : tags ) . map ( getTag ) , splitSize ) . map ( tags => ( {
@@ -470,7 +470,6 @@ export default class GitHelper {
470
470
* @return {Promise<void> } void
471
471
*/
472
472
public copyTag = async ( workDir : string , newTag : string , fromTag : string , context : Context ) : Promise < void > => {
473
- const url = this . getOrigin ( context ) ;
474
473
await this . deleteTag ( workDir , newTag , context ) ;
475
474
await this . runCommand ( workDir , [
476
475
{
@@ -479,9 +478,9 @@ export default class GitHelper {
479
478
} ,
480
479
{
481
480
command : 'git push' ,
482
- args : [ url , `refs/tags/${ newTag } ` ] ,
481
+ args : [ this . getRemote ( context ) , `refs/tags/${ newTag } ` ] ,
483
482
quiet : this . isQuiet ( ) ,
484
- altCommand : `git push origin refs/tags/${ newTag } ` ,
483
+ altCommand : `git push ${ this . getRemoteName ( ) } refs/tags/${ newTag } ` ,
485
484
} ,
486
485
] ) ;
487
486
} ;
@@ -509,7 +508,6 @@ export default class GitHelper {
509
508
* @return {Promise<void> } void
510
509
*/
511
510
public push = async ( workDir : string , branch : string , context : Context , options ?: { withTag ?: boolean ; force ?: boolean ; args ?: Array < string > } ) : Promise < void > => {
512
- const url = this . getOrigin ( context ) ;
513
511
const args : Array < string > = [ ] ;
514
512
if ( options ?. withTag ) {
515
513
args . push ( '--tags' ) ;
@@ -522,9 +520,9 @@ export default class GitHelper {
522
520
}
523
521
await this . runCommand ( workDir , {
524
522
command : 'git push' ,
525
- args : args . concat ( [ url , `${ branch } :refs/heads/${ branch } ` ] ) ,
523
+ args : args . concat ( [ this . getRemote ( context ) , `${ branch } :refs/heads/${ branch } ` ] ) ,
526
524
quiet : this . isQuiet ( ) ,
527
- altCommand : `git push ${ args . concat ( [ 'origin' , `${ branch } :refs/heads/${ branch } ` ] ) . join ( ' ' ) } ` ,
525
+ altCommand : `git push ${ args . concat ( [ this . getRemoteName ( ) , `${ branch } :refs/heads/${ branch } ` ] ) . join ( ' ' ) } ` ,
528
526
suppressError : true ,
529
527
} ) ;
530
528
} ;
0 commit comments