Skip to content

Instantly share code, notes, and snippets.

@cho45
Created September 30, 2009 09:27
Show Gist options
  • Save cho45/197936 to your computer and use it in GitHub Desktop.
Save cho45/197936 to your computer and use it in GitHub Desktop.
" git で現在開いているファイルの前回の変更を開く。(require metarw-git)
nnoremap <silent> eg :<C-u>call <SID>git_prev_rev()<CR>
function! s:git_prev_rev()
let path = expand('%')
let commit = "HEAD"
let l = matchlist(path, 'git:\([^:]\+\):\([^:]\+\)')
if len(l) > 0
let commit = l[1]
let path = l[2]
endif
let output = system(printf("git log -n 2 --pretty=format:'%%h %%s' HEAD~1000..%s -- %s",
\ shellescape(commit),
\ shellescape(path)))
if v:shell_error != 0
echoerr 'git log failed with the following reason:'
echoerr output
return
endif
let commits = split(output, "\n")
let prev = commits[1]
let [commit_id, subject] = matchlist(prev, '^\(\S*\)\s\(.*\)$')[1:2]
let cursor = getpos(".")
silent edit `=printf('git:%s:%s', commit_id, path)`
call setpos('.', cursor)
echo subject
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment