diff --git a/src/annotations/annotations.ts b/src/annotations/annotations.ts index b4041e0..161153a 100644 --- a/src/annotations/annotations.ts +++ b/src/annotations/annotations.ts @@ -77,7 +77,7 @@ export class Annotations { hunkLine?: GitDiffHunkLine ): Promise { const documentRef = uri.sha; - if (commit instanceof GitBlameCommit) { + if (GitBlameCommit.is(commit)) { // TODO: Figure out how to optimize this let ref; if (commit.isUncommitted) { diff --git a/src/commands/common.ts b/src/commands/common.ts index d54690c..1001ea1 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -239,7 +239,7 @@ export function isCommandViewContextWithBranch( ): context is CommandViewItemContext & { node: ViewNode & { branch: GitBranch } } { if (context.type !== 'viewItem') return false; - return (context.node as ViewNode & { branch: GitBranch }).branch instanceof GitBranch; + return GitBranch.is((context.node as ViewNode & { branch: GitBranch }).branch); } export function isCommandViewContextWithCommit( @@ -247,7 +247,7 @@ export function isCommandViewContextWithCommit( ): context is CommandViewItemContext & { node: ViewNode & { commit: T } } { if (context.type !== 'viewItem') return false; - return (context.node as ViewNode & { commit: GitCommit }).commit instanceof GitCommit; + return GitCommit.is((context.node as ViewNode & { commit: GitCommit }).commit); } export function isCommandViewContextWithContributor( @@ -255,7 +255,7 @@ export function isCommandViewContextWithContributor( ): context is CommandViewItemContext & { node: ViewNode & { contributor: GitContributor } } { if (context.type !== 'viewItem') return false; - return (context.node as ViewNode & { contributor: GitContributor }).contributor instanceof GitContributor; + return GitContributor.is((context.node as ViewNode & { contributor: GitContributor }).contributor); } export function isCommandViewContextWithFile( @@ -275,7 +275,7 @@ export function isCommandViewContextWithFileCommit( const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string }; return ( node.file !== undefined && - node.commit instanceof GitCommit && + GitCommit.is(node.commit) && (node.file.repoPath !== undefined || node.repoPath !== undefined) ); } @@ -307,7 +307,7 @@ export function isCommandViewContextWithRemote( ): context is CommandViewItemContext & { node: ViewNode & { remote: GitRemote } } { if (context.type !== 'viewItem') return false; - return (context.node as ViewNode & { remote: GitRemote }).remote instanceof GitRemote; + return GitRemote.is((context.node as ViewNode & { remote: GitRemote }).remote); } export function isCommandViewContextWithRepo( @@ -548,7 +548,7 @@ export async function openEditor( ): Promise { const { rethrow, ...opts } = options; try { - if (uri instanceof GitUri) { + if (GitUri.is(uri)) { uri = uri.documentUri(); } diff --git a/src/commands/diffWith.ts b/src/commands/diffWith.ts index a4e1e43..3198c24 100644 --- a/src/commands/diffWith.ts +++ b/src/commands/diffWith.ts @@ -29,7 +29,7 @@ export class DiffWithCommand extends ActiveEditorCommand { static getMarkdownCommandArgs(commit: GitCommit, line?: number): string; static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string { let args: DiffWithCommandArgs | GitCommit; - if (argsOrCommit instanceof GitCommit) { + if (GitCommit.is(argsOrCommit)) { const commit = argsOrCommit; if (commit.isUncommitted) { diff --git a/src/commands/diffWithRevision.ts b/src/commands/diffWithRevision.ts index ae04d94..58b4600 100644 --- a/src/commands/diffWithRevision.ts +++ b/src/commands/diffWithRevision.ts @@ -86,12 +86,7 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand { } commandArgs = { ...args }; - const icon = - args.reference instanceof GitTag - ? '$(tag) ' - : args.reference instanceof GitBranch - ? '$(git-branch) ' - : ''; + const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : ''; const currentCommand = new CommandQuickPickItem( { label: `go back ${GlyphChars.ArrowBack}`, diff --git a/src/commands/openFileRevision.ts b/src/commands/openFileRevision.ts index f338bc1..0ab6217 100644 --- a/src/commands/openFileRevision.ts +++ b/src/commands/openFileRevision.ts @@ -117,12 +117,11 @@ export class OpenFileRevisionCommand extends ActiveEditorCommand { } commandArgs = { ...args }; - const icon = - args.reference instanceof GitTag - ? '$(tag) ' - : args.reference instanceof GitBranch - ? '$(git-branch) ' - : ''; + const icon = GitTag.is(args.reference) + ? '$(tag) ' + : GitBranch.is(args.reference) + ? '$(git-branch) ' + : ''; const currentCommand = new CommandQuickPickItem( { label: `go back ${GlyphChars.ArrowBack}`, diff --git a/src/commands/openWorkingFile.ts b/src/commands/openWorkingFile.ts index cf72c69..8359c08 100644 --- a/src/commands/openWorkingFile.ts +++ b/src/commands/openWorkingFile.ts @@ -36,7 +36,7 @@ export class OpenWorkingFileCommand extends ActiveEditorCommand { } args.uri = await GitUri.fromUri(uri); - if (args.uri instanceof GitUri && args.uri.sha) { + if (GitUri.is(args.uri) && args.uri.sha) { const workingUri = await Container.git.getWorkingUri(args.uri.repoPath!, args.uri); if (workingUri === undefined) { return window.showWarningMessage( diff --git a/src/commands/showQuickFileHistory.ts b/src/commands/showQuickFileHistory.ts index 55515a3..155e85b 100644 --- a/src/commands/showQuickFileHistory.ts +++ b/src/commands/showQuickFileHistory.ts @@ -111,12 +111,7 @@ export class ShowQuickFileHistoryCommand extends ActiveEditorCachedCommand { } } - const icon = - args.reference instanceof GitTag - ? '$(tag) ' - : args.reference instanceof GitBranch - ? '$(git-branch) ' - : ''; + const icon = GitTag.is(args.reference) ? '$(tag) ' : GitBranch.is(args.reference) ? '$(git-branch) ' : ''; // Create a command to get back to where we are right now const currentCommand = new CommandQuickPickItem( { diff --git a/src/extension.ts b/src/extension.ts index 30861e1..d5896ff 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -18,13 +18,13 @@ export async function activate(context: ExtensionContext) { setCommandContext(CommandContext.Enabled, true); Logger.configure(context, configuration.get(configuration.name('outputLevel').value), o => { - if (o instanceof GitUri) { + if (GitUri.is(o)) { return `GitUri(${o.toString(true)}${o.repoPath ? ` repoPath=${o.repoPath}` : ''}${ o.sha ? ` sha=${o.sha}` : '' })`; } - if (o instanceof GitCommit) { + if (GitCommit.is(o)) { return `GitCommit(${o.sha ? ` sha=${o.sha}` : ''}${o.repoPath ? ` repoPath=${o.repoPath}` : ''})`; } diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index b72942a..0b24c54 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -142,14 +142,14 @@ export class CommitFormatter extends Formatter { get changes() { return this._padOrTruncate( - this._item instanceof GitLogCommit ? this._item.getFormattedDiffStatus() : emptyStr, + GitLogCommit.is(this._item) ? this._item.getFormattedDiffStatus() : emptyStr, this._options.tokenOptions.changes ); } get changesShort() { return this._padOrTruncate( - this._item instanceof GitLogCommit + GitLogCommit.is(this._item) ? this._item.getFormattedDiffStatus({ compact: true, separator: emptyStr }) : emptyStr, this._options.tokenOptions.changesShort diff --git a/src/git/fsProvider.ts b/src/git/fsProvider.ts index 4f727a0..40cf380 100644 --- a/src/git/fsProvider.ts +++ b/src/git/fsProvider.ts @@ -20,7 +20,7 @@ import { Iterables, Strings, TernarySearchTree } from '../system'; const emptyArray = new Uint8Array(0); export function fromGitLensFSUri(uri: Uri): { path: string; ref: string; repoPath: string } { - const gitUri = uri instanceof GitUri ? uri : GitUri.fromRevisionUri(uri); + const gitUri = GitUri.is(uri) ? uri : GitUri.fromRevisionUri(uri); return { path: gitUri.relativePath, ref: gitUri.sha!, repoPath: gitUri.repoPath! }; } diff --git a/src/git/gitService.ts b/src/git/gitService.ts index e3665c2..b9d7231 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -2021,7 +2021,7 @@ export class GitService implements Disposable { options: { ref?: string } = {} ): Promise { if (filePathOrUri == null) return this.getHighlanderRepoPath(); - if (filePathOrUri instanceof GitUri) return filePathOrUri.repoPath; + if (GitUri.is(filePathOrUri)) return filePathOrUri.repoPath; const cc = Logger.getCorrelationContext(); @@ -2153,7 +2153,7 @@ export class GitService implements Disposable { isVslsScheme = undefined; } else { - if (repoPathOrUri instanceof GitUri) { + if (GitUri.is(repoPathOrUri)) { if (repoPathOrUri.repoPath) { const repo = repositoryTree.get(repoPathOrUri.repoPath); if (repo !== undefined) return repo; diff --git a/src/git/gitUri.ts b/src/git/gitUri.ts index e93c769..3d2b09d 100644 --- a/src/git/gitUri.ts +++ b/src/git/gitUri.ts @@ -36,6 +36,10 @@ interface UriEx { } export class GitUri extends ((Uri as any) as UriEx) { + static is(uri: any): uri is GitUri { + return uri instanceof GitUri; + } + readonly repoPath?: string; readonly sha?: string; readonly versionedPath?: string; @@ -176,7 +180,7 @@ export class GitUri extends ((Uri as any) as UriEx) { equals(uri: Uri | undefined) { if (!UriComparer.equals(this, uri)) return false; - return this.sha === (uri instanceof GitUri ? uri.sha : undefined); + return this.sha === (GitUri.is(uri) ? uri.sha : undefined); } getFormattedPath(options: { relativeTo?: string; separator?: string; suffix?: string } = {}): string { @@ -259,7 +263,7 @@ export class GitUri extends ((Uri as any) as UriEx) { exit: uri => `returned ${Logger.toLoggable(uri)}` }) static async fromUri(uri: Uri) { - if (uri instanceof GitUri) return uri; + if (GitUri.is(uri)) return uri; if (!Container.git.isTrackable(uri)) return new GitUri(uri); @@ -315,7 +319,7 @@ export class GitUri extends ((Uri as any) as UriEx) { let fileName: string; if (fileNameOrUri instanceof Uri) { - if (fileNameOrUri instanceof GitUri) return fileNameOrUri.getFormattedPath(options); + if (GitUri.is(fileNameOrUri)) return fileNameOrUri.getFormattedPath(options); fileName = fileNameOrUri.fsPath; } diff --git a/src/git/models/blameCommit.ts b/src/git/models/blameCommit.ts index 96d9218..07ae5b7 100644 --- a/src/git/models/blameCommit.ts +++ b/src/git/models/blameCommit.ts @@ -2,6 +2,13 @@ import { GitCommit, GitCommitLine, GitCommitType } from './commit'; export class GitBlameCommit extends GitCommit { + static is(commit: any): commit is GitBlameCommit { + return ( + commit instanceof GitBlameCommit + //|| (commit.repoPath !== undefined && commit.sha !== undefined && commit.type === GitCommitType.Blame) + ); + } + constructor( repoPath: string, sha: string, diff --git a/src/git/models/branch.ts b/src/git/models/branch.ts index ce18916..af998ee 100644 --- a/src/git/models/branch.ts +++ b/src/git/models/branch.ts @@ -11,6 +11,10 @@ export interface GitTrackingState { } export class GitBranch { + static is(branch: any): branch is GitBranch { + return branch instanceof GitBranch; + } + readonly detached: boolean; readonly id: string; readonly tracking?: string; diff --git a/src/git/models/commit.ts b/src/git/models/commit.ts index 4010e5c..c889732 100644 --- a/src/git/models/commit.ts +++ b/src/git/models/commit.ts @@ -42,6 +42,19 @@ export const CommitFormatting = { }; export abstract class GitCommit { + static is(commit: any): commit is GitCommit { + return ( + commit instanceof GitCommit + // || (commit.repoPath !== undefined && + // commit.sha !== undefined && + // (commit.type === GitCommitType.Blame || + // commit.type === GitCommitType.Log || + // commit.type === GitCommitType.LogFile || + // commit.type === GitCommitType.Stash || + // commit.type === GitCommitType.StashFile)) + ); + } + constructor( public readonly type: GitCommitType, public readonly repoPath: string, diff --git a/src/git/models/contributor.ts b/src/git/models/contributor.ts index a4710ad..91edb42 100644 --- a/src/git/models/contributor.ts +++ b/src/git/models/contributor.ts @@ -4,6 +4,10 @@ import { GravatarDefaultStyle } from '../../configuration'; import { getGravatarUri } from '../../avatars'; export class GitContributor { + static is(contributor: any): contributor is GitContributor { + return contributor instanceof GitContributor; + } + constructor( public readonly repoPath: string, public readonly name: string, diff --git a/src/git/models/logCommit.ts b/src/git/models/logCommit.ts index a5ee6a1..ef84834 100644 --- a/src/git/models/logCommit.ts +++ b/src/git/models/logCommit.ts @@ -24,6 +24,15 @@ export interface GitLogCommitLine { } export class GitLogCommit extends GitCommit { + static is(commit: any): commit is GitLogCommit { + return ( + commit instanceof GitLogCommit + // || (commit.repoPath !== undefined && + // commit.sha !== undefined && + // (commit.type === GitCommitType.Log || commit.type === GitCommitType.LogFile)) + ); + } + nextSha?: string; nextFileName?: string; diff --git a/src/git/models/remote.ts b/src/git/models/remote.ts index add94b0..36a26ac 100644 --- a/src/git/models/remote.ts +++ b/src/git/models/remote.ts @@ -9,6 +9,10 @@ export enum GitRemoteType { } export class GitRemote { + static is(remote: any): remote is GitRemote { + return remote instanceof GitRemote; + } + constructor( public readonly repoPath: string, public readonly id: string, diff --git a/src/git/models/repository.ts b/src/git/models/repository.ts index f1d8ef8..464d153 100644 --- a/src/git/models/repository.ts +++ b/src/git/models/repository.ts @@ -228,7 +228,7 @@ export class Repository implements Disposable { } containsUri(uri: Uri) { - if (uri instanceof GitUri) { + if (GitUri.is(uri)) { uri = uri.repoPath !== undefined ? GitUri.file(uri.repoPath) : uri.documentUri(); } diff --git a/src/git/models/stashCommit.ts b/src/git/models/stashCommit.ts index cf05085..1d894e3 100644 --- a/src/git/models/stashCommit.ts +++ b/src/git/models/stashCommit.ts @@ -4,8 +4,13 @@ import { GitFile } from './file'; import { GitLogCommit } from './logCommit'; export class GitStashCommit extends GitLogCommit { - static is(commit: GitLogCommit): commit is GitStashCommit { - return commit.isStash; + static is(commit: any): commit is GitStashCommit { + return ( + commit instanceof GitStashCommit + // || (commit.repoPath !== undefined && + // commit.sha !== undefined && + // (commit.type === GitCommitType.Stash || commit.type === GitCommitType.StashFile)) + ); } constructor( diff --git a/src/git/models/tag.ts b/src/git/models/tag.ts index 0b98f42..2b14606 100644 --- a/src/git/models/tag.ts +++ b/src/git/models/tag.ts @@ -2,6 +2,10 @@ import { memoize } from '../../system'; export class GitTag { + static is(tag: any): tag is GitTag { + return tag instanceof GitTag; + } + constructor( public readonly repoPath: string, public readonly name: string, diff --git a/src/trackers/documentTracker.ts b/src/trackers/documentTracker.ts index 1336087..aad1162 100644 --- a/src/trackers/documentTracker.ts +++ b/src/trackers/documentTracker.ts @@ -236,7 +236,7 @@ export class DocumentTracker implements Disposable { private async _add(documentOrId: TextDocument | Uri): Promise> { let document; - if (documentOrId instanceof GitUri) { + if (GitUri.is(documentOrId)) { try { document = await workspace.openTextDocument(documentOrId.documentUri({ useVersionedPath: true })); } @@ -273,7 +273,7 @@ export class DocumentTracker implements Disposable { } private async _get(documentOrId: string | TextDocument | Uri) { - if (documentOrId instanceof GitUri) { + if (GitUri.is(documentOrId)) { documentOrId = GitUri.toKey(documentOrId.documentUri({ useVersionedPath: true })); } else if (typeof documentOrId === 'string' || documentOrId instanceof Uri) {