From ccd4724680e6eba096db3e5bef7030d029864667 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 30 Sep 2020 03:33:54 -0400 Subject: [PATCH] Fixes typing branch/tags names in ref picker --- src/commands/quickCommand.steps.ts | 14 ++++++-- src/git/git.ts | 2 +- src/git/gitService.ts | 65 ++++++++++++++++++++++++++------------ 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/commands/quickCommand.steps.ts b/src/commands/quickCommand.steps.ts index ba2fbb0..24daa15 100644 --- a/src/commands/quickCommand.steps.ts +++ b/src/commands/quickCommand.steps.ts @@ -283,6 +283,16 @@ export function getValidateGitReferenceFn(repos: Repository | Repository[]) { return false; } + if (!inRefMode) { + if ( + await Container.git.hasBranchesAndOrTags(repos.path, { + filter: { branches: b => b.name.includes(value), tags: t => t.name.includes(value) }, + }) + ) { + return false; + } + } + const commit = await Container.git.getCommit(repos.path, value); quickpick.items = [CommitQuickPickItem.create(commit!, true, { alwaysShow: true, compact: true, icon: true })]; return true; @@ -308,7 +318,7 @@ export async function* inputBranchNameStep< value = value.trim(); if (value.length === 0) return [false, 'Please enter a valid branch name']; - const valid = Boolean(await Container.git.validateBranchOrTagName(value)); + const valid = await Container.git.validateBranchOrTagName(value); return [valid, valid ? undefined : `'${value}' isn't a valid branch name`]; }, }); @@ -343,7 +353,7 @@ export async function* inputTagNameStep< value = value.trim(); if (value.length === 0) return [false, 'Please enter a valid tag name']; - const valid = Boolean(await Container.git.validateBranchOrTagName(value)); + const valid = await Container.git.validateBranchOrTagName(value); return [valid, valid ? undefined : `'${value}' isn't a valid tag name`]; }, }); diff --git a/src/git/git.ts b/src/git/git.ts index 863a758..79a8a9b 100644 --- a/src/git/git.ts +++ b/src/git/git.ts @@ -422,7 +422,7 @@ export namespace Git { ...params, ref, ); - return data.trim(); + return Boolean(data.trim()); } catch { return false; } diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 1baf6bb..68292e5 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -2151,26 +2151,6 @@ export class GitService implements Disposable { } @log() - async hasRemotes(repoPath: string | undefined): Promise { - if (repoPath == null) return false; - - const repository = await this.getRepository(repoPath); - if (repository == null) return false; - - return repository.hasRemotes(); - } - - @log() - async hasTrackingBranch(repoPath: string | undefined): Promise { - if (repoPath == null) return false; - - const repository = await this.getRepository(repoPath); - if (repository == null) return false; - - return repository.hasTrackingBranch(); - } - - @log() async getMergeBase(repoPath: string, ref1: string, ref2: string, options: { forkPoint?: boolean } = {}) { const cc = Logger.getCorrelationContext(); @@ -3186,6 +3166,49 @@ export class GitService implements Disposable { return (await fsExists(uri.fsPath)) ? uri : undefined; } + @log() + async hasBranchesAndOrTags( + repoPath: string | undefined, + { + filter, + }: { + filter?: { branches?: (b: GitBranch) => boolean; tags?: (t: GitTag) => boolean }; + } = {}, + ) { + const [branches, tags] = await Promise.all([ + this.getBranches(repoPath, { + filter: filter?.branches, + sort: false, + }), + this.getTags(repoPath, { + filter: filter?.tags, + sort: false, + }), + ]); + + return (branches != null && branches.length !== 0) || (tags != null && tags.length !== 0); + } + + @log() + async hasRemotes(repoPath: string | undefined): Promise { + if (repoPath == null) return false; + + const repository = await this.getRepository(repoPath); + if (repository == null) return false; + + return repository.hasRemotes(); + } + + @log() + async hasTrackingBranch(repoPath: string | undefined): Promise { + if (repoPath == null) return false; + + const repository = await this.getRepository(repoPath); + if (repository == null) return false; + + return repository.hasTrackingBranch(); + } + isTrackable(scheme: string): boolean; isTrackable(uri: Uri): boolean; isTrackable(schemeOruri: string | Uri): boolean { @@ -3362,7 +3385,7 @@ export class GitService implements Disposable { } @log() - validateBranchOrTagName(ref: string, repoPath?: string) { + validateBranchOrTagName(ref: string, repoPath?: string): Promise { return Git.check_ref_format(ref, repoPath); }