Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{githooks} improved pre-commit message #30476

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
8 changes: 5 additions & 3 deletions .githooks/pre-commit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ if (git rev-parse --verify HEAD 2>$null) {
$against = "HEAD"
}
else {
# Initial commit: diff against an empty tree object
Write-Host "Using empty tree object as the previous commit"
Write-Host "Using an empty tree object as the previous commit"
$against = $(git hash-object -t tree /dev/null)
}

Expand All @@ -29,7 +28,10 @@ foreach ($file in $files) {
# Check if the file contains secrets
$detected = $(azdev scan -f $file | ConvertFrom-Json).secrets_detected
if ($detected -eq "True") {
Write-Host "Detected secrets from $file. You can run 'azdev mask' to remove secrets before commit." -ForegroundColor Red
Write-Host "Detected secrets from $file. Please run the following command to mask it:" -ForegroundColor Red
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
Write-Host "azdev mask -f $file" -ForegroundColor Red
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Red
$hasSecrets = 1
}
}
Expand Down
11 changes: 5 additions & 6 deletions .githooks/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ then
printf "Using HEAD as the previous commit\n"
against=HEAD
else
printf "Using empty tree object as the previous commit\n"
printf "Using an empty tree object as the previous commit\n"
against=$(git hash-object -t tree /dev/null)
fi
has_secrets=0

IFS_OLD=${IFS}
IFS=$'\n'
for FILE in `git diff --cached --name-only --diff-filter=AM $against` ; do
# Check if the file contains secrets
detected=$(azdev scan -f "$FILE" | python -c "import sys, json; print(json.load(sys.stdin)['secrets_detected'])")
if [ "$detected" = "True" ]; then
printf "\033[0;31mDetected secrets from %s, You can run 'azdev mask' to remove secrets before commit.\033[0m\n" "$FILE"
printf "\033[0;31mDetected secrets from %s, Please run the following command to mask it:\033[0m\n" "$FILE"
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\033[0;31mazdev mask -f %s\033[0m\n" "$FILE"
printf "\033[0;31m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
has_secrets=1
fi
done
IFS=${IFS_OLD}

if [ $has_secrets -eq 1 ]; then
printf "\033[0;31mSecret detected. If you want to skip that, run add '--no-verify' in the end of 'git commit' command.\033[0m\n"
Expand Down
Loading