Skip to content

feat: update push helper #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions __tests__/git-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,20 +603,20 @@ describe('GitHelper', () => {
it('should run push', async() => {
const mockExec = spyOnExec();

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

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

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

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

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

execCalledWith(mockExec, [
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1',
'git push --force \'https://octocat:[email protected]/hello/world.git\' \'test-branch:refs/heads/test-branch\' > /dev/null 2>&1 || :',
]);
});
});
Expand Down
32 changes: 17 additions & 15 deletions src/git-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,18 +496,28 @@ export default class GitHelper {
/**
* @param {string} workDir work dir
* @param {string} branch branch
* @param {boolean} withTag with tag?
* @param {Context} context context
* @param {object} options options
* @return {Promise<void>} void
*/
public push = async(workDir: string, branch: string, withTag: boolean, context: Context): Promise<void> => {
const url = this.getOrigin(context);
const tags = withTag ? ' --tags' : '';
public push = async(workDir: string, branch: string, context: Context, options?: { withTag?: boolean; force?: boolean; args?: Array<string> }): Promise<void> => {
const url = this.getOrigin(context);
const args: Array<string> = [];
if (options?.withTag) {
args.push('--tags');
}
if (options?.force) {
args.push('--force');
}
if (options?.args) {
args.push(...options.args);
}
await this.runCommand(workDir, {
command: 'git push',
args: [withTag ? '--tags' : '', url, `${branch}:refs/heads/${branch}`],
args: args.concat([url, `${branch}:refs/heads/${branch}`]),
quiet: this.isQuiet(),
altCommand: `git push${tags} origin ${branch}:refs/heads/${branch}`,
altCommand: `git push${args.concat(['origin', `${branch}:refs/heads/${branch}`]).join(' ')}`,
suppressError: true,
});
};

Expand All @@ -517,15 +527,7 @@ export default class GitHelper {
* @param {Context} context context
* @return {Promise<void>} void
*/
public forcePush = async(workDir: string, branch: string, context: Context): Promise<void> => {
const url = this.getOrigin(context);
await this.runCommand(workDir, {
command: 'git push',
args: ['--force', url, `${branch}:refs/heads/${branch}`],
quiet: this.isQuiet(),
altCommand: `git push --force origin ${branch}:refs/heads/${branch}`,
});
};
public forcePush = async(workDir: string, branch: string, context: Context): Promise<void> => this.push(workDir, branch, context, {force: true});

/**
* @param {string} workDir work dir
Expand Down