Skip to content

Commit 3b4bd86

Browse files
committed
use system command to find git root by default
1 parent a6ff14a commit 3b4bd86

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

autoload/easygit.vim

+25-13
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,33 @@ function! easygit#gitdir(path, ...) abort
2020
endfunction
2121

2222
function! s:FindGitdir(path)
23-
let dir = finddir('.git', expand(a:path).';')
24-
if empty(dir) | return '' | endif
25-
return fnamemodify(dir, ':p:h')
23+
if !empty($GIT_DIR) | return $GIT_DIR | endif
24+
if get(g:, 'easygit_enable_root_rev_parse', 1)
25+
let old_cwd = getcwd()
26+
let cwd = fnamemodify(a:path, ':p:h')
27+
execute 'lcd '.cwd
28+
let root = system('git rev-parse --show-toplevel')
29+
execute 'lcd '.old_cwd
30+
if v:shell_error | return '' | endif
31+
return substitute(root, '\r\?\n', '', '') . '/.git'
32+
else
33+
let dir = finddir('.git', expand(a:path).';')
34+
if dir | return fnamemodify(dir, ':p:h') | endif
35+
return ''
36+
endif
37+
endfunction
38+
39+
" If cwd inside current file git root, return cwd, otherwise return git root
40+
function! easygit#smartRoot(...)
41+
let suspend = a:0 ? a:1 : 0
42+
let gitdir = easygit#gitdir(expand('%'), suspend)
43+
if empty(gitdir) | return '' | endif
44+
let root = fnamemodify(gitdir, ':h')
45+
let cwd = getcwd()
46+
return cwd =~# '^' . root ? cwd : root
2647
endfunction
2748

49+
2850
" cd or lcd to base directory of current file's git root
2951
function! easygit#cd(local) abort
3052
let dir = easygit#gitdir(expand('%'))
@@ -575,16 +597,6 @@ function! easygit#listRemotes(...)
575597
return substitute(output, '\v(^|\n)\zs\s*', '', 'g')
576598
endfunction
577599

578-
" If cwd inside current file git root, return cwd, otherwise return git root
579-
function! easygit#smartRoot(...)
580-
let suspend = a:0 ? a:1 : 0
581-
let gitdir = easygit#gitdir(expand('%'), suspend)
582-
if empty(gitdir) | return '' | endif
583-
let root = fnamemodify(gitdir, ':h')
584-
let cwd = getcwd()
585-
return cwd =~# '^' . root ? cwd : root
586-
endfunction
587-
588600
function! easygit#revert(args)
589601
let root = easygit#smartRoot()
590602
if empty(root) | return | endif

doc/easygit.txt

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
: :: :: : : : :: : : : :: :: : : : ~
1313
~
1414
Author : Qiming Zhao <[email protected]>
15-
Version : 0.5.1
16-
Last Change : Jun 14, 2017
15+
Version : 0.6.0
16+
Last Change : Apr 02, 2018
1717

1818
License : MIT license {{{
1919

@@ -222,6 +222,11 @@ with golbal variables.
222222

223223
------------------------------------------------------------------------------
224224

225+
*g:easygit_enable_root_rev_parse*
226+
227+
Use `git rev-parse --show-toplevel` to find git root instead of find
228+
`.git` directory, it's slower.
229+
225230
*g:easygit_enable_command*
226231
Commands are disabled by default to make it works with |fugitive|
227232
without any effort. To enable the commands of easygit, use: >
@@ -377,6 +382,12 @@ easygit#grep({args}) *easygit#grep*
377382
==============================================================================
378383
CHANGELOG *easygit-changelog*
379384

385+
0.6.0 2018-04-02
386+
387+
- Use $GIT_DIR or `git rev-parse --show-toplevel` to find git root
388+
directory by default.
389+
390+
380391
0.5.1 2017-06-14
381392

382393
- add easygit#diffPreview function

0 commit comments

Comments
 (0)