MCPcopy Index your code
hub / github.com/rtfpessoa/diff2html / parse

Function parse

src/diff-parser.ts:55–481  ·  view source on GitHub ↗
(diffInput: string, config: DiffParserConfig = {})

Source from the content-addressed store, hash-verified

53 *
54 */
55export 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 */

Callers 1

Calls 10

startFileFunction · 0.85
getFilenameFunction · 0.85
startBlockFunction · 0.85
getSrcFilenameFunction · 0.85
getExtensionFunction · 0.85
getDstFilenameFunction · 0.85
createLineFunction · 0.85
existHunkHeaderFunction · 0.85
saveBlockFunction · 0.85
saveFileFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…