From d77693b6fbad61da14ffaa2a34cf2afef39f9f18 Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Fri, 30 Oct 2015 11:50:46 +0100
Subject: [PATCH] autoload/magit/utils.vim: add git_add and git_reset helper
 functions

---
 autoload/magit/utils.vim | 25 +++++++++++++++++++++++++
 plugin/magit.vim         |  6 ++----
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/autoload/magit/utils.vim b/autoload/magit/utils.vim
index 0b1c8a4..767aef8 100644
--- a/autoload/magit/utils.vim
+++ b/autoload/magit/utils.vim
@@ -33,6 +33,31 @@ function! magit#utils#git_dir()
 	return s:magit_git_dir
 endfunction
 
+" magit#utils#git_add: helper function to add a whole file
+" nota: when git fail (due to misformated patch for example), an error
+" message is raised.
+" param[in] filemane: it must be quoted if it contains spaces
+function! magit#utils#git_add(filename)
+	let git_cmd="git add -- " . a:filename
+	silent let git_result=magit#utils#system(git_cmd)
+	if ( v:shell_error != 0 )
+		echoerr "Git error: " . git_result
+		echoerr "Git cmd: " . git_cmd
+	endif
+endfunction
+
+" magit#utils#git_reset: helper function to add a whole file
+" nota: when git fail (due to misformated patch for example), an error
+" message is raised.
+" param[in] filemane: it must be quoted if it contains spaces
+function! magit#utils#git_reset(filename)
+	let git_cmd="git reset -- " . a:filename
+	silent let git_result=magit#utils#system(git_cmd)
+	if ( v:shell_error != 0 )
+		echoerr "Git error: " . git_result
+		echoerr "Git cmd: " . git_cmd
+	endif
+endfunction
 
 " magit#utils#git_apply: helper function to stage a selection
 " nota: when git fail (due to misformated patch for example), an error
diff --git a/plugin/magit.vim b/plugin/magit.vim
index 3461bf0..44fc02b 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -677,15 +677,13 @@ function! magit#stage_block(selection, discard) abort
 	if ( a:discard == 0 )
 		if ( section == 'unstaged' )
 			if ( file.must_be_added() )
-				call magit#utils#system('git add ' .
-					\ magit#utils#add_quotes(filename))
+				call magit#utils#git_add(magit#utils#add_quotes(filename))
 			else
 				call magit#utils#git_apply(header, a:selection)
 			endif
 		elseif ( section == 'staged' )
 			if ( file.must_be_added() )
-				call magit#utils#system('git reset ' .
-					\ magit#utils#add_quotes(filename))
+				call magit#utils#git_reset(magit#utils#add_quotes(filename))
 			else
 				call magit#utils#git_unapply(header, a:selection, 'staged')
 			endif
-- 
GitLab