Skip to content

Commit

Permalink
feat: new/deleted file mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Delian Jekov committed Jan 9, 2024
1 parent 2e82b0b commit 9222908
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 41 deletions.
Binary file added .vs/CSharpDiff/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .vs/CSharpDiff/v17/.suo
Binary file not shown.
Empty file.
Empty file.
74 changes: 37 additions & 37 deletions CSharpDiff.Tests/PatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public void ShouldHandleLastLine()
{
var patch = new Patch();

var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,3 +1,4 @@\n"
Expand All @@ -98,8 +98,8 @@ public void ShouldHandleLastLine()
var res = patch.create("test", "test", "line2\nline3\nline5\n", "line2\nline3\nline4\nline5\n", "header1", "header2");
Assert.Equal(expected, res);

expected = "Index: test\n"
+ "===================================================================\n"
expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,3 +1,4 @@\n"
Expand All @@ -111,8 +111,8 @@ public void ShouldHandleLastLine()
res = patch.create("test", "test", "line2\nline3\nline4\n", "line2\nline3\nline4\nline5\n", "header1", "header2");
Assert.Equal(expected, res);

expected = "Index: test\n"
+ "===================================================================\n"
expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,4 +1,5 @@\n"
Expand All @@ -131,8 +131,8 @@ public void ShouldHandleLastLine()
public void ShouldHandleNoNewLineNewEOF()
{
var patch = new Patch();
var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,4 +1,4 @@\n"
Expand All @@ -150,8 +150,8 @@ public void ShouldHandleNoNewLineNewEOF()
public void ShouldHandleNoNewLineOldEOF()
{
var patch = new Patch();
var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,4 +1,4 @@\n"
Expand All @@ -169,8 +169,8 @@ public void ShouldHandleNoNewLineOldEOF()
public void ShouldHandleNoNewLineContextMissing()
{
var patch = new Patch();
var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,4 +1,4 @@\n"
Expand All @@ -188,8 +188,8 @@ public void ShouldHandleNoNewLineContextMissing()
public void ShouldOutPutNoNewLineOnEmpty()
{
var patch = new Patch();
var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -0,0 +1,4 @@\n"
Expand All @@ -206,8 +206,8 @@ public void ShouldOutPutNoNewLineOnEmpty()
public void ShouldNotOutputNoNewLineWhenEofOutsideHunk()
{
var patch = new Patch();
var expected = "Index: test\n"
+ "===================================================================\n"
var expected = "diff --git test test\n"
+ "Index: test\n"
+ "--- test\theader1\n"
+ "+++ test\theader2\n"
+ "@@ -1,5 +1,5 @@\n"
Expand All @@ -225,8 +225,8 @@ public void ShouldNotOutputNoNewLineWhenEofOutsideHunk()
public void ShouldGeneratePatchDefaultContextSize()
{
var patch = new Patch();
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\tOld Header\n"
+ "+++ testFileName\tNew Header\n"
+ "@@ -1,5 +1,6 @@\n"
Expand Down Expand Up @@ -283,8 +283,8 @@ public void ShouldGeneratePatchWithContextSize0()
{
Context = 0
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\tOld Header\n"
+ "+++ testFileName\tNew Header\n"
+ "@@ -1,1 +1,2 @@\n"
Expand Down Expand Up @@ -312,8 +312,8 @@ public void ShouldGeneratePatchWithContextSize2()
{
Context = 2
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\tOld Header\n"
+ "+++ testFileName\tNew Header\n"
+ "@@ -1,3 +1,4 @@\n"
Expand Down Expand Up @@ -356,8 +356,8 @@ public void ShouldGeneratePatchWithContextSize2()
public void ShouldOutputHeadersOnlyForIdentical()
{
var patch = new Patch();
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\tOld Header\n"
+ "+++ testFileName\tNew Header\n";
var res = patch.create("testFileName", "testFileName", oldFile, oldFile, "Old Header", "New Header");
Expand All @@ -368,8 +368,8 @@ public void ShouldOutputHeadersOnlyForIdentical()
public void ShouldOmitHeadersIfEmpty()
{
var patch = new Patch();
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n";
var res = patch.create("testFileName", "testFileName", oldFile, oldFile, null, null);
Expand All @@ -380,8 +380,8 @@ public void ShouldOmitHeadersIfEmpty()
public void ShouldHandleEmpty()
{
var patch = new Patch();
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n";
var res = patch.create("testFileName", "testFileName", "", "", null, null);
Expand All @@ -392,7 +392,7 @@ public void ShouldHandleEmpty()
public void ShouldOmitIndexDifferentFilenames()
{
var patch = new Patch();
var expected = "===================================================================\n"
var expected = "diff --git foo bar\n"
+ "--- foo\n"
+ "+++ bar\n";
var res = patch.create("foo", "bar", "", "", null, null);
Expand All @@ -406,8 +406,8 @@ public void ShouldNotIgnoreWhiteSpace()
{
IgnoreWhiteSpace = false
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n"
+ "@@ -1,2 +1,2 @@\n"
Expand All @@ -428,8 +428,8 @@ public void ShouldIgnoreWhiteSpace()
{
IgnoreWhiteSpace = true
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n";
var res = patch.create("testFileName", "testFileName", "line \n line", "line\nline", null, null);
Expand All @@ -443,8 +443,8 @@ public void ShouldNotConsiderNewLineToken()
{
NewlineIsToken = false
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n"
+ "@@ -1,2 +1,2 @@\n"
Expand All @@ -466,8 +466,8 @@ public void ShouldConsiderNewLineToken()
{
NewlineIsToken = true
});
var expected = "Index: testFileName\n"
+ "===================================================================\n"
var expected = "diff --git testFileName testFileName\n"
+ "Index: testFileName\n"
+ "--- testFileName\n"
+ "+++ testFileName\n"
+ "@@ -1,3 +1,3 @@\n"
Expand Down
24 changes: 20 additions & 4 deletions CSharpDiff/Patches/Patch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.RegularExpressions;
using CSharpDiff.Diffs;
using CSharpDiff.Diffs.Models;
using CSharpDiff.Patches.Models;
using System.Text.RegularExpressions;

namespace CSharpDiff.Patches
{
Expand Down Expand Up @@ -185,13 +185,29 @@ public PatchResult createPatchResult(string oldFileName, string newFileName, str
public string formatPatch(PatchResult diff)
{
var ret = new List<string>();
var oldFileName = (String.IsNullOrEmpty(diff.OldFileName) ? "/dev/null" : diff.OldFileName);
var newFileName = (String.IsNullOrEmpty(diff.NewFileName) ? "/dev/null" : diff.NewFileName);

ret.Add($"diff --git {oldFileName} {newFileName}");

if (diff.OldFileName == diff.NewFileName)
{
ret.Add("Index: " + diff.OldFileName);
}
ret.Add("===================================================================");
ret.Add("--- " + diff.OldFileName + (String.IsNullOrEmpty(diff.OldHeader) ? "" : '\t' + diff.OldHeader));
ret.Add("+++ " + diff.NewFileName + (String.IsNullOrEmpty(diff.NewHeader) ? "" : '\t' + diff.NewHeader));
else if (diff.OldFileName is null && diff.NewFileName is not null)
{
ret.Add("new file mode 100644");
}
else if (diff.NewFileName is null && diff.OldFileName is not null)
{
ret.Add("deleted file mode 100644");
}


ret.Add("--- " + oldFileName + (String.IsNullOrEmpty(diff.OldHeader) ? "" : '\t' + diff.OldHeader));
ret.Add("+++ " + newFileName + (String.IsNullOrEmpty(diff.NewHeader) ? "" : '\t' + diff.NewHeader));



for (var i = 0; i < diff.Hunks.Count(); i++)
{
Expand Down

0 comments on commit 9222908

Please sign in to comment.