Skip to content

Commit 3863843

Browse files
committed
feat(patches): remove file from patch
1 parent fbb2c86 commit 3863843

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
"title": "Remove Patch",
163163
"category": "Electron Build Tools"
164164
},
165+
{
166+
"command": "electron-build-tools.patches.removeFileFromPatch",
167+
"title": "Remove File from Patch",
168+
"category": "Electron Build Tools"
169+
},
165170
{
166171
"command": "electron-build-tools.patches.search",
167172
"title": "Search Patches",
@@ -732,6 +737,10 @@
732737
"command": "electron-build-tools.patches.remove",
733738
"when": "false"
734739
},
740+
{
741+
"command": "electron-build-tools.patches.removeFileFromPatch",
742+
"when": "false"
743+
},
735744
{
736745
"command": "electron-build-tools.patches.search",
737746
"when": "electron-build-tools:active"
@@ -868,6 +877,11 @@
868877
"when": "view == electron-build-tools:patches && viewItem == patch",
869878
"group": "7_modification"
870879
},
880+
{
881+
"command": "electron-build-tools.patches.removeFileFromPatch",
882+
"when": "view == electron-build-tools:patches && viewItem == file-in-patch",
883+
"group": "1_modification"
884+
},
871885
{
872886
"command": "electron-build-tools.removeConfig",
873887
"when": "view == electron-build-tools:configs && viewItem =~ /^active-config$|^config$/"

src/commands/patches.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ export function registerPatchesCommands(
174174
);
175175
},
176176
),
177+
vscode.commands.registerCommand(
178+
`${commandPrefix}.patches.removeFileFromPatch`,
179+
(patchFileTreeItem: FileInPatchTreeItem) => {
180+
const uri = patchFileTreeItem.resourceUri.with({
181+
scheme: virtualPatchFsScheme,
182+
});
183+
184+
return vscode.workspace.fs.delete(uri);
185+
},
186+
),
177187
vscode.commands.registerCommand(
178188
`${commandPrefix}.removePullRequestPatch`,
179189
(treeItem: PullRequestTreeItem) => {

src/fileSystemProvider.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class ElectronFileSystemProvider implements vscode.FileSystemProvider {
5959
throw new vscode.FileSystemError("Method not implemented.");
6060
}
6161

62-
delete(): void {
62+
delete(_uri: vscode.Uri): void {
6363
throw new vscode.FileSystemError("Method not implemented.");
6464
}
6565

@@ -73,6 +73,26 @@ export class ElectronPatchFileSystemProvider extends ElectronFileSystemProvider
7373
super();
7474
}
7575

76+
async delete(uri: vscode.Uri): Promise<void> {
77+
const queryParams = new URLSearchParams(uri.query);
78+
const patchFileUri = vscode.Uri.parse(queryParams.get("patch")!, true);
79+
80+
const patchDirectory = vscode.Uri.file(path.dirname(patchFileUri.fsPath));
81+
const cwd =
82+
await this.patchesProvider.getCheckoutDirectoryForPatchDirectory(
83+
patchDirectory,
84+
);
85+
86+
// Update the file so that its start and end blob IDs are the same,
87+
// which will result in an empty diff and the file removed from the patch
88+
await this.updateFileInPatch(
89+
patchFileUri,
90+
uri,
91+
cwd,
92+
queryParams.get("blobIdA")!,
93+
);
94+
}
95+
7696
async writeFile(uri: vscode.Uri, content: Uint8Array): Promise<void> {
7797
const queryParams = new URLSearchParams(uri.query);
7898
const patchFileUri = vscode.Uri.parse(queryParams.get("patch")!, true);
@@ -88,6 +108,15 @@ export class ElectronPatchFileSystemProvider extends ElectronFileSystemProvider
88108
Buffer.from(content).toString("utf8"),
89109
);
90110

111+
await this.updateFileInPatch(patchFileUri, uri, cwd, newBlobId);
112+
}
113+
114+
private async updateFileInPatch(
115+
patchFileUri: vscode.Uri,
116+
uri: vscode.Uri,
117+
cwd: vscode.Uri,
118+
newBlobId: string,
119+
) {
91120
// Parse the existing patch file to get the metadata
92121
const patchContents = (
93122
await vscode.workspace.fs.readFile(patchFileUri)

0 commit comments

Comments
 (0)