From a9e5c1fd1bbb33a9c0028f5c88d6b4395af95857 Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Mon, 8 Feb 2016 21:50:22 +0100
Subject: [PATCH] autoload/magit/state.vim: remove is_binary function, replaced
 by git diff parsing (fix #60, #61)

for the moment, git diff output is not localized in case of binary detection
---
 autoload/magit/git.vim   |  7 ++++++-
 autoload/magit/state.vim | 16 ++++++++++++----
 autoload/magit/utils.vim |  7 -------
 3 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/autoload/magit/git.vim b/autoload/magit/git.vim
index c0c2673..9c48939 100644
--- a/autoload/magit/git.vim
+++ b/autoload/magit/git.vim
@@ -102,6 +102,9 @@ endfunction
 " param[in] filemane: it must be quoted if it contains spaces
 " param[in] status: status of the file (see g:magit_git_status_code)
 " param[in] mode: can be staged or unstaged
+" return: two values
+"        [0]: boolean, if true current file is binary
+"        [1]: string array containing diff output
 function! magit#git#git_diff(filename, status, mode)
 	let dev_null = ( a:status == '?' ) ? "/dev/null " : ""
 	let staged_flag = ( a:mode == 'staged' ) ? "--staged" : ""
@@ -122,7 +125,9 @@ function! magit#git#git_diff(filename, status, mode)
 		echohl None
 		throw 'diff error'
 	endif
-	return diff_list
+	return [
+		\ ( diff_list[-1] =~ "^Binary files .* differ$" && len(diff_list) <= 4 )
+		\, diff_list ]
 endfunction
 
 " magit#git#sub_check: this function checks if given submodule has modified or
diff --git a/autoload/magit/state.vim b/autoload/magit/state.vim
index 4f2cbb4..fc225f2 100644
--- a/autoload/magit/state.vim
+++ b/autoload/magit/state.vim
@@ -206,17 +206,25 @@ function! magit#state#add_file(mode, status, filename, depth) dict
 		let file.status = 'E'
 		let file.empty = 1
 		let file.diff.hunks[0].header = 'New empty file'
-	elseif ( magit#utils#is_binary(magit#utils#add_quotes(a:filename)))
-		let file.binary = 1
-		let file.diff.hunks[0].header = 'Binary file'
 	else
 		if ( !file.init_visible() )
 			return
 		endif
 		let line = 0
 		" match(
-		let diff_list=magit#git#git_diff(magit#utils#add_quotes(a:filename),
+		let [ is_bin, diff_list ] =
+					\ magit#git#git_diff(magit#utils#add_quotes(a:filename),
 					\ a:status, a:mode)
+
+		if ( is_bin )
+			let file.binary = 1
+			let file.diff.hunks[0].header = 'Binary file'
+			if ( file.new )
+				call file.set_visible(0)
+			endif
+			return
+		endif
+
 		let file.diff.len = len(diff_list)
 
 		if ( self.check_max_lines(file) != 0 )
diff --git a/autoload/magit/utils.vim b/autoload/magit/utils.vim
index 3b5dd5b..9bcf679 100644
--- a/autoload/magit/utils.vim
+++ b/autoload/magit/utils.vim
@@ -1,11 +1,4 @@
 
-" s:magit#utils#is_binary: check if file is a binary file
-" param[in] filename: the file path. it must quoted if it contains spaces
-function! magit#utils#is_binary(filename)
-	return ( match(system("file --mime " . a:filename ),
-				\ ".*charset=binary") != -1 )
-endfunction
-
 " magit#utils#ls_all: list all files (including hidden ones) in a given path
 " return : list of filenames
 function! magit#utils#ls_all(path)
-- 
GitLab