Skip to content

Implement Patch Apply #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -38,12 +38,12 @@ 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: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"ThomasHambach_csharpdiff" /o:"thomashambach" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
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 }}"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"source.sortMembers": true
},
"editor.formatOnSave": true,
"omnisharp.enableImportCompletion": true
"dotnet.completion.showCompletionItemsFromUnimportedNamespaces": true
}
32 changes: 32 additions & 0 deletions CSharpDiff.Tests/PatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
47 changes: 46 additions & 1 deletion CSharpDiff/Patches/PatchApply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Change> ParseUnifiedDiff(string uniDiff)
{
var lines = uniDiff.Split('\n');
var changes = new List<Change>();
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);
}
}
}
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.