Package: precommit-message-preservation
precommit-message-perservationThis is a simple library that makes it easier to code hooks forpre-committhat validate commit messages that preserve the commit message on failure.
In other words, if the user writes a long commit message and your pre-commit hook tells them the message is bad, they won't have their message entirely thrown away.For usersYou need to make sure that you include this hook in your list of hooks that prepare commit messages, ideally the first entry. This allows the hook to populate your editor with previously saved commit messages. You can install the hook by adding something like this to your.pre-commit-config.yamlfile:...repos:-repo:https://github.com/EliRibble/precommit-message-preservation.gitrev:1.0hooks:-id:precommit-message-preservationstages:[prepare-commit-msg]...Any pre-commit hook that uses this library when analyzing commit messages will dump rejected commit messages into the SQLite database at$XDG_CACHE_HOME/precommit-message-preservation.db. Theprecommit-message-preservationhook will then pull out these commit messages when the repositoryand branchmatch and populate your editor with the saved message.For developersIf you are working on apre-commithook that analyzes (and rejects) commit messages then you can use this library to improve your user experience. Normally whenpre-commithooks fail oncommit-msgthe contents of the commit message are thrown away. Instead you can use this library to save the messages for your users. To use it follow these three simple stepsStep 1: Add dependencyDepending on your packaging software you'll edit something likesetup.pyorsetup.cfgand add a dependency on this library.Step 2: Initialize your argument parserimport argparse

import precommit_message_preservation

def main():
	parser = argparse.ArgumentParser()
	precommit_message_preservation.add_arguments(parser)You need to create anArgumentParserand letprecommit-message-preservationpopulate it with some standard arguments. These are used internally in the next step.Step 3: Analyze the commitargs = parser.parse_args()
try:
	with precommit_message_preservation.GetAndPreserveMessage(args, hookname="my hook") as message:
		# do analysis on 'message'
except:
	print("your message is inadequate because X, Y, and Z")
	sys.exit()precommit-message-preservationexpects to know whether or not it should keep the message based on whether or not an exception was emitted within theGetAndPreserveMessagecontext manager. This context manager extracts the commit message using the arguments provided inargsand gives the cleaned message back to the calling code. The message will not have any code diffs or comments in it. The context manager immediately saves the message in the database. If it exits cleanly it deletes the message. Otherwise the message is saved and will be provided back to the user on the next commit attempt.HackingYou'll want to install the developer dependencies:pip install -e .[develop]This will includenose2, which is the test runner of choice. After you make modifications you can run tests withnose2When you're satisfied you'll want to update the version number and do build-and-upload:python setup.py sdist bdist_wheel
twine upload dist/* --verbose