|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Functions for colored text output |
| 4 | +print_green() { |
| 5 | + echo -e "\033[0;32m$1\033[0m" |
| 6 | +} |
| 7 | + |
| 8 | +print_yellow() { |
| 9 | + echo -e "\033[0;33m$1\033[0m" |
| 10 | +} |
| 11 | + |
| 12 | +print_red() { |
| 13 | + echo -e "\033[0;31m$1\033[0m" |
| 14 | +} |
| 15 | + |
| 16 | +# Function to add executable permissions |
| 17 | +ensure_executable() { |
| 18 | + if [ -f "$1" ] && [ ! -x "$1" ]; then |
| 19 | + chmod +x "$1" |
| 20 | + print_green "Added executable permission to $1" |
| 21 | + fi |
| 22 | +} |
| 23 | + |
| 24 | +# Ensure script runs from project root directory |
| 25 | +if [ ! -d ".git" ]; then |
| 26 | + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 27 | + cd "$(dirname "$SCRIPT_DIR")" || { print_red "Cannot find project root directory"; exit 1; } |
| 28 | + |
| 29 | + if [ ! -d ".git" ]; then |
| 30 | + print_red "Please run this script from the project root directory" |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | +fi |
| 34 | + |
| 35 | +# Check if pre-commit is installed |
| 36 | +if ! command -v pre-commit &> /dev/null; then |
| 37 | + print_yellow "pre-commit not found, attempting to install..." |
| 38 | + if command -v pip3 &> /dev/null; then |
| 39 | + pip3 install pre-commit |
| 40 | + elif command -v pip &> /dev/null; then |
| 41 | + pip install pre-commit |
| 42 | + else |
| 43 | + print_red "pip not found, please install Python and pip first, then run this script again" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + |
| 47 | + if [ $? -ne 0 ]; then |
| 48 | + print_red "Failed to install pre-commit, please install manually: pip install pre-commit" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + print_green "pre-commit installed successfully!" |
| 52 | +else |
| 53 | + print_green "pre-commit is already installed!" |
| 54 | +fi |
| 55 | + |
| 56 | +# Check if gitleaks is installed |
| 57 | +if ! command -v gitleaks &> /dev/null; then |
| 58 | + print_yellow "gitleaks not found, please install it..." |
| 59 | + print_yellow "Installation guide: https://github.com/gitleaks/gitleaks#installing" |
| 60 | + |
| 61 | + # Attempt automatic installation (based on OS) |
| 62 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 63 | + print_yellow "Detected macOS, attempting to install gitleaks via Homebrew..." |
| 64 | + if command -v brew &> /dev/null; then |
| 65 | + brew install gitleaks |
| 66 | + if [ $? -eq 0 ]; then |
| 67 | + print_green "gitleaks installed successfully!" |
| 68 | + else |
| 69 | + print_red "Cannot automatically install gitleaks, please install manually" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | + else |
| 73 | + print_red "Homebrew not found, please install Homebrew or install gitleaks manually" |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + else |
| 77 | + print_red "Please install gitleaks manually and try again" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +fi |
| 81 | + |
| 82 | +# Check required files and directories |
| 83 | +if [ ! -d ".git-hooks" ]; then |
| 84 | + print_red "Cannot find .git-hooks directory, please ensure you're in the correct project" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +if [ ! -f ".gitleaks.toml" ]; then |
| 89 | + print_red "Cannot find .gitleaks.toml configuration file, please ensure it exists" |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +if [ ! -f ".git-hooks/check-commit-message.sh" ]; then |
| 94 | + print_red "Cannot find .git-hooks/check-commit-message.sh file, please ensure it exists" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +# Ensure all scripts have executable permissions |
| 99 | +print_yellow "Granting executable permissions to hook scripts..." |
| 100 | +ensure_executable ".git-hooks/check-commit-message.sh" |
| 101 | +ensure_executable ".git-hooks/post-commit" |
| 102 | +ensure_executable ".git-hooks/pre-commit" |
| 103 | + |
| 104 | +# Install pre-commit hook |
| 105 | +print_yellow "Installing pre-commit hook..." |
| 106 | +pre-commit install |
| 107 | +if [ $? -ne 0 ]; then |
| 108 | + print_red "Failed to install pre-commit hook!" |
| 109 | + exit 1 |
| 110 | +fi |
| 111 | +print_green "pre-commit hook installed successfully!" |
| 112 | + |
| 113 | +# Install commit-msg hook |
| 114 | +print_yellow "Installing commit-msg hook..." |
| 115 | +pre-commit install --hook-type commit-msg |
| 116 | +if [ $? -ne 0 ]; then |
| 117 | + print_red "Failed to install commit-msg hook!" |
| 118 | + exit 1 |
| 119 | +fi |
| 120 | +print_green "pre-commit commit-msg hook installed successfully!" |
| 121 | + |
| 122 | +# Copy and set up custom hooks |
| 123 | +print_yellow "Setting up custom hooks..." |
| 124 | +# Copy commit-msg hook |
| 125 | +cp .git-hooks/check-commit-message.sh .git/hooks/commit-msg |
| 126 | +chmod +x .git/hooks/commit-msg |
| 127 | + |
| 128 | +# Copy post-commit hook (if exists) |
| 129 | +if [ -f ".git-hooks/post-commit" ]; then |
| 130 | + cp .git-hooks/post-commit .git/hooks/post-commit |
| 131 | + chmod +x .git/hooks/post-commit |
| 132 | +fi |
| 133 | + |
| 134 | +# Copy pre-commit hook (if exists) |
| 135 | +if [ -f ".git-hooks/pre-commit" ]; then |
| 136 | + # Backup pre-commit hook |
| 137 | + if [ -f ".git/hooks/pre-commit" ]; then |
| 138 | + cp .git/hooks/pre-commit .git/hooks/pre-commit.bak |
| 139 | + fi |
| 140 | + |
| 141 | + cp .git-hooks/pre-commit .git/hooks/pre-commit.custom |
| 142 | + chmod +x .git/hooks/pre-commit.custom |
| 143 | + |
| 144 | + # Add custom pre-commit to existing hook chain |
| 145 | + if [ -f ".git/hooks/pre-commit" ]; then |
| 146 | + HOOK_CONTENT=$(cat .git/hooks/pre-commit) |
| 147 | + if ! grep -q "pre-commit.custom" .git/hooks/pre-commit; then |
| 148 | + echo -e "\n# Run custom pre-commit hook\n.git/hooks/pre-commit.custom || exit 1" >> .git/hooks/pre-commit |
| 149 | + chmod +x .git/hooks/pre-commit |
| 150 | + fi |
| 151 | + else |
| 152 | + echo -e "#!/bin/bash\n\n# Run custom pre-commit hook\n.git/hooks/pre-commit.custom" > .git/hooks/pre-commit |
| 153 | + chmod +x .git/hooks/pre-commit |
| 154 | + fi |
| 155 | +fi |
| 156 | + |
| 157 | +pre-commit clean && pre-commit install && pre-commit install --hook-type commit-msg |
| 158 | + |
| 159 | +print_green "================================================================" |
| 160 | +print_green "🎉 Git hooks setup complete! Your repository now has:" |
| 161 | +print_green " - Sensitive information leak detection using gitleaks" |
| 162 | +print_green " - Chinese character detection in commit messages" |
| 163 | +print_green "================================================================" |
0 commit comments