(diff: ChangeObjectPlusLines[] | undefined)
| 240 | } |
| 241 | |
| 242 | function diffLinesResultToPatch(diff: ChangeObjectPlusLines[] | undefined) { |
| 243 | // STEP 1: Build up the patch with no "\ No newline at end of file" lines and with the arrays |
| 244 | // of lines containing trailing newline characters. We'll tidy up later... |
| 245 | |
| 246 | if(!diff) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | diff.push({value: '', lines: []}); // Append an empty value to make cleanup easier |
| 251 | |
| 252 | function contextLines(lines: string[]) { |
| 253 | return lines.map(function(entry) { return ' ' + entry; }); |
| 254 | } |
| 255 | |
| 256 | const hunks = []; |
| 257 | let oldRangeStart = 0, newRangeStart = 0, curRange: string[] = [], |
| 258 | oldLine = 1, newLine = 1; |
| 259 | for (let i = 0; i < diff.length; i++) { |
| 260 | const current = diff[i], |
| 261 | lines = current.lines || splitLines(current.value); |
| 262 | current.lines = lines; |
| 263 | |
| 264 | if (current.added || current.removed) { |
| 265 | // If we have previous context, start with that |
| 266 | if (!oldRangeStart) { |
| 267 | const prev = diff[i - 1]; |
| 268 | oldRangeStart = oldLine; |
| 269 | newRangeStart = newLine; |
| 270 | |
| 271 | if (prev) { |
| 272 | curRange = context > 0 ? contextLines(prev.lines!.slice(-context)) : []; |
| 273 | oldRangeStart -= curRange.length; |
| 274 | newRangeStart -= curRange.length; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Output our changes |
| 279 | for (const line of lines) { |
| 280 | curRange.push((current.added ? '+' : '-') + line); |
| 281 | } |
| 282 | |
| 283 | // Track the updated file position |
| 284 | if (current.added) { |
| 285 | newLine += lines.length; |
| 286 | } else { |
| 287 | oldLine += lines.length; |
| 288 | } |
| 289 | } else { |
| 290 | // Identical context lines. Track line changes |
| 291 | if (oldRangeStart) { |
| 292 | // Close out any changes that have been output (or join overlapping) |
| 293 | if (lines.length <= context * 2 && i < diff.length - 2) { |
| 294 | // Overlapping |
| 295 | for (const line of contextLines(lines)) { |
| 296 | curRange.push(line); |
| 297 | } |
| 298 | } else { |
| 299 | // end the range and output |
no test coverage detected