Skip to content

Commit

Permalink
Insomnia-Sync: Do not add unused keys to staged changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gatzjames committed Jun 17, 2024
1 parent fd8f12e commit cfdc831
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/insomnia/src/sync/vcs/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseModel | null>) {
const stagable: StageEntry[] = [];
const stateMap = generateStateMap(state);
const candidateMap = generateCandidateMap(candidates);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/sync/vcs/vcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class VCS {
const state = snapshot ? snapshot.state : [];
const unstaged: Record<DocumentKey, StageEntry> = {};

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];

Expand Down

0 comments on commit cfdc831

Please sign in to comment.