Skip to content

Make the rot13 test filter stream its output #958

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

Merged
merged 6 commits into from
Feb 16, 2015
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace LibGit2Sharp.Tests
{
public class SubstitutionCipherFilterFixture : BaseFixture
public class FilterSubstitutionCipherFixture : BaseFixture
{
[Fact]
public void CorrectlyEncodesAndDecodesInput()
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<Compile Include="ResetIndexFixture.cs" />
<Compile Include="SmartSubtransportFixture.cs" />
<Compile Include="StatusFixture.cs" />
<Compile Include="SubstitutionCipherFilterFixture.cs" />
<Compile Include="FilterSubstitutionCipherFixture.cs" />
<Compile Include="TestHelpers\BaseFixture.cs" />
<Compile Include="BlobFixture.cs" />
<Compile Include="BranchFixture.cs" />
Expand Down
23 changes: 9 additions & 14 deletions LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace LibGit2Sharp.Tests.TestHelpers
{
Expand Down Expand Up @@ -35,27 +34,23 @@ protected override int Smudge(string path, Stream input, Stream output)

public static int RotateByThirteenPlaces(Stream input, Stream output)
{
int value;

using (var streamReader = new StreamReader(input, Encoding.UTF8))
while ((value = input.ReadByte()) != -1)
{
var inputString = streamReader.ReadToEnd();
char[] array = inputString.ToCharArray();
for (int i = 0; i < inputString.Length; i++)
if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M'))
{
var value = inputString[i];
if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M'))
array[i] = (char)(value + 13);
else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z'))
array[i] = (char)(value - 13);
value += 13;
}

using (var streamWriter = new StreamWriter(output, Encoding.UTF8))
else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z'))
{
streamWriter.Write(array);
value -= 13;
}

return 0;
output.WriteByte((byte)value);
}

return 0;
}
}
}
25 changes: 1 addition & 24 deletions LibGit2Sharp/Core/GitBufWriteStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal GitBufWriteStream(IntPtr gitBufPointer)
this.gitBufPointer = gitBufPointer;

//Preallocate the buffer
Proxy.git_buf_grow(gitBufPointer, 1024);
Proxy.git_buf_grow(gitBufPointer, 4096);
}

public override void Flush()
Expand All @@ -37,32 +37,9 @@ public override int Read(byte[] buffer, int offset, int count)

public override void Write(byte[] buffer, int offset, int count)
{
AutoGrowBuffer(count);

Proxy.git_buf_put(gitBufPointer, buffer, offset, count);
}

private void AutoGrowBuffer(int count)
{
var gitBuf = gitBufPointer.MarshalAs<GitBuf>();

var ulongCount = Convert.ToUInt64(count);
var asize = (ulong)gitBuf.asize;
var size = (ulong)gitBuf.size;

var isBufferLargeEnoughToHoldTheNewData = (asize - size) > ulongCount;
var filledBufferPercentage = (100.0 * size / asize);

if (isBufferLargeEnoughToHoldTheNewData && filledBufferPercentage < 90)
{
return;
}

var targetSize = (ulong)(1.5 * (asize + ulongCount));

Proxy.git_buf_grow(gitBufPointer, targetSize);
}

public override bool CanRead
{
get { return false; }
Expand Down
5 changes: 3 additions & 2 deletions LibGit2Sharp/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,11 @@ int ApplyCallback(GitFilter gitFilter, IntPtr payload,
var filterSource = FilterSource.FromNativePtr(filterSourcePtr);
using (var reader = new GitBufReadStream(gitBufferFromPtr))
using (var writer = new GitBufWriteStream(gitBufferToPtr))
using (var bufferedWriter = new BufferedStream(writer))
{
return filterSource.SourceMode == FilterMode.Clean ?
Clean(filterSource.Path, reader, writer) :
Smudge(filterSource.Path, reader, writer);
Clean(filterSource.Path, reader, bufferedWriter) :
Smudge(filterSource.Path, reader, bufferedWriter);
}
}
}
Expand Down