From 17f19806f7de23212561c879c0f2b003f3958642 Mon Sep 17 00:00:00 2001 From: Geert Uytterhoeven Date: Thu, 22 Aug 2019 15:24:08 +0200 Subject: scripts/pre-commit: Auto-generate keys when needed Generate unique keys for YAML project files lacking them. There are two minor issues left: 1. Due to the "git add", not only the key is added to the file, but also any other changes that are not yet staged, 2. When doing "git add ; git commit ", the addition of the key is also left in the unstaged changes as a side effect, so "git diff" still shows that change as uncommitted. More investigation shows this only happens when passing the file name to the "git commit" command. Hence it does not happen when doing "git add ; git commit". Signed-off-by: Geert Uytterhoeven Tested-by: Yoshihiro Shimoda --- scripts/pre-commit.sh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/scripts/pre-commit.sh b/scripts/pre-commit.sh index 38894f4..bc7fb6e 100755 --- a/scripts/pre-commit.sh +++ b/scripts/pre-commit.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + if git rev-parse --verify HEAD >/dev/null 2>&1 then base=HEAD @@ -10,9 +12,22 @@ fi error=0 -# Validate new/modified YAML project files for i in $(git diff --cached --name-only --diff-filter=AM $base -- "*yaml" | grep -vF .schema.yaml) do + # Generate unique key if missing + if ! grep -q ^key: "$i" + then + key=$(uuid) + while grep -qrw "$key" . + do + key=$(uuid) + done + echo "Generated key $key for $i" + sed -i "/^team:/akey: $key" "$i" + git add "$i" + fi + + # Validate new/modified YAML project files echo -n "Validating $i: " if ! scripts/validator "$i" then -- cgit v1.2.3