Skip to content

Instantly share code, notes, and snippets.

@ptrv
Created August 16, 2014 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptrv/62dedef9269f0d7702d0 to your computer and use it in GitHub Desktop.
Save ptrv/62dedef9269f0d7702d0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# test args
if [ ! ${#} -ge 2 ]; then
echo 1>&2 "Usage: ${0} LOCAL REMOTE [MERGED BASE]"
echo 1>&2 " (LOCAL, REMOTE, MERGED, BASE can be provided by \`git mergetool'.)"
exit 1
fi
# tools
_EMACSCLIENT=/usr/bin/emacsclient
_BASENAME=/usr/bin/basename
_CP=/usr/bin/cp
_EGREP=/usr/bin/egrep
_MKTEMP=/usr/bin/mktemp
# args
_LOCAL=${1}
_REMOTE=${2}
if [ ${3} ] ; then
_MERGED=${3}
else
_MERGED=${_REMOTE}
fi
if [ ${4} -a -r ${4} ] ; then
_BASE=${4}
_EDIFF=ediff-merge-files-with-ancestor
_EVAL="${_EDIFF} \"${_LOCAL}\" \"${_REMOTE}\" \"${_BASE}\" nil \"${_MERGED}\""
elif [ ${_REMOTE} = ${_MERGED} ] ; then
_EDIFF=ediff
_EVAL="${_EDIFF} \"${_LOCAL}\" \"${_REMOTE}\""
else
_EDIFF=ediff-merge-files
_EVAL="${_EDIFF} \"${_LOCAL}\" \"${_REMOTE}\" nil \"${_MERGED}\""
fi
# console vs. X
if [ "${TERM}" = "linux" ]; then
unset DISPLAY
_EMACSCLIENTOPTS="-t"
else
_EMACSCLIENTOPTS="-c"
fi
_EMACSCLIENTOPTS="-c"
# run emacsclient
# ${_EMACSCLIENT} ${_EMACSCLIENTOPTS} -e "(toggle-frame-maximized)" -e "(raise-frame)"-e "(${_EVAL})" 2>&1
${_EMACSCLIENT} ${_EMACSCLIENTOPTS} -e "(toggle-frame-maximized)" -e "(${_EVAL})" 2>&1
# check modified file
if [ ! $(${_EGREP} -c '^(<<<<<<<|=======|>>>>>>>|####### Ancestor)' ${_MERGED}) = 0 ]; then
_MERGEDSAVE=$(${_MKTEMP} --tmpdir `${_BASENAME} ${_MERGED}`.XXXXXXXXXX)
${_CP} ${_MERGED} ${_MERGEDSAVE}
echo 1>&2 "Oops! Conflict markers detected in $_MERGED."
echo 1>&2 "Saved your changes to ${_MERGEDSAVE}"
echo 1>&2 "Exiting with code 1."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment