Skip to content

Commit ee9ff77

Browse files
committed
Add check-restored-packages pre-commit hook
Signed-off-by: Łukasz 'sil2100' Zemczak <lukasz.zemczak@chainguard.dev>
1 parent 626b499 commit ee9ff77

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@
2020
- manual
2121
types:
2222
- yaml
23+
- id: check-restored-packages
24+
name: Check restored packages against withdrawn
25+
description: Ensures packages being added to restored-packages.txt are removed from withdrawn-packages.txt first
26+
entry: scripts/check-restored-packages.sh
27+
language: script
28+
stages:
29+
- pre-commit
30+
- manual
31+
files: ^restored-packages\.txt$

scripts/check-restored-packages.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Check if restored-packages.txt is being modified
5+
if ! git diff --cached --name-only | grep -q "restored-packages.txt"; then
6+
exit 0
7+
fi
8+
9+
if [ ! -f "withdrawn-packages.txt" ] || [ ! -f "restored-packages.txt" ]; then
10+
exit 0
11+
fi
12+
13+
# Get the new lines being added to restored-packages.txt
14+
NEW_PACKAGES=$(git diff --cached "restored-packages.txt" | grep "^+" | grep -v "^+++" | sed 's/^+//' | grep -v '^$' | grep -v '^#')
15+
16+
if [ -z "$NEW_PACKAGES" ]; then
17+
exit 0
18+
fi
19+
20+
# Check if any new packages are in withdrawn-packages.txt
21+
CONFLICTS=""
22+
while IFS= read -r package; do
23+
if [ -n "$package" ]; then
24+
if grep -Fxq "$package" "withdrawn-packages.txt"; then
25+
CONFLICTS="${CONFLICTS}${package}\n"
26+
fi
27+
fi
28+
done <<< "$NEW_PACKAGES"
29+
30+
if [ -n "$CONFLICTS" ]; then
31+
echo "ERROR: The following packages are being added to restored-packages.txt but are still present in withdrawn-packages.txt:"
32+
echo -e "$CONFLICTS"
33+
echo "Please remove these packages from withdrawn-packages.txt first, then commit again."
34+
exit 1
35+
fi
36+
37+
exit 0

0 commit comments

Comments
 (0)