Skip to content

Commit e029b63

Browse files
Merge pull request #222 from technote-space/feature/push_helper
feat: update push helper
2 parents be72712 + 663be5b commit e029b63

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

__tests__/git-helper.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,20 @@ describe('GitHelper', () => {
603603
it('should run push', async() => {
604604
const mockExec = spyOnExec();
605605

606-
await helper.push(workDir, 'test-branch', true, context());
606+
await helper.push(workDir, 'test-branch', context(), {withTag: true, args: ['--prune', '--verbose']});
607607

608608
execCalledWith(mockExec, [
609-
'git push --tags \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
609+
'git push --tags --prune --verbose \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
610610
]);
611611
});
612612

613613
it('should run push without tags', async() => {
614614
const mockExec = spyOnExec();
615615

616-
await helper.push(workDir, 'test-branch', false, context());
616+
await helper.push(workDir, 'test-branch', context());
617617

618618
execCalledWith(mockExec, [
619-
'git push \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
619+
'git push \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
620620
]);
621621
});
622622
});
@@ -628,7 +628,7 @@ describe('GitHelper', () => {
628628
await helper.forcePush(workDir, 'test-branch', context());
629629

630630
execCalledWith(mockExec, [
631-
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
631+
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
632632
]);
633633
});
634634
});

src/git-helper.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -496,18 +496,28 @@ export default class GitHelper {
496496
/**
497497
* @param {string} workDir work dir
498498
* @param {string} branch branch
499-
* @param {boolean} withTag with tag?
500499
* @param {Context} context context
500+
* @param {object} options options
501501
* @return {Promise<void>} void
502502
*/
503-
public push = async(workDir: string, branch: string, withTag: boolean, context: Context): Promise<void> => {
504-
const url = this.getOrigin(context);
505-
const tags = withTag ? ' --tags' : '';
503+
public push = async(workDir: string, branch: string, context: Context, options?: { withTag?: boolean; force?: boolean; args?: Array<string> }): Promise<void> => {
504+
const url = this.getOrigin(context);
505+
const args: Array<string> = [];
506+
if (options?.withTag) {
507+
args.push('--tags');
508+
}
509+
if (options?.force) {
510+
args.push('--force');
511+
}
512+
if (options?.args) {
513+
args.push(...options.args);
514+
}
506515
await this.runCommand(workDir, {
507516
command: 'git push',
508-
args: [withTag ? '--tags' : '', url, `${branch}:refs/heads/${branch}`],
517+
args: args.concat([url, `${branch}:refs/heads/${branch}`]),
509518
quiet: this.isQuiet(),
510-
altCommand: `git push${tags} origin ${branch}:refs/heads/${branch}`,
519+
altCommand: `git push${args.concat(['origin', `${branch}:refs/heads/${branch}`]).join(' ')}`,
520+
suppressError: true,
511521
});
512522
};
513523

@@ -517,15 +527,7 @@ export default class GitHelper {
517527
* @param {Context} context context
518528
* @return {Promise<void>} void
519529
*/
520-
public forcePush = async(workDir: string, branch: string, context: Context): Promise<void> => {
521-
const url = this.getOrigin(context);
522-
await this.runCommand(workDir, {
523-
command: 'git push',
524-
args: ['--force', url, `${branch}:refs/heads/${branch}`],
525-
quiet: this.isQuiet(),
526-
altCommand: `git push --force origin ${branch}:refs/heads/${branch}`,
527-
});
528-
};
530+
public forcePush = async(workDir: string, branch: string, context: Context): Promise<void> => this.push(workDir, branch, context, {force: true});
529531

530532
/**
531533
* @param {string} workDir work dir

0 commit comments

Comments
 (0)