From 95e7501c9cb655c163eea1b199369995a0db7bd1 Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Mon, 8 Oct 2018 14:09:36 +0200
Subject: [PATCH] Discarding untracked file results in an error in some cases
 fix #165

In the case the untracked file is:
- an empty file
- a directory
- a binary
the discard fails.

Discard command was not working when the file was unfolded.
---
 plugin/magit.vim | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/plugin/magit.vim b/plugin/magit.vim
index ec3a3b0..03e5d5d 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -913,9 +913,9 @@ function! s:mg_stage_closed_file(discard)
 				endif
 			else
 				if ( section == 'unstaged' )
-					if ( file.status == '?' )
+					if ( file.status == '?' || file.must_be_added() )
 						if ( g:magit_discard_untracked_do_delete == 1 )
-							if ( delete(filename) != 0 )
+							if ( delete(filename, "rf") != 0 )
 								echoerr "Can not delete \"" . filename . "\""
 								return
 							endif
@@ -992,8 +992,21 @@ function! magit#stage_block(selection, discard) abort
 		endif
 	else
 		if ( section == 'unstaged' )
-			if ( file.must_be_added() )
-				call magit#git#git_checkout(magit#utils#add_quotes(filename))
+			if ( file.status == '?' || file.must_be_added() )
+				if ( g:magit_discard_untracked_do_delete == 1 )
+					if ( delete(filename, "rf") != 0 )
+						echoerr "Can not delete \"" . filename . "\""
+						return
+					endif
+				else
+					echohl WarningMsg
+					echomsg "By default, vimagit won't discard "
+								\ "untracked file (which means delete this file)"
+					echomsg "You can force this behaviour, "
+								\ "setting g:magit_discard_untracked_do_delete=1"
+					echohl None
+					return
+				endif
 			else
 				call magit#git#git_unapply(header, a:selection, 'unstaged')
 			endif
-- 
GitLab