MCPcopy Create free account
hub / github.com/ds300/patch-package / parsePatchLines

Function parsePatchLines

src/patch/parse.ts:150–299  ·  view source on GitHub ↗
(
  lines: string[],
  { supportLegacyDiffs }: { supportLegacyDiffs: boolean },
)

Source from the content-addressed store, hash-verified

148}
149
150function parsePatchLines(
151 lines: string[],
152 { supportLegacyDiffs }: { supportLegacyDiffs: boolean },
153): FileDeets[] {
154 const result: FileDeets[] = []
155 let currentFilePatch: FileDeets = emptyFilePatch()
156 let state: State = "parsing header"
157 let currentHunk: Hunk | null = null
158 let currentHunkMutationPart: PatchMutationPart | null = null
159 let hunkStartLineIndex = 0
160
161 function commitHunk(i: number) {
162 if (currentHunk) {
163 if (currentHunkMutationPart) {
164 currentHunk.parts.push(currentHunkMutationPart)
165 currentHunkMutationPart = null
166 }
167 currentHunk.source = lines.slice(hunkStartLineIndex, i).join("\n")
168 currentFilePatch.hunks!.push(currentHunk)
169 currentHunk = null
170 }
171 }
172
173 function commitFilePatch(i: number) {
174 commitHunk(i)
175 result.push(currentFilePatch)
176 currentFilePatch = emptyFilePatch()
177 }
178
179 for (let i = 0; i < lines.length; i++) {
180 const line = lines[i]
181
182 if (state === "parsing header") {
183 if (line.startsWith("@@")) {
184 hunkStartLineIndex = i
185 state = "parsing hunks"
186 currentFilePatch.hunks = []
187 i--
188 } else if (line.startsWith("diff --git ")) {
189 if (currentFilePatch && currentFilePatch.diffLineFromPath) {
190 commitFilePatch(i)
191 }
192 const match = line.match(/^diff --git a\/(.*?) b\/(.*?)\s*$/)
193 if (!match) {
194 throw new Error("Bad diff line: " + line)
195 }
196 currentFilePatch.diffLineFromPath = match[1]
197 currentFilePatch.diffLineToPath = match[2]
198 } else if (line.startsWith("old mode ")) {
199 currentFilePatch.oldMode = line.slice("old mode ".length).trim()
200 } else if (line.startsWith("new mode ")) {
201 currentFilePatch.newMode = line.slice("new mode ".length).trim()
202 } else if (line.startsWith("deleted file mode ")) {
203 currentFilePatch.deletedFileMode = line
204 .slice("deleted file mode ".length)
205 .trim()
206 } else if (line.startsWith("new file mode ")) {
207 currentFilePatch.newFileMode = line

Callers 1

parsePatchFileFunction · 0.85

Calls 6

assertNeverFunction · 0.90
emptyFilePatchFunction · 0.85
commitFilePatchFunction · 0.85
commitHunkFunction · 0.85
emptyHunkFunction · 0.85
verifyHunkIntegrityFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…