From 64783a4eb9e896feadb2cbbd767b7ba8013980ee Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Fri, 10 Nov 2023 02:40:58 +0100 Subject: [PATCH] Git - incoming/outgoing polish (#197877) * Git - refactor diffBetweenShortStat to return an object with files, insertions, and deletions * Add statistics label tooltip --- extensions/git/src/git.ts | 14 +++++++++++--- extensions/git/src/historyProvider.ts | 18 ++---------------- extensions/git/src/repository.ts | 2 +- .../contrib/scm/browser/scmViewPane.ts | 18 +++++++++++++++--- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index f072af20935..5dde9b26da9 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -1316,15 +1316,23 @@ export class Repository { return result.stdout.trim(); } - async diffBetweenShortStat(ref1: string, ref2: string): Promise { + async diffBetweenShortStat(ref1: string, ref2: string): Promise<{ files: number; insertions: number; deletions: number }> { const args = ['diff', '--shortstat', `${ref1}...${ref2}`]; const result = await this.exec(args); if (result.exitCode) { - return ''; + return { files: 0, insertions: 0, deletions: 0 }; } - return result.stdout.trim(); + const regex = /(\d+) files? changed(?:, (\d+) insertions\(\+\))?(?:, (\d+) deletions\(-\))?/; + const matches = result.stdout.trim().match(regex); + + if (!matches) { + return { files: 0, insertions: 0, deletions: 0 }; + } + + const [, files, insertions = undefined, deletions = undefined] = matches; + return { files: parseInt(files), insertions: parseInt(insertions ?? '0'), deletions: parseInt(deletions ?? '0') }; } private async diffFiles(cached: boolean, ref?: string): Promise { diff --git a/extensions/git/src/historyProvider.ts b/extensions/git/src/historyProvider.ts index 60e6cddb24a..0256ffdcc1c 100644 --- a/extensions/git/src/historyProvider.ts +++ b/extensions/git/src/historyProvider.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ -import { Disposable, Event, EventEmitter, FileDecoration, FileDecorationProvider, SourceControlActionButton, SourceControlHistoryItem, SourceControlHistoryItemChange, SourceControlHistoryItemGroup, SourceControlHistoryOptions, SourceControlHistoryProvider, ThemeIcon, Uri, window, l10n, SourceControlHistoryItemStatistics } from 'vscode'; +import { Disposable, Event, EventEmitter, FileDecoration, FileDecorationProvider, SourceControlActionButton, SourceControlHistoryItem, SourceControlHistoryItemChange, SourceControlHistoryItemGroup, SourceControlHistoryOptions, SourceControlHistoryProvider, ThemeIcon, Uri, window, l10n } from 'vscode'; import { Repository, Resource } from './repository'; import { IDisposable } from './util'; import { toGitUri } from './uri'; @@ -195,21 +195,7 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec } private async getSummaryHistoryItem(ref1: string, ref2: string): Promise { - let statistics: SourceControlHistoryItemStatistics | undefined; - - const diffShortStat = await this.repository.diffBetweenShortStat(ref1, ref2); - const regex = /(\d+) files? changed(?:, (\d+) insertions\(\+\))?(?:, (\d+) deletions\(-\))?/; - const matches = diffShortStat.match(regex); - - if (matches) { - const [, files, insertions = undefined, deletions = undefined] = matches; - statistics = { - files: parseInt(files), - insertions: parseInt(insertions ?? '0'), - deletions: parseInt(deletions ?? '0') - }; - } - + const statistics = await this.repository.diffBetweenShortStat(ref1, ref2); return { id: `${ref1}..${ref2}`, parentIds: [], icon: new ThemeIcon('files'), label: l10n.t('All Changes'), statistics }; } diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index cebecfd239a..d4000aec87b 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -1185,7 +1185,7 @@ export class Repository implements Disposable { return this.run(Operation.Diff, () => this.repository.diffBetween(ref1, ref2, path)); } - diffBetweenShortStat(ref1: string, ref2: string): Promise { + diffBetweenShortStat(ref1: string, ref2: string): Promise<{ files: number; insertions: number; deletions: number }> { return this.run(Operation.Diff, () => this.repository.diffBetweenShortStat(ref1, ref2)); } diff --git a/src/vs/workbench/contrib/scm/browser/scmViewPane.ts b/src/vs/workbench/contrib/scm/browser/scmViewPane.ts index 6829069c7e4..c451a544065 100644 --- a/src/vs/workbench/contrib/scm/browser/scmViewPane.ts +++ b/src/vs/workbench/contrib/scm/browser/scmViewPane.ts @@ -787,11 +787,23 @@ class HistoryItemRenderer implements ICompressibleTreeRenderer