Instantly share code, notes, and snippets.

What would you like to do?
" 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]
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment