Skip to content

Commit 3a1b10a

Browse files
heipeipieter
authored andcommitted
PBGitTree: Improve binary-file decision
This uses the same mechanisms as git to determine whether a file is binary: By simply searching the first 8000 bytes for a 0-byte. This gets rid of the call to "file" and is a much cleaner and shorter implementation. Signed-off-by: Johannes Gilger <[email protected]>
1 parent 3861dee commit 3a1b10a

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

PBGitTree.m

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,12 @@ - (BOOL) isLocallyCached
6363
return NO;
6464
}
6565

66-
- (BOOL)hasBinaryHeader:(NSString *)fileHeader
66+
- (BOOL)hasBinaryHeader:(NSString*)contents
6767
{
68-
if (!fileHeader)
68+
if(!contents)
6969
return NO;
7070

71-
NSString *filetype = [PBEasyPipe outputForCommand:@"/usr/bin/file"
72-
withArgs:[NSArray arrayWithObjects:@"-b", @"-N", @"-", nil]
73-
inDir:[repository workingDirectory]
74-
inputString:fileHeader
75-
retValue:nil];
76-
77-
return [filetype rangeOfString:@"text"].location == NSNotFound;
71+
return [contents rangeOfString:@"\0" options:0 range:NSMakeRange(0, ([contents length] >= 8000) ? 7999 : [contents length])].location != NSNotFound;
7872
}
7973

8074
- (BOOL)hasBinaryAttributes
@@ -143,14 +137,13 @@ - (NSString *)textContents
143137
if ([self hasBinaryAttributes])
144138
return [NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], [self fileSize]];
145139

146-
long long fileSize = [self fileSize];
147-
if (fileSize > 52428800) // ~50MB
148-
return [NSString stringWithFormat:@"%@ is too big to be displayed (%d bytes)", [self fullPath], fileSize];
140+
if ([self fileSize] > 52428800) // ~50MB
141+
return [NSString stringWithFormat:@"%@ is too big to be displayed (%d bytes)", [self fullPath], [self fileSize]];
149142

150-
NSString *contents = [self contents];
143+
NSString* contents = [self contents];
151144

152-
if ([self hasBinaryHeader:([contents length] >= 100) ? [contents substringToIndex:99] : contents])
153-
return [NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], fileSize];
145+
if ([self hasBinaryHeader:contents])
146+
return [NSString stringWithFormat:@"%@ appears to be a binary file of %d bytes", [self fullPath], [self fileSize]];
154147

155148
return contents;
156149
}

0 commit comments

Comments
 (0)