-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffconflicts
More file actions
executable file
·93 lines (84 loc) · 3.2 KB
/
diffconflicts
File metadata and controls
executable file
·93 lines (84 loc) · 3.2 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
# A better vimdiff mergetool for Git
#
# NOTE: Now also an installable Vim plugin:
# https://github.com/whiteinge/diffconflicts
#
# Actual source:
# http://vim.wikia.com/wiki/A_better_Vimdiff_Git_mergetool
#
# Add this mergetool to your ~/.gitconfig (you can substitute gvim for vim):
#
# git config --global merge.tool diffconflicts
# git config --global mergetool.diffconflicts.cmd 'diffconflicts vim $BASE $LOCAL $REMOTE $MERGED'
# git config --global mergetool.diffconflicts.trustExitCode true
# git config --global mergetool.diffconflicts.keepBackup false
#
# The next time you perform a merge with conflicts, invoke this tool with the
# following command. (Of course you can set it as your default mergetool as
# well.)
#
# git mergetool --tool diffconflicts
#
# This tool can open three tabs in Vim that each provide a different way to
# view the conflicts. You can resolve the conflicts in the first tab and save
# and exit the file. This will also mark the conflict as resolved in Git.
# Only the first tab is opened by default so Vim loads more quickly and also
# because the other tabs are only occasionally useful for tough merges. To open
# Tab2 and Tab3 use the mapping <leader>D.
#
# Tab1 is a two-way diff of just the conflicts. Resolve the conflicts here
# and save the file.
# +--------------------------------+
# | LCONFL | RCONFL |
# +--------------------------------+
# Tab2 is a three-way diff of the original files and the merge base. This is
# the traditional three-way diff. Although noisy, it is occasionally useful
# to view the three original states of the conflicting file before the merge.
# +--------------------------------+
# | LOCAL | BASE | REMOTE |
# +--------------------------------+
# Workflow:
#
# 1. Save your changes to the LCONFL temporary file (the left window on the
# first tab; also the only file that isn't read-only).
# 2. The LOCAL, BASE, and REMOTE versions of the file are available in the
# second tabpage if you want to look at them.
# 3. When vimdiff exits cleanly, the file containing the conflict markers
# will be updated with the contents of your LCONFL file edits.
#
# NOTE: Use :cq to abort the merge and exit Vim with an error code.
if [[ -z $@ || $# != "5" ]] ; then
echo -e "Usage: $0 \$EDITOR \$BASE \$LOCAL \$REMOTE \$MERGED"
exit 1
fi
cmd="$1"
BASE="$2"
LOCAL="$3"
REMOTE="$4"
MERGED="$5"
LCONFL="${MERGED}.$$.LCONFL"
RCONFL="${MERGED}.$$.RCONFL"
cmdfile=$(mktemp /tmp/vimcmd-XXXXX)
# Always delete our temp files; Git will handle it's own temp files.
trap 'rm -f "'"${LCONFL}"'" "'"${RCONFL}"'" "'"$cmdfile"'"' SIGINT SIGTERM EXIT
# Remove the conflict markers for each 'side' and put each into a temp file
sed -E -e '/^=======\r?$/,/^>>>>>>> /d' -e '/^<<<<<<< /d' "${MERGED}" > "${LCONFL}"
sed -E -e '/^<<<<<<< /,/^=======\r?$/d' -e '/^>>>>>>> /d' "${MERGED}" > "${RCONFL}"
cat <<EOF > $cmdfile
edit $LCONFL
set noro
vert diffs $RCONFL
tabedit $LOCAL
vert diffs $BASE
vert diffs $REMOTE
tabfirst
EOF
# Fire up vimdiff
$cmd -f -R -c "source $cmdfile"
EC=$?
# Overwrite $MERGED only if vimdiff exits cleanly.
if [[ $EC == "0" ]] ; then
cat "${LCONFL}" > "${MERGED}"
fi
exit $EC