2009-07-19 21:05:21 +08:00
|
|
|
" ============================================================================
|
|
|
|
" File: nerdtree_git_menu.vim
|
|
|
|
" Description: plugin for the NERD Tree that provides a git menu
|
|
|
|
" Maintainer: Martin Grenfell <martin_grenfell at msn dot com>
|
|
|
|
" Last Change: 20 July, 2009
|
|
|
|
" License: This program is free software. It comes without any warranty,
|
|
|
|
" to the extent permitted by applicable law. You can redistribute
|
|
|
|
" it and/or modify it under the terms of the Do What The Fuck You
|
|
|
|
" Want To Public License, Version 2, as published by Sam Hocevar.
|
|
|
|
" See http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
|
|
"
|
|
|
|
" ============================================================================
|
2009-07-21 15:27:10 +08:00
|
|
|
"
|
|
|
|
" 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
|
|
|
|
"
|
2009-07-19 21:05:21 +08:00
|
|
|
if exists("g:loaded_nerdtree_git_menu")
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
let g:loaded_nerdtree_git_menu = 1
|
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
call NERDTreeAddMenuSeparator({'isActiveCallback': 'NERDTreeGitMenuEnabled'})
|
2009-08-21 21:27:07 +08:00
|
|
|
let s:menu = NERDTreeAddSubmenu({
|
2009-07-21 15:27:10 +08:00
|
|
|
\ 'text': '(g)it menu',
|
|
|
|
\ 'shortcut': 'g',
|
2009-08-21 21:27:07 +08:00
|
|
|
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled' })
|
2009-07-21 15:27:10 +08:00
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
call NERDTreeAddMenuItem({
|
|
|
|
\ 'text': 'git (a)dd',
|
|
|
|
\ 'shortcut': 'a',
|
|
|
|
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled',
|
|
|
|
\ 'callback': 'NERDTreeGitAdd',
|
|
|
|
\ 'parent': s:menu })
|
|
|
|
|
|
|
|
call NERDTreeAddMenuItem({
|
|
|
|
\ 'text': 'git (c)heckout',
|
|
|
|
\ 'shortcut': 'c',
|
|
|
|
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled',
|
|
|
|
\ 'callback': 'NERDTreeGitCheckout',
|
|
|
|
\ 'parent': s:menu })
|
|
|
|
|
|
|
|
call NERDTreeAddMenuItem({
|
|
|
|
\ 'text': 'git (m)v',
|
|
|
|
\ 'shortcut': 'm',
|
|
|
|
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled',
|
|
|
|
\ 'callback': 'NERDTreeGitMove',
|
|
|
|
\ 'parent': s:menu })
|
|
|
|
|
|
|
|
call NERDTreeAddMenuItem({
|
|
|
|
\ 'text': 'git (r)m',
|
|
|
|
\ 'shortcut': 'r',
|
|
|
|
\ 'isActiveCallback': 'NERDTreeGitMenuEnabled',
|
|
|
|
\ 'callback': 'NERDTreeGitRemove',
|
|
|
|
\ 'parent': s:menu })
|
|
|
|
|
2009-07-21 15:27:10 +08:00
|
|
|
function! NERDTreeGitMenuEnabled()
|
|
|
|
return isdirectory(s:GitRepoPath())
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:GitRepoPath()
|
2009-08-23 19:56:33 +08:00
|
|
|
return b:NERDTreeRoot.path.str() . ".git"
|
2009-07-21 15:27:10 +08:00
|
|
|
endfunction
|
2009-07-19 21:05:21 +08:00
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
function! NERDTreeGitMove()
|
2009-07-19 21:05:21 +08:00
|
|
|
let node = g:NERDTreeFileNode.GetSelected()
|
|
|
|
let path = node.path
|
2009-08-18 22:05:36 +08:00
|
|
|
let p = path.strForOS(1)
|
2009-08-19 23:08:18 +08:00
|
|
|
|
|
|
|
let newPath = input("==========================================================\n" .
|
|
|
|
\ "Enter the new path for the file: \n" .
|
|
|
|
\ "", node.path.strForOS(0))
|
|
|
|
if newPath ==# ''
|
|
|
|
call s:echo("git mv aborted.")
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
call s:execGitCmd('mv ' . p . ' ' . newPath)
|
2009-08-18 22:05:36 +08:00
|
|
|
endfunction
|
2009-07-19 21:05:21 +08:00
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
function! NERDTreeGitAdd()
|
|
|
|
let node = g:NERDTreeFileNode.GetSelected()
|
|
|
|
let path = node.path
|
2009-08-19 23:08:18 +08:00
|
|
|
call s:execGitCmd('add ' . path.strForOS(1))
|
2009-08-18 22:05:36 +08:00
|
|
|
endfunction
|
2009-07-19 21:05:21 +08:00
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
function! NERDTreeGitRemove()
|
|
|
|
let node = g:NERDTreeFileNode.GetSelected()
|
|
|
|
let path = node.path
|
2009-08-19 23:08:18 +08:00
|
|
|
call s:execGitCmd('rm ' . path.strForOS(1))
|
2009-08-18 22:05:36 +08:00
|
|
|
endfunction
|
2009-07-19 21:05:21 +08:00
|
|
|
|
2009-08-18 22:05:36 +08:00
|
|
|
function! NERDTreeGitCheckout()
|
|
|
|
let node = g:NERDTreeFileNode.GetSelected()
|
|
|
|
let path = node.path
|
2009-08-19 23:08:18 +08:00
|
|
|
call s:execGitCmd('checkout ' . path.strForOS(1))
|
2009-07-19 21:05:21 +08:00
|
|
|
endfunction
|
|
|
|
|
2009-08-19 23:08:18 +08:00
|
|
|
function! s:execGitCmd(sub_cmd)
|
|
|
|
let extra_options = '--git-dir=' . s:GitRepoPath() . ' '
|
2009-08-23 19:56:33 +08:00
|
|
|
let extra_options .= '--work-tree=' . b:NERDTreeRoot.path.str()
|
2009-08-19 23:08:18 +08:00
|
|
|
let cmd = "git" . ' ' . extra_options . ' ' . a:sub_cmd
|
2009-08-18 22:05:36 +08:00
|
|
|
|
2009-08-19 23:08:18 +08:00
|
|
|
let output = system(cmd)
|
|
|
|
redraw!
|
|
|
|
if v:shell_error == 0
|
|
|
|
let node = g:NERDTreeFileNode.GetSelected()
|
|
|
|
if !node.isRoot()
|
2009-08-18 22:05:36 +08:00
|
|
|
call node.parent.refresh()
|
|
|
|
call NERDTreeRender()
|
2009-07-19 21:05:21 +08:00
|
|
|
endif
|
2009-07-21 15:27:10 +08:00
|
|
|
else
|
2009-08-19 23:08:18 +08:00
|
|
|
echomsg output
|
2009-07-19 21:05:21 +08:00
|
|
|
endif
|
|
|
|
endfunction
|