Skip to content

Commit f8f6f4e

Browse files
committed
Merge pull request #958 from libgit2/ntk/test_filters
Make the rot13 test filter stream its output
2 parents dacf03c + 46a7034 commit f8f6f4e

File tree

5 files changed

+15
-42
lines changed

5 files changed

+15
-42
lines changed

LibGit2Sharp.Tests/SubstitutionCipherFilterFixture.cs renamed to LibGit2Sharp.Tests/FilterSubstitutionCipherFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace LibGit2Sharp.Tests
88
{
9-
public class SubstitutionCipherFilterFixture : BaseFixture
9+
public class FilterSubstitutionCipherFixture : BaseFixture
1010
{
1111
[Fact]
1212
public void CorrectlyEncodesAndDecodesInput()

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
<Compile Include="ResetIndexFixture.cs" />
9797
<Compile Include="SmartSubtransportFixture.cs" />
9898
<Compile Include="StatusFixture.cs" />
99-
<Compile Include="SubstitutionCipherFilterFixture.cs" />
99+
<Compile Include="FilterSubstitutionCipherFixture.cs" />
100100
<Compile Include="TestHelpers\BaseFixture.cs" />
101101
<Compile Include="BlobFixture.cs" />
102102
<Compile Include="BranchFixture.cs" />

LibGit2Sharp.Tests/TestHelpers/SubstitutionCipherFilter.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.IO;
3-
using System.Text;
43

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

3635
public static int RotateByThirteenPlaces(Stream input, Stream output)
3736
{
37+
int value;
3838

39-
using (var streamReader = new StreamReader(input, Encoding.UTF8))
39+
while ((value = input.ReadByte()) != -1)
4040
{
41-
var inputString = streamReader.ReadToEnd();
42-
char[] array = inputString.ToCharArray();
43-
for (int i = 0; i < inputString.Length; i++)
41+
if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M'))
4442
{
45-
var value = inputString[i];
46-
if ((value >= 'a' && value <= 'm') || (value >= 'A' && value <= 'M'))
47-
array[i] = (char)(value + 13);
48-
else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z'))
49-
array[i] = (char)(value - 13);
43+
value += 13;
5044
}
51-
52-
using (var streamWriter = new StreamWriter(output, Encoding.UTF8))
45+
else if ((value >= 'n' && value <= 'z') || (value >= 'N' && value <= 'Z'))
5346
{
54-
streamWriter.Write(array);
47+
value -= 13;
5548
}
5649

57-
return 0;
50+
output.WriteByte((byte)value);
5851
}
52+
53+
return 0;
5954
}
6055
}
6156
}

LibGit2Sharp/Core/GitBufWriteStream.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal GitBufWriteStream(IntPtr gitBufPointer)
1313
this.gitBufPointer = gitBufPointer;
1414

1515
//Preallocate the buffer
16-
Proxy.git_buf_grow(gitBufPointer, 1024);
16+
Proxy.git_buf_grow(gitBufPointer, 4096);
1717
}
1818

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

3838
public override void Write(byte[] buffer, int offset, int count)
3939
{
40-
AutoGrowBuffer(count);
41-
4240
Proxy.git_buf_put(gitBufPointer, buffer, offset, count);
4341
}
4442

45-
private void AutoGrowBuffer(int count)
46-
{
47-
var gitBuf = gitBufPointer.MarshalAs<GitBuf>();
48-
49-
var ulongCount = Convert.ToUInt64(count);
50-
var asize = (ulong)gitBuf.asize;
51-
var size = (ulong)gitBuf.size;
52-
53-
var isBufferLargeEnoughToHoldTheNewData = (asize - size) > ulongCount;
54-
var filledBufferPercentage = (100.0 * size / asize);
55-
56-
if (isBufferLargeEnoughToHoldTheNewData && filledBufferPercentage < 90)
57-
{
58-
return;
59-
}
60-
61-
var targetSize = (ulong)(1.5 * (asize + ulongCount));
62-
63-
Proxy.git_buf_grow(gitBufPointer, targetSize);
64-
}
65-
6643
public override bool CanRead
6744
{
6845
get { return false; }

LibGit2Sharp/Filter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ int ApplyCallback(GitFilter filter, IntPtr payload,
237237
var filterSource = FilterSource.FromNativePtr(filterSourcePtr);
238238
using (var reader = new GitBufReadStream(gitBufferFromPtr))
239239
using (var writer = new GitBufWriteStream(gitBufferToPtr))
240+
using (var bufferedWriter = new BufferedStream(writer))
240241
{
241242
return filterSource.SourceMode == FilterMode.Clean ?
242-
Clean(filterSource.Path, reader, writer) :
243-
Smudge(filterSource.Path, reader, writer);
243+
Clean(filterSource.Path, reader, bufferedWriter) :
244+
Smudge(filterSource.Path, reader, bufferedWriter);
244245
}
245246
}
246247
}

0 commit comments

Comments
 (0)