diff --git a/autoload/magit/git.vim b/autoload/magit/git.vim index f57ca095eb869dbcc2fcb45cc7ed3bfe0248b308..b009105d86deec52a3ae4b2d167e869bc1ce5267 100644 --- a/autoload/magit/git.vim +++ b/autoload/magit/git.vim @@ -19,9 +19,28 @@ function! magit#git#get_status() return file_list endfunction -" magit#git#set_top_dir: this function set b:magit_top_dir according to a path -" param[in] path: path to check if it is in a git repository -" return: 1 if path is in a git repo and b:magit_top_dir is set, 0 otherwise +" magit#git#is_work_tree: this function check that path passed as parameter is +" inside a git work tree +" param[in] path: path to check +" return: top work tree path if in a work tree, empty string otherwise +function! magit#git#is_work_tree(path) + let dir = getcwd() + try + call magit#utils#lcd(a:path) + let top_dir=magit#utils#strip( + \ system(s:git_cmd . " rev-parse --show-toplevel")) . "/" + if ( v:shell_error != 0 ) + return '' + endif + return top_dir + finally + call magit#utils#lcd(dir) + endtry +endfunction + +" magit#git#set_top_dir: this function set b:magit_top_dir and b:magit_git_dir +" according to a path +" param[in] path: path to set. This path must be in a git repository work tree function! magit#git#set_top_dir(path) let dir = getcwd() try @@ -29,15 +48,14 @@ function! magit#git#set_top_dir(path) let top_dir=magit#utils#strip( \ system(s:git_cmd . " rev-parse --show-toplevel")) . "/" if ( v:shell_error != 0 ) - return 0 + throw "magit: git-show-toplevel error: " . top_dir endif let git_dir=magit#utils#strip(system(s:git_cmd . " rev-parse --git-dir")) . "/" if ( v:shell_error != 0 ) - throw "magit: git_dir error " . b:magit_git_dir + throw "magit: git-git-dir error: " . git_dir endif let b:magit_top_dir=top_dir let b:magit_git_dir=git_dir - return 1 finally call magit#utils#lcd(dir) endtry diff --git a/plugin/magit.vim b/plugin/magit.vim index b1a225bfa1424e16ec36ec04f6e94c4ae5b015f8..9aaf55d7f1bb1ce3884a46d2eb0c4f493faf9320 100644 --- a/plugin/magit.vim +++ b/plugin/magit.vim @@ -583,30 +583,17 @@ function! magit#show_magit(display, ...) let cur_file = expand("%:p") let cur_file_path = isdirectory(cur_file) ? cur_file : fnamemodify(cur_file, ":h") endif - let try_paths = [ cur_file_path, getcwd() ] - - if ( a:display == 'v' ) - vnew - elseif ( a:display == 'h' ) - new - elseif ( a:display == 'c' ) - if ( bufname("%") == "" ) - keepalt enew - else - enew - endif - else - throw 'parameter_error' - endif + let git_dir='' + let try_paths = [ cur_file_path, getcwd() ] for path in try_paths - if ( magit#git#set_top_dir(path) == 1 ) + let git_dir=magit#git#is_work_tree(path) + if ( git_dir != '' ) break endif endfor - try - let top_dir=magit#git#top_dir() - catch 'top_dir_not_set' + + if ( git_dir == '' ) echohl ErrorMsg echom "magit can not find any git repository" echom "make sure that current opened file or vim current directory points to a git repository" @@ -615,16 +602,31 @@ function! magit#show_magit(display, ...) echom path endfor echohl None - call magit#close_magit() throw 'magit_not_in_git_repo' - endtry + endif - let buffer_name='magit://' . top_dir - try - silent execute "buffer " . buffer_name - catch /^Vim\%((\a\+)\)\=:E94/ - silent execute "keepalt file " . buffer_name - endtry + let buffer_name='magit://' . git_dir + + if ( a:display == 'v' ) + silent execute "vnew " . buffer_name + elseif ( a:display == 'h' ) + silent execute "new " . buffer_name + elseif ( a:display == 'c' ) + if ( bufexists(buffer_name) ) + silent execute "buffer " . buffer_name + else + if ( bufname("%") == "" ) + keepalt enew + else + enew + endif + silent execute "file " . buffer_name + endif + else + throw 'parameter_error' + endif + + call magit#git#set_top_dir(git_dir) let b:magit_default_show_all_files = g:magit_default_show_all_files let b:magit_default_fold_level = g:magit_default_fold_level