Skip to main content

precommit-message-perservation

This is a simple library that makes it easier to code hooks for pre-commit that 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 users

You 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.yaml file:

...
repos:
-   repo: https://github.com/EliRibble/precommit-message-preservation.git
    rev: 1.0
    hooks:
    -   id: precommit-message-preservation
        stages: [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. The precommit-message-preservation hook will then pull out these commit messages when the repository and branch match and populate your editor with the saved message.

For developers

If you are working on a pre-commit hook that analyzes (and rejects) commit messages then you can use this library to improve your user experience. Normally when pre-commit hooks fail on commit-msg the 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 steps

Step 1: Add dependency

Depending on your packaging software you'll edit something like setup.py or setup.cfg and add a dependency on this library.

Step 2: Initialize your argument parser

import argparse

import precommit_message_preservation

def main():
	parser = argparse.ArgumentParser()
	precommit_message_preservation.add_arguments(parser)

You need to create an ArgumentParser and let precommit-message-preservation populate it with some standard arguments. These are used internally in the next step.

Step 3: Analyze the commit

args = 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-preservation expects to know whether or not it should keep the message based on whether or not an exception was emitted within the GetAndPreserveMessage context manager. This context manager extracts the commit message using the arguments provided in args and 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.

Hacking

You'll want to install the developer dependencies:

pip install -e .[develop]

This will include nose2, which is the test runner of choice. After you make modifications you can run tests with

nose2

When 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

Metadata

Release files for precommit-message-preservation 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for precommit-message-preservation 1.3
File Size Uploaded Provenance
precommit_message_preservation-1.3.tar.gz 6.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for precommit-message-preservation 1.3
File Interpreter ABI Platform
precommit_message_preservation-1.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 14.5 kB

Release files / precommit_message_preservation-1.3.tar.gz

Download URL precommit_message_preservation-1.3.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
13510425cf145b58290d7491b1a656638aa554e9538228b0ea0c292bb5f8f3f3
BLAKE2b-256 checksum
How to use checksums
c53f0baf47988ddb01aab529761bbf17fdd582d8de285a9c33ec4328dd0dfc2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Release files / precommit_message_preservation-1.3-py2.py3-none-any.whl

Download URL precommit_message_preservation-1.3-py2.py3-none-any.whl
Size 7.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
14f569616848d7e9d2356bd2f310421960da749fba28982ed6cbbf6ba29acd13
BLAKE2b-256 checksum
How to use checksums
a3f04cddb6626d815ffe646c67b2f5566771d64f1bc27c32a72a2dfae0f50b7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

Security for precommit-message-preservation 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page