diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 804ba74..04d8716 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,13 +10,13 @@ jobs: name: SonarScanner runs-on: windows-latest steps: - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v1 with: - java-version: 1.11 + java-version: "17" - uses: actions/checkout@v2 with: - fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis - name: Cache SonarCloud packages uses: actions/cache@v1 with: @@ -38,7 +38,7 @@ jobs: dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner - name: Build and analyze env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} shell: powershell run: | @@ -46,4 +46,4 @@ jobs: dotnet tool install --global dotnet-coverage dotnet build --no-incremental dotnet-coverage collect 'dotnet test' -f xml -o 'coverage.xml' - .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" \ No newline at end of file + .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}" diff --git a/.vscode/settings.json b/.vscode/settings.json index 9016904..47835ad 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,5 +6,5 @@ "source.sortMembers": true }, "editor.formatOnSave": true, - "omnisharp.enableImportCompletion": true + "dotnet.completion.showCompletionItemsFromUnimportedNamespaces": true } \ No newline at end of file diff --git a/CSharpDiff.Tests/PatchTests.cs b/CSharpDiff.Tests/PatchTests.cs index 501510d..eae481e 100644 --- a/CSharpDiff.Tests/PatchTests.cs +++ b/CSharpDiff.Tests/PatchTests.cs @@ -483,5 +483,37 @@ public void ShouldConsiderNewLineToken() Assert.Equal(expected, res); } + [Fact] + public void ShouldApplyPatchCorrectly() + { + var patchApply = new PatchApply(); + var source = "Hello, World!"; + var patch = "-Hello\n+Goodbye"; + var expected = "Goodbye, World!"; + var result = patchApply.Apply(source, patch); + Assert.Equal(expected, result); + } + + [Fact] + public void ShouldHandleEmptyPatch() + { + var patchApply = new PatchApply(); + var source = "Hello, World!"; + var patch = ""; + var expected = "Hello, World!"; + var result = patchApply.Apply(source, patch); + Assert.Equal(expected, result); + } + + [Fact] + public void ShouldHandleEmptySourceString() + { + var patchApply = new PatchApply(); + var source = ""; + var patch = "+Hello, World!"; + var expected = "Hello, World!"; + var result = patchApply.Apply(source, patch); + Assert.Equal(expected, result); } } +} \ No newline at end of file diff --git a/CSharpDiff/Patches/PatchApply.cs b/CSharpDiff/Patches/PatchApply.cs index 84d1afc..3feea64 100644 --- a/CSharpDiff/Patches/PatchApply.cs +++ b/CSharpDiff/Patches/PatchApply.cs @@ -4,10 +4,55 @@ public class PatchApply { public string Apply(string source, string uniDiff) { + var changes = ParseUnifiedDiff(uniDiff); + foreach (var change in changes) + { + source = ApplyChange(source, change); + } + return source; + } + public class Change + { + public string Operation { get; set; } + public int Position { get; set; } + public string Text { get; set; } + } + private List ParseUnifiedDiff(string uniDiff) + { + var lines = uniDiff.Split('\n'); + var changes = new List(); + foreach (var line in lines) + { + var parts = line.Split(' '); + var change = new Change + { + Operation = parts[0], + Position = int.Parse(parts[1]), + Text = parts[2] + }; + changes.Add(change); + } + return changes; + } - return ""; + private string ApplyChange(string source, Change change) + { + var lines = source.Split('\n').ToList(); + switch (change.Operation) + { + case "add": + lines.Insert(change.Position, change.Text); + break; + case "delete": + lines.RemoveAt(change.Position); + break; + case "replace": + lines[change.Position] = change.Text; + break; + } + return String.Join("\n", lines); } } } \ No newline at end of file diff --git a/README.md b/README.md index 1a4f767..a424bd2 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,23 @@ var ps = new Patch(); string patch = ps.create("filename1", "filename2", text1, text2, "header1", "header2", new PatchOptions()); ``` +#### Apply + +The `Apply` method in the `PatchApply` class applies a patch to a given source string. Here is an example of how to use it: + +```c# +using CSharpDiff.Patches; + +var patchApply = new PatchApply(); +var source = "Hello, World!"; +var patch = "-Hello\n+Goodbye"; +var result = patchApply.Apply(source, patch); +``` + ### Works Well With * [Diff2HTML](https://diff2html.xyz/) ## Contributing -Idk, just make a pull request. +Idk, just make a pull request. \ No newline at end of file