(diffInput: string, config: DiffParserConfig = {})
| 53 | * |
| 54 | */ |
| 55 | export function parse(diffInput: string, config: DiffParserConfig = {}): DiffFile[] { |
| 56 | const files: DiffFile[] = []; |
| 57 | let currentFile: DiffFile | null = null; |
| 58 | let currentBlock: DiffBlock | null = null; |
| 59 | let oldLine: number | null = null; |
| 60 | let oldLine2: number | null = null; // Used for combined diff |
| 61 | let newLine: number | null = null; |
| 62 | |
| 63 | let possibleOldName: string | null = null; |
| 64 | let possibleNewName: string | null = null; |
| 65 | |
| 66 | /* Diff Header */ |
| 67 | const oldFileNameHeader = '--- '; |
| 68 | const newFileNameHeader = '+++ '; |
| 69 | const hunkHeaderPrefix = '@@'; |
| 70 | |
| 71 | /* Diff */ |
| 72 | const oldMode = /^old mode (\d{6})/; |
| 73 | const newMode = /^new mode (\d{6})/; |
| 74 | const deletedFileMode = /^deleted file mode (\d{6})/; |
| 75 | const newFileMode = /^new file mode (\d{6})/; |
| 76 | |
| 77 | const copyFrom = /^copy from "?(.+)"?/; |
| 78 | const copyTo = /^copy to "?(.+)"?/; |
| 79 | |
| 80 | const renameFrom = /^rename from "?(.+)"?/; |
| 81 | const renameTo = /^rename to "?(.+)"?/; |
| 82 | |
| 83 | const similarityIndex = /^similarity index (\d+)%/; |
| 84 | const dissimilarityIndex = /^dissimilarity index (\d+)%/; |
| 85 | const index = /^index ([\da-z]+)\.\.([\da-z]+)\s*(\d{6})?/; |
| 86 | |
| 87 | const binaryFiles = /^Binary files (.*) and (.*) differ/; |
| 88 | const binaryDiff = /^GIT binary patch/; |
| 89 | |
| 90 | /* Combined Diff */ |
| 91 | const combinedIndex = /^index ([\da-z]+),([\da-z]+)\.\.([\da-z]+)/; |
| 92 | const combinedMode = /^mode (\d{6}),(\d{6})\.\.(\d{6})/; |
| 93 | const combinedNewFile = /^new file mode (\d{6})/; |
| 94 | const combinedDeletedFile = /^deleted file mode (\d{6}),(\d{6})/; |
| 95 | |
| 96 | const diffLines = diffInput |
| 97 | .replace(/\\ No newline at end of file/g, '') |
| 98 | .replace(/\r\n?/g, '\n') |
| 99 | .split('\n'); |
| 100 | |
| 101 | /* Add previous block(if exists) before start a new file */ |
| 102 | function saveBlock(): void { |
| 103 | if (currentBlock !== null && currentFile !== null) { |
| 104 | currentFile.blocks.push(currentBlock); |
| 105 | currentBlock = null; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Add previous file(if exists) before start a new one |
| 111 | * if it has name (to avoid binary files errors) |
| 112 | */ |
no test coverage detected
searching dependent graphs…