This guide explains how to set up a pre-commit hook to automatically format .xml
, .html
, and .htm
files using the xmllint
tool. Pre-commit hooks ensure files are formatted correctly before being committed to version control, maintaining consistency across your repository.
NOTE: The script has been modified to work with only GitBash in 2.0.2
IF you need the script to work with other versions, you might need to try 2.0.0/2.0.1 (bash - depending on where it is),or sh (version 1.0.1).
xmllint
is a command-line tool for validating and formatting XML files. It ensures files are formatted correctly according to standard XML conventions. In this guide, we'll integrate xmllint
into a pre-commit hook to automatically format .xml
, .html
, and .htm
files.
-
format-xmllint
:- Formats
.xml
,.html
, and.htm
files usingxmllint --format
. - Overwrites the files if they are not properly formatted.
- Formats
-
check-xmllint
:- Checks if the files are formatted according to
xmllint --format
. - Note: We recommend using
format-xmllint
, ascheck-xmllint
only verifies formatting without fixing it.
- Checks if the files are formatted according to
Before setting up the hook, you'll need to install the pre-commit
tool. Here's how to install it on different environments:
-
Open your terminal.
-
Install
pre-commit
using Homebrew:brew install pre-commit
-
Verify the installation:
pre-commit --version
-
Open your terminal.
-
Install
pre-commit
using pip (Python package manager):pip install pre-commit
-
Verify the installation:
pre-commit --version
-
On some distributions, you may need to install additional dependencies:
sudo apt install python3-pip
-
If you don't have Python installed, download and install Python, which comes with pip.
-
Open PowerShell or Command Prompt.
-
Install
pre-commit
using pip:pip install pre-commit
-
Verify the installation:
pre-commit --version
If you prefer Docker, you can run pre-commit
in a Docker container:
docker run --rm -v $(pwd):/repo pre-commit run --all-files
Ensure that xmllint
is installed on your system. Here’s how to install it on different operating systems:
brew install libxml2
sudo apt install libxml2-utils
You can use Windows Subsystem for Linux (WSL) or a package manager like Cygwin to install xmllint
. In WSL, run:
sudo apt install libxml2-utils
Verify that xmllint
is available by running:
xmllint --version
In the root directory of your repository, create or update a .pre-commit-config.yaml
file with the following configuration:
repos:
- repo: https://github.com/Patrick-Bonini/xml-format-githook
rev: v1.0.1
hooks:
- id: format-xmllint
repo:
The repository URL hosting the pre-commit hook.rev:
The version of the hook to use (v1.0.1
in this example).id:
The identifier for the hook (format-xmllint
).files:
The regex pattern that specifies which file types the hook should target (.xml
,.html
, and.htm
).
After configuring the .pre-commit-config.yaml
file, install the pre-commit hook by running:
pre-commit install
This will ensure that every time you make a commit, the hook will check and format any .xml
, .html
, and .htm
files before committing them.
You can manually trigger the pre-commit hook to check and format all files in your repository by running:
pre-commit run --all-files
- When you attempt to commit files, the hook will automatically run and check the formatting of any
.xml
,.html
, or.htm
files. - If the files are not properly formatted,
format-xmllint
will reformat them, and you’ll need to stage the corrected files and commit again. - This process ensures that your repository maintains consistent and correct formatting.
-
xmllint
not found: Ensure thatxmllint
is installed and accessible via your command line. -
Hook not running on
.html
or.htm
files: Double-check that your.pre-commit-config.yaml
file is correctly targeting the file extensions. -
Permission errors: Make sure the
check-xmllint
script is executable by running:chmod +x check-xmllint
By setting up the xmllint
pre-commit hook, you ensure that your .xml
, .html
, and .htm
files are always formatted correctly before they are committed. This automated process helps maintain code quality, consistency, and readability across your project.
For further assistance or questions, feel free to reach out to your development team or consult the pre-commit documentation.