| 1 |
#!/usr/bin/env bash |
| 2 |
# A simple script for opening merge conflicts in the editor. |
| 3 |
# Use the following Mercurial settings to enable it. |
| 4 |
# |
| 5 |
# [ui] |
| 6 |
# merge = editmerge |
| 7 |
# |
| 8 |
# [merge-tools] |
| 9 |
# editmerge.args=$output |
| 10 |
# editmerge.check=changed |
| 11 |
# editmerge.premerge=keep |
| 12 |
|
| 13 |
FILE="$1" |
| 14 |
|
| 15 |
getlines() { |
| 16 |
grep -n "^<<<<<<" "$FILE" | cut -f1 -d: |
| 17 |
} |
| 18 |
|
| 19 |
# editor preference loosely based on https://mercurial-scm.org/wiki/editor |
| 20 |
# hg showconfig is at the bottom though, since it's slow to run (0.15 seconds) |
| 21 |
ED="$HGEDITOR" |
| 22 |
if [ "$ED" = "" ] ; then |
| 23 |
ED="$VISUAL" |
| 24 |
fi |
| 25 |
if [ "$ED" = "" ] ; then |
| 26 |
ED="$EDITOR" |
| 27 |
fi |
| 28 |
if [ "$ED" = "" ] ; then |
| 29 |
ED="$(hg showconfig ui.editor)" |
| 30 |
fi |
| 31 |
if [ "$ED" = "" ] ; then |
| 32 |
echo "merge failed - unable to find editor" |
| 33 |
exit 1 |
| 34 |
fi |
| 35 |
|
| 36 |
if [ "$ED" = "emacs" ] || [ "$ED" = "nano" ] || [ "$ED" = "vim" ] ; then |
| 37 |
FIRSTLINE="$(getlines | head -n 1)" |
| 38 |
PREVIOUSLINE="" |
| 39 |
|
| 40 |
# open the editor to the first conflict until there are no more |
| 41 |
# or the user stops editing the file |
| 42 |
while [ ! "$FIRSTLINE" = "" ] && [ ! "$FIRSTLINE" = "$PREVIOUSLINE" ] ; do |
| 43 |
$ED "+$FIRSTLINE" "$FILE" |
| 44 |
PREVIOUSLINE="$FIRSTLINE" |
| 45 |
FIRSTLINE="$(getlines | head -n 1)" |
| 46 |
done |
| 47 |
else |
| 48 |
$ED "$FILE" |
| 49 |
fi |
| 50 |
|
| 51 |
# get the line numbers of the remaining conflicts |
| 52 |
CONFLICTS="$(getlines | sed ':a;N;$!ba;s/\n/, /g')" |
| 53 |
if [ ! "$CONFLICTS" = "" ] ; then |
| 54 |
echo "merge failed - resolve the conflicts (line $CONFLICTS) then use 'hg resolve --mark'" |
| 55 |
exit 1 |
| 56 |
fi |
| 57 |
|
| 58 |
exit 0 |