From 72e0f14e7a95a04e0e0526c208bd41c53afc6620 Mon Sep 17 00:00:00 2001 From: Stefano Dafarra Date: Thu, 18 Jul 2024 16:34:39 +0200 Subject: [PATCH 1/3] [WORKFLOW] Added post-commit hook to add robot tag to commit --- .githooks/post-commit | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 .githooks/post-commit diff --git a/.githooks/post-commit b/.githooks/post-commit new file mode 100755 index 0000000000..99b13ec7e5 --- /dev/null +++ b/.githooks/post-commit @@ -0,0 +1,31 @@ +#!/bin/bash + +# Retrieve the value of the environment variable +ENV_VAR_VALUE=$(printenv YARP_ROBOT_NAME) + +echo "Post-commit hook running..." + +# Check if the environment variable is set +if [ -z "$ENV_VAR_VALUE" ]; then + echo "Environment variable YARP_ROBOT_NAME is not set. Exiting post-commit hook." + exit 1 +fi + +# Get the current commit message +CURRENT_COMMIT_MSG=$(git log -1 --pretty=%B) + +# Check if the commit message already starts with a [ +if [[ "$CURRENT_COMMIT_MSG" =~ ^\[ ]]; then + echo "Commit message already starts with '[', not modifying the commit message." + exit 0 +fi + +# Prepend the environment variable value to the commit message +COMMIT_MSG_FILE=$(mktemp) +echo "[$ENV_VAR_VALUE] $CURRENT_COMMIT_MSG" > "$COMMIT_MSG_FILE" + +# Amend the commit with the new message +git commit --amend -F "$COMMIT_MSG_FILE" + +# Clean up +rm "$COMMIT_MSG_FILE" From b9c8e2f740bc6fb824c5f781ddcb3ba870c2f935 Mon Sep 17 00:00:00 2001 From: Stefano Dafarra Date: Thu, 18 Jul 2024 17:55:41 +0200 Subject: [PATCH 2/3] [WORKFLOW] Avoid to trigger hooks when amending the commit in the post-commit hook --- .githooks/post-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.githooks/post-commit b/.githooks/post-commit index 99b13ec7e5..2e85d1ab44 100755 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -25,7 +25,7 @@ COMMIT_MSG_FILE=$(mktemp) echo "[$ENV_VAR_VALUE] $CURRENT_COMMIT_MSG" > "$COMMIT_MSG_FILE" # Amend the commit with the new message -git commit --amend -F "$COMMIT_MSG_FILE" +git -c core.hooksPath=/dev/null commit --amend -F "$COMMIT_MSG_FILE" # Clean up rm "$COMMIT_MSG_FILE" From fcfe24d076b869398d87850476a1104c7cb3d4ba Mon Sep 17 00:00:00 2001 From: Stefano Dafarra Date: Thu, 18 Jul 2024 18:05:21 +0200 Subject: [PATCH 3/3] [WORKFLOW] Avoid exiting with error in post-commit if the YARP_ROBOT_NAME is not set --- .githooks/post-commit | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.githooks/post-commit b/.githooks/post-commit index 2e85d1ab44..8caff6ad7e 100755 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -7,8 +7,8 @@ echo "Post-commit hook running..." # Check if the environment variable is set if [ -z "$ENV_VAR_VALUE" ]; then - echo "Environment variable YARP_ROBOT_NAME is not set. Exiting post-commit hook." - exit 1 + echo "Environment variable YARP_ROBOT_NAME is not set. Skipping post-commit hook." + exit 0 fi # Get the current commit message