* Remove the marker blocks `owns` claims (and the blank line that follows * one), leaving every other block and all user content in place.
(content: string, owns: BlockOwner)
| 172 | * one), leaving every other block and all user content in place. |
| 173 | */ |
| 174 | function stripMarkerBlocks(content: string, owns: BlockOwner): string { |
| 175 | const lines = content.split('\n'); |
| 176 | const kept: string[] = []; |
| 177 | for (let i = 0; i < lines.length; i++) { |
| 178 | if (lines[i]!.trim() !== MARKER_BEGIN) { |
| 179 | kept.push(lines[i]!); |
| 180 | continue; |
| 181 | } |
| 182 | let end = i; |
| 183 | while (end < lines.length && lines[end]!.trim() !== MARKER_END) end++; |
| 184 | const block = lines.slice(i, end + 1); |
| 185 | const rootLine = block.find((l) => l.trim().startsWith(ROOT_PREFIX)); |
| 186 | const blockRoot = rootLine ? rootLine.trim().slice(ROOT_PREFIX.length) : null; |
| 187 | if (owns(blockRoot)) { |
| 188 | if (lines[end + 1]?.trim() === '') end++; |
| 189 | } else { |
| 190 | kept.push(...block); |
| 191 | } |
| 192 | i = end; |
| 193 | } |
| 194 | return kept.join('\n'); |
| 195 | } |
| 196 | |
| 197 | /** Whether a hook's shebang names a shell (a hook with none runs under sh). */ |
| 198 | function isShellHook(content: string): boolean { |
no test coverage detected