1- import { parseDiffFromFile , type FileDiffMetadata } from "@pierre/diffs"
1+ import { parseDiffFromFile , parsePatchFiles , type FileDiffMetadata } from "@pierre/diffs"
22import { formatPatch , parsePatch , structuredPatch } from "diff"
33import type { SnapshotFileDiff , VcsFileDiff } from "@opencode-ai/sdk/v2"
44
@@ -34,6 +34,8 @@ function patch(diff: ReviewDiff) {
3434 const afterLines : Array < { text : string ; newline : boolean } > = [ ]
3535 let previous : "-" | "+" | " " | undefined
3636
37+ const patchIsPartial = patch . hunks . every ( ( h ) => h . oldStart > 1 )
38+
3739 for ( const hunk of patch . hunks ) {
3840 for ( const line of hunk . lines ) {
3941 if ( line . startsWith ( "\\" ) ) {
@@ -67,9 +69,10 @@ function patch(diff: ReviewDiff) {
6769 before : beforeLines . map ( ( line ) => line . text + ( line . newline ? "\n" : "" ) ) . join ( "" ) ,
6870 after : afterLines . map ( ( line ) => line . text + ( line . newline ? "\n" : "" ) ) . join ( "" ) ,
6971 patch : diff . patch ,
72+ patchIsPartial,
7073 }
7174 } catch {
72- return { before : "" , after : "" , patch : diff . patch }
75+ return { before : "" , after : "" , patch : diff . patch , patchIsPartial : false }
7376 }
7477 }
7578 return {
@@ -86,27 +89,32 @@ function patch(diff: ReviewDiff) {
8689 { context : Number . MAX_SAFE_INTEGER } ,
8790 ) ,
8891 ) ,
92+ patchIsPartial : false ,
8993 }
9094}
9195
92- function file ( file : string , patch : string , before : string , after : string ) {
96+ function file ( file : string , patch : string , before : string , after : string , partial = false ) {
9397 const hit = cache . get ( patch )
9498 if ( hit ) return hit
9599
96- const value = parseDiffFromFile ( { name : file , contents : before } , { name : file , contents : after } )
100+ let value : FileDiffMetadata | undefined
101+ if ( partial ) value = parsePatchFiles ( patch ) [ 0 ] ?. files [ 0 ]
102+ if ( value === undefined ) value = parseDiffFromFile ( { name : file , contents : before } , { name : file , contents : after } )
103+
97104 cache . set ( patch , value )
98105 return value
99106}
100107
101108export function normalize ( diff : ReviewDiff ) : ViewDiff {
102109 const next = patch ( diff )
110+ const fileDiff = file ( diff . file , next . patch , next . before , next . after , next . patchIsPartial )
103111 return {
104112 file : diff . file ,
105113 patch : next . patch ,
106114 additions : diff . additions ,
107115 deletions : diff . deletions ,
108116 status : diff . status ,
109- fileDiff : file ( diff . file , next . patch , next . before , next . after ) ,
117+ fileDiff,
110118 }
111119}
112120
0 commit comments