Skip to content

Commit

Permalink
fixes #106664
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Nov 27, 2020
1 parent 8012c25 commit 0321ca5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion extensions/git/src/api/api1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export function registerAPICommands(extension: GitExtensionImpl): Disposable {
return;
}

return pickRemoteSource(extension.model, opts);
return pickRemoteSource(extension.model, opts as any);
}));

return Disposable.from(...disposables);
Expand Down
1 change: 1 addition & 0 deletions extensions/git/src/api/git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export interface RemoteSourceProvider {
readonly icon?: string; // codicon name
readonly supportsQuery?: boolean;
getRemoteSources(query?: string): ProviderResult<RemoteSource[]>;
getBranches?(url: string): ProviderResult<string[]>;
publishRepository?(repository: Repository): Promise<void>;
}

Expand Down
46 changes: 39 additions & 7 deletions extensions/git/src/remoteSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,17 @@ export interface PickRemoteSourceOptions {
readonly providerLabel?: (provider: RemoteSourceProvider) => string;
readonly urlLabel?: string;
readonly providerName?: string;
readonly branch?: boolean; // then result is PickRemoteSourceResult
}

export async function pickRemoteSource(model: Model, options: PickRemoteSourceOptions = {}): Promise<string | undefined> {
export interface PickRemoteSourceResult {
readonly url: string;
readonly branch?: string;
}

export async function pickRemoteSource(model: Model, options: PickRemoteSourceOptions & { branch?: false | undefined }): Promise<string | undefined>;
export async function pickRemoteSource(model: Model, options: PickRemoteSourceOptions & { branch: true }): Promise<PickRemoteSourceResult | undefined>;
export async function pickRemoteSource(model: Model, options: PickRemoteSourceOptions = {}): Promise<string | PickRemoteSourceResult | undefined> {
const quickpick = window.createQuickPick<(QuickPickItem & { provider?: RemoteSourceProvider, url?: string })>();
quickpick.ignoreFocusOut = true;

Expand All @@ -93,7 +101,7 @@ export async function pickRemoteSource(model: Model, options: PickRemoteSourceOp
.filter(provider => provider.name === options.providerName)[0];

if (provider) {
return await pickProviderSource(provider);
return await pickProviderSource(provider, options);
}
}

Expand Down Expand Up @@ -127,24 +135,48 @@ export async function pickRemoteSource(model: Model, options: PickRemoteSourceOp
if (result.url) {
return result.url;
} else if (result.provider) {
return await pickProviderSource(result.provider);
return await pickProviderSource(result.provider, options);
}
}

return undefined;
}

async function pickProviderSource(provider: RemoteSourceProvider): Promise<string | undefined> {
async function pickProviderSource(provider: RemoteSourceProvider, options: PickRemoteSourceOptions = {}): Promise<string | PickRemoteSourceResult | undefined> {
const quickpick = new RemoteSourceProviderQuickPick(provider);
const remote = await quickpick.pick();

let url: string | undefined;

if (remote) {
if (typeof remote.url === 'string') {
return remote.url;
url = remote.url;
} else if (remote.url.length > 0) {
return await window.showQuickPick(remote.url, { ignoreFocusOut: true, placeHolder: localize('pick url', "Choose a URL to clone from.") });
url = await window.showQuickPick(remote.url, { ignoreFocusOut: true, placeHolder: localize('pick url', "Choose a URL to clone from.") });
}
}

return undefined;
if (!url || !options.branch) {
return url;
}

if (!provider.getBranches) {
return { url };
}

const branches = await provider.getBranches(url);

if (!branches) {
return { url };
}

const branch = await window.showQuickPick(branches, {
placeHolder: localize('branch name', "Branch name")
});

if (!branch) {
return { url };
}

return { url, branch };
}
28 changes: 24 additions & 4 deletions extensions/github/src/remoteSourceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { getOctokit } from './auth';
import { Octokit } from '@octokit/rest';
import { publishRepository } from './publish';

function parse(url: string): { owner: string, repo: string } | undefined {
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\.git/i.exec(url)
|| /^git@github\.com:([^/]+)\/([^/]+)\.git/i.exec(url);
return (match && { owner: match[1], repo: match[2] }) ?? undefined;
}

function asRemoteSource(raw: any): RemoteSource {
return {
name: `$(github) ${raw.full_name}`,
Expand All @@ -30,11 +36,10 @@ export class GithubRemoteSourceProvider implements RemoteSourceProvider {
const octokit = await getOctokit();

if (query) {
const match = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\.git/i.exec(query)
|| /^git@github\.com:([^/]+)\/([^/]+)\.git/i.exec(query);
const repository = parse(query);

if (match) {
const raw = await octokit.repos.get({ owner: match[1], repo: match[2] });
if (repository) {
const raw = await octokit.repos.get(repository);
return [asRemoteSource(raw.data)];
}
}
Expand Down Expand Up @@ -75,6 +80,21 @@ export class GithubRemoteSourceProvider implements RemoteSourceProvider {
return raw.data.items.map(asRemoteSource);
}

async getBranches(url: string): Promise<string[]> {
const repository = parse(url);

if (!repository) {
return [];
}

const octokit = await getOctokit();
const branches = await octokit.repos.listBranches(repository);
const repo = await octokit.repos.get(repository);
const defaultBranch = repo.data.default_branch;

return branches.data.map(b => b.name).sort((a, b) => a === defaultBranch ? -1 : b === defaultBranch ? 1 : 0);
}

publishRepository(repository: Repository): Promise<void> {
return publishRepository(this.gitAPI, repository);
}
Expand Down
2 changes: 2 additions & 0 deletions extensions/github/src/typings/git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export interface CommitOptions {
signoff?: boolean;
signCommit?: boolean;
empty?: boolean;
noVerify?: boolean;
}

export interface BranchQuery {
Expand Down Expand Up @@ -211,6 +212,7 @@ export interface RemoteSourceProvider {
readonly icon?: string; // codicon name
readonly supportsQuery?: boolean;
getRemoteSources(query?: string): ProviderResult<RemoteSource[]>;
getBranches?(url: string): ProviderResult<string[]>;
publishRepository?(repository: Repository): Promise<void>;
}

Expand Down

0 comments on commit 0321ca5

Please sign in to comment.