1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Linq ;
34using System . Text ;
45using LibGit2Sharp . Tests . TestHelpers ;
@@ -22,6 +23,28 @@ public void CanGetBlobAsText()
2223 }
2324 }
2425
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+
2548 [ Theory ]
2649 [ InlineData ( "ascii" , 4 , "31 32 33 34" ) ]
2750 [ InlineData ( "utf-7" , 4 , "31 32 33 34" ) ]
@@ -99,14 +122,59 @@ public void CanReadBlobStream()
99122 {
100123 var blob = repo . Lookup < Blob > ( "a8233120f6ad708f843d861ce2b7228ec4e3dec6" ) ;
101124
102- using ( var tr = new StreamReader ( blob . ContentStream , Encoding . UTF8 ) )
125+ using ( var tr = new StreamReader ( blob . ContentStream ( ) , Encoding . UTF8 ) )
103126 {
104127 string content = tr . ReadToEnd ( ) ;
105128 Assert . Equal ( "hey there\n " , content ) ;
106129 }
107130 }
108131 }
109132
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+
110178 public static void CopyStream ( Stream input , Stream output )
111179 {
112180 // Reused from the following Stack Overflow post with permission
@@ -120,6 +188,19 @@ public static void CopyStream(Stream input, Stream output)
120188 }
121189 }
122190
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+
123204 [ Fact ]
124205 public void CanStageAFileGeneratedFromABlobContentStream ( )
125206 {
@@ -143,7 +224,7 @@ public void CanStageAFileGeneratedFromABlobContentStream()
143224
144225 var blob = repo . Lookup < Blob > ( entry . Id . Sha ) ;
145226
146- using ( Stream stream = blob . ContentStream )
227+ using ( Stream stream = blob . ContentStream ( ) )
147228 using ( Stream file = File . OpenWrite ( Path . Combine ( repo . Info . WorkingDirectory , "small.fromblob.txt" ) ) )
148229 {
149230 CopyStream ( stream , file ) ;
0 commit comments