1
- using System . IO ;
1
+ using System ;
2
+ using System . IO ;
2
3
using System . Linq ;
3
4
using System . Text ;
4
5
using LibGit2Sharp . Tests . TestHelpers ;
@@ -22,6 +23,28 @@ public void CanGetBlobAsText()
22
23
}
23
24
}
24
25
26
+ [ Fact ]
27
+ public void CanGetBlobAsFilteredText ( )
28
+ {
29
+ using ( var repo = new Repository ( BareTestRepoPath ) )
30
+ {
31
+ var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
32
+
33
+ var text = blob . ContentAsText ( new FilteringOptions ( "foo.txt" ) ) ;
34
+
35
+ ConfigurationEntry < bool > autocrlf = repo . Config . Get < bool > ( "core.autocrlf" ) ;
36
+
37
+ if ( autocrlf != null && autocrlf . Value )
38
+ {
39
+ Assert . Equal ( "hey there\r \n " , text ) ;
40
+ }
41
+ else
42
+ {
43
+ Assert . Equal ( "hey there\n " , text ) ;
44
+ }
45
+ }
46
+ }
47
+
25
48
[ Theory ]
26
49
[ InlineData ( "ascii" , 4 , "31 32 33 34" ) ]
27
50
[ InlineData ( "utf-7" , 4 , "31 32 33 34" ) ]
@@ -99,14 +122,59 @@ public void CanReadBlobStream()
99
122
{
100
123
var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
101
124
102
- using ( var tr = new StreamReader ( blob . ContentStream , Encoding . UTF8 ) )
125
+ using ( var tr = new StreamReader ( blob . ContentStream ( ) , Encoding . UTF8 ) )
103
126
{
104
127
string content = tr . ReadToEnd ( ) ;
105
128
Assert . Equal ( "hey there\n " , content ) ;
106
129
}
107
130
}
108
131
}
109
132
133
+ [ Fact ]
134
+ public void CanReadBlobFilteredStream ( )
135
+ {
136
+ using ( var repo = new Repository ( BareTestRepoPath ) )
137
+ {
138
+ var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
139
+
140
+ using ( var tr = new StreamReader ( blob . ContentStream ( new FilteringOptions ( "foo.txt" ) ) , Encoding . UTF8 ) )
141
+ {
142
+ string content = tr . ReadToEnd ( ) ;
143
+
144
+ ConfigurationEntry < bool > autocrlf = repo . Config . Get < bool > ( "core.autocrlf" ) ;
145
+
146
+ if ( autocrlf != null && autocrlf . Value )
147
+ {
148
+ Assert . Equal ( "hey there\r \n " , content ) ;
149
+ }
150
+ else
151
+ {
152
+ Assert . Equal ( "hey there\n " , content ) ;
153
+ }
154
+ }
155
+ }
156
+ }
157
+
158
+ [ Fact ]
159
+ public void CanReadBlobFilteredStreamOfUnmodifiedBinary ( )
160
+ {
161
+ var binaryContent = new byte [ ] { 0 , 1 , 2 , 3 , 4 , 5 } ;
162
+
163
+ string path = CloneBareTestRepo ( ) ;
164
+ using ( var repo = new Repository ( path ) )
165
+ {
166
+ using ( var stream = new MemoryStream ( binaryContent ) )
167
+ {
168
+ Blob blob = repo . ObjectDatabase . CreateBlob ( stream ) ;
169
+
170
+ using ( var filtered = blob . ContentStream ( new FilteringOptions ( "foo.txt" ) ) )
171
+ {
172
+ Assert . True ( StreamEquals ( stream , filtered ) ) ;
173
+ }
174
+ }
175
+ }
176
+ }
177
+
110
178
public static void CopyStream ( Stream input , Stream output )
111
179
{
112
180
// Reused from the following Stack Overflow post with permission
@@ -120,6 +188,19 @@ public static void CopyStream(Stream input, Stream output)
120
188
}
121
189
}
122
190
191
+ public static bool StreamEquals ( Stream one , Stream two )
192
+ {
193
+ int onebyte , twobyte ;
194
+
195
+ while ( ( onebyte = one . ReadByte ( ) ) >= 0 && ( twobyte = two . ReadByte ( ) ) >= 0 )
196
+ {
197
+ if ( onebyte != twobyte )
198
+ return false ;
199
+ }
200
+
201
+ return true ;
202
+ }
203
+
123
204
[ Fact ]
124
205
public void CanStageAFileGeneratedFromABlobContentStream ( )
125
206
{
@@ -143,7 +224,7 @@ public void CanStageAFileGeneratedFromABlobContentStream()
143
224
144
225
var blob = repo . Lookup < Blob > ( entry . Id . Sha ) ;
145
226
146
- using ( Stream stream = blob . ContentStream )
227
+ using ( Stream stream = blob . ContentStream ( ) )
147
228
using ( Stream file = File . OpenWrite ( Path . Combine ( repo . Info . WorkingDirectory , "small.fromblob.txt" ) ) )
148
229
{
149
230
CopyStream ( stream , file ) ;
0 commit comments