parsePatch function

PatchParse parsePatch(
  1. String? patch, {
  2. String? fallbackPath,
})

Parses patch once, honoring the executor's fallbackPath contract: a header-less input whose lines are still recognizable hashline ops parses with fallbackPath as the section path — exactly what HashlinePatch.parse(patch, fallbackPath: path) does at apply time.

Implementation

PatchParse parsePatch(String? patch, {String? fallbackPath}) {
  if (patch == null || patch.trim().isEmpty) {
    return const PatchParse.failed('no patch argument');
  }
  try {
    final parsed = HashlinePatch.parse(patch, fallbackPath: fallbackPath);
    if (parsed.sections.isEmpty) {
      return const PatchParse.failed('no [path#TAG] section header found');
    }
    return PatchParse.usable(parsed);
  } on Object catch (error) {
    return PatchParse.failed(
      '$error'.replaceFirst(
        RegExp(r'^(Bad state:|HashlineFormatException:)\s*'),
        '',
      ),
    );
  }
}