Source

scripts-hg / hgeditor

#!/bin/sh
#
# This is an example of using HGEDITOR to create of diff to review the
# changes while commiting.  Written by Matt Mackall and others under GPLv2+.

# set envvar that can be useful in the editor
REPO="$PWD"; export REPO
HGTMP=""

cleanup_exit() {
    rm -rf "$HGTMP"
}

# Remove temporary files even if we get interrupted
trap "cleanup_exit" 0 # normal exit
trap "exit 255" HUP INT QUIT ABRT TERM

HGTMP=$(mktemp -d ${TMPDIR-/tmp}/hgeditor.XXXXXX)
[ x$HGTMP != x -a -d $HGTMP ] || {
  echo "$0: could not create temporary directory, exiting" 1>&2
  exit 1
}

(
    grep '^HG: changed' "$1" | cut -b 13- | while read changed; do
        "$HG" diff "$changed" >> "$HGTMP/diff"
    done
)

cat "$1" > "$HGTMP/message"

MD5=$(which md5sum 2>/dev/null) || \
    MD5=$(which md5 2>/dev/null)
[ -x "${MD5}" ] && CHECKSUM=`${MD5} "$HGTMP/message"`
if [ -s "$HGTMP/diff" ]; then
    # This buftype + vsplit combo makes vim quit when the diff buffer is
    # closed, even if the diff buffer is not
    vim -f "$HGTMP/diff" +"set buftype=help" +"vsplit $HGTMP/message" +"set textwidth=72" || exit $?
else
    vim -f "$HGTMP/message" +"set textwidth=72" || exit $?
fi

[ -x "${MD5}" ] && (echo "$CHECKSUM" | ${MD5} -c >/dev/null 2>&1 && exit 13)

mv "$HGTMP/message" "$1"

exit $?