Skip to content

Instantly share code, notes, and snippets.

@igrep
Forked from scrooloose/git_menu.vim
Last active August 29, 2015 13:55
Show Gist options
  • Save igrep/8751390 to your computer and use it in GitHub Desktop.
Save igrep/8751390 to your computer and use it in GitHub Desktop.
Adds git command menu items.
" Put this in ~/.vim/after/nerdtree_plugin/git_menu.vim
" NOTE: I fixed a bug of NERDTree to get this plugin work.
" You may have to cherry-pick or fork the pull request to use this plugin.
" https://github.com/scrooloose/nerdtree/pull/308
"
" Adds git command menu items
"
" TODO: Behave like nerdtree_plugin/fs_menu.vim
" Extend nerdtree_plugin/fs_menu.vim without deleting any defalut menu items.
"
if exists("g:loaded_nerdtree_git_menu")
finish
endif
let g:loaded_nerdtree_git_menu = 1
call NERDTreeAddMenuSeparator({'isActiveCallback': 'NERDTreeGitMenuEnabled'})
call NERDTreeAddMenuItem({
\ 'text': 'git (a)dd',
\ 'shortcut': 'a',
\ 'callback': 'NERDTreeGitAdd',
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled' })
call NERDTreeAddMenuItem({
\ 'text': 'git (c)heckout',
\ 'shortcut': 'c',
\ 'callback': 'NERDTreeGitCheckout',
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled' })
call NERDTreeAddMenuItem({
\ 'text': 'git (m)v',
\ 'shortcut': 'm',
\ 'callback': 'NERDTreeGitMove',
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled' })
call NERDTreeAddMenuItem({
\ 'text': 'git (r)m',
\ 'shortcut': 'r',
\ 'callback': 'NERDTreeGitRemove',
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled' })
function! NERDTreeGitMenuEnabled()
return isdirectory(s:GitRepoPath())
endfunction
function! s:GitRepoPath()
return getcwd() . "/.git"
endfunction
function! NERDTreeGitMove()
let node = g:NERDTreeFileNode.GetSelected()
let path = node.path
let p = path.str({'escape': 1})
let newPath = input("==========================================================\n" .
\ "Enter the new path for the file: \n" .
\ "", node.path.str())
if newPath ==# ''
call s:echo("git mv aborted.")
return
endif
call s:execGitCmd('mv ' . p . ' ' . newPath)
endfunction
function! NERDTreeGitAdd()
let node = g:NERDTreeFileNode.GetSelected()
let path = node.path
call s:execGitCmd('add ' . path.str({'escape': 1}))
endfunction
function! NERDTreeGitRemove()
let node = g:NERDTreeFileNode.GetSelected()
let path = node.path
call s:execGitCmd('rm ' . path.str({'escape': 1}))
endfunction
function! NERDTreeGitCheckout()
let node = g:NERDTreeFileNode.GetSelected()
let path = node.path
call s:execGitCmd('checkout ' . path.str({'escape': 1}))
endfunction
function! s:execGitCmd(sub_cmd)
let extra_options = '--git-dir=' . s:GitRepoPath() . ' '
let extra_options .= '--work-tree=' . getcwd()
let cmd = "git" . ' ' . extra_options . ' ' . a:sub_cmd
let output = system(cmd)
redraw!
if v:shell_error == 0
let node = g:NERDTreeFileNode.GetSelected()
if !node.isRoot()
call node.parent.refresh()
call NERDTreeRender()
endif
else
echomsg output
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment