make the git menu own harder

This commit is contained in:
Martin Grenfell 2009-07-21 19:27:10 +12:00
parent e9d6a7209c
commit a163f327eb

View File

@ -10,12 +10,30 @@
" See http://sam.zoy.org/wtfpl/COPYING for more details. " See http://sam.zoy.org/wtfpl/COPYING for more details.
" "
" ============================================================================ " ============================================================================
"
" Adds a "g" submenu to the NERD tree menu.
"
" Note: this plugin assumes that the current tree root has a .git dir under
" it, and that the working tree and the .git repo are in the same place
"
if exists("g:loaded_nerdtree_git_menu") if exists("g:loaded_nerdtree_git_menu")
finish finish
endif endif
let g:loaded_nerdtree_git_menu = 1 let g:loaded_nerdtree_git_menu = 1
call NERDTreeAddMenuItem('(g)it menu', 'g', 'NERDTreeGitMenu') call NERDTreeAddMenuItem({
\ 'text': '(g)it menu',
\ 'shortcut': 'g',
\ 'check_to_enable_callback': 'NERDTreeGitMenuEnabled',
\ 'callback': 'NERDTreeGitMenu' })
function! NERDTreeGitMenuEnabled()
return isdirectory(s:GitRepoPath())
endfunction
function! s:GitRepoPath()
return b:NERDTreeRoot.path.str(0) . ".git"
endfunction
function! NERDTreeGitMenu() function! NERDTreeGitMenu()
let node = g:NERDTreeFileNode.GetSelected() let node = g:NERDTreeFileNode.GetSelected()
@ -36,26 +54,34 @@ function! NERDTreeGitMenu()
let choice = nr2char(getchar()) let choice = nr2char(getchar())
if choice ==# "a" if choice ==# "a"
call s:promptCommand('git add ', path.strForOS(1), 'file') call s:promptCommand('add ', path.strForOS(1), 'file')
elseif choice ==# "c" elseif choice ==# "c"
call s:promptCommand('git checkout ', path.strForOS(1), 'file') call s:promptCommand('checkout ', path.strForOS(1), 'file')
elseif choice ==# "m" elseif choice ==# "m"
call s:promptCommand('git mv ', path.strForOS(1), 'file') let p = path.strForOS(1)
call s:promptCommand('mv ', p . ' ' . p, 'file')
elseif choice ==# "r" elseif choice ==# "r"
call s:promptCommand('git rm ', path.strForOS(1), 'file') call s:promptCommand('rm ', path.strForOS(1), 'file')
endif endif
call node.parent.refresh() call node.parent.refresh()
call NERDTreeRender() call NERDTreeRender()
endfunction endfunction
function! s:promptCommand(cmd_base, cmd_tail_default, complete) function! s:promptCommand(sub_command, cmd_tail_default, complete)
let cmd_tail = input(":!" . a:cmd_base, a:cmd_tail_default, a:complete) let extra_options = ' --git-dir=' . s:GitRepoPath()
let extra_options .= ' --work-tree=' . b:NERDTreeRoot.path.str(0) . ' '
let base = "git" . extra_options . a:sub_command
let cmd_tail = input(":!" . base, a:cmd_tail_default, a:complete)
if cmd_tail != '' if cmd_tail != ''
let output = system(a:cmd_base . cmd_tail) let output = system(base . cmd_tail)
redraw! redraw!
if v:shell_error != 0 if v:shell_error != 0
echom output echo output
endif endif
else
redraw
echo "Aborted"
endif endif
endfunction endfunction