( hunk: Hunk, readFile: (path: string) => Promise<string>, )
| 149 | * @returns The file change result |
| 150 | */ |
| 151 | export async function processHunk( |
| 152 | hunk: Hunk, |
| 153 | readFile: (path: string) => Promise<string>, |
| 154 | ): Promise<ApplyPatchFileChange> { |
| 155 | switch (hunk.type) { |
| 156 | case "AddFile": |
| 157 | return { |
| 158 | type: "add", |
| 159 | path: hunk.path, |
| 160 | newContent: hunk.contents, |
| 161 | } |
| 162 | |
| 163 | case "DeleteFile": { |
| 164 | const content = await readFile(hunk.path) |
| 165 | return { |
| 166 | type: "delete", |
| 167 | path: hunk.path, |
| 168 | originalContent: content, |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | case "UpdateFile": { |
| 173 | const originalContent = await readFile(hunk.path) |
| 174 | const newContent = applyChunksToContent(originalContent, hunk.path, hunk.chunks) |
| 175 | return { |
| 176 | type: "update", |
| 177 | path: hunk.path, |
| 178 | movePath: hunk.movePath ?? undefined, |
| 179 | originalContent, |
| 180 | newContent, |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Process all hunks in a patch. |
no test coverage detected