fix(web): hide duplicate local commits when PR commits exist (#494)

This commit is contained in:
Carlos Florêncio
2026-03-29 22:11:42 +01:00
committed by GitHub
parent f479393928
commit acd2b9770d
3 changed files with 235 additions and 4 deletions
@@ -0,0 +1,40 @@
import { describe, it, expect } from "vitest";
import { filterUnpushedCommits } from "./changes-panel";
const makeLocal = (sha: string, message = "msg") => ({
commit_sha: sha,
commit_message: message,
insertions: 1,
deletions: 0,
});
const makePR = (sha: string) => ({
sha,
message: "msg",
author_login: "user",
author_date: "2026-03-29T00:00:00Z",
});
describe("filterUnpushedCommits", () => {
it("returns empty when all local commits are in PR", () => {
const local = [makeLocal("abc1234def"), makeLocal("fff9999aaa")];
const pr = [makePR("abc1234def567890"), makePR("fff9999aaa123456")];
expect(filterUnpushedCommits(local, pr)).toEqual([]);
});
it("returns only unpushed commits", () => {
const local = [makeLocal("abc1234def"), makeLocal("fff9999aaa")];
const pr = [makePR("abc1234def567890")];
expect(filterUnpushedCommits(local, pr)).toEqual([makeLocal("fff9999aaa")]);
});
it("returns all local commits when no PR commits exist", () => {
const local = [makeLocal("abc1234def"), makeLocal("fff9999aaa")];
expect(filterUnpushedCommits(local, [])).toEqual(local);
});
it("returns empty when no local commits exist", () => {
const pr = [makePR("abc1234def567890")];
expect(filterUnpushedCommits([], pr)).toEqual([]);
});
});
+23 -4
View File
@@ -152,6 +152,17 @@ function mapPRFilesToChangedFiles(files: PRDiffFile[]): PRChangedFile[] {
});
}
export function filterUnpushedCommits<T extends { commit_sha: string }>(
localCommits: T[],
prCommits: { sha: string }[],
): T[] {
if (prCommits.length === 0) return localCommits;
return localCommits.filter(
(c) =>
!prCommits.some((pr) => pr.sha.startsWith(c.commit_sha) || c.commit_sha.startsWith(pr.sha)),
);
}
function getBaseBranchDisplay(baseBranch: string | undefined): string {
return baseBranch ? baseBranch.replace(/^origin\//, "") : "main";
}
@@ -280,9 +291,11 @@ type TimelineProps = Pick<
| "onForcePush"
>;
function TimelineLocalChanges(props: TimelineProps) {
function TimelineLocalChanges(props: TimelineProps & { hasCommitsToShow?: boolean }) {
const hasCommitsToShow = props.hasCommitsToShow ?? props.hasCommits;
const showStaged = props.hasUnstaged || props.hasStaged;
const showCommits = props.hasStaged || props.hasCommits;
const showCommitsList = props.hasStaged || hasCommitsToShow;
// Only show bulk-action spinners when no individual files are pending
// (per-file ops set pendingStageFiles; bulk ops don't)
const isBulkOp = props.pendingStageFiles.size === 0;
@@ -324,7 +337,7 @@ function TimelineLocalChanges(props: TimelineProps) {
onDiscard={props.dialogs.handleDiscardClick}
/>
)}
{showCommits && (
{showCommitsList && (
<CommitsSection
commits={props.commits}
isLast={false}
@@ -360,7 +373,9 @@ function ChangesPanelTimeline(props: TimelineProps) {
);
}
const hasLocalChanges = props.hasUnstaged || props.hasStaged || props.hasCommits;
const unpushedCommits = filterUnpushedCommits(props.commits, props.prCommits);
const hasUnpushedCommits = unpushedCommits.length > 0;
const hasLocalChanges = props.hasUnstaged || props.hasStaged || hasUnpushedCommits;
return (
<div className="flex flex-col">
@@ -382,7 +397,11 @@ function ChangesPanelTimeline(props: TimelineProps) {
/>
</div>
)}
<TimelineLocalChanges {...props} />
<TimelineLocalChanges
{...props}
commits={unpushedCommits}
hasCommitsToShow={hasUnpushedCommits}
/>
</div>
);
}
@@ -1123,4 +1123,176 @@ test.describe("Git Changes Panel", () => {
// Clean up
git.deleteFile("collapse-unstaged.txt");
});
/**
* Verifies that local commits section is hidden when all commits are already
* in the PR. Only unpushed commits should be shown in the local section.
*/
test("hides local commits section when all commits are in PR", async ({
testPage,
apiClient,
seedData,
backend,
}) => {
const profile = await createStandardProfile(apiClient, "Git PR Dedup Profile");
const task = await apiClient.createTaskWithAgent(
seedData.workspaceId,
"Git PR Dedup Test",
profile.id,
{
description: "Testing PR commit deduplication",
workflow_id: seedData.workflowId,
workflow_step_id: seedData.startStepId,
repository_ids: [seedData.repositoryId],
},
);
const session = await openTaskSession(testPage, "Git PR Dedup Test");
// Set up git helper
const repoDir = path.join(backend.tmpDir, "repos", "e2e-repo");
const gitEnv = {
...process.env,
HOME: backend.tmpDir,
GIT_AUTHOR_NAME: "E2E Test",
GIT_AUTHOR_EMAIL: "e2e@test.local",
GIT_COMMITTER_NAME: "E2E Test",
GIT_COMMITTER_EMAIL: "e2e@test.local",
};
const git = new GitHelper(repoDir, gitEnv);
// Create a commit
git.createFile("pr-dedup.txt", "dedup content");
git.stageFile("pr-dedup.txt");
const sha = git.commit("Commit in PR");
// Click the Changes tab and verify commit appears locally
await session.clickTab("Changes");
await expect(session.changes).toBeVisible({ timeout: 10_000 });
await expect(testPage.getByTestId("commits-section")).toBeVisible({ timeout: 15_000 });
await expect(session.changes.getByText("Commit in PR")).toBeVisible({ timeout: 10_000 });
// Mock a PR that contains the same commit
await apiClient.mockGitHubAssociateTaskPR({
task_id: task.id,
owner: "test-org",
repo: "test-repo",
pr_number: 1,
pr_url: "https://github.com/test-org/test-repo/pull/1",
pr_title: "Test PR",
head_branch: "feature",
base_branch: "main",
author_login: "e2e-test",
});
await apiClient.mockGitHubAddPRCommits("test-org", "test-repo", 1, [
{
sha,
message: "Commit in PR",
author_login: "e2e-test",
author_date: new Date().toISOString(),
},
]);
// Reload to pick up the PR association
await testPage.reload();
await session.waitForLoad();
await session.clickTab("Changes");
await expect(session.changes).toBeVisible({ timeout: 10_000 });
// PR commits section should be visible
await expect(session.prCommitsSection()).toBeVisible({ timeout: 15_000 });
// Local commits section should be hidden (all commits are in the PR)
await expect(testPage.getByTestId("commits-section")).not.toBeVisible({ timeout: 10_000 });
});
/**
* Verifies that only unpushed commits appear in the local section
* when some commits are already in the PR.
*/
test("shows only unpushed commits when some commits are in PR", async ({
testPage,
apiClient,
seedData,
backend,
}) => {
const profile = await createStandardProfile(apiClient, "Git PR Partial Profile");
const task = await apiClient.createTaskWithAgent(
seedData.workspaceId,
"Git PR Partial Test",
profile.id,
{
description: "Testing partial PR commit deduplication",
workflow_id: seedData.workflowId,
workflow_step_id: seedData.startStepId,
repository_ids: [seedData.repositoryId],
},
);
const session = await openTaskSession(testPage, "Git PR Partial Test");
// Set up git helper
const repoDir = path.join(backend.tmpDir, "repos", "e2e-repo");
const gitEnv = {
...process.env,
HOME: backend.tmpDir,
GIT_AUTHOR_NAME: "E2E Test",
GIT_AUTHOR_EMAIL: "e2e@test.local",
GIT_COMMITTER_NAME: "E2E Test",
GIT_COMMITTER_EMAIL: "e2e@test.local",
};
const git = new GitHelper(repoDir, gitEnv);
// Create two commits
git.createFile("pushed.txt", "pushed content");
git.stageFile("pushed.txt");
const pushedSha = git.commit("Pushed commit");
git.createFile("unpushed.txt", "unpushed content");
git.stageFile("unpushed.txt");
const unpushedSha = git.commit("Unpushed commit");
// Mock a PR that only contains the first commit
await apiClient.mockGitHubAssociateTaskPR({
task_id: task.id,
owner: "test-org",
repo: "test-repo",
pr_number: 2,
pr_url: "https://github.com/test-org/test-repo/pull/2",
pr_title: "Partial PR",
head_branch: "feature",
base_branch: "main",
author_login: "e2e-test",
});
await apiClient.mockGitHubAddPRCommits("test-org", "test-repo", 2, [
{
sha: pushedSha,
message: "Pushed commit",
author_login: "e2e-test",
author_date: new Date().toISOString(),
},
]);
// Reload to pick up the PR association
await testPage.reload();
await session.waitForLoad();
await session.clickTab("Changes");
await expect(session.changes).toBeVisible({ timeout: 10_000 });
// PR commits section should show
await expect(session.prCommitsSection()).toBeVisible({ timeout: 15_000 });
// Local commits section should show only the unpushed commit
await expect(testPage.getByTestId("commits-section")).toBeVisible({ timeout: 15_000 });
await expect(session.changes.getByText("Unpushed commit")).toBeVisible({ timeout: 10_000 });
await expect(session.changes.getByText(unpushedSha.slice(0, 7))).toBeVisible({
timeout: 5_000,
});
// The local commits section should only contain the unpushed commit
const commitsList = testPage.getByTestId("commits-list");
await expect(commitsList.locator("li")).toHaveCount(1, { timeout: 5_000 });
});
});