diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 096a1b4b8f3..913fc1f2a93 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -3145,7 +3145,7 @@ export class CommandCenter { const sourceCommit = sourceRef.ref.commit; try { - const changes = await repository.diffTrees(sourceCommit, historyItem.id); + const changes = await repository.diffBetween2(sourceCommit, historyItem.id); if (changes.length === 0) { window.showInformationMessage(l10n.t('The selected references have no differences.')); @@ -3203,7 +3203,7 @@ export class CommandCenter { } try { - const changes = await repository.diffTrees(ref1.id, ref2.id); + const changes = await repository.diffBetween2(ref1.id, ref2.id); if (changes.length === 0) { window.showInformationMessage(l10n.t('There are no changes between "{0}" and "{1}".', ref1.name, ref2.name)); @@ -4897,7 +4897,7 @@ export class CommandCenter { const commit = await repository.getCommit(item.ref); const commitParentId = commit.parents.length > 0 ? commit.parents[0] : await repository.getEmptyTree(); - const changes = await repository.diffTrees(commitParentId, commit.hash); + const changes = await repository.diffBetween2(commitParentId, commit.hash); const resources = changes.map(c => toMultiFileDiffEditorUris(c, commitParentId, commit.hash)); const title = `${item.shortRef} - ${subject(commit.message)}`; @@ -5171,7 +5171,7 @@ export class CommandCenter { const multiDiffSourceUri = Uri.from({ scheme: 'scm-history-item', path: `${repository.root}/${historyItemParentId}..${historyItemId}` }); - const changes = await repository.diffTrees(historyItemParentId, historyItemId); + const changes = await repository.diffBetween2(historyItemParentId, historyItemId); const resources = changes.map(c => toMultiFileDiffEditorUris(c, historyItemParentId, historyItemId)); const reveal = revealUri ? { modifiedUri: toGitUri(revealUri, historyItemId) } : undefined; diff --git a/extensions/git/src/decorationProvider.ts b/extensions/git/src/decorationProvider.ts index 763d4aaa751..ea4f031e0f9 100644 --- a/extensions/git/src/decorationProvider.ts +++ b/extensions/git/src/decorationProvider.ts @@ -257,7 +257,7 @@ class GitIncomingChangesFileDecorationProvider implements FileDecorationProvider return []; } - const changes = await this.repository.diffBetween(ancestor, currentHistoryItemRemoteRef.id); + const changes = await this.repository.diffBetween2(ancestor, currentHistoryItemRemoteRef.id); return changes; } catch (err) { return []; diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 98fbc032ef8..2b0e2d321ee 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -1620,7 +1620,7 @@ export class Repository { diffWithHEAD(path?: string | undefined): Promise; async diffWithHEAD(path?: string | undefined): Promise { if (!path) { - return await this.diffFiles(false); + return await this.diffFiles(undefined, { cached: false }); } const args = ['diff', '--', this.sanitizeRelativePath(path)]; @@ -1633,7 +1633,7 @@ export class Repository { diffWith(ref: string, path?: string | undefined): Promise; async diffWith(ref: string, path?: string): Promise { if (!path) { - return await this.diffFiles(false, ref); + return await this.diffFiles(ref, { cached: false }); } const args = ['diff', ref, '--', this.sanitizeRelativePath(path)]; @@ -1646,7 +1646,7 @@ export class Repository { diffIndexWithHEAD(path?: string | undefined): Promise; async diffIndexWithHEAD(path?: string): Promise { if (!path) { - return await this.diffFiles(true); + return await this.diffFiles(undefined, { cached: true }); } const args = ['diff', '--cached', '--', this.sanitizeRelativePath(path)]; @@ -1659,7 +1659,7 @@ export class Repository { diffIndexWith(ref: string, path?: string | undefined): Promise; async diffIndexWith(ref: string, path?: string): Promise { if (!path) { - return await this.diffFiles(true, ref); + return await this.diffFiles(ref, { cached: true }); } const args = ['diff', '--cached', ref, '--', this.sanitizeRelativePath(path)]; @@ -1679,7 +1679,7 @@ export class Repository { async diffBetween(ref1: string, ref2: string, path?: string): Promise { const range = `${ref1}...${ref2}`; if (!path) { - return await this.diffFiles(false, range); + return await this.diffFiles(range, { cached: false }); } const args = ['diff', range, '--', this.sanitizeRelativePath(path)]; @@ -1688,48 +1688,26 @@ export class Repository { return result.stdout.trim(); } - 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 { files: 0, insertions: 0, deletions: 0 }; - } - - return parseGitDiffShortStat(result.stdout.trim()); + async diffBetween2(ref1: string, ref2: string, options: { similarityThreshold?: number; symmetric?: boolean }): Promise { + const range = options.symmetric ? `${ref1}...${ref2}` : `${ref1}..${ref2}`; + return await this.diffFiles(range, { cached: false, similarityThreshold: options.similarityThreshold }); } - private async diffFiles(cached: boolean, ref?: string): Promise { + private async diffFiles(ref: string | undefined, options: { cached: boolean; similarityThreshold?: number }): Promise { const args = ['diff', '--name-status', '-z', '--diff-filter=ADMR']; - if (cached) { + + if (options.cached) { args.push('--cached'); } + if (options.similarityThreshold) { + args.push(`--find-renames=${options.similarityThreshold}%`); + } + if (ref) { args.push(ref); } - const gitResult = await this.exec(args); - if (gitResult.exitCode) { - return []; - } - - return parseGitChanges(this.repositoryRoot, gitResult.stdout); - } - - async diffTrees(treeish1: string, treeish2?: string, options?: { similarityThreshold?: number }): Promise { - const args = ['diff-tree', '-r', '--name-status', '-z', '--diff-filter=ADMR']; - - if (options?.similarityThreshold) { - args.push(`--find-renames=${options.similarityThreshold}%`); - } - - args.push(treeish1); - - if (treeish2) { - args.push(treeish2); - } - args.push('--'); const gitResult = await this.exec(args); diff --git a/extensions/git/src/historyProvider.ts b/extensions/git/src/historyProvider.ts index cefdd2eda76..1ed44906ab3 100644 --- a/extensions/git/src/historyProvider.ts +++ b/extensions/git/src/historyProvider.ts @@ -339,7 +339,7 @@ export class GitHistoryProvider implements SourceControlHistoryProvider, FileDec const historyItemChangesUri: Uri[] = []; const historyItemChanges: SourceControlHistoryItemChange[] = []; - const changes = await this.repository.diffTrees(historyItemParentId, historyItemId); + const changes = await this.repository.diffBetween2(historyItemParentId, historyItemId); for (const change of changes) { const historyItemUri = change.uri.with({ diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 6db96628cc7..12516966e3d 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -1231,15 +1231,11 @@ export class Repository implements Disposable { return this.run(Operation.Diff, () => this.repository.diffBetween(ref1, ref2, path)); } - diffBetweenShortStat(ref1: string, ref2: string): Promise<{ files: number; insertions: number; deletions: number }> { - return this.run(Operation.Diff, () => this.repository.diffBetweenShortStat(ref1, ref2)); - } - - diffTrees(treeish1: string, treeish2?: string): Promise { + diffBetween2(ref1: string, ref2: string): Promise { const scopedConfig = workspace.getConfiguration('git', Uri.file(this.root)); const similarityThreshold = scopedConfig.get('similarityThreshold', 50); - return this.run(Operation.Diff, () => this.repository.diffTrees(treeish1, treeish2, { similarityThreshold })); + return this.run(Operation.Diff, () => this.repository.diffBetween2(ref1, ref2, { similarityThreshold })); } getMergeBase(ref1: string, ref2: string, ...refs: string[]): Promise {