Size: 3188
Comment: another way to add a commit message template
|
Size: 3192
Comment: converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 6: | Line 6: |
Here's an example patch for [:hgeditor] that adds a user-defined template to the [:Commit:commit] message: | Here's an example patch for [[hgeditor]] that adds a user-defined template to the [[Commit|commit]] message: |
Line 23: | Line 23: |
Validating that the commit message is correct can either be done by hgeditor or enforced by a precommit [:Hook:hook]. | Validating that the commit message is correct can either be done by hgeditor or enforced by a precommit [[Hook|hook]]. |
Line 27: | Line 27: |
The following pair of scripts respects user's chosen [:editor]. | The following pair of scripts respects user's chosen [[editor]]. |
Adding a commit message template
Mercurial calls a user-defined program to edit commit messages. If that program returns false, the commit is aborted.
Here's an example patch for hgeditor that adds a user-defined template to the commit message:
diff -r 33988aaa1652 hgeditor --- a/hgeditor Sun Dec 17 22:16:57 2006 -0600 +++ b/hgeditor Tue Dec 19 08:08:57 2006 -0600 @@ -39,6 +39,7 @@ HGTMP="${TMPDIR-/tmp}/hgeditor.$RANDOM.$ done ) -cat "$1" > "$HGTMP/msg" +cat `hg root`/.commit-template > "$HGTMP/msg" +cat "$1" >> "$HGTMP/msg" MD5=$(which md5sum 2>/dev/null) || \
Validating that the commit message is correct can either be done by hgeditor or enforced by a precommit hook.
Another way
The following pair of scripts respects user's chosen editor.
A usage scenatio is a reposotory with main branch being the "default" branch. Users create named side branches, and when work is done (including code review) they merge to default branch and push to central repository. Users call "hg_commit_default" to ensure that the merge commit goes to default branch (it is not hard to make a mistake) and are presented with commit template that contains the recommended fields. Users may elect to delete some fields if they don't apply - template is not enforced.
Script hg_commit_default:
# # Commit to default branch. # Ensure that branch is default. # Provide commit message template. # # TODO: Disallow commit message passed with -m or -l. # Perhaps include it in the template? # BRANCH=`hg branch` || exit $? if [ "$BRANCH" != default ] ; then echo "Branch $BRANCH is not default branch" >&2 exit 1 fi # Save the value to be overridden export HGEDITOR_ORIG="$HGEDITOR" HGEDITOR=hg_templated_editor exec hg commit "$@"
Script hg_templated_editor:
# # Called by hg_commit_default. # Calls configured commit editor with a template commit message. # FILE="$1" TMP="$FILE.hg_templated_editor.$$.txt" HG_CONF_EDITOR=`hg showconfig ui.editor` if [ "$HGEDITOR_ORIG" ] ; then ED="$HGEDITOR_ORIG" elif [ "$HG_CONF_EDITOR" ] ; then ED="$HG_CONF_EDITOR" elif [ "$VISUAL" ] ; then ED="$VISUAL" elif [ "$EDITOR" ] ; then ED="$EDITOR" else ED=vi fi cleanup_exit() { /bin/rm -f "$TMP" } # Cleanup temporary file on exit or abnormal interruption trap "cleanup_exit" 0 # normal exit trap "exit 255" 1 2 3 6 15 # HUP INT QUIT ABRT TERM # $TMP should match the permissions of $FILE umask 077 # Prepend the custom template. The first line is empty. echo "" > "$TMP" || exit $? echo "Bug: " >> "$TMP" echo "Reviewer: " >> "$TMP" echo "Tests: " >> "$TMP" # Append the original template. # grep removes the first empty line. grep -v '^$' "$FILE" >> "$TMP" CHECKSUM=`md5sum "$TMP"` $ED "$TMP" || exit $? # Detect if no change was made to the commit message echo "$CHECKSUM" | md5sum -c --status && exit 0 # On exit 0 original $FILE remains unchanged and hg will complain about that. /bin/mv "$TMP" "$FILE"