File tree 2 files changed +34
-5
lines changed
2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change 19
19
*/
20
20
class Blob
21
21
{
22
+ /**
23
+ * @var int Size that git uses to look for NULL byte: https://git.kernel.org/pub/scm/git/git.git/tree/xdiff-interface.c?h=v2.44.0#n193
24
+ */
25
+ private const FIRST_FEW_BYTES = 8000 ;
26
+
22
27
/**
23
28
* @var Repository
24
29
*/
@@ -39,6 +44,11 @@ class Blob
39
44
*/
40
45
protected $ mimetype ;
41
46
47
+ /**
48
+ * @var bool
49
+ */
50
+ protected $ text ;
51
+
42
52
/**
43
53
* @param Repository $repository Repository where the blob is located
44
54
* @param string $hash Hash of the blob
@@ -89,6 +99,9 @@ public function getMimetype()
89
99
/**
90
100
* Determines if file is binary.
91
101
*
102
+ * Uses the same check that git uses to determine if a file is binary or not
103
+ * https://git.kernel.org/pub/scm/git/git.git/tree/xdiff-interface.c?h=v2.44.0#n193
104
+ *
92
105
* @return bool
93
106
*/
94
107
public function isBinary ()
@@ -99,10 +112,17 @@ public function isBinary()
99
112
/**
100
113
* Determines if file is text.
101
114
*
115
+ * Uses the same check that git uses to determine if a file is binary or not
116
+ * https://git.kernel.org/pub/scm/git/git.git/tree/xdiff-interface.c?h=v2.44.0#n193
117
+ *
102
118
* @return bool
103
119
*/
104
120
public function isText ()
105
121
{
106
- return (bool ) preg_match ('#^text/|^application/xml# ' , $ this ->getMimetype ());
122
+ if (null === $ this ->text ) {
123
+ $ this ->text = !str_contains (substr ($ this ->getContent (), 0 , self ::FIRST_FEW_BYTES ), chr (0 ));
124
+ }
125
+
126
+ return $ this ->text ;
107
127
}
108
128
}
Original file line number Diff line number Diff line change @@ -23,6 +23,11 @@ public function getReadmeBlob($repository)
23
23
return $ repository ->getCommit (self ::LONGFILE_COMMIT )->getTree ()->resolvePath ('README.md ' );
24
24
}
25
25
26
+ public function getImageBlob ($ repository )
27
+ {
28
+ return $ repository ->getCommit (self ::LONGFILE_COMMIT )->getTree ()->resolvePath ('image.jpg ' );
29
+ }
30
+
26
31
/**
27
32
* @dataProvider provideFoobar
28
33
*/
@@ -67,16 +72,20 @@ public function testGetMimetype($repository)
67
72
*/
68
73
public function testIsText ($ repository )
69
74
{
70
- $ blob = $ this ->getReadmeBlob ($ repository );
71
- $ this ->assertTrue ($ blob ->isText ());
75
+ $ readmeBlob = $ this ->getReadmeBlob ($ repository );
76
+ $ this ->assertTrue ($ readmeBlob ->isText ());
77
+ $ imageBlob = $ this ->getImageBlob ($ repository );
78
+ $ this ->assertFalse ($ imageBlob ->isText ());
72
79
}
73
80
74
81
/**
75
82
* @dataProvider provideFoobar
76
83
*/
77
84
public function testIsBinary ($ repository )
78
85
{
79
- $ blob = $ this ->getReadmeBlob ($ repository );
80
- $ this ->assertFalse ($ blob ->isBinary ());
86
+ $ readmeBlob = $ this ->getReadmeBlob ($ repository );
87
+ $ this ->assertFalse ($ readmeBlob ->isBinary ());
88
+ $ imageBlob = $ this ->getImageBlob ($ repository );
89
+ $ this ->assertTrue ($ imageBlob ->isBinary ());
81
90
}
82
91
}
You can’t perform that action at this time.
0 commit comments