Skip to main content

detect_patch

Function detect_patch 

Source
pub fn detect_patch(content: &str) -> PatchFormat
Expand description

Automatically detects the patch format of the provided content.

This function scans the content efficiently (without parsing the full structure) to determine if it contains Markdown code blocks, standard unified diff headers, or conflict markers.

§Behavior

The detection follows this priority:

  1. Markdown: If code fences (3+ backticks) are found containing diff signatures, it is treated as Markdown.
  2. Unified: If --- a/ or diff --git headers are found, it is treated as a Unified Diff.
  3. Conflict: If <<<< markers are found, it is treated as Conflict Markers.

§Arguments

  • content - A string slice containing the patch data to analyze.

§Returns

The detected PatchFormat.

§Examples

use mpatch::{detect_patch, PatchFormat};

let md = "```diff\n--- a/f\n+++ b/f\n```";
assert_eq!(detect_patch(md), PatchFormat::Markdown);

let raw = "--- a/f\n+++ b/f\n@@ -1 +1 @@";
assert_eq!(detect_patch(raw), PatchFormat::Unified);