Skip to content

Instantly share code, notes, and snippets.

@jeenuv
Created November 9, 2015 09:50
Show Gist options
  • Select an option

  • Save jeenuv/a1a1e32d86d941adc955 to your computer and use it in GitHub Desktop.

Select an option

Save jeenuv/a1a1e32d86d941adc955 to your computer and use it in GitHub Desktop.
Examine git conflict by opening ancestors in separate Vim windows
#!/bin/bash
#
# One file name is expected as arguent, which is assumed to be a file in
# conflict, after a merge or cherry-pick. This script opens the file that
# belongs to the common ancestor, our (the target branch's) copy, their
# (incoming commit), and the current copy (conflicted) in a layout below
#
# +---------------------+
# | common |
# +---------+-----------+
# | our | their |
# +---------+-----------+
# | current |
# +---------+-----------+
file="$1"
if [ -z "$file" ]; then
echo "Usage: $0 <path>"
exit
fi
common="$(mktemp)"
if ! git show ":1:$file" >"$common" 2>/dev/null; then
echo "Error getting common ancestor"
exit 1
fi
our="$(mktemp)"
if ! git show ":2:$file" >"$our" 2>/dev/null; then
echo "Error getting our"
exit 1
fi
their="$(mktemp)"
if ! git show ":3:$file" >"$their" 2>/dev/null; then
echo "Error getting their"
exit 1
fi
# Create a vim script open files in the layout above
vims="$(mktemp)"
cat > "$vims" <<EOF
try
" Create layout
new
new
2wincmd k
wincmd j
vnew
" Move to top window and open common ancestor
wincmd k
silent! e $common
0
file common
" Move to middle-left window and open our copy
wincmd j
wincmd h
silent! e $our
0
file our
" Move to right window and open their copy
wincmd l
silent! e $their
0
file their
" Set scroll bind, and non-modifiable for all windows
windo set scb noma nomod
" Finally open the current file. Reset scroll bind and make modifiable
wincmd j
set noscb ma
silent! e $file
windo 0
catch
qa!
endtry
" Make all windows jump to first conflict marker, if any
2wincmd j
try
exec "silent normal /^<<<<<<<\\<CR>"
catch
finally
exec "windo " . line(".")
endtry
EOF
set -x
exec vim -S "$vims"
# vim: set sw=2 tw=80:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment