SCM - wire-up the onDidChangeHistoryItemRefs event (#228042)

Wire-up the event
This commit is contained in:
Ladislau Szomoru
2024-09-10 01:51:31 +02:00
committed by GitHub
parent dd9e7ee344
commit d8af24e15d
7 changed files with 177 additions and 38 deletions

View File

@@ -4,21 +4,53 @@
*--------------------------------------------------------------------------------------------*/
import { Disposable, Event, EventEmitter, FileDecoration, FileDecorationProvider, SourceControlHistoryItem, SourceControlHistoryItemChange, SourceControlHistoryItemGroup, SourceControlHistoryOptions, SourceControlHistoryProvider, ThemeIcon, Uri, window, LogOutputChannel, SourceControlHistoryChangeEvent, SourceControlHistoryItemRef, l10n } from 'vscode';
import { Disposable, Event, EventEmitter, FileDecoration, FileDecorationProvider, SourceControlHistoryItem, SourceControlHistoryItemChange, SourceControlHistoryItemGroup, SourceControlHistoryOptions, SourceControlHistoryProvider, ThemeIcon, Uri, window, LogOutputChannel, SourceControlHistoryItemRef, l10n, SourceControlHistoryItemRefsChangeEvent } from 'vscode';
import { Repository, Resource } from './repository';
import { IDisposable, dispose } from './util';
import { IDisposable, deltaHistoryItemRefs, dispose } from './util';
import { toGitUri } from './uri';
import { Branch, LogOptions, RefType } from './api/git';
import { Branch, LogOptions, Ref, RefType } from './api/git';
import { emojify, ensureEmojis } from './emoji';
import { Commit } from './git';
function toSourceControlHistoryItemRef(ref: Ref): SourceControlHistoryItemRef {
switch (ref.type) {
case RefType.RemoteHead:
return {
id: `refs/remotes/${ref.remote}/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? l10n.t('Remote branch at {0}', ref.commit.substring(0, 8)) : undefined,
revision: ref.commit,
icon: new ThemeIcon('cloud'),
category: l10n.t('remote branches')
};
case RefType.Tag:
return {
id: `refs/tags/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? l10n.t('Tag at {0}', ref.commit.substring(0, 8)) : undefined,
revision: ref.commit,
icon: new ThemeIcon('tag'),
category: l10n.t('tags')
};
default:
return {
id: `refs/heads/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? ref.commit.substring(0, 8) : undefined,
revision: ref.commit,
icon: new ThemeIcon('git-branch'),
category: l10n.t('branches')
};
}
}
export class GitHistoryProvider implements SourceControlHistoryProvider, FileDecorationProvider, IDisposable {
private readonly _onDidChangeCurrentHistoryItemGroup = new EventEmitter<void>();
readonly onDidChangeCurrentHistoryItemGroup: Event<void> = this._onDidChangeCurrentHistoryItemGroup.event;
private readonly _onDidChangeHistory = new EventEmitter<SourceControlHistoryChangeEvent>();
readonly onDidChangeHistory: Event<SourceControlHistoryChangeEvent> = this._onDidChangeHistory.event;
private readonly _onDidChangeHistoryItemRefs = new EventEmitter<SourceControlHistoryItemRefsChangeEvent>();
readonly onDidChangeHistoryItemRefs: Event<SourceControlHistoryItemRefsChangeEvent> = this._onDidChangeHistoryItemRefs.event;
private readonly _onDidChangeDecorations = new EventEmitter<Uri[]>();
readonly onDidChangeFileDecorations: Event<Uri[]> = this._onDidChangeDecorations.event;
@@ -30,6 +62,7 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec
this._onDidChangeCurrentHistoryItemGroup.fire();
}
private historyItemRefs: SourceControlHistoryItemRef[] = [];
private historyItemDecorations = new Map<string, FileDecoration>();
private disposables: Disposable[] = [];
@@ -80,6 +113,21 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec
};
this.logger.trace(`[GitHistoryProvider][onDidRunGitStatus] currentHistoryItemGroup: ${JSON.stringify(this.currentHistoryItemGroup)}`);
// Refs (alphabetically)
const refs = await this.repository.getRefs({ sort: 'alphabetically' });
const historyItemRefs = refs.map(ref => toSourceControlHistoryItemRef(ref));
const delta = deltaHistoryItemRefs(this.historyItemRefs, historyItemRefs);
this._onDidChangeHistoryItemRefs.fire(delta);
this.historyItemRefs = historyItemRefs;
const deltaLog = {
added: delta.added.map(ref => ref.id),
modified: delta.modified.map(ref => ref.id),
removed: delta.removed.map(ref => ref.id)
};
this.logger.trace(`[GitHistoryProvider][onDidRunGitStatus] historyItemRefs: ${JSON.stringify(deltaLog)}`);
}
async provideHistoryItemRefs(): Promise<SourceControlHistoryItemRef[]> {
@@ -92,34 +140,13 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec
for (const ref of refs) {
switch (ref.type) {
case RefType.RemoteHead:
remoteBranches.push({
id: `refs/remotes/${ref.remote}/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? l10n.t('Remote branch at {0}', ref.commit.substring(0, 8)) : undefined,
revision: ref.commit,
icon: new ThemeIcon('cloud'),
category: l10n.t('remote branches')
});
remoteBranches.push(toSourceControlHistoryItemRef(ref));
break;
case RefType.Tag:
tags.push({
id: `refs/tags/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? l10n.t('Tag at {0}', ref.commit.substring(0, 8)) : undefined,
revision: ref.commit,
icon: new ThemeIcon('tag'),
category: l10n.t('tags')
});
tags.push(toSourceControlHistoryItemRef(ref));
break;
default:
branches.push({
id: `refs/heads/${ref.name}`,
name: ref.name ?? '',
description: ref.commit ? ref.commit.substring(0, 8) : undefined,
revision: ref.commit,
icon: new ThemeIcon('git-branch'),
category: l10n.t('branches')
});
branches.push(toSourceControlHistoryItemRef(ref));
break;
}
}