Skip to content

Commit

Permalink
Freshly created repository fails to open in vimagit fix #169
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jreybert committed Oct 9, 2018
1 parent 6509e93 commit 3340dfd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
28 changes: 28 additions & 0 deletions autoload/magit/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down

0 comments on commit 3340dfd

Please sign in to comment.