* Parse a well-formed unified diff into hunks and lines. * * @param text A unified diff produced by git diff, git log --patch * or any other git plumbing command that produces unified * diffs.
(text: string)
| 397 | * diffs. |
| 398 | */ |
| 399 | public parse(text: string): IRawDiff { |
| 400 | this.text = text |
| 401 | |
| 402 | try { |
| 403 | const headerInfo = this.parseDiffHeader() |
| 404 | |
| 405 | const headerEnd = this.le |
| 406 | const header = this.text.substring(0, headerEnd) |
| 407 | |
| 408 | // empty diff |
| 409 | if (!headerInfo) { |
| 410 | return { |
| 411 | header, |
| 412 | contents: '', |
| 413 | hunks: [], |
| 414 | isBinary: false, |
| 415 | maxLineNumber: 0, |
| 416 | hasHiddenBidiChars: false, |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (headerInfo.isBinary) { |
| 421 | return { |
| 422 | header, |
| 423 | contents: '', |
| 424 | hunks: [], |
| 425 | isBinary: true, |
| 426 | maxLineNumber: 0, |
| 427 | hasHiddenBidiChars: false, |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | const hunks = new Array<DiffHunk>() |
| 432 | let linesConsumed = 0 |
| 433 | let previousHunk: DiffHunk | null = null |
| 434 | |
| 435 | do { |
| 436 | const hunk = this.parseHunk(linesConsumed, hunks.length, previousHunk) |
| 437 | hunks.push(hunk) |
| 438 | previousHunk = hunk |
| 439 | linesConsumed += hunk.lines.length |
| 440 | } while (this.peek()) |
| 441 | |
| 442 | const contents = this.text |
| 443 | .substring(headerEnd + 1, this.le) |
| 444 | // Note that this simply returns a reference to the |
| 445 | // substring if no match is found, it does not create |
| 446 | // a new string instance. |
| 447 | .replace(/\n\\ No newline at end of file/g, '') |
| 448 | |
| 449 | return { |
| 450 | header, |
| 451 | contents, |
| 452 | hunks, |
| 453 | isBinary: headerInfo.isBinary, |
| 454 | maxLineNumber: getLargestLineNumber(hunks), |
| 455 | hasHiddenBidiChars: HiddenBidiCharsRegex.test(text), |
| 456 | } |
no test coverage detected