-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-vdiff
More file actions
executable file
·57 lines (48 loc) · 1.57 KB
/
git-vdiff
File metadata and controls
executable file
·57 lines (48 loc) · 1.57 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
# Lets you view the regular 'git diff' output in Vim and jump to the relevant
# file by pressing Enter
worktree_root=$(git rev-parse --show-toplevel) || exit
git diff --quiet "$@" >/dev/null 2>&1 && exit
tmpdir=$(mktemp -d) && trap 'rm -rf "$tmpdir"' EXIT || exit
cat > "$tmpdir/jump.vim" <<'EOF'
function! Jump()
let selected_lineno = line('.')
let linecount = 0
if getline('.')[0:9] == 'diff --git'
let diffline = selected_lineno
else
let diffline = search('^diff --git', 'bn')
endif
let plusline = diffline + 1
while getline(plusline)[0:3] != '+++ '
let plusline += 1
endwhile
let file = getline(plusline)[6:]
if selected_lineno <= plusline
let alfaline = plusline + 1
elseif getline('.')[0:1] == '@@'
let alfaline = selected_lineno
else
let alfaline = search('^@@', 'bn')
endif
for line in getline(alfaline + 1, selected_lineno)
if line[0] != '-'
let linecount += 1
endif
endfor
let linenumber = matchstr(getline(alfaline), '+\zs\d\+') + linecount - 1
execute 'split' '+call\ cursor('.linenumber.','.(col('.') - 1).')' file
endfunction
nnoremap <buffer> <Enter> :call Jump()<Enter>
nnoremap <buffer> <C-l> :!eval "$VDIFF_CMD"<Enter>
setlocal filetype=diff
set autoread
EOF
printf -v VDIFF_CMD 'cd %q && git diff %s > %q' \
"$PWD" \
"$(for arg ; do printf '%q ' "$arg" ; done)" \
"$tmpdir/diff"
export VDIFF_CMD
git diff "$@" > "$tmpdir/diff" &&
cd -- "${worktree_root:?}" &&
vim -S "$tmpdir/jump.vim" "$tmpdir/diff"