From 18d9fc6cf976a8dc09d1a44331616b7436b3d23c Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Mon, 8 Oct 2018 14:26:32 +0200
Subject: [PATCH] Ungraceful behaviour when opening magit outside of a git repo
 fix #162

The error was not catchup early enough withthe new shell error
management.

It mades a ciphered error message.

Now, no vim error is thrown. It could be improved, setting the magit
file with magit properties, and display the error message in the buffer.
---
 autoload/magit/git.vim | 10 ++++------
 plugin/magit.vim       |  4 ++--
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/autoload/magit/git.vim b/autoload/magit/git.vim
index a2ed3da..8e0142c 100644
--- a/autoload/magit/git.vim
+++ b/autoload/magit/git.vim
@@ -51,13 +51,11 @@ function! magit#git#is_work_tree(path)
 	let dir = getcwd()
 	try
 		call magit#utils#chdir(a:path)
-		try
-			let top_dir=magit#utils#strip(
-					\ system(g:magit_git_cmd . " rev-parse --show-toplevel")) . "/"
-		catch 'shell_error'
+		let top_dir=system(g:magit_git_cmd . " rev-parse --show-toplevel")
+		if ( v:shell_error != 0 )
 			return ''
-		endtry
-		return top_dir
+		endif
+		return magit#utils#strip(top_dir) . "/"
 	finally
 		call magit#utils#chdir(dir)
 	endtry
diff --git a/plugin/magit.vim b/plugin/magit.vim
index 03e5d5d..53c44c6 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -732,13 +732,13 @@ function! magit#show_magit(display, ...)
 	if ( git_dir == '' )
 		echohl ErrorMsg
 		echom "magit can not find any git repository"
+		echohl None
 		echom "make sure that current opened file or vim current directory points to a git repository"
 		echom "search paths:"
 		for path in try_paths
 			echom path
 		endfor
-		echohl None
-		throw 'magit_not_in_git_repo'
+		return
 	endif
 
 	let buffer_name=fnameescape('magit://' . git_dir)
-- 
GitLab