From a8b2444df154f6d1ea5e6625c18c67df9dc873a5 Mon Sep 17 00:00:00 2001 From: Sandeep Somavarapu Date: Fri, 24 Jul 2020 13:40:10 +0200 Subject: [PATCH] fix accepting local and remote content during merge --- .../userDataSync/common/keybindingsSync.ts | 72 +++++++++++-------- .../userDataSync/common/settingsSync.ts | 66 +++++++++-------- 2 files changed, 80 insertions(+), 58 deletions(-) diff --git a/src/vs/platform/userDataSync/common/keybindingsSync.ts b/src/vs/platform/userDataSync/common/keybindingsSync.ts index b0a93988b67..8062afd9963 100644 --- a/src/vs/platform/userDataSync/common/keybindingsSync.ts +++ b/src/vs/platform/userDataSync/common/keybindingsSync.ts @@ -177,42 +177,58 @@ export class KeybindingsSynchroniser extends AbstractJsonFileSynchroniser implem }]; } + protected async updateResourcePreview(resourcePreview: IFileResourcePreview, resource: URI, acceptedContent: string | null): Promise { + return { + ...resourcePreview, + acceptedContent, + localChange: isEqual(resource, this.localResource) ? Change.None : Change.Modified, + remoteChange: isEqual(resource, this.remoteResource) ? Change.None : Change.Modified, + }; + } + protected async applyPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, resourcePreviews: IFileResourcePreview[], force: boolean): Promise { let { fileContent, acceptedContent: content, localChange, remoteChange } = resourcePreviews[0]; - if (content !== null) { - if (this.hasErrors(content)) { - throw new UserDataSyncError(localize('errorInvalidSettings', "Unable to sync keybindings because the content in the file is not valid. Please open the file and correct it."), UserDataSyncErrorCode.LocalInvalidContent, this.resource); - } - - if (localChange !== Change.None) { - this.logService.trace(`${this.syncResourceLogLabel}: Updating local keybindings...`); - if (fileContent) { - await this.backupLocal(this.toSyncContent(fileContent.value.toString(), null)); - } - await this.updateLocalFileContent(content, fileContent, force); - this.logService.info(`${this.syncResourceLogLabel}: Updated local keybindings`); - } - - if (remoteChange !== Change.None) { - this.logService.trace(`${this.syncResourceLogLabel}: Updating remote keybindings...`); - const remoteContents = this.toSyncContent(content, remoteUserData.syncData ? remoteUserData.syncData.content : null); - remoteUserData = await this.updateRemoteUserData(remoteContents, force ? null : remoteUserData.ref); - this.logService.info(`${this.syncResourceLogLabel}: Updated remote keybindings`); - } - - // Delete the preview - try { - await this.fileService.del(this.previewResource); - } catch (e) { /* ignore */ } - } else { + if (localChange === Change.None && remoteChange === Change.None) { this.logService.info(`${this.syncResourceLogLabel}: No changes found during synchronizing keybindings.`); } + if (content !== null && this.hasErrors(content)) { + throw new UserDataSyncError(localize('errorInvalidSettings', "Unable to sync keybindings because the content in the file is not valid. Please open the file and correct it."), UserDataSyncErrorCode.LocalInvalidContent, this.resource); + } + + if (localChange !== Change.None) { + this.logService.trace(`${this.syncResourceLogLabel}: Updating local keybindings...`); + if (fileContent) { + await this.backupLocal(this.toSyncContent(fileContent.value.toString(), null)); + } + await this.updateLocalFileContent(content || '[]', fileContent, force); + this.logService.info(`${this.syncResourceLogLabel}: Updated local keybindings`); + } + + if (remoteChange !== Change.None) { + this.logService.trace(`${this.syncResourceLogLabel}: Updating remote keybindings...`); + const remoteContents = this.toSyncContent(content || '[]', remoteUserData.syncData ? remoteUserData.syncData.content : null); + remoteUserData = await this.updateRemoteUserData(remoteContents, force ? null : remoteUserData.ref); + this.logService.info(`${this.syncResourceLogLabel}: Updated remote keybindings`); + } + + // Delete the preview + try { + await this.fileService.del(this.previewResource); + } catch (e) { /* ignore */ } + if (lastSyncUserData?.ref !== remoteUserData.ref) { this.logService.trace(`${this.syncResourceLogLabel}: Updating last synchronized keybindings...`); - const lastSyncContent = content !== null || fileContent !== null ? this.toSyncContent(content !== null ? content : fileContent!.value.toString(), null) : null; - await this.updateLastSyncUserData({ ref: remoteUserData.ref, syncData: lastSyncContent ? { version: remoteUserData.syncData!.version, machineId: remoteUserData.syncData!.machineId, content: lastSyncContent } : null }); + const lastSyncContent = content !== null ? this.toSyncContent(content, null) : null; + await this.updateLastSyncUserData({ + ref: remoteUserData.ref, + syncData: lastSyncContent ? { + version: remoteUserData.syncData ? remoteUserData.syncData.version : this.version, + machineId: remoteUserData.syncData!.machineId, + content: lastSyncContent + } : null + }); this.logService.info(`${this.syncResourceLogLabel}: Updated last synchronized keybindings`); } diff --git a/src/vs/platform/userDataSync/common/settingsSync.ts b/src/vs/platform/userDataSync/common/settingsSync.ts index 87d352779bf..778875415fc 100644 --- a/src/vs/platform/userDataSync/common/settingsSync.ts +++ b/src/vs/platform/userDataSync/common/settingsSync.ts @@ -193,7 +193,7 @@ export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implement previewContent, acceptedResource: this.acceptedResource, acceptedContent, - localChange: hasLocalChanged ? fileContent ? Change.Modified : Change.Added : Change.None, + localChange: hasLocalChanged ? Change.Modified : Change.None, remoteChange: hasRemoteChanged ? Change.Modified : Change.None, hasConflicts, }]; @@ -206,43 +206,49 @@ export class SettingsSynchroniser extends AbstractJsonFileSynchroniser implement const ignoredSettings = await this.getIgnoredSettings(); acceptedContent = updateIgnoredSettings(acceptedContent, resourcePreview.fileContent ? resourcePreview.fileContent.value.toString() : '{}', ignoredSettings, formatUtils); } - return super.updateResourcePreview(resourcePreview, resource, acceptedContent) as Promise; + return { + ...resourcePreview, + acceptedContent, + localChange: isEqual(resource, this.localResource) ? Change.None : Change.Modified, + remoteChange: isEqual(resource, this.remoteResource) ? Change.None : Change.Modified, + }; } protected async applyPreview(remoteUserData: IRemoteUserData, lastSyncUserData: IRemoteUserData | null, resourcePreviews: IFileResourcePreview[], force: boolean): Promise { let { fileContent, acceptedContent: content, localChange, remoteChange } = resourcePreviews[0]; - if (content !== null) { - - this.validateContent(content); - - if (localChange !== Change.None) { - this.logService.trace(`${this.syncResourceLogLabel}: Updating local settings...`); - if (fileContent) { - await this.backupLocal(JSON.stringify(this.toSettingsSyncContent(fileContent.value.toString()))); - } - await this.updateLocalFileContent(content, fileContent, force); - this.logService.info(`${this.syncResourceLogLabel}: Updated local settings`); - } - if (remoteChange !== Change.None) { - const formatUtils = await this.getFormattingOptions(); - // Update ignored settings from remote - const remoteSettingsSyncContent = this.getSettingsSyncContent(remoteUserData); - const ignoredSettings = await this.getIgnoredSettings(content); - content = updateIgnoredSettings(content, remoteSettingsSyncContent ? remoteSettingsSyncContent.settings : '{}', ignoredSettings, formatUtils); - this.logService.trace(`${this.syncResourceLogLabel}: Updating remote settings...`); - remoteUserData = await this.updateRemoteUserData(JSON.stringify(this.toSettingsSyncContent(content)), force ? null : remoteUserData.ref); - this.logService.info(`${this.syncResourceLogLabel}: Updated remote settings`); - } - - // Delete the preview - try { - await this.fileService.del(this.previewResource); - } catch (e) { /* ignore */ } - } else { + if (localChange === Change.None && remoteChange === Change.None) { this.logService.info(`${this.syncResourceLogLabel}: No changes found during synchronizing settings.`); } + content = content !== null ? content : '{}'; + this.validateContent(content); + + if (localChange !== Change.None) { + this.logService.trace(`${this.syncResourceLogLabel}: Updating local settings...`); + if (fileContent) { + await this.backupLocal(JSON.stringify(this.toSettingsSyncContent(fileContent.value.toString()))); + } + await this.updateLocalFileContent(content, fileContent, force); + this.logService.info(`${this.syncResourceLogLabel}: Updated local settings`); + } + + if (remoteChange !== Change.None) { + const formatUtils = await this.getFormattingOptions(); + // Update ignored settings from remote + const remoteSettingsSyncContent = this.getSettingsSyncContent(remoteUserData); + const ignoredSettings = await this.getIgnoredSettings(content); + content = updateIgnoredSettings(content, remoteSettingsSyncContent ? remoteSettingsSyncContent.settings : '{}', ignoredSettings, formatUtils); + this.logService.trace(`${this.syncResourceLogLabel}: Updating remote settings...`); + remoteUserData = await this.updateRemoteUserData(JSON.stringify(this.toSettingsSyncContent(content)), force ? null : remoteUserData.ref); + this.logService.info(`${this.syncResourceLogLabel}: Updated remote settings`); + } + + // Delete the preview + try { + await this.fileService.del(this.previewResource); + } catch (e) { /* ignore */ } + if (lastSyncUserData?.ref !== remoteUserData.ref) { this.logService.trace(`${this.syncResourceLogLabel}: Updating last synchronized settings...`); await this.updateLastSyncUserData(remoteUserData);