Skip to content

Instantly share code, notes, and snippets.

@Rob--W
Last active August 29, 2015 14:04
Show Gist options
  • Save Rob--W/6fac402c955625639808 to your computer and use it in GitHub Desktop.
Save Rob--W/6fac402c955625639808 to your computer and use it in GitHub Desktop.
"git commit -v" for hg (Mecurial) (appends the diff of the changeset to the hg message)
[ui]
# Usage: Assuming that you've saved hgeditor as ~/.mercurial/hgeditor
editor = ~/.mercurial/hgeditor
#!/bin/sh
# Append the diff to hg commit, like git commit -v
# Based on hgeditor downloaded on 19 July 2014 from http://mercurial.selenic.com/wiki/hgeditor
# Completely rewritten though.
#
# Known issue: Does not work for hg commit --amend
#
# Created by Rob Wu <rob@robwu.nl> (https://robwu.nl)
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
}
DIFFLINE='# ------------------------ >8 ------------------------'
sed 's/^HG:/#HG:/' "$1" > "$HGTMP/msg"
>>/"$HGTMP/msg" cat <<DIFFHEADER
$DIFFLINE
# Do not touch the line above.
# Everything below will be removed.
DIFFHEADER
(
# GNU grep
grep -oP '^HG: (added|changed|removed)\K.+' "$1" | while read changed; do
"$HG" diff --git "$changed" >> "$HGTMP/msg"
done
)
MD5=$(which md5sum 2>/dev/null) || \
MD5=$(which md5 2>/dev/null)
[ -x "${MD5}" ] && CHECKSUM=`${MD5} "$HGTMP/msg"`
case "${EDITOR}" in
"")
vi "$HGTMP/msg" +'set syntax=diff' || $?
;;
emacs)
$EDITOR -nw "$HGTMP/msg" || exit $?
;;
gvim|vim)
$EDITOR -f -o "$HGTMP/msg" +'set syntax=diff' || exit $?
;;
esac
[ -x "${MD5}" ] && (echo "$CHECKSUM" | ${MD5} -c >/dev/null 2>&1 && exit 13)
# Escape DIFFLINE, just in case anyone ever wants to put metacharacters in it.
DIFFLINE=$(sed 's/[\*\.]/\\&/g' <<<"$DIFFLINE")
sed "0,/$DIFFLINE/p; d" "$HGTMP/msg" | \
sed "/$DIFFLINE/d" | \
sed "s/^#HG:/HG:/" \
> "$1"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment