cho45 (owner)

Forks

Revisions

gist: 197936 Download_button fork
public
Public Clone URL: git://gist.github.com/197936.git
Embed All Files: show embed
git_prev_commit.vim #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
" 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