Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
feat: quick pick branches for pull request
Browse files Browse the repository at this point in the history
Show a quick pick with all remote branches as target when creating pull request.

Closes #223
  • Loading branch information
KnisterPeter committed Oct 6, 2017
1 parent 2a9e537 commit 53aa27d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/commands/pull-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<string | undefined> {
// 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})
Expand Down
9 changes: 9 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export class Git {
}
}

public async getRemoteBranches(): Promise<string[]> {
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.
Expand Down

0 comments on commit 53aa27d

Please sign in to comment.