Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 105 additions & 4 deletions src/renderer/src/components/right-sidebar/ChecksPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ import {
buildChecksPanelGitStatusContextKey,
readChecksPanelPublishActionGitStatus,
readChecksPanelGitStatusSnapshot,
readChecksPanelRefreshGitIdentitySnapshot,
hasChecksPanelGitStatusBranchChanged,
shouldClearChecksPanelGitStatusSnapshot,
shouldCoalesceChecksPanelGitStatusSnapshotRefresh,
shouldCommitChecksPanelGitStatusSnapshot,
Expand Down Expand Up @@ -397,6 +399,7 @@ export default function ChecksPanel(): React.JSX.Element {
const setRightSidebarOpen = useAppStore((s) => s.setRightSidebarOpen)
const setRightSidebarTab = useAppStore((s) => s.setRightSidebarTab)
const updateWorktreeMeta = useAppStore((s) => s.updateWorktreeMeta)
const updateWorktreeGitIdentity = useAppStore((s) => s.updateWorktreeGitIdentity)
const openModal = useAppStore((s) => s.openModal)

// Why: the sidebar stays mounted when closed (for performance). Gate
Expand Down Expand Up @@ -1293,6 +1296,17 @@ export default function ChecksPanel(): React.JSX.Element {
}
void (async () => {
const status = await getRuntimeGitStatus(context)
if (
!stale &&
shouldCommitChecksPanelGitStatusSnapshot(panelContextKeyRef.current, requestContextKey)
) {
// Why: the Checks tab can be the only visible git surface; commit
// branch identity before branch-scoped upstream refresh can fail.
updateWorktreeGitIdentity(activeWorktreeId, {
head: status.head,
branch: status.branch ?? (status.head ? null : undefined)
})
}
let freshRemoteStatus = status.upstreamStatus
if (activeWorktreePushTarget) {
freshRemoteStatus = await getRuntimeGitUpstreamStatus(context, activeWorktreePushTarget)
Expand All @@ -1314,7 +1328,11 @@ export default function ChecksPanel(): React.JSX.Element {
setGitStatusSnapshot({
contextKey: requestContextKey,
hasUncommittedChanges: status.entries.length > 0,
remoteStatus
remoteStatus,
gitIdentity: {
head: status.head,
branch: status.branch ?? (status.head ? null : undefined)
}
})
}
})
Expand Down Expand Up @@ -1375,7 +1393,8 @@ export default function ChecksPanel(): React.JSX.Element {
repoConnectionId,
remoteStatusInvalidation,
runtimeEnvironmentId,
sshConnectionStatus
sshConnectionStatus,
updateWorktreeGitIdentity
])

useEffect(() => {
Expand Down Expand Up @@ -1843,7 +1862,6 @@ export default function ChecksPanel(): React.JSX.Element {
const refreshProvider = isGitLabReviewContext ? 'gitlab' : 'github'
let refreshOutcome = 'started'
setIsRefreshing(true)
setGitStatusRefreshNonce((value) => value + 1)
recordChecksPanelPRRefreshBreadcrumb({
event: 'start',
provider: refreshProvider,
Expand All @@ -1857,6 +1875,81 @@ export default function ChecksPanel(): React.JSX.Element {
refreshState: prCacheKey ? useAppStore.getState().prRefreshStates[prCacheKey] : null
})
try {
if (activeWorktreeId && activeWorktreePath && !isFolder) {
const snapshotIdentity = readChecksPanelRefreshGitIdentitySnapshot({
snapshot: gitStatusSnapshot,
contextKey: panelContextKey,
currentBranch: branch
})
if (snapshotIdentity.kind === 'changed') {
updateWorktreeGitIdentity(activeWorktreeId, {
head: snapshotIdentity.head,
branch: snapshotIdentity.branch
})
// Why: this click discovered a terminal branch switch. Let the
// branch-keyed render/effects restart instead of refreshing old PR data.
refreshOutcome = 'branch-changed'
return
}
try {
const statusContext = {
settings: ownerSettings,
worktreeId: activeWorktreeId,
worktreePath: activeWorktreePath,
connectionId: activeConnectionId ?? undefined
}
const status = await getRuntimeGitStatus(statusContext)
const observedBranch = status.branch ?? (status.head ? null : undefined)
updateWorktreeGitIdentity(activeWorktreeId, {
head: status.head,
branch: observedBranch
})
if (
observedBranch !== undefined &&
hasChecksPanelGitStatusBranchChanged({
observedBranch,
currentBranch: branch
})
) {
// Why: this click discovered a terminal branch switch. Let the
// branch-keyed render/effects restart instead of refreshing old PR data.
refreshOutcome = 'branch-changed'
return
}
let freshRemoteStatus = status.upstreamStatus
if (activeWorktreePushTarget) {
freshRemoteStatus = await getRuntimeGitUpstreamStatus(
statusContext,
activeWorktreePushTarget
)
} else if (
!freshRemoteStatus ||
(freshRemoteStatus.ahead > 0 &&
freshRemoteStatus.behind > 0 &&
freshRemoteStatus.behindCommitsArePatchEquivalent === undefined)
) {
freshRemoteStatus = await getRuntimeGitUpstreamStatus(statusContext)
}
if (
isCurrentRequest() &&
shouldCommitChecksPanelGitStatusSnapshot(panelContextKeyRef.current, panelContextKey)
) {
// Why: the explicit Refresh click already paid for this status read;
// commit it so empty-state Publish/Create eligibility is fresh.
setGitStatusSnapshot({
contextKey: panelContextKey,
hasUncommittedChanges: status.entries.length > 0,
remoteStatus: freshRemoteStatus,
gitIdentity: {
head: status.head,
branch: observedBranch
}
})
}
} catch (error) {
console.warn('[ChecksPanel] pre-refresh git identity refresh failed', error)
}
}
if (isGitLabReviewContext) {
const refreshedReview = await refreshHostedReviewCard(fetchHostedReviewForBranch, {
repoPath: repo.path,
Expand Down Expand Up @@ -2038,7 +2131,10 @@ export default function ChecksPanel(): React.JSX.Element {
}, [
repo,
branch,
activeConnectionId,
activeWorktreeId,
activeWorktreePath,
activeWorktreePushTarget,
activeGitLabReview,
prNumber,
pr?.checksStatus,
Expand All @@ -2053,13 +2149,18 @@ export default function ChecksPanel(): React.JSX.Element {
linkedBitbucketPR,
linkedGiteaPR,
linkedGitLabMR,
isFolder,
isGitLabReviewContext,
gitStatusSnapshot,
panelContextKey,
fetchPRForBranch,
fetchPRChecks,
fetchPRComments,
fetchHostedReviewForBranch,
expireGitHubPRRefreshState,
isCurrentAsyncResult
isCurrentAsyncResult,
ownerSettings,
updateWorktreeGitIdentity
])

const handleEntryRefresh = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { describe, expect, it } from 'vitest'
import {
buildChecksPanelGitStatusContextKey,
readChecksPanelPublishActionGitStatus,
readChecksPanelRefreshGitIdentitySnapshot,
readChecksPanelGitStatusSnapshot,
hasChecksPanelGitStatusBranchChanged,
shouldClearChecksPanelGitStatusSnapshot,
shouldCoalesceChecksPanelGitStatusSnapshotRefresh,
shouldCommitChecksPanelGitStatusSnapshot,
Expand All @@ -17,6 +19,10 @@ const SNAPSHOT: ChecksPanelGitStatusSnapshot = {
hasUpstream: false,
ahead: 0,
behind: 0
},
gitIdentity: {
head: 'abc123',
branch: 'refs/heads/feature/checks'
}
}

Expand Down Expand Up @@ -188,6 +194,82 @@ describe('readChecksPanelPublishActionGitStatus', () => {
})
})

describe('readChecksPanelRefreshGitIdentitySnapshot', () => {
it('treats refs/heads display differences as the same branch identity', () => {
expect(
readChecksPanelRefreshGitIdentitySnapshot({
snapshot: SNAPSHOT,
contextKey: SNAPSHOT.contextKey,
currentBranch: 'feature/checks'
})
).toEqual({ kind: 'same' })
})

it('reports missing when the snapshot is from a different execution context', () => {
expect(
readChecksPanelRefreshGitIdentitySnapshot({
snapshot: SNAPSHOT,
contextKey: 'runtime:env-2::repo::worktree::branch',
currentBranch: 'feature/checks'
})
).toEqual({ kind: 'missing' })
})

it('reports changed when the observed branch is a different branch', () => {
expect(
readChecksPanelRefreshGitIdentitySnapshot({
snapshot: {
...SNAPSHOT,
gitIdentity: {
head: 'def456',
branch: 'refs/heads/feature/next'
}
},
contextKey: SNAPSHOT.contextKey,
currentBranch: 'feature/checks'
})
).toEqual({
kind: 'changed',
head: 'def456',
branch: 'refs/heads/feature/next'
})
})

it('reports missing when the snapshot lacks branch/head identity', () => {
expect(
readChecksPanelRefreshGitIdentitySnapshot({
snapshot: {
contextKey: SNAPSHOT.contextKey,
hasUncommittedChanges: false,
remoteStatus: undefined
},
contextKey: SNAPSHOT.contextKey,
currentBranch: 'feature/checks'
})
).toEqual({ kind: 'missing' })
})
})

describe('hasChecksPanelGitStatusBranchChanged', () => {
it('does not treat refs/heads display differences as branch changes', () => {
expect(
hasChecksPanelGitStatusBranchChanged({
observedBranch: 'refs/heads/feature/checks',
currentBranch: 'feature/checks'
})
).toBe(false)
})

it('treats detached HEAD as changed from the rendered branch', () => {
expect(
hasChecksPanelGitStatusBranchChanged({
observedBranch: null,
currentBranch: 'feature/checks'
})
).toBe(true)
})
})

describe('shouldCommitChecksPanelGitStatusSnapshot', () => {
it('suppresses stale status refresh completions from an older execution boundary', () => {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,30 @@ export type ChecksPanelGitStatusSnapshot = {
contextKey: string
hasUncommittedChanges: boolean
remoteStatus: GitUpstreamStatus | undefined
gitIdentity?: {
head?: string
branch?: string | null
}
}

export type ChecksPanelGitStatusInputs = {
hasUncommittedChanges: boolean | undefined
remoteStatus: GitUpstreamStatus | undefined
}

export type ChecksPanelRefreshGitIdentitySnapshot =
| {
kind: 'missing'
}
| {
kind: 'same'
}
| {
kind: 'changed'
head?: string
branch: string | null
}

export function buildChecksPanelGitStatusContextKey(
input: ChecksPanelGitStatusContextInput
): string {
Expand Down Expand Up @@ -118,3 +135,44 @@ export function readChecksPanelPublishActionGitStatus(input: {
remoteStatus: input.fallbackRemoteStatus
}
}

function canonicalBranchIdentity(branch: string | null | undefined): string {
return (branch ?? '').replace(/^refs\/heads\//, '').trim()
}

export function readChecksPanelRefreshGitIdentitySnapshot(input: {
snapshot: ChecksPanelGitStatusSnapshot | null
contextKey: string
currentBranch: string
}): ChecksPanelRefreshGitIdentitySnapshot {
if (
!input.snapshot ||
input.snapshot.contextKey !== input.contextKey ||
!input.snapshot.gitIdentity ||
input.snapshot.gitIdentity.branch === undefined
) {
return { kind: 'missing' }
}

if (
canonicalBranchIdentity(input.snapshot.gitIdentity.branch) ===
canonicalBranchIdentity(input.currentBranch)
) {
return { kind: 'same' }
}

return {
kind: 'changed',
head: input.snapshot.gitIdentity.head,
branch: input.snapshot.gitIdentity.branch
}
}

export function hasChecksPanelGitStatusBranchChanged(input: {
observedBranch: string | null | undefined
currentBranch: string
}): boolean {
return (
canonicalBranchIdentity(input.observedBranch) !== canonicalBranchIdentity(input.currentBranch)
)
}
Loading
Loading