From cfdc831028b236a3674715b544706775a80abdc8 Mon Sep 17 00:00:00 2001 From: gatzjames Date: Mon, 17 Jun 2024 15:22:45 +0200 Subject: [PATCH] Insomnia-Sync: Do not add unused keys to staged changes --- packages/insomnia/src/sync/vcs/util.ts | 24 +++++++++++++++++++++++- packages/insomnia/src/sync/vcs/vcs.ts | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/insomnia/src/sync/vcs/util.ts b/packages/insomnia/src/sync/vcs/util.ts index 2a37d8e2968..c22c91541fc 100644 --- a/packages/insomnia/src/sync/vcs/util.ts +++ b/packages/insomnia/src/sync/vcs/util.ts @@ -322,7 +322,7 @@ export function stateDelta( return result; } -export function getStagable(state: SnapshotState, candidates: StatusCandidate[]) { +export async function getStagable(state: SnapshotState, candidates: StatusCandidate[], getPreviousDocument: (key: string) => Promise) { const stagable: StageEntry[] = []; const stateMap = generateStateMap(state); const candidateMap = generateCandidateMap(candidates); @@ -358,6 +358,28 @@ export function getStagable(state: SnapshotState, candidates: StatusCandidate[]) if (entry && candidate) { const { document, name } = candidate; + + const prevDocument = await getPreviousDocument(key); + // Filter out keys that don't exist in the current document and have a falsy value in the new document + if (prevDocument) { + for (const key of Object.keys(document)) { + const keyNotExistsInPrevDocument = !prevDocument.hasOwnProperty(key); + const keyExistsInDocument = document.hasOwnProperty(key); + const keyIsEmptyInDocument = !document[key as keyof BaseModel]; + + const shouldIgnoreKey = keyNotExistsInPrevDocument && keyExistsInDocument && keyIsEmptyInDocument; + + if (shouldIgnoreKey) { + delete document[key as keyof BaseModel]; + } + } + } + + console.log({ + prevDocument, + document, + }); + const { hash: blobId, content: blobContent } = hashDocument(document); if (entry.blob !== blobId) { diff --git a/packages/insomnia/src/sync/vcs/vcs.ts b/packages/insomnia/src/sync/vcs/vcs.ts index cb444e2112a..8a87a3ccf9d 100644 --- a/packages/insomnia/src/sync/vcs/vcs.ts +++ b/packages/insomnia/src/sync/vcs/vcs.ts @@ -198,7 +198,7 @@ export class VCS { const state = snapshot ? snapshot.state : []; const unstaged: Record = {}; - for (const entry of getStagable(state, candidates)) { + for (const entry of await getStagable(state, candidates, key => this.blobFromLastSnapshot(key))) { const { key } = entry; const stageEntry = stage[key];