Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion src/renderer/src/components/PullRequestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ import {
createCombinedDiffSectionIndexMap,
handleCombinedDiffFileTreeNavigation
} from '@/components/editor/CombinedDiffFileTree'
import {
buildSourceControlTree,
collectSourceControlTreeFileEntriesInOrder
} from '@/components/right-sidebar/source-control-tree'
import {
getDiffSectionEstimatedHeight,
isIntrinsicHeightImageDiff
Expand Down Expand Up @@ -2259,7 +2263,12 @@ function PRFilesCombinedDiffViewer({
if (entriesCacheRef.current?.signature === diffEntrySignature) {
return entriesCacheRef.current.entries
}
const nextEntries = files.map(gitHubPRFileToBranchEntry)
// Why: the file tree renders in source-control DFS order (directories first,
// then path-sorted files); order the diff sections identically so the list
// and the diff detail scroll match, like GitHub's Files changed view.
const nextEntries = collectSourceControlTreeFileEntriesInOrder(
buildSourceControlTree('combined-commit', files.map(gitHubPRFileToBranchEntry))
)
entriesCacheRef.current = {
signature: diffEntrySignature,
entries: nextEntries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
buildSourceControlTree,
applyGitStatusEntryAreasToSourceControlTree,
collectSourceControlTreeFileEntries,
collectSourceControlTreeFileEntriesInOrder,
compactSourceControlTree,
flattenSourceControlTree,
namespaceSourceControlTreeDirectoryKeys
Expand Down Expand Up @@ -85,6 +86,44 @@ describe('buildSourceControlTree', () => {
])
})

it('orders file entries to match the file tree the PR view actually renders', () => {
// Why: the PR "Files changed" diff sections order via
// collectSourceControlTreeFileEntriesInOrder, while the file tree renders via
// compactSourceControlTree(buildSourceControlTree(...)) + flatten (see
// CombinedDiffFileTree.buildBranchRows). Assert both leaf orders match so the
// list and diff stay in sync — including that compaction preserves leaf order.
const entries: GitBranchChangeEntry[] = [
{ path: 'README.md', status: 'modified' },
{ path: 'src/renderer/index.ts', status: 'modified' },
{ path: 'src/renderer/components/Button.tsx', status: 'added' },
{ path: 'src/main/app.ts', status: 'modified' },
{ path: 'docs/guide.md', status: 'modified' }
]

// Mirror CombinedDiffFileTree.buildBranchRows exactly (compact + flatten).
const renderedTreeFileOrder = flattenSourceControlTree(
compactSourceControlTree(buildSourceControlTree('combined-commit', entries)),
new Set()
)
.filter((node) => node.type === 'file')
.map((node) => node.path)

const sectionOrder = collectSourceControlTreeFileEntriesInOrder(
buildSourceControlTree('combined-commit', entries)
).map((item) => item.path)

// The diff section order must equal what the file tree renders.
expect(sectionOrder).toEqual(renderedTreeFileOrder)
// API input order is reordered into directory-first, path-sorted tree order.
expect(sectionOrder).toEqual([
'docs/guide.md',
'src/main/app.ts',
'src/renderer/components/Button.tsx',
'src/renderer/index.ts',
'README.md'
])
})

it('groups committed branch entries with branch-scoped directory keys', () => {
const entries: GitBranchChangeEntry[] = [
{ path: 'packages/app/src/index.ts', status: 'modified' },
Expand Down
18 changes: 18 additions & 0 deletions src/renderer/src/components/right-sidebar/source-control-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ export function applyGitStatusEntryAreasToSourceControlTree(
return nodes.map(applyEntryArea)
}

/**
* Flattens source-control tree roots into their file entries in tree render
* order — depth-first, directories before files, path-sorted within each level.
*
* Why: the file list and the PR "Files changed" diff sections must share one
* ordering. Deriving the section order from the same tree the list renders keeps
* the two views in sync (GitHub-web behavior).
*
* @param nodes - Top-level source-control tree nodes (from `buildSourceControlTree`).
* @returns The file entries in the order their rows appear in the tree.
*/
export function collectSourceControlTreeFileEntriesInOrder<
Entry extends SourceControlTreeEntry,
Area extends string
>(nodes: SourceControlTreeNode<Entry, Area>[]): Entry[] {
return nodes.flatMap((node) => collectSourceControlTreeFileEntries(node))
}

export function collectSourceControlTreeFileEntries<
Entry extends SourceControlTreeEntry,
Area extends string
Expand Down
Loading