-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-vdiff
executable file
·38 lines (31 loc) · 946 Bytes
/
git-vdiff
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
34
35
36
37
38
#!/usr/bin/env bash
# Lets you view the regular 'git diff' output in Vim and jump to the relevant
# file by pressing Enter
git diff --quiet && exit
tmpfile=$(mktemp)
vimfile=$(mktemp)
git diff "$@" > "$tmpfile"
cd "$(git rev-parse --show-toplevel)"
cat > "$vimfile" <<'EOF'
function! Jump()
if getline('.')[0:3] == 'diff'
let diffline = line('.')
else
let diffline = search('^diff --git', 'bn')
endif
let file = getline(diffline + 3)[6:]
if getline('.')[0:1] == '@@'
let alfaline = line('.')
elseif line('.') >= diffline && line('.') <= diffline + 3
let alfaline = diffline + 4
else
let alfaline = search('^@@', 'bn')
endif
let linenumber = matchstr(getline(alfaline), '+\zs\d\+') + 3
execute 'edit' '+'.linenumber file
endfunction
nnoremap <buffer> <Enter> :call Jump()<Enter>
setlocal filetype=diff
EOF
vim -S "$vimfile" "$tmpfile"
rm "$tmpfile" "$vimfile"