Skip to content

Commit 7f167ac

Browse files
authored
Fix unstaging renamed files
Generated-By: PostHog Code Task-Id: 496a1722-3291-4262-9b8b-1dccc2c7a652
1 parent 0f9e76d commit 7f167ac

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { ChangedFile } from "@posthog/shared/domain-types";
2+
import { describe, expect, it } from "vitest";
3+
import { getStageTogglePaths } from "./stageTogglePaths";
4+
5+
describe("getStageTogglePaths", () => {
6+
it.each([
7+
{
8+
name: "uses the current path for a regular file",
9+
file: { path: "current.ts", status: "modified" } satisfies ChangedFile,
10+
expected: ["current.ts"],
11+
},
12+
{
13+
name: "uses both paths for a renamed file",
14+
file: {
15+
path: "new.ts",
16+
originalPath: "old.ts",
17+
status: "renamed",
18+
} satisfies ChangedFile,
19+
expected: ["old.ts", "new.ts"],
20+
},
21+
])("$name", ({ file, expected }) => {
22+
expect(getStageTogglePaths(file)).toEqual(expected);
23+
});
24+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { ChangedFile } from "@posthog/shared/domain-types";
2+
3+
export function getStageTogglePaths(file: ChangedFile): string[] {
4+
if (file.originalPath && file.originalPath !== file.path) {
5+
return [file.originalPath, file.path];
6+
}
7+
8+
return [file.path];
9+
}

packages/ui/src/features/task-detail/hooks/useStageToggle.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useCallback } from "react";
55
import { logger } from "../../../shell/logger";
66
import { invalidateGitWorkingTreeQueries } from "../../git-interaction/gitCacheKeys";
77
import { updateGitCacheFromSnapshot } from "../../git-interaction/utils/updateGitCache";
8+
import { getStageTogglePaths } from "./stageTogglePaths";
89

910
const log = logger.scope("use-stage-toggle");
1011

@@ -21,7 +22,7 @@ export function useStageToggle(repoPath: string | undefined) {
2122
try {
2223
const result = await endpoint.mutateAsync({
2324
directoryPath: repoPath,
24-
paths: [file.originalPath ?? file.path],
25+
paths: getStageTogglePaths(file),
2526
});
2627
updateGitCacheFromSnapshot(queryClient, repoPath, result);
2728
invalidateGitWorkingTreeQueries(repoPath);

0 commit comments

Comments
 (0)