Skip to content

Commit

Permalink
plugin/magit.vim,autoload/{git,sign}.vim: magit can handle multiple g…
Browse files Browse the repository at this point in the history
…it repository within one vim session (fix #44,#34,#29)

it also adds the ability to open a magit buffer from a netrw buffer
  • Loading branch information
jreybert committed Nov 17, 2015
1 parent 4237501 commit 982d161
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 43 deletions.
56 changes: 32 additions & 24 deletions autoload/magit/git.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,47 @@ function! magit#git#get_status()
return file_list
endfunction

" s:magit_top_dir: top directory of git tree
" it is evaluated only once
" FIXME: it won't work when playing with multiple git directories wihtin one
" vim session
let s:magit_top_dir=''
" magit#git#top_dir: return the absolute path of current git worktree
" return top directory
function! magit#git#top_dir()
if ( s:magit_top_dir == '' )
let s:magit_top_dir=magit#utils#strip(
\ system(s:git_cmd . " rev-parse --show-toplevel")) . "/"
" 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
function! magit#git#set_top_dir(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 )
echoerr "Git error: " . s:magit_top_dir
return 0
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
endif
let b:magit_top_dir=top_dir
let b:magit_git_dir=git_dir
return 1
finally
call magit#utils#lcd(dir)
endtry
endfunction

" magit#git#top_dir: return the absolute path of current git worktree for the
" current magit buffer
" return top directory
function! magit#git#top_dir()
if ( !exists("b:magit_top_dir") )
throw 'top_dir_not_set'
endif
return s:magit_top_dir
return b:magit_top_dir
endfunction

" s:magit_git_dir: git directory
" it is evaluated only once
" FIXME: it won't work when playing with multiple git directories wihtin one
" vim session
let s:magit_git_dir=''
" magit#git#git_dir: return the absolute path of current git worktree
" return git directory
function! magit#git#git_dir()
if ( s:magit_git_dir == '' )
let s:magit_git_dir=magit#utils#strip(system(s:git_cmd . " rev-parse --git-dir")) . "/"
if ( v:shell_error != 0 )
echoerr "Git error: " . s:magit_git_dir
endif
if ( !exists("b:magit_git_dir") )
throw 'git_dir_not_set'
endif
return s:magit_git_dir
return b:magit_git_dir
endfunction

" magit#git#git_diff: helper function to get diff of a file
Expand Down
2 changes: 0 additions & 2 deletions autoload/magit/sign.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ let s:dummy_sign_id = s:first_sign_id - 1
" Remove-all-signs optimisation requires Vim 7.3.596+.
let s:supports_star = v:version > 703 || (v:version == 703 && has("patch596"))

let s:bufnr = bufnr(g:magit_buffer_name)

function! magit#sign#remove_all(...)
if ( a:0 == 1 )
let pattern = a:1
Expand Down
58 changes: 41 additions & 17 deletions plugin/magit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ let g:loaded_magit = 1
" syntax files
execute 'source ' . resolve(expand('<sfile>:p:h')) . '/../common/magit_common.vim'

" g:magit_buffer_name: vim buffer name for vimagit
let g:magit_buffer_name = "magit-playground"

let s:state = deepcopy(magit#state#state)

" these mappings are broadly applied, for all vim buffers
Expand Down Expand Up @@ -125,8 +122,9 @@ function! s:mg_get_info()
silent put =''
let branch=magit#utils#system("git rev-parse --abbrev-ref HEAD")
let commit=magit#utils#system("git show -s --oneline")
silent put ='Current branch: ' . branch
silent put ='Last commit: ' . commit
silent put ='Current repository: ' . magit#git#top_dir()
silent put ='Current branch: ' . branch
silent put ='Last commit: ' . commit
silent put =''
endfunction

Expand Down Expand Up @@ -522,8 +520,9 @@ let s:mg_display_functions = {
" 4. fills with unstage stuff
" 5. restore window state
function! magit#update_buffer()
if ( @% != g:magit_buffer_name )
echoerr "Not in magit buffer " . g:magit_buffer_name . " but in " . @%
let buffer_name=bufname("%")
if ( buffer_name !~ 'magit://.*' )
echoerr "Not in magit buffer but in " . buffer_name
return
endif
" FIXME: find a way to save folding state. According to help, this won't
Expand Down Expand Up @@ -578,10 +577,14 @@ endfunction
" 'h': horizontal split
" 'c': current buffer (should be used when opening vim in vimagit mode
function! magit#show_magit(display, ...)
if ( magit#utils#strip(system("git rev-parse --is-inside-work-tree")) != 'true' )
echoerr "Magit must be started from a git repository"
return
if ( &filetype == 'netrw' )
let cur_file_path = b:netrw_curdir
else
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' )
Expand All @@ -596,6 +599,33 @@ function! magit#show_magit(display, ...)
throw 'parameter_error'
endif

for path in try_paths
if ( magit#git#set_top_dir(path) == 1 )
break
endif
endfor
try
let top_dir=magit#git#top_dir()
catch 'top_dir_not_set'
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"
echom "search paths:"
for path in try_paths
echom path
endfor
echohl None
call magit#close_magit()
throw 'magit_not_in_git_repo'
endtry

let buffer_name='magit://' . top_dir
try
silent execute "buffer " . buffer_name
catch /^Vim\%((\a\+)\)\=:E94/
silent execute "keepalt file " . buffer_name
endtry

let b:magit_default_show_all_files = g:magit_default_show_all_files
let b:magit_default_fold_level = g:magit_default_fold_level
let b:magit_warning_max_lines_answered = 0
Expand All @@ -616,13 +646,7 @@ function! magit#show_magit(display, ...)
setlocal filetype=magit
"setlocal readonly

try
silent execute "buffer " . g:magit_buffer_name
catch /^Vim\%((\a\+)\)\=:E94/
silent execute "keepalt file " . g:magit_buffer_name
endtry

call magit#utils#setbufnr(bufnr(g:magit_buffer_name))
call magit#utils#setbufnr(bufnr(buffer_name))
call magit#sign#init()

execute "nnoremap <buffer> <silent> " . g:magit_stage_file_mapping . " :call magit#stage_file()<cr>"
Expand Down

0 comments on commit 982d161

Please sign in to comment.