From 3340dfd4766503aeefe3fc8048711afb94947408 Mon Sep 17 00:00:00 2001
From: Jerome Reybert <jreybert@gmail.com>
Date: Tue, 9 Oct 2018 21:28:46 +0200
Subject: [PATCH] Freshly created repository fails to open in vimagit fix #169

When a repository is freshly created, it contains no commit. It was
failing while looking for current HEAD.

This commmit also check for a faulty repository, and stops buffer
initialization.
---
 autoload/magit/git.vim | 28 ++++++++++++++++++++++++++++
 plugin/magit.vim       | 18 ++++++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/autoload/magit/git.vim b/autoload/magit/git.vim
index 8e0142c..4a17e03 100644
--- a/autoload/magit/git.vim
+++ b/autoload/magit/git.vim
@@ -283,6 +283,34 @@ function! magit#git#get_branch_name(ref)
 	return magit#utils#strip(magit#sys#system(g:magit_git_cmd . " rev-parse --abbrev-ref " . a:ref))
 endfunction
 
+" magit#git#count_object: this function returns the output of git
+" count-objects, in a dict object
+" It contains the following information: count, size, in-pack, packs,
+" size-pack, prune-packable, garbage, size-garbage
+function! magit#git#count_object()
+	let count_object=magit#sys#systemlist(g:magit_git_cmd . " count-objects -v")
+	let refs={}
+	for line in count_object
+		let ref=split(line, ":")
+		let refs[ref[0]] = ref[1]
+	endfor
+	return refs
+endfunction
+
+" magit#git#check_repo: check the health of the repo
+" return 0 if everything is fine, 1 otherwise
+function! magit#git#check_repo()
+	try
+		let head_br=magit#git#get_branch_name("HEAD")
+	catch 'shell_error'
+		let count = magit#git#count_object()['count']
+		if ( count != 0 )
+			return 1
+		endif
+	endtry
+	return 0
+endfunction
+
 " magit#git#get_commit_subject: get the subject of a commit (first line)
 " param[in] ref: reference, can be SHA1, brnach name or HEAD
 " return commit subject
diff --git a/plugin/magit.vim b/plugin/magit.vim
index 53c44c6..3702a38 100644
--- a/plugin/magit.vim
+++ b/plugin/magit.vim
@@ -78,7 +78,11 @@ function! s:mg_get_info()
 				\ g:magit_section_info.cur_repo,
 				\ magit#git#top_dir())
 
-	let head_br=magit#git#get_branch_name("HEAD")
+	try
+		let head_br=magit#git#get_branch_name("HEAD")
+	catch 'shell_error'
+		let head_br="Empty repository"
+	endtry
 	let upstream_br=magit#git#get_remote_branch("HEAD", "upstream")
 	let push_br=magit#git#get_remote_branch("HEAD", "push")
 	let max_br_w = max([len(head_br), len(upstream_br), len(push_br)])
@@ -815,7 +819,6 @@ function! magit#show_magit(display, ...)
 	      \ endif"
 	augroup END
 
-	let b:state = deepcopy(g:magit#state#state)
 	" s:magit_commit_mode: global variable which states in which commit mode we are
 	" values are:
 	"       '': not in commit mode
@@ -829,6 +832,17 @@ function! magit#show_magit(display, ...)
 
 	let b:magit_just_commited = 0
 
+	if ( magit#git#check_repo() != 0 )
+		echohl ErrorMsg
+		echom "git repository seems to be corrupted"
+		echohl None
+		echom "To be safe, vimagit ends now"
+		echom "Check your repository health with git fsck"
+		echom "If the result shows no problem, open an issue"
+		return
+	endif
+
+	let b:state = deepcopy(g:magit#state#state)
 	call magit#utils#setbufnr(bufnr(buffer_name))
 	call magit#sign#init()
 
-- 
GitLab