Copy sample.diff below or get it in the zip. Skips binary and rename-only sections.
dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff.Patching; var text = File.ReadAllText("sample.diff"); var patch = UnifiedPatch.Parse(text); foreach (var file in patch.Files) Console.WriteLine( $"{file.NewName}: {file.Hunks.Count}");
Unzip the example, then dotnet run.
Prints each new-side name and its count of change blocks (hunks).
b/notes.txt: 2 b/config.txt: 1
The full program
The download contains this generated sample.diff. Put it next to your project to run the snippet at the top. The labels a/notes.txt and b/notes.txt are names in the patch, not files you need to create.
--- a/notes.txt +++ b/notes.txt @@ -1 +1 @@ -title: draft +title: final @@ -3 +3 @@ -status: open +status: closed --- a/config.txt +++ b/config.txt @@ -1 +1 @@ -debug=true +debug=false
using OneDiff.Patching; var text = File.ReadAllText("sample.diff"); if (!UnifiedPatch.TryParse(text, out var patch, out var error)) { Console.Error.WriteLine($"Line {error.Line}: {error.Kind}: {error.Message}"); return 1; } foreach (var file in patch.Files) Console.WriteLine($"{file.OldName} -> {file.NewName}: hunks: {file.Hunks.Count}"); return 0;
dotnet run prints:
a/notes.txt -> b/notes.txt: hunks: 2 a/config.txt -> b/config.txt: hunks: 1
The only file the example reads is sample.diff. Parse throws on malformed patch text. The full program uses TryParse instead: if parsing fails, it reports the error kind and line and exits with code 1 before printing a summary.
Variations
Count added and removed lines. Replace the foreach in the full program to count lines inside the parsed hunks, ignoring unchanged context:
foreach (var file in patch.Files) { int added = 0, removed = 0; foreach (var hunk in file.Hunks) foreach (var line in hunk.Lines) { if (line.Kind == UnifiedLineKind.Insert) added++; if (line.Kind == UnifiedLineKind.Delete) removed++; } Console.WriteLine($"{file.NewName}: +{added} -{removed}"); }
b/notes.txt: +2 -2 b/config.txt: +1 -1
Related recipes
- Apply a unified patch to a string
- Apply a multi-file unified patch
- Write a line diff as a Markdown report
The short answer
Read the patch text and call UnifiedPatch.TryParse. On success, loop over patch.Files and read each entry’s OldName, NewName, and Hunks.Count. That gives you a summary of the parsed text sections even when the files they describe aren’t on disk. On failure, PatchParseError tells you the kind, line, offset, and message. Don’t interpret success as validation of a complete Git patch or confirmation that the patch applies.
What is a hunk in a unified diff?
A hunk is a block of changes in one file, optionally including nearby unchanged lines for context. The GNU unified format uses --- and +++ for the file names and an @@ header for each hunk, with the old range after - and the new range after +. In the sample, @@ -3 +3 @@ describes a change at line 3 on each side. The count is omitted because each range is one line long. The notes.txt section has two separate headers, so the summary reports two hunks. The count isn’t a count of changed lines: one hunk can contain several additions and deletions plus context.
What’s the difference between reading a diff and applying it?
Reading a diff tells you what the patch text describes. Applying it also requires the original content and checks the hunk lines against that content. The example never performs that second step. You can use the summary to show which names a text patch mentions before anyone decides to apply it. Treat the names as labels from the headers: the b/ in b/notes.txt isn’t removed by the example and doesn’t tell you where a file is on disk.
Does a unified diff list binary changes and renames?
Git can report binary changes and rename information, but OneDiff’s unified parser skips binary and rename-only sections. It can retain extended header lines that precede a parsed text file section, but those aren’t a separate list of all changes in the patch. An empty Files list means there were no file sections to report; it does not mean the patch described no changes. This summary is for unified text hunks, not a replacement for a Git change inventory.