Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created August 31, 2010 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootleq/558647 to your computer and use it in GitHub Desktop.
Save bootleq/558647 to your computer and use it in GitHub Desktop.
" Most adopted from Bob Hiestand's vsccommand plugin
" http://www.vim.org/scripts/script.php?script_id=90
command! -nargs=? GitDiff cal GitDiff(<f-args>)
function! GitDiff(...)
let rev = "HEAD~" . (a:0 > 0 ? a:1 : 0)
let oldDir = getcwd()
let newDir = fnameescape(expand('%:p:h'))
let filetype = &filetype
let cdCommand = haslocaldir() ? 'lcd' : 'cd'
exec cdCommand . " " . newDir
let prefix = substitute(system("git rev-parse --show-prefix"), '\n$', '', '')
let subject = system("git log -1 --format=format:%s " . rev)
let info = system("git log -1 --format=format:%s\\ %n%h' -- '%an' -- '%ai\\ '('%ar')' " . rev)
let result = system("git show " . rev . ":" . shellescape(prefix . expand('%:.')))
exec cdCommand . " " . oldDir
redraw!
if v:shell_error
echohl ErrorMsg | echomsg "GitDiff ERROR: " . substitute(result, '[\n]', ' ', 'g') | echohl None
else
let b:stashFoldMethod=&fdm
exec 'vertical new'
silent put=result | 0delete _
exec 'set filetype=' . filetype
let t:git_diff_info = info
set buftype=nofile
diffthis | wincmd p | diffthis
endif
endf
command! GitDiffOff cal GitDiffOff()
function! GitDiffOff()
if &diff
if bufname("%") =~ '^\[git '
wincmd q
endif
only | call MyDiffOff()
endif
endf
command! MyDiffOff cal MyDiffOff()
function! MyDiffOff()
if &diff
setlocal diff&
setlocal scrollbind&
setlocal cursorbind&
set scrollopt=ver,hor,jump
setlocal wrap&
let fdm = exists('b:stashFoldMethod') ? b:stashFoldMethod : 'marker'
exec "setlocal foldmethod=" . fdm
setlocal foldcolumn&
else
echomsg 'Not in diff mode.'
endif
endf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment