Git - incoming/outgoing polish (#197877)

* Git - refactor diffBetweenShortStat to return an object with files, insertions, and deletions

* Add statistics label tooltip
This commit is contained in:
Ladislau Szomoru 2023-11-10 02:40:58 +01:00 committed by GitHub
parent 8de9ba22b2
commit 64783a4eb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 23 deletions

View file

@ -1316,15 +1316,23 @@ export class Repository {
return result.stdout.trim();
}
async diffBetweenShortStat(ref1: string, ref2: string): Promise<string> {
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<Change[]> {

View file

@ -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<SourceControlHistoryItem> {
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 };
}

View file

@ -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<string> {
diffBetweenShortStat(ref1: string, ref2: string): Promise<{ files: number; insertions: number; deletions: number }> {
return this.run(Operation.Diff, () => this.repository.diffBetweenShortStat(ref1, ref2));
}

View file

@ -787,11 +787,23 @@ class HistoryItemRenderer implements ICompressibleTreeRenderer<SCMHistoryItemTre
templateData.iconLabel.setLabel(historyItem.label, historyItem.description);
if (historyItem.statistics?.files || historyItem.statistics?.insertions || historyItem.statistics?.deletions) {
const statsLabelTitle: string[] = [];
const filesLabel = historyItem.statistics?.files ? `${historyItem.statistics.files}$(files)` : '';
const additionsLabel = historyItem.statistics?.insertions ? ` ${historyItem.statistics.insertions}$(diff-added)` : '';
const deletionsLabel = historyItem.statistics?.deletions ? ` ${historyItem.statistics.deletions}$(diff-removed)` : '';
if (filesLabel !== '') {
statsLabelTitle.push(`${historyItem.statistics.files} ${historyItem.statistics.files === 1 ? 'file' : 'files'} changed`);
}
templateData.statsLabel.setLabel(`${filesLabel}${additionsLabel}${deletionsLabel}`);
const insertionsLabel = historyItem.statistics?.insertions ? ` ${historyItem.statistics.insertions}$(diff-added)` : '';
if (insertionsLabel !== '') {
statsLabelTitle.push(`${historyItem.statistics.insertions} insertions(+)`);
}
const deletionsLabel = historyItem.statistics?.deletions ? ` ${historyItem.statistics.deletions}$(diff-removed)` : '';
if (deletionsLabel !== '') {
statsLabelTitle.push(`${historyItem.statistics.deletions} deletions(-)`);
}
templateData.statsLabel.setLabel(`${filesLabel}${insertionsLabel}${deletionsLabel}`, undefined, { title: statsLabelTitle.join(', ') });
templateData.statsContainer.style.display = '';
} else {
templateData.statsContainer.style.display = 'none';