This uses the tool "file" to determine whether a file in a PBGitTree is
binary or not, i.e. whether it can be displayed in a sensible way. It
only takes the first 100 characters to make that decision, since it has
to invoke "file" using the file-content as an input-pipe. For binary
files a placeholder is returned. This does _not_ affect QuickLook.

Signed-off-by: Johannes Gilger <[email protected]>
---
This has been annoying me for quite a while and the way I "solved" it here 
sure isn't pretty, but it was the only way I could think of. I don't know how 
git determines whether a file is ascii/binary internally, but the way I did it 
here seems fast enough and worked in all the test-cases I tried it on. 

For future feature requests one might dream of a hexdump being invoked when a 
file is determined to be binary.

Comments would be appreciated!

 PBGitTree.m |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/PBGitTree.m b/PBGitTree.m
index 51fadd2..22cb9f9 100644
--- a/PBGitTree.m
+++ b/PBGitTree.m
@@ -81,6 +81,14 @@ - (NSString*) contents
        if (!string) {
                string = [[NSString alloc] initWithData:data 
encoding:NSISOLatin1StringEncoding];
        }
+
+       if (string) {
+               NSString* filetype = [PBEasyPipe 
outputForCommand:@"/usr/bin/file" withArgs:[NSArray arrayWithObjects:@"-b", 
@"-N", @"-", nil] inDir:[repository workingDirectory] inputString:([string 
length] > 100) ? [string substringToIndex:100] : string retValue:nil];
+               if([filetype rangeOfString:@"text"].location == NSNotFound)
+                       return [NSString stringWithFormat:@"%@ appears to be a 
binary file", [self fullPath]];
+
+       }
+
        return string;
 }
 
-- 
1.6.4.rc3.12.gdf73a

Reply via email to