From 42c34143789ca1c9d1c7afb5240d710f10f92f76 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Mon, 18 Nov 2019 13:20:55 +0100 Subject: [PATCH] Improve vim git commands --- git.vim | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/git.vim b/git.vim index 824cc85..085198e 100644 --- a/git.vim +++ b/git.vim @@ -17,7 +17,7 @@ function! s:gitroot() end endf -function! s:git_root(path) +function! s:cd_git_root(path) let l:path = fnamemodify(a:path, ':p') let l:wd = getcwd() if isdirectory(l:path) @@ -29,14 +29,12 @@ function! s:git_root(path) endif let l:ret = substitute(system('git rev-parse --show-toplevel'), '\n\_.*', '', '') if v:shell_error - echom l:ret exec 'cd '.l:wd return 0 else - exec 'cd '.l:wd + exec 'cd '.l:ret return l:ret end - exec 'cd '.l:wd endf " Returns an array containing chronologically sorted commits @@ -149,13 +147,16 @@ endfun function! s:git_diff(...) if a:0 - vert bot split + let l:wd = getcwd() + call s:cd_git_root(expand('%')) + split call s:file_at_revision(a:1) diffthis au BufUnload diffoff! exec "normal \\" diffthis - call s:git_info() + exec "normal \\" + cd l:wd else if exists("b:git_revision_hash") call s:git_diff(get(s:git_history(), index(s:git_history(), b:git_revision_hash)+1, "NIL")) @@ -174,10 +175,12 @@ endfun command! -range Blame echom join(uniq(sort(git_blame(, ))), ', ') command! -range DBlame !git blame % -L , command! GitNext try - \| call s:gitroot() | call git_next() | call s:git_info() + \| call s:gitroot() | call git_next() + \| GitInfo \| catch | echo 'Not a git repo!' \| endtry -command! GitPrev call git_prev() | call s:git_info() +command! GitPrev call git_prev() + \| GitInfo command! GitFirst call git_first() | call s:git_info() command! GitLast call git_last() | call s:git_info() command! GitInfo call git_info() @@ -187,10 +190,11 @@ command! -nargs=? GitCompare try \| catch | echo 'Not a git repo!' \| endtry command! Uncommited try - \| call s:gitroot() | call git_diff() + \| call git_diff() \| catch | echo 'Not a git repo!' \| endtry -command! GitRoot try +command! GitRoot call cd_git_root('.') +command! ShowGitRoot try \| echo gitroot() \| catch | echo 'Not a git repository' \| endtry