|
| 1 | +import path from "node:path"; |
| 2 | + |
| 3 | +import * as vscode from "vscode"; |
| 4 | + |
| 5 | +import { type ElectronPatchesProvider } from "../../views/patches"; |
| 6 | + |
| 7 | +async function fileExists(uri: vscode.Uri): Promise<boolean> { |
| 8 | + try { |
| 9 | + await vscode.workspace.fs.stat(uri); |
| 10 | + return true; |
| 11 | + } catch { |
| 12 | + return false; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +export async function listPatches( |
| 17 | + chromiumRoot: vscode.Uri, |
| 18 | + electronRoot: vscode.Uri, |
| 19 | + patchesProvider: ElectronPatchesProvider, |
| 20 | + request: vscode.ChatRequest, |
| 21 | + stream: vscode.ChatResponseStream, |
| 22 | + token: vscode.CancellationToken, |
| 23 | +) { |
| 24 | + // Find the file the user is asking about |
| 25 | + const filename = request.prompt.trim(); |
| 26 | + let patchedFile: vscode.Uri; |
| 27 | + |
| 28 | + const absoluteFileUri = vscode.Uri.file(filename); |
| 29 | + const chromiumRelativeFileUri = vscode.Uri.joinPath(chromiumRoot, filename); |
| 30 | + |
| 31 | + if (await fileExists(absoluteFileUri)) { |
| 32 | + patchedFile = absoluteFileUri; |
| 33 | + } else if (await fileExists(chromiumRelativeFileUri)) { |
| 34 | + patchedFile = chromiumRelativeFileUri; |
| 35 | + } else { |
| 36 | + stream.markdown("File not found"); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const patches = await vscode.workspace.findFiles( |
| 41 | + new vscode.RelativePattern( |
| 42 | + vscode.Uri.joinPath(electronRoot, "patches"), |
| 43 | + "**/*.patch", |
| 44 | + ), |
| 45 | + null, |
| 46 | + undefined, |
| 47 | + token, |
| 48 | + ); |
| 49 | + |
| 50 | + const matchingPatches = new Map<string, string[]>(); |
| 51 | + |
| 52 | + for (const patch of patches) { |
| 53 | + const patchDirectory = vscode.Uri.file(path.dirname(patch.fsPath)); |
| 54 | + const checkoutDirectory = |
| 55 | + await patchesProvider.getCheckoutDirectoryForPatchDirectory( |
| 56 | + patchDirectory, |
| 57 | + ); |
| 58 | + const relativeFilename = path.relative( |
| 59 | + checkoutDirectory.fsPath, |
| 60 | + patchedFile.fsPath, |
| 61 | + ); |
| 62 | + |
| 63 | + if (relativeFilename.startsWith("..")) { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + const contents = (await vscode.workspace.fs.readFile(patch)).toString(); |
| 68 | + if ( |
| 69 | + contents.includes( |
| 70 | + `diff --git a/${relativeFilename} b/${relativeFilename}`, |
| 71 | + ) |
| 72 | + ) { |
| 73 | + const patchDirectoryName = path.basename(patchDirectory.fsPath); |
| 74 | + if (!matchingPatches.has(patchDirectoryName)) { |
| 75 | + matchingPatches.set(patchDirectoryName, []); |
| 76 | + } |
| 77 | + matchingPatches |
| 78 | + .get(patchDirectoryName)! |
| 79 | + .push(path.basename(patch.fsPath)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (matchingPatches.size === 0) { |
| 84 | + stream.markdown("No patches found"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + stream.markdown("Found the following patches for "); |
| 89 | + stream.anchor(patchedFile); |
| 90 | + stream.markdown(":\n"); |
| 91 | + |
| 92 | + // Output the final file tree |
| 93 | + stream.filetree( |
| 94 | + [ |
| 95 | + { |
| 96 | + name: "patches", |
| 97 | + children: Array.from(matchingPatches.entries()).map( |
| 98 | + ([patchDirectory, patches]) => { |
| 99 | + return { |
| 100 | + name: patchDirectory, |
| 101 | + children: patches.map((patch) => ({ name: patch })), |
| 102 | + }; |
| 103 | + }, |
| 104 | + ), |
| 105 | + }, |
| 106 | + ], |
| 107 | + electronRoot, |
| 108 | + ); |
| 109 | +} |
0 commit comments