Git - extract toMultiFileDiffEditorUris (#203688)

This commit is contained in:
Ladislau Szomoru 2024-01-29 13:53:28 +01:00 committed by GitHub
parent 536960bf36
commit 2ba398845f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 29 deletions

View file

@ -13,7 +13,7 @@ import { Git, Stash } from './git';
import { Model } from './model';
import { Repository, Resource, ResourceGroupType } from './repository';
import { applyLineChanges, getModifiedRange, intersectDiffWithRange, invertLineChange, toLineRanges } from './staging';
import { fromGitUri, toGitUri, isGitUri, toMergeUris } from './uri';
import { fromGitUri, toGitUri, isGitUri, toMergeUris, toMultiFileDiffEditorUris } from './uri';
import { dispose, grep, isDefined, isDescendant, pathEquals, relativePath } from './util';
import { GitTimelineItem } from './timelineProvider';
import { ApiRepository } from './api/api1';
@ -3734,20 +3734,7 @@ export class CommandCenter {
const isChangeUntracked = !!stashUntrackedFiles.find(f => pathEquals(f, change.uri.fsPath));
const modifiedUriRef = !isChangeUntracked ? stash.hash : stashUntrackedFilesParentCommit ?? stash.hash;
switch (change.status) {
case Status.INDEX_ADDED:
resources.push({ originalUri: undefined, modifiedUri: toGitUri(change.uri, modifiedUriRef) });
break;
case Status.DELETED:
resources.push({ originalUri: toGitUri(change.uri, stashFirstParentCommit), modifiedUri: undefined });
break;
case Status.INDEX_RENAMED:
resources.push({ originalUri: toGitUri(change.originalUri, stashFirstParentCommit), modifiedUri: toGitUri(change.uri, modifiedUriRef) });
break;
default:
resources.push({ originalUri: toGitUri(change.uri, stashFirstParentCommit), modifiedUri: toGitUri(change.uri, modifiedUriRef) });
break;
}
resources.push(toMultiFileDiffEditorUris(change, stashFirstParentCommit, modifiedUriRef));
}
commands.executeCommand('_workbench.openMultiDiffEditor', { multiDiffSourceUri, title, resources });
@ -3868,15 +3855,15 @@ export class CommandCenter {
}
const commit = await repository.getCommit(item.ref);
const commitFiles = await repository.getCommitFiles(item.ref);
const commitParentId = commit.parents.length > 0 ? commit.parents[0] : `${commit.hash}^`;
const changes = await repository.diffBetween(commitParentId, commit.hash);
const title = `${item.shortRef} - ${commit.message}`;
const multiDiffSourceUri = toGitUri(Uri.file(repository.root), item.ref, { scheme: 'git-commit' });
const resources: { originalUri: Uri; modifiedUri: Uri }[] = [];
for (const commitFile of commitFiles) {
const commitFileUri = Uri.file(path.join(repository.root, commitFile));
resources.push({ originalUri: toGitUri(commitFileUri, item.previousRef), modifiedUri: toGitUri(commitFileUri, item.ref) });
const resources: { originalUri: Uri | undefined; modifiedUri: Uri | undefined }[] = [];
for (const change of changes) {
resources.push(toMultiFileDiffEditorUris(change, item.previousRef, item.ref));
}
return {
@ -4073,15 +4060,14 @@ export class CommandCenter {
}
async _viewChanges(repository: Repository, historyItem: SourceControlHistoryItem, multiDiffSourceUri: Uri, title: string): Promise<void> {
const historyProvider = repository.historyProvider;
const historyItemParentId = historyItem.parentIds.length > 0 ? historyItem.parentIds[0] : undefined;
const files = await historyProvider.provideHistoryItemChanges(historyItem.id, historyItemParentId);
const historyItemParentId = historyItem.parentIds.length > 0 ? historyItem.parentIds[0] : `${historyItem.id}^`;
const changes = await repository.diffBetween(historyItemParentId, historyItem.id);
if (files.length === 0) {
if (changes.length === 0) {
return;
}
const resources = files.map(f => ({ originalUri: f.originalUri, modifiedUri: f.modifiedUri }));
const resources = changes.map(c => toMultiFileDiffEditorUris(c, historyItemParentId, historyItem.id));
await commands.executeCommand('_workbench.openMultiDiffEditor', { multiDiffSourceUri, title, resources });
}

View file

@ -1652,10 +1652,6 @@ export class Repository implements Disposable {
return await this.repository.getCommit(ref);
}
async getCommitFiles(ref: string): Promise<string[]> {
return await this.repository.getCommitFiles(ref);
}
async getCommitCount(range: string): Promise<{ ahead: number; behind: number }> {
return await this.run(Operation.RevList, () => this.repository.getCommitCount(range));
}

View file

@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { Uri } from 'vscode';
import { Change, Status } from './api/git';
export interface GitUriParams {
path: string;
@ -59,3 +60,16 @@ export function toMergeUris(uri: Uri): { base: Uri; ours: Uri; theirs: Uri } {
theirs: toGitUri(uri, ':3'),
};
}
export function toMultiFileDiffEditorUris(change: Change, originalRef: string, modifiedRef: string): { originalUri: Uri | undefined; modifiedUri: Uri | undefined } {
switch (change.status) {
case Status.INDEX_ADDED:
return { originalUri: undefined, modifiedUri: toGitUri(change.uri, modifiedRef) };
case Status.DELETED:
return { originalUri: toGitUri(change.uri, originalRef), modifiedUri: undefined };
case Status.INDEX_RENAMED:
return { originalUri: toGitUri(change.originalUri, originalRef), modifiedUri: toGitUri(change.uri, modifiedRef) };
default:
return { originalUri: toGitUri(change.uri, originalRef), modifiedUri: toGitUri(change.uri, modifiedRef) };
}
}