A unified patch lists changed lines and nearby unchanged lines.
dotnet add package RecursiveAI.OneDiffRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff; string patch = Diff.Unified("off\n", "on\n"); Console.Write(patch);
Unzip the example, then dotnet run.
Prints raw patch text:
--- a +++ b @@ -1 +1 @@ -off +on
The full program
The example reads two versions of a settings file, writes settings.patch, and leaves both inputs untouched. The zip includes before.txt and after.txt; both end with a newline.
before.txt:
size=small mode=auto
after.txt:
size=large mode=auto
using OneDiff; string before = File.ReadAllText("before.txt"); string after = File.ReadAllText("after.txt"); string patch = Diff.Unified(before, after, "a/settings.txt", "b/settings.txt"); if (patch.Length == 0) { Console.WriteLine("No changes; no patch written."); return; } File.WriteAllText("settings.patch", patch); Console.Write(patch); Console.WriteLine("Wrote settings.patch");
--- a/settings.txt +++ b/settings.txt @@ -1,2 +1,2 @@ -size=small +size=large mode=auto Wrote settings.patch
The last line is a status message, not part of the patch. If the input texts are identical, Diff.Unified returns an empty string and the program skips the write.
Variations
Export only the changes, with no unchanged lines. Set contextLines: 0:
using OneDiff; Console.Write(Diff.Unified( "mode=auto\noff\n", "mode=auto\non\n", contextLines: 0));
--- a +++ b @@ -2 +2 @@ -off +on
Related recipes
The short answer
Call Diff.Unified(before, after) to create the patch text that describes a change from the old version to the new one. The full program reads both files and saves the returned string directly with File.WriteAllText. It doesn’t apply the patch and doesn’t need a Git repository. If there are no changes, the return value is empty rather than a file header with no edits.
How do you create a patch file from a diff?
Write the raw unified diff, not a Markdown code block or a terminal rendering. A unified patch begins with --- and +++ file headers, followed by one or more hunks. Each hunk records changes in one part of the file. In the full program’s patch, @@ -1,2 +1,2 @@ says that the hunk starts at line 1 and covers two lines in both versions. - removes a line, + adds one, and a leading space preserves a context line. See the GNU unified format description and OneDiff’s patch docs.
The short example uses the default labels a and b. The full program instead passes a/settings.txt and b/settings.txt to describe two versions of the same target file. These labels go into the headers; they don’t tell Diff.Unified what to open. The actual input paths are the two arguments to File.ReadAllText. Save only the patch string, without the program’s confirmation line or any additional commentary.
How many context lines should a unified diff have?
Start with the default three. These unchanged lines give the reader surrounding text and give a patch tool more information about where the change belongs. Changes separated by at most twice the chosen context length share a hunk. Set contextLines: 0 when you want the shortest changed-line listing, as in the variation; it leaves out those matching cues.
OneDiff splits patch input at \n and retains line terminators. Don’t normalize them before comparing if a line-ending edit is part of the change you want to capture. A missing final newline isn’t silently corrected: a changed line without a terminator is followed by \ No newline at end of file. Keep that marker in the file you export.