Skip to content

Commit 2ba3988

Browse files
authored
Git - extract toMultiFileDiffEditorUris (microsoft#203688)
1 parent 536960b commit 2ba3988

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

extensions/git/src/commands.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Git, Stash } from './git';
1313
import { Model } from './model';
1414
import { Repository, Resource, ResourceGroupType } from './repository';
1515
import { applyLineChanges, getModifiedRange, intersectDiffWithRange, invertLineChange, toLineRanges } from './staging';
16-
import { fromGitUri, toGitUri, isGitUri, toMergeUris } from './uri';
16+
import { fromGitUri, toGitUri, isGitUri, toMergeUris, toMultiFileDiffEditorUris } from './uri';
1717
import { dispose, grep, isDefined, isDescendant, pathEquals, relativePath } from './util';
1818
import { GitTimelineItem } from './timelineProvider';
1919
import { ApiRepository } from './api/api1';
@@ -3734,20 +3734,7 @@ export class CommandCenter {
37343734
const isChangeUntracked = !!stashUntrackedFiles.find(f => pathEquals(f, change.uri.fsPath));
37353735
const modifiedUriRef = !isChangeUntracked ? stash.hash : stashUntrackedFilesParentCommit ?? stash.hash;
37363736

3737-
switch (change.status) {
3738-
case Status.INDEX_ADDED:
3739-
resources.push({ originalUri: undefined, modifiedUri: toGitUri(change.uri, modifiedUriRef) });
3740-
break;
3741-
case Status.DELETED:
3742-
resources.push({ originalUri: toGitUri(change.uri, stashFirstParentCommit), modifiedUri: undefined });
3743-
break;
3744-
case Status.INDEX_RENAMED:
3745-
resources.push({ originalUri: toGitUri(change.originalUri, stashFirstParentCommit), modifiedUri: toGitUri(change.uri, modifiedUriRef) });
3746-
break;
3747-
default:
3748-
resources.push({ originalUri: toGitUri(change.uri, stashFirstParentCommit), modifiedUri: toGitUri(change.uri, modifiedUriRef) });
3749-
break;
3750-
}
3737+
resources.push(toMultiFileDiffEditorUris(change, stashFirstParentCommit, modifiedUriRef));
37513738
}
37523739

37533740
commands.executeCommand('_workbench.openMultiDiffEditor', { multiDiffSourceUri, title, resources });
@@ -3868,15 +3855,15 @@ export class CommandCenter {
38683855
}
38693856

38703857
const commit = await repository.getCommit(item.ref);
3871-
const commitFiles = await repository.getCommitFiles(item.ref);
3858+
const commitParentId = commit.parents.length > 0 ? commit.parents[0] : `${commit.hash}^`;
3859+
const changes = await repository.diffBetween(commitParentId, commit.hash);
38723860

38733861
const title = `${item.shortRef} - ${commit.message}`;
38743862
const multiDiffSourceUri = toGitUri(Uri.file(repository.root), item.ref, { scheme: 'git-commit' });
38753863

3876-
const resources: { originalUri: Uri; modifiedUri: Uri }[] = [];
3877-
for (const commitFile of commitFiles) {
3878-
const commitFileUri = Uri.file(path.join(repository.root, commitFile));
3879-
resources.push({ originalUri: toGitUri(commitFileUri, item.previousRef), modifiedUri: toGitUri(commitFileUri, item.ref) });
3864+
const resources: { originalUri: Uri | undefined; modifiedUri: Uri | undefined }[] = [];
3865+
for (const change of changes) {
3866+
resources.push(toMultiFileDiffEditorUris(change, item.previousRef, item.ref));
38803867
}
38813868

38823869
return {
@@ -4073,15 +4060,14 @@ export class CommandCenter {
40734060
}
40744061

40754062
async _viewChanges(repository: Repository, historyItem: SourceControlHistoryItem, multiDiffSourceUri: Uri, title: string): Promise<void> {
4076-
const historyProvider = repository.historyProvider;
4077-
const historyItemParentId = historyItem.parentIds.length > 0 ? historyItem.parentIds[0] : undefined;
4078-
const files = await historyProvider.provideHistoryItemChanges(historyItem.id, historyItemParentId);
4063+
const historyItemParentId = historyItem.parentIds.length > 0 ? historyItem.parentIds[0] : `${historyItem.id}^`;
4064+
const changes = await repository.diffBetween(historyItemParentId, historyItem.id);
40794065

4080-
if (files.length === 0) {
4066+
if (changes.length === 0) {
40814067
return;
40824068
}
40834069

4084-
const resources = files.map(f => ({ originalUri: f.originalUri, modifiedUri: f.modifiedUri }));
4070+
const resources = changes.map(c => toMultiFileDiffEditorUris(c, historyItemParentId, historyItem.id));
40854071

40864072
await commands.executeCommand('_workbench.openMultiDiffEditor', { multiDiffSourceUri, title, resources });
40874073
}

extensions/git/src/repository.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,10 +1652,6 @@ export class Repository implements Disposable {
16521652
return await this.repository.getCommit(ref);
16531653
}
16541654

1655-
async getCommitFiles(ref: string): Promise<string[]> {
1656-
return await this.repository.getCommitFiles(ref);
1657-
}
1658-
16591655
async getCommitCount(range: string): Promise<{ ahead: number; behind: number }> {
16601656
return await this.run(Operation.RevList, () => this.repository.getCommitCount(range));
16611657
}

extensions/git/src/uri.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { Uri } from 'vscode';
7+
import { Change, Status } from './api/git';
78

89
export interface GitUriParams {
910
path: string;
@@ -59,3 +60,16 @@ export function toMergeUris(uri: Uri): { base: Uri; ours: Uri; theirs: Uri } {
5960
theirs: toGitUri(uri, ':3'),
6061
};
6162
}
63+
64+
export function toMultiFileDiffEditorUris(change: Change, originalRef: string, modifiedRef: string): { originalUri: Uri | undefined; modifiedUri: Uri | undefined } {
65+
switch (change.status) {
66+
case Status.INDEX_ADDED:
67+
return { originalUri: undefined, modifiedUri: toGitUri(change.uri, modifiedRef) };
68+
case Status.DELETED:
69+
return { originalUri: toGitUri(change.uri, originalRef), modifiedUri: undefined };
70+
case Status.INDEX_RENAMED:
71+
return { originalUri: toGitUri(change.originalUri, originalRef), modifiedUri: toGitUri(change.uri, modifiedRef) };
72+
default:
73+
return { originalUri: toGitUri(change.uri, originalRef), modifiedUri: toGitUri(change.uri, modifiedRef) };
74+
}
75+
}

0 commit comments

Comments
 (0)