Skip to main content

Pre-commit hook that syncs markdown snippets from a central repository

Project description

pre-commit-snippets

PyPI CI Documentation License: MIT

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-snippets
    rev: v1.0.4  # Use the latest tag
    hooks:
      - id: snippet-sync

Manual installation

  1. Clone this repository
  2. Create a .pre-commit-snippets-config.yaml configuration file (see below)
  3. Run python main.py before committing

Configuration

Create a .pre-commit-snippets-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

# Path for the snippet hash cache file (optional, default: .snippet-hashes.json)
cache_path: .snippet-hashes.json

# 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:

  1. Clone the snippet repository (shallow clone)
  2. Find all SNIPPET-START / SNIPPET-END marker pairs
  3. Replace the content between markers with the corresponding snippet file
  4. Cache hashes to avoid rewriting unchanged blocks
  5. Stage any modified files

Command Line Options

pre-commit-snippets [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)
--config PATH Path to config file (default: .pre-commit-snippets-config.yaml in repo root)

Examples

# Normal run
pre-commit-snippets

# Preview what would change
pre-commit-snippets --dry-run

# See detailed processing info
pre-commit-snippets --verbose

# Debug issues with full detail
pre-commit-snippets --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-snippets/
├── 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pre_commit_snippets-2.0.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pre_commit_snippets-2.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file pre_commit_snippets-2.0.1.tar.gz.

File metadata

  • Download URL: pre_commit_snippets-2.0.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pre_commit_snippets-2.0.1.tar.gz
Algorithm Hash digest
SHA256 e2bde7b30571ffa48a31629d49580ea3ccf0d9189ac71cef398bc70d445258b8
MD5 d269da6166064100a7bb9b00db2edf04
BLAKE2b-256 4e9328a2fc74eb0026c48c07975c5269238cdc05e51378eff278829258bbc0cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pre_commit_snippets-2.0.1.tar.gz:

Publisher: release-please.yaml on RemoteRabbit/pre-commit-snippets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pre_commit_snippets-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pre_commit_snippets-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b5184fd9b4f3a01518da6a2610bc4a69582eb4505d180c6302e367be75a62d8
MD5 a6536a22a45d47240a5e0cf8a643b776
BLAKE2b-256 907c130b4c683eb4ae6a628c6f3d591ffac6f25ca504612d15efd9e627d3be56

See more details on using hashes here.

Provenance

The following attestation bundles were made for pre_commit_snippets-2.0.1-py3-none-any.whl:

Publisher: release-please.yaml on RemoteRabbit/pre-commit-snippets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page