Skip to content

Commit 06be0b7

Browse files
committed
history - prevent unwanted entries from move operation
1 parent 6f834fa commit 06be0b7

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/vs/platform/files/common/files.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,27 @@ export const enum FileOperation {
702702
WRITE
703703
}
704704

705-
export class FileOperationEvent {
705+
export interface IFileOperationEvent {
706+
707+
readonly resource: URI;
708+
readonly operation: FileOperation;
709+
710+
isOperation(operation: FileOperation.DELETE | FileOperation.WRITE): boolean;
711+
isOperation(operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY): this is IFileOperationEventWithMetadata;
712+
}
713+
714+
export interface IFileOperationEventWithMetadata extends IFileOperationEvent {
715+
readonly target: IFileStatWithMetadata;
716+
}
717+
718+
export class FileOperationEvent implements IFileOperationEvent {
706719

707720
constructor(resource: URI, operation: FileOperation.DELETE | FileOperation.WRITE);
708721
constructor(resource: URI, operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY, target: IFileStatWithMetadata);
709722
constructor(readonly resource: URI, readonly operation: FileOperation, readonly target?: IFileStatWithMetadata) { }
710723

711724
isOperation(operation: FileOperation.DELETE | FileOperation.WRITE): boolean;
712-
isOperation(operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY): this is { readonly target: IFileStatWithMetadata };
725+
isOperation(operation: FileOperation.CREATE | FileOperation.MOVE | FileOperation.COPY): this is IFileOperationEventWithMetadata;
713726
isOperation(operation: FileOperation): boolean {
714727
return this.operation === operation;
715728
}

src/vs/workbench/services/workingCopy/common/workingCopyHistoryTracker.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { IWorkingCopySaveEvent, IWorkingCopyService } from 'vs/workbench/service
2323
import { Schemas } from 'vs/base/common/network';
2424
import { ResourceGlobMatcher } from 'vs/workbench/common/resources';
2525
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
26-
import { FileOperation, FileOperationEvent, IFileService } from 'vs/platform/files/common/files';
26+
import { FileOperation, FileOperationEvent, IFileOperationEventWithMetadata, IFileService, IFileStatWithMetadata } from 'vs/platform/files/common/files';
2727

2828
export class WorkingCopyHistoryTracker extends Disposable implements IWorkbenchContribution {
2929

@@ -79,8 +79,8 @@ export class WorkingCopyHistoryTracker extends Disposable implements IWorkbenchC
7979
}
8080

8181
private async onDidRunFileOperation(e: FileOperationEvent): Promise<void> {
82-
if (!e.isOperation(FileOperation.MOVE)) {
83-
return; // only interested in move operations
82+
if (!this.shouldTrackHistoryFromFileOperationEvent(e)) {
83+
return; // return early for working copies we are not interested in
8484
}
8585

8686
const source = e.resource;
@@ -111,7 +111,7 @@ export class WorkingCopyHistoryTracker extends Disposable implements IWorkbenchC
111111
}
112112

113113
private onDidSave(e: IWorkingCopySaveEvent): void {
114-
if (!this.shouldTrackHistory(e)) {
114+
if (!this.shouldTrackHistoryFromSaveEvent(e)) {
115115
return; // return early for working copies we are not interested in
116116
}
117117

@@ -174,28 +174,40 @@ export class WorkingCopyHistoryTracker extends Disposable implements IWorkbenchC
174174
return undefined;
175175
}
176176

177-
private shouldTrackHistory(e: IWorkingCopySaveEvent): e is IStoredFileWorkingCopySaveEvent<IStoredFileWorkingCopyModel> {
177+
private shouldTrackHistoryFromSaveEvent(e: IWorkingCopySaveEvent): e is IStoredFileWorkingCopySaveEvent<IStoredFileWorkingCopyModel> {
178+
if (!isStoredFileWorkingCopySaveEvent(e)) {
179+
return false; // only support working copies that are backed by stored files
180+
}
181+
182+
return this.shouldTrackHistory(e.workingCopy.resource, e.stat);
183+
}
184+
185+
private shouldTrackHistoryFromFileOperationEvent(e: FileOperationEvent): e is IFileOperationEventWithMetadata {
186+
if (!e.isOperation(FileOperation.MOVE)) {
187+
return false; // only interested in move operations
188+
}
189+
190+
return this.shouldTrackHistory(e.target.resource, e.target);
191+
}
192+
193+
private shouldTrackHistory(resource: URI, stat: IFileStatWithMetadata): boolean {
178194
if (
179-
e.workingCopy.resource.scheme !== this.pathService.defaultUriScheme && // track history for all workspace resources
180-
e.workingCopy.resource.scheme !== Schemas.vscodeUserData // track history for all settings
195+
resource.scheme !== this.pathService.defaultUriScheme && // track history for all workspace resources
196+
resource.scheme !== Schemas.vscodeUserData // track history for all settings
181197
) {
182198
return false; // do not support unknown resources
183199
}
184200

185-
if (!isStoredFileWorkingCopySaveEvent(e)) {
186-
return false; // only support working copies that are backed by stored files
187-
}
188-
189-
const configuredMaxFileSizeInBytes = 1024 * this.configurationService.getValue<number>(WorkingCopyHistoryTracker.SETTINGS.SIZE_LIMIT, { resource: e.workingCopy.resource });
190-
if (e.stat.size > configuredMaxFileSizeInBytes) {
201+
const configuredMaxFileSizeInBytes = 1024 * this.configurationService.getValue<number>(WorkingCopyHistoryTracker.SETTINGS.SIZE_LIMIT, { resource });
202+
if (stat.size > configuredMaxFileSizeInBytes) {
191203
return false; // only track files that are not too large
192204
}
193205

194-
if (this.configurationService.getValue(WorkingCopyHistoryTracker.SETTINGS.ENABLED, { resource: e.workingCopy.resource }) === false) {
206+
if (this.configurationService.getValue(WorkingCopyHistoryTracker.SETTINGS.ENABLED, { resource }) === false) {
195207
return false; // do not track when history is disabled
196208
}
197209

198210
// Finally check for exclude setting
199-
return !this.resourceExcludeMatcher.value.matches(e.workingCopy.resource);
211+
return !this.resourceExcludeMatcher.value.matches(resource);
200212
}
201213
}

0 commit comments

Comments
 (0)