Skip to content

Commit c2bd1bd

Browse files
authored
Merge pull request #455 from AgoraIO/dev/commit_scan
Add commit message scanning script
2 parents a4428fb + db06fcc commit c2bd1bd

File tree

8 files changed

+469
-38
lines changed

8 files changed

+469
-38
lines changed

.git-hooks/check-commit-message.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
commit_msg_file=$1
4+
commit_msg=$(cat "$commit_msg_file")
5+
6+
7+
if perl -e '
8+
binmode(STDIN, ":utf8");
9+
$/ = undef;
10+
$text = <>;
11+
if ($text =~ /[\x{4e00}-\x{9fff}]/) {
12+
exit(1);
13+
} else {
14+
exit(0);
15+
}' < "$commit_msg_file"
16+
then
17+
exit 0
18+
else
19+
echo "Error: Commit message contains Chinese characters."
20+
echo "Please use English only in commit messages."
21+
exit 1
22+
fi

.git-hooks/install-hooks.sh

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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 "================================================================"

.git-hooks/post-commit

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Check if required hooks are installed
4+
if [ ! -f ".git/hooks/commit-msg" ] || [ ! -x ".git/hooks/commit-msg" ]; then
5+
echo "============================================================"
6+
echo "Note: Git hooks for checking Chinese characters in commit messages are not installed."
7+
echo "Please run the following commands to install:"
8+
echo ""
9+
echo " 1. Install pre-commit:"
10+
echo " pip install pre-commit"
11+
echo ""
12+
echo " 2. Install pre-commit hook:"
13+
echo " pre-commit install"
14+
echo ""
15+
echo " 3. Install commit-msg hook:"
16+
echo " pre-commit install --hook-type commit-msg"
17+
echo " cp .git-hooks/check-commit-message.sh .git/hooks/commit-msg"
18+
echo " chmod +x .git/hooks/commit-msg"
19+
echo ""
20+
echo "These hooks will help detect sensitive information leaks and Chinese characters in commit messages."
21+
echo "============================================================"
22+
fi
23+
24+
# Ensure the hook itself is executable
25+
if [ -f ".git-hooks/check-commit-message.sh" ] && [ ! -x ".git-hooks/check-commit-message.sh" ]; then
26+
chmod +x .git-hooks/check-commit-message.sh
27+
fi

.git-hooks/pre-commit

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Check if gitleaks is configured
4+
if ! command -v gitleaks &> /dev/null; then
5+
echo "============================================================"
6+
echo "Gitleaks not detected. This is a required tool to prevent sensitive information leaks."
7+
echo "Please install gitleaks first: https://github.com/gitleaks/gitleaks#installing"
8+
echo "After installation, run: ./.git-hooks/install-hooks.sh"
9+
echo "============================================================"
10+
exit 1
11+
fi
12+
13+
# Check for sensitive information
14+
if [ -f ".gitleaks.toml" ]; then
15+
gitleaks detect --source . --config .gitleaks.toml
16+
if [ $? -ne 0 ]; then
17+
echo "Gitleaks detected sensitive information. Commit rejected."
18+
echo "Please review the output above and remove sensitive information."
19+
exit 1
20+
fi
21+
else
22+
echo "No .gitleaks.toml configuration file found, skipping sensitive information check."
23+
fi
24+
25+
exit 0

.githooks/pre-commit

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)