Skip to content

Commit 36fb199

Browse files
committed
Revert "fix: Discard actions now respect staged vs unstaged changes and prevent silent data loss [INS-1099] (#9141)"
This reverts commit bf611ff.
1 parent bf611ff commit 36fb199

File tree

3 files changed

+11
-48
lines changed

3 files changed

+11
-48
lines changed

packages/insomnia/src/main/git-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ export const discardChangesAction = async ({
18271827

18281828
const files = changes.unstaged.filter(change => paths.includes(change.path));
18291829

1830-
await GitVCS.discardChanges(files, { discardUnstaged: true });
1830+
await GitVCS.discardChanges(files);
18311831

18321832
await models.gitRepository.update(gitRepository, {
18331833
cachedGitLastCommitTime: Date.now(),

packages/insomnia/src/sync/git/__tests__/git-vcs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ First commit!
319319
],
320320
});
321321
// Discard changes
322-
await GitVCS.discardChanges(status2.unstaged, { discardUnstaged: true });
322+
await GitVCS.discardChanges(status2.unstaged);
323323

324324
const status3 = await GitVCS.status();
325325

@@ -362,7 +362,7 @@ First commit!
362362
const status2 = await GitVCS.status();
363363
// Undo foo1 and foo2, but not foo3
364364
const changesToUndo = status2.unstaged.filter(change => !change.path.includes(foo3Txt));
365-
await GitVCS.discardChanges(changesToUndo, { discardUnstaged: true });
365+
await GitVCS.discardChanges(changesToUndo);
366366
const status3 = await GitVCS.status();
367367
expect(status3).toEqual({
368368
staged: [],

packages/insomnia/src/sync/git/git-vcs.ts

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,57 +1449,20 @@ export class GitVCS {
14491449
}
14501450
}
14511451

1452-
async discardChanges(
1453-
changes: { path: string; status: [git.HeadStatus, git.WorkdirStatus, git.StageStatus] }[],
1454-
options?: { discardStaged?: boolean; discardUnstaged?: boolean },
1455-
) {
1452+
async discardChanges(changes: { path: string; status: [git.HeadStatus, git.WorkdirStatus, git.StageStatus] }[]) {
14561453
for (const change of changes) {
1457-
// If the file didn't exist in HEAD, remove it
1454+
// If the file didn't exist in HEAD, we need to remove it
14581455
if (change.status[0] === 0) {
14591456
await git.remove({ ...this._baseOpts, filepath: change.path });
14601457
// @ts-expect-error -- TSCONVERSION
14611458
await this._baseOpts.fs.promises.unlink(change.path);
14621459
} else {
1463-
// Discard staged changes only
1464-
if (options?.discardStaged) {
1465-
await git.resetIndex({ ...this._baseOpts, filepath: change.path });
1466-
}
1467-
1468-
// Discard unstaged changes only.
1469-
if (options?.discardUnstaged) {
1470-
// Restore workdir from index (staged version)
1471-
// 1. Get staged blob OID
1472-
const statusMatrix = await git.statusMatrix({ ...this._baseOpts });
1473-
const row = statusMatrix.find(([filepath]) => filepath === change.path);
1474-
if (row) {
1475-
const [, , , stageStatusCode] = row;
1476-
if (stageStatusCode !== 0) {
1477-
// 2. Get staged blob content
1478-
const index = await git.listFiles({ ...this._baseOpts });
1479-
if (index.includes(change.path)) {
1480-
// Use fileStatus logic to get staged content:
1481-
const { stage } = await this.fileStatus(change.path);
1482-
if (stage !== null) {
1483-
// 3. Write staged content to workdir
1484-
// @ts-expect-error -- TSCONVERSION
1485-
await this._baseOpts.fs.promises.writeFile(change.path, stage, 'utf8');
1486-
}
1487-
}
1488-
}
1489-
}
1490-
// Do NOT touch the index (staged changes are preserved)
1491-
continue;
1492-
}
1493-
1494-
// Default: discard both (current behavior)
1495-
if (!options?.discardStaged && !options?.discardUnstaged) {
1496-
await git.checkout({
1497-
...this._baseOpts,
1498-
force: true,
1499-
ref: await this.getCurrentBranch(),
1500-
filepaths: [convertToPosixSep(change.path)],
1501-
});
1502-
}
1460+
await git.checkout({
1461+
...this._baseOpts,
1462+
force: true,
1463+
ref: await this.getCurrentBranch(),
1464+
filepaths: [convertToPosixSep(change.path)],
1465+
});
15031466
}
15041467
}
15051468
}

0 commit comments

Comments
 (0)