Skip to main content

split_diff_sections

Function split_diff_sections 

Source
pub fn split_diff_sections(diff: &str) -> Vec<(String, String)>
Expand description

Split a unified diff into per-file sections, returning (path, section_text) for each. Sections start at diff --git lines (primary) or, when the diff carries no git headers, at the --- line preceding each +++ marker (or the +++ line itself). This is the same splitting the glob filter and the size packer both build on.

The derived path is the new-side (b/) path; it is the empty string for a section whose path can’t be determined (a /dev/null deletion or a leading preamble). section_text reproduces the section’s lines with a trailing newline, so concatenating every section reconstructs the diff.

If the diff can’t be parsed into any section, a single entry ("".to_string(), diff.to_string()) is returned so callers degrade gracefully rather than losing the diff.

§Examples

let d = "diff --git a/src/a.rs b/src/a.rs\n+++ b/src/a.rs\n@@ -1 +1 @@\n+y\n";
let secs = split_diff_sections(d);
assert_eq!(secs.len(), 1);
assert_eq!(secs[0].0, "src/a.rs");