dispatch: handle IOError when writing to stderr
Previously, attempts to write to stderr in dispatch.run() may lead to
an exception being thrown. This would likely be handled by Python's
default exception handler, which would print the exception and exit
1.
Code in this function is already catching IOError for stdout failures
and converting to exit code 255 (-1 & 255 == 255). Why we weren't
doing the same for stderr for the sake of consistency, I don't know.
I do know that chg and hg diverged in behavior here (as the changed
test-basic.t shows).
After this commit, we catch I/O failure on stderr and change the
exit code to 255. chg and hg now behave consistently. As a bonus,
Rust hg also now passes this test.
I'm skeptical at changing the exit code due to failures this late
in the process. I think we should consider preserving the current
exit code - assuming it is non-0. And, we may want to preserve the
exit code completely if the I/O error is EPIPE (and potentially
other special error classes). There's definitely room to tweak
behavior. But for now, let's at least prevent the uncaught exception.
Differential Revision: https://phab.mercurial-scm.org/D1860
#!/bin/sh
#
# This is an example of using HGEDITOR to create of diff to review the
# changes while committing.
# If you want to pass your favourite editor some other parameters
# only for Mercurial, modify this:
case "${EDITOR}" in
"")
EDITOR="vi"
;;
emacs)
EDITOR="$EDITOR -nw"
;;
gvim|vim)
EDITOR="$EDITOR -f -o"
;;
esac
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 "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/msg"
MD5=$(which md5sum 2>/dev/null) || \
MD5=$(which md5 2>/dev/null)
[ -x "${MD5}" ] && CHECKSUM=`${MD5} "$HGTMP/msg"`
if [ -s "$HGTMP/diff" ]; then
$EDITOR "$HGTMP/msg" "$HGTMP/diff" || exit $?
else
$EDITOR "$HGTMP/msg" || exit $?
fi
[ -x "${MD5}" ] && (echo "$CHECKSUM" | ${MD5} -c >/dev/null 2>&1 && exit 13)
mv "$HGTMP/msg" "$1"
exit $?