|
5 | 5 |
|
6 | 6 | import * as nodePath from 'path'; |
7 | 7 | import * as vscode from 'vscode'; |
8 | | -import type { Branch, Repository } from '../api/api'; |
| 8 | +import type { Branch, Change, Repository } from '../api/api'; |
9 | 9 | import { GitApiImpl, GitErrorCodes, Status } from '../api/api1'; |
10 | 10 | import { openDescription } from '../commands'; |
11 | 11 | import { DiffChangeType, DiffHunk, parsePatch, splitIntoSmallerHunks } from '../common/diffHunk'; |
@@ -782,21 +782,50 @@ export class ReviewManager extends Disposable { |
782 | 782 | let hasError: boolean = false; |
783 | 783 | let diff: DiffHunk[] = []; |
784 | 784 | const convertedFiles: vscode.Uri[] = []; |
| 785 | + |
| 786 | + const convertOneSmallHunk = async (changeFile: Change, hunk: DiffHunk) => { |
| 787 | + try { |
| 788 | + await this._reviewCommentController?.createSuggestionsFromChanges(changeFile.uri, this.convertDiffHunkToSuggestion(hunk)); |
| 789 | + convertedFiles.push(changeFile.uri); |
| 790 | + } catch (e) { |
| 791 | + hasError = true; |
| 792 | + } |
| 793 | + }; |
| 794 | + |
| 795 | + const getDiffFromChange = async (changeFile: Change) => { |
| 796 | + if (!resourceStrings.includes(changeFile.uri.toString()) || (changeFile.status !== Status.MODIFIED)) { |
| 797 | + return; |
| 798 | + } |
| 799 | + return parsePatch(await this._folderRepoManager.repository.diffWithHEAD(changeFile.uri.fsPath)).map(hunk => splitIntoSmallerHunks(hunk)).flat(); |
| 800 | + }; |
| 801 | + |
785 | 802 | await vscode.window.withProgress({ location: vscode.ProgressLocation.Window, title: 'Converting changes to suggestions' }, async () => { |
786 | | - await Promise.all(this._folderRepoManager.repository.state.workingTreeChanges.map(async changeFile => { |
787 | | - if (!resourceStrings.includes(changeFile.uri.toString()) || (changeFile.status !== Status.MODIFIED)) { |
788 | | - return; |
| 803 | + // We need to create one suggestion first. This let's us ensure that only one review will be created. |
| 804 | + let i = 0; |
| 805 | + for (; (convertedFiles.length === 0) && (i < this._folderRepoManager.repository.state.workingTreeChanges.length); i++) { |
| 806 | + const changeFile = this._folderRepoManager.repository.state.workingTreeChanges[i]; |
| 807 | + const diff = await getDiffFromChange(changeFile); |
| 808 | + if (diff) { |
| 809 | + for (const hunk of diff) { |
| 810 | + await convertOneSmallHunk(changeFile, hunk); |
| 811 | + } |
789 | 812 | } |
790 | | - diff = parsePatch(await this._folderRepoManager.repository.diffWithHEAD(changeFile.uri.fsPath)).map(hunk => splitIntoSmallerHunks(hunk)).flat(); |
791 | | - await Promise.allSettled(diff.map(async hunk => { |
792 | | - try { |
793 | | - await this._reviewCommentController?.createSuggestionsFromChanges(changeFile.uri, this.convertDiffHunkToSuggestion(hunk)); |
794 | | - convertedFiles.push(changeFile.uri); |
795 | | - } catch (e) { |
796 | | - hasError = true; |
| 813 | + } |
| 814 | + |
| 815 | + // If we have already created a suggestion, we can create the rest in parallel |
| 816 | + const promises: Promise<void>[] = []; |
| 817 | + for (; i < this._folderRepoManager.repository.state.workingTreeChanges.length; i++) { |
| 818 | + const changeFile = this._folderRepoManager.repository.state.workingTreeChanges[i]; |
| 819 | + promises.push(getDiffFromChange(changeFile).then(async (diff) => { |
| 820 | + if (diff) { |
| 821 | + await Promise.allSettled(diff.map(async hunk => { |
| 822 | + return convertOneSmallHunk(changeFile, hunk); |
| 823 | + })); |
797 | 824 | } |
798 | 825 | })); |
799 | | - })); |
| 826 | + } |
| 827 | + |
| 828 | + await Promise.all(promises); |
800 | 829 | }); |
801 | 830 | if (!hasError) { |
802 | 831 | const checkoutAllFilesResponse = vscode.l10n.t('Reset all changes'); |
|
0 commit comments