diff --git a/src/commands/pull-requests.ts b/src/commands/pull-requests.ts index d1b2f0f1..fe1be884 100644 --- a/src/commands/pull-requests.ts +++ b/src/commands/pull-requests.ts @@ -174,30 +174,12 @@ export class CreatePullRequest extends PullRequestCommand { return; } let [owner, repo] = await this.git.getGitProviderOwnerAndRepository(); - const repository = await this.githubManager.getRepository(); - const items = [{ - label: repository.name, - description: '', - repo: repository as { defaultBranch: string } - }]; - if (repository.parent) { - items.push({ - label: repository.parent.name, - description: '', - repo: repository.parent as { defaultBranch: string } - }); - } - const selectedRepository = await vscode.window.showQuickPick(items, - { placeHolder: 'Select a repository to create the pull request in' }); + const selectedRepository = await this.getRepository(); if (!selectedRepository) { return; } [owner, repo] = selectedRepository.label.split('/'); - const branch = await vscode.window.showInputBox({ - ignoreFocusOut: true, - prompt: 'Select a branch to create the pull request for', - value: selectedRepository.repo.defaultBranch - }); + const branch = await this.getTargetBranch(selectedRepository.repo.defaultBranch); if (!branch) { return; } @@ -212,6 +194,44 @@ export class CreatePullRequest extends PullRequestCommand { } } + private async getRepository(): Promise<{label: string, repo: { defaultBranch: string }} | undefined> { + const repository = await this.githubManager.getRepository(); + const items = [{ + label: repository.name, + description: '', + repo: repository as { defaultBranch: string } + }]; + if (repository.parent) { + items.push({ + label: repository.parent.name, + description: '', + repo: repository.parent as { defaultBranch: string } + }); + } + if (items.length === 1) { + return items[0]; + } + return await vscode.window.showQuickPick(items, + { placeHolder: 'Select a repository to create the pull request in' }); + } + + private async getTargetBranch(defaultBranch: string): Promise { + // sort default branch up + const picks = (await this.git.getRemoteBranches()) + .sort((b1, b2) => { + if (b1 === defaultBranch) { + return -1; + } else if (b2 === defaultBranch) { + return 1; + } + return b1.localeCompare(b2); + }); + return await vscode.window.showQuickPick(picks, { + ignoreFocusOut: true, + placeHolder: 'Select a branch to create the pull request for' + }); + } + } @component({eager: true}) diff --git a/src/git.ts b/src/git.ts index ac27770d..85b58ca8 100644 --- a/src/git.ts +++ b/src/git.ts @@ -36,6 +36,15 @@ export class Git { } } + public async getRemoteBranches(): Promise { + const response = await this.execute('git branch --list --remotes --no-color'); + return response.stdout + .split('\n') + .filter(line => !line.match('->')) + .map(line => line.replace(`${this.remoteName}/`, '')) + .map(line => line.trim()); + } + /** * Check config for a default upstream and if none found look in .git/config for a remote origin and * parses it to get username and repository.