Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
entry: hooks/yaml/yaml-extension.sh
language: script
files: \.yml$
- id: yaml-sort
name: "yaml-sort: Style formatting"
description: "Ensure top-level nodes are sorted in YAML files"
entry: hooks/yaml/yaml-sort.sh
language: script
files: \.yaml$
######################
# Yalc related hook
- id: yalc-check
Expand Down
36 changes: 36 additions & 0 deletions hooks/yaml/yaml-sort.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

set -e

yaml_file="$1"

main() {
if [[ ! -f "$yaml_file" ]]; then
echo "Error: $yaml_file not found"
exit 1
fi
if ! command -v yq &> /dev/null; then
echo "Error: yq is required but not installed"
echo "Install with: brew install yq"
exit 1
fi

temp_file=$(mktemp)

yq eval 'sort_keys(.)' "$yaml_file" > "$temp_file"

if [[ $? -eq 0 ]]; then
mv "$temp_file" "$yaml_file"
echo "Sorted top-level keys in $yaml_file."
git add "$yaml_file"
exit 0
else
echo "Error: Failed to sort YAML file"
rm -f "$temp_file"
exit 1
fi
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Loading