Skip to content

Instantly share code, notes, and snippets.

@teddichiiwa
Created September 9, 2024 04:43
Show Gist options
  • Select an option

  • Save teddichiiwa/adaec41ceaaaf171b2eae54d959795bf to your computer and use it in GitHub Desktop.

Select an option

Save teddichiiwa/adaec41ceaaaf171b2eae54d959795bf to your computer and use it in GitHub Desktop.
Git pre-commit rules
#!/bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to check a single line
check_line() {
local line="$1"
local line_number="$2"
if echo "$line" | grep -qE '^\s*([-\s]*[a-zA-Z0-9_]+:)?\s*path:\s*(\.\.|\/)'; then
printf "${RED}Error on line %d:${NC}\n" "$line_number"
echo "$line"
return 1
fi
return 0
}
# Print title
printf "${BLUE}======================================${NC}\n"
printf "${BLUE} pubspec.yaml Dependency Check ${NC}\n"
printf "${BLUE}======================================${NC}\n"
# Initialize error flag
error_found=0
# Read pubspec.yaml line by line
line_number=0
while IFS= read -r line
do
((line_number++))
if ! check_line "$line" "$line_number"; then
error_found=1
fi
done < pubspec.yaml
# If errors were found, exit with an error message
if [ $error_found -eq 1 ]; then
printf "\n"
printf "${RED}pubspec.yaml contains local path or absolute path references.${NC}\n"
printf "${YELLOW}Please remove all local path and absolute path dependencies before committing.${NC}\n"
printf "\n"
printf "${BLUE}Examples of problematic entries:${NC}\n"
printf " path: ../some_directory\n"
printf " path: /Users/username/some_directory\n"
printf "\n"
printf "${GREEN}Correct format example:${NC}\n"
printf " git:\n"
printf " url: ssh://git@example.com:port/repo.git\n"
printf " ref: branch-or-tag\n"
printf " path: subdirectory\n"
exit 1
fi
# If we get here, the check passed
printf "${GREEN}pubspec.yaml check passed. No local or absolute path references found.${NC}\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment