Pre-commit hook that syncs markdown snippets from a central repository
Project description
pre-commit-snippet
A pre-commit hook that automatically syncs markdown snippets from a central repository into your documentation files.
Features
- Marker-based replacement: Uses
<!-- SNIPPET-START: name -->/<!-- SNIPPET-END -->markers to identify replaceable blocks - SHA-256 caching: Only rewrites blocks when the central snippet has actually changed, avoiding unnecessary file churn
- Automatic staging: Modified files are automatically staged for commit
- Branch/tag support: Pin snippets to a specific branch or tag
- Dry-run mode: Preview changes without modifying files
- Debug logging: Detailed logging for troubleshooting
Installation
Using pre-commit framework
Add this to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/RemoteRabbit/pre-commit-snippet
rev: v1.0.4 # Use the latest tag
hooks:
- id: snippet-sync
Manual installation
- Clone this repository
- Create a
pre-commit-snippet-config.yamlconfiguration file (see below) - Run
python main.pybefore committing
Configuration
Create a pre-commit-snippet-config.yaml file in your repository root:
# URL of the repository containing your snippets (required)
snippet_repo: https://github.com/your-org/snippets.git
# Branch or tag to use (optional, default: default branch)
snippet_branch: main
# Subdirectory within the snippet repo where snippets are stored (optional)
snippet_subdir: snippets
# File extension for snippet files (optional, default: .md)
snippet_ext: .md
# List of files to process (required)
target_files:
- README.md
- docs/CONTRIBUTING.md
- docs/SECURITY.md
Usage
In your markdown files, wrap the areas you want to sync with snippet markers:
# My Project
<!-- SNIPPET-START: license-notice -->
This content will be replaced with the contents of `license-notice.md` from your snippet repo.
<!-- SNIPPET-END -->
## Other content...
When the hook runs, it will:
- Clone the snippet repository (shallow clone)
- Find all
SNIPPET-START/SNIPPET-ENDmarker pairs - Replace the content between markers with the corresponding snippet file
- Cache hashes to avoid rewriting unchanged blocks
- Stage any modified files
Command Line Options
pre-commit-snippet [OPTIONS]
# or
python main.py [OPTIONS]
| Option | Description |
|---|---|
--dry-run |
Preview changes without modifying files |
--verbose, -v |
Print info-level logs (files being processed, updates) |
--debug |
Print debug-level logs with timestamps (commands, hashes, paths) |
Examples
# Normal run
pre-commit-snippet
# Preview what would change
pre-commit-snippet --dry-run
# See detailed processing info
pre-commit-snippet --verbose
# Debug issues with full detail
pre-commit-snippet --debug
Cache File
The hook creates a .snippet-hashes.json file to track which snippets have been applied. You should commit this file to avoid unnecessary rewrites on other machines.
Project Structure
pre-commit-snippet/
├── main.py # CLI entry point
├── pre_commit_snippet/
│ ├── __init__.py # Package init
│ ├── cache.py # Hash computation and caching
│ ├── cli.py # Argument parsing and main logic
│ ├── config.py # Configuration loading
│ ├── git.py # Git operations
│ ├── logging.py # Logging configuration
│ └── snippet.py # Snippet replacement logic
└── tests/
└── test_main.py # Test suite
Development
See CONTRIBUTING.md for development setup, conventional commits, and release process.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pre_commit_snippets-1.0.6.dev20260115221749.tar.gz.
File metadata
- Download URL: pre_commit_snippets-1.0.6.dev20260115221749.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
309d30f041d2993506d5b94e01a242b95b47c576fd1882d0be83eba8dcafa2e0
|
|
| MD5 |
cd7ea84109909de0dddc84f168dfb1b2
|
|
| BLAKE2b-256 |
b28cfd3664f532a4b7e0dd4787afa0e447cbc40ebc4f261abad9084ce31c3ed7
|
Provenance
The following attestation bundles were made for pre_commit_snippets-1.0.6.dev20260115221749.tar.gz:
Publisher:
publish-beta.yaml on RemoteRabbit/pre-commit-snippets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pre_commit_snippets-1.0.6.dev20260115221749.tar.gz -
Subject digest:
309d30f041d2993506d5b94e01a242b95b47c576fd1882d0be83eba8dcafa2e0 - Sigstore transparency entry: 829385167
- Sigstore integration time:
-
Permalink:
RemoteRabbit/pre-commit-snippets@2d0e2cc3314ac576eb0d8111ab17803b37d7d2bf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/RemoteRabbit
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-beta.yaml@2d0e2cc3314ac576eb0d8111ab17803b37d7d2bf -
Trigger Event:
push
-
Statement type:
File details
Details for the file pre_commit_snippets-1.0.6.dev20260115221749-py3-none-any.whl.
File metadata
- Download URL: pre_commit_snippets-1.0.6.dev20260115221749-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b5ccb5e2e8d99ee5f5cabddc2a2811979a194db2d391dd17048d31c53ee6d69
|
|
| MD5 |
3502c2bebaffa329f5fafb9714c83f7f
|
|
| BLAKE2b-256 |
c184078164a4d3f5ddb08f6b406c06282537f80001ef09db39e02079bc426e8c
|
Provenance
The following attestation bundles were made for pre_commit_snippets-1.0.6.dev20260115221749-py3-none-any.whl:
Publisher:
publish-beta.yaml on RemoteRabbit/pre-commit-snippets
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pre_commit_snippets-1.0.6.dev20260115221749-py3-none-any.whl -
Subject digest:
6b5ccb5e2e8d99ee5f5cabddc2a2811979a194db2d391dd17048d31c53ee6d69 - Sigstore transparency entry: 829385170
- Sigstore integration time:
-
Permalink:
RemoteRabbit/pre-commit-snippets@2d0e2cc3314ac576eb0d8111ab17803b37d7d2bf -
Branch / Tag:
refs/heads/main - Owner: https://github.com/RemoteRabbit
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-beta.yaml@2d0e2cc3314ac576eb0d8111ab17803b37d7d2bf -
Trigger Event:
push
-
Statement type: