diff --git a/src/annotations/gutterBlameAnnotationProvider.ts b/src/annotations/gutterBlameAnnotationProvider.ts index aa4cc52..0fd7b1e 100644 --- a/src/annotations/gutterBlameAnnotationProvider.ts +++ b/src/annotations/gutterBlameAnnotationProvider.ts @@ -191,7 +191,7 @@ export class GutterBlameAnnotationProvider extends BlameAnnotationProviderBase { sha = commitLine?.sha; } } else { - sha = first(blame.commits.values()).sha; + sha = first(blame.commits.values())?.sha; } if (!sha) { diff --git a/src/codelens/codeLensProvider.ts b/src/codelens/codeLensProvider.ts index 46560a2..79340b9 100644 --- a/src/codelens/codeLensProvider.ts +++ b/src/codelens/codeLensProvider.ts @@ -480,9 +480,11 @@ export class GitCodeLensProvider implements CodeLensProvider { private resolveGitRecentChangeCodeLens(lens: GitRecentChangeCodeLens, _token: CancellationToken): CodeLens { const blame = lens.getBlame(); - if (blame === undefined) return lens; + if (blame == null) return lens; const recentCommit = first(blame.commits.values()); + if (recentCommit == null) return lens; + // TODO@eamodio This is FAR too expensive, but this accounts for commits that delete lines -- is there another way? // if (lens.uri != null) { // const commit = await this.container.git.getCommitForFile(lens.uri.repoPath, lens.uri.fsPath, { @@ -564,11 +566,10 @@ export class GitCodeLensProvider implements CodeLensProvider { private resolveGitAuthorsCodeLens(lens: GitAuthorsCodeLens, _token: CancellationToken): CodeLens { const blame = lens.getBlame(); - if (blame === undefined) return lens; + if (blame == null) return lens; const count = blame.authors.size; - - const author = first(blame.authors.values()).name; + const author = first(blame.authors.values())?.name ?? 'Unknown'; let title = `${count} ${count > 1 ? 'authors' : 'author'} (${author}${count > 1 ? ' and others' : ''})`; if (configuration.get('debug')) { @@ -589,6 +590,7 @@ export class GitCodeLensProvider implements CodeLensProvider { } const commit = find(blame.commits.values(), c => c.author.name === author) ?? first(blame.commits.values()); + if (commit == null) return lens; switch (lens.desiredCommand) { case CodeLensCommand.CopyRemoteCommitUrl: diff --git a/src/commands/copyShaToClipboard.ts b/src/commands/copyShaToClipboard.ts index 7a88bc0..541ddfa 100644 --- a/src/commands/copyShaToClipboard.ts +++ b/src/commands/copyShaToClipboard.ts @@ -65,7 +65,8 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand { const log = await this.container.git.getLog(repoPath, { limit: 1 }); if (log == null) return; - args.sha = first(log.commits.values()).sha; + args.sha = first(log.commits.values())?.sha; + if (args.sha == null) return; } else if (args.sha == null) { const blameline = editor?.selection.active.line ?? 0; if (blameline < 0) return; diff --git a/src/commands/remoteProviders.ts b/src/commands/remoteProviders.ts index c00165c..bcde509 100644 --- a/src/commands/remoteProviders.ts +++ b/src/commands/remoteProviders.ts @@ -62,7 +62,7 @@ export class ConnectRemoteProviderCommand extends Command { if (repos.size === 0) return false; if (repos.size === 1) { let repo; - [repo, remote] = first(repos); + [repo, remote] = first(repos)!; repoPath = repo.path; } else { const pick = await RepositoryPicker.show( @@ -153,7 +153,7 @@ export class DisconnectRemoteProviderCommand extends Command { if (repos.size === 0) return undefined; if (repos.size === 1) { let repo; - [repo, remote] = first(repos); + [repo, remote] = first(repos)!; repoPath = repo.path; } else { const pick = await RepositoryPicker.show( diff --git a/src/env/node/git/localGitProvider.ts b/src/env/node/git/localGitProvider.ts index 1a5b2d7..11516ca 100644 --- a/src/env/node/git/localGitProvider.ts +++ b/src/env/node/git/localGitProvider.ts @@ -1279,8 +1279,8 @@ export class LocalGitProvider implements GitProvider, Disposable { if (blame == null) return undefined; return { - author: first(blame.authors.values()), - commit: first(blame.commits.values()), + author: first(blame.authors.values())!, + commit: first(blame.commits.values())!, line: blame.lines[editorLine], }; } catch { @@ -1330,8 +1330,8 @@ export class LocalGitProvider implements GitProvider, Disposable { if (blame == null) return undefined; return { - author: first(blame.authors.values()), - commit: first(blame.commits.values()), + author: first(blame.authors.values())!, + commit: first(blame.commits.values())!, line: blame.lines[editorLine], }; } catch { diff --git a/src/system/iterable.ts b/src/system/iterable.ts index eb6a904..ae47f67 100644 --- a/src/system/iterable.ts +++ b/src/system/iterable.ts @@ -107,8 +107,8 @@ export function find(source: Iterable | IterableIterator, predicate: (i return null; } -export function first(source: Iterable | IterableIterator): T { - return source[Symbol.iterator]().next().value as T; +export function first(source: Iterable | IterableIterator): T | undefined { + return source[Symbol.iterator]().next().value as T | undefined; } export function* flatMap( diff --git a/src/views/nodes/branchTrackingStatusNode.ts b/src/views/nodes/branchTrackingStatusNode.ts index 0eceb5a..7af300b 100644 --- a/src/views/nodes/branchTrackingStatusNode.ts +++ b/src/views/nodes/branchTrackingStatusNode.ts @@ -92,7 +92,7 @@ export class BranchTrackingStatusNode extends ViewNode impleme ref: commit.sha, }); if (previousLog != null) { - commits[commits.length - 1] = first(previousLog.commits.values()); + commits[commits.length - 1] = first(previousLog.commits.values())!; } } } else {