forked from nsavinda/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·60 lines (47 loc) · 1.86 KB
/
pre-commit
File metadata and controls
executable file
·60 lines (47 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
# Redirect output to stderr.
exec 1>&2
# Define color codes
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
NO_COLOR='\033[0m'
MAX_FILE_SIZE=5120
# Allow .env commits if GIT_ALLOW_ENV is set
if [ "$GIT_ALLOW_ENV" != "1" ]; then
# Check for .env file
if git diff --cached --name-only | grep -q '^\.env$'; then
# Output with limited colors
echo -e "${RED}Error: Attempt to commit .env file detected.${NO_COLOR}"
echo -e "${YELLOW}Please remove the .env file from the index by running:${NO_COLOR}"
echo -e "${YELLOW}git reset HEAD .env${NO_COLOR}"
echo -e "${YELLOW}and add .env to your .gitignore file if it's not already there.${NO_COLOR}"
echo -e "${YELLOW}If you really want to commit the .env file, run:${NO_COLOR}"
echo -e "${GREEN}GIT_ALLOW_ENV=1 git commit -m 'Committing .env file'${NO_COLOR}"
exit 1
fi
fi
# Allow large files if GIT_ALLOW_LARGE_FILES is set
if [ "$GIT_ALLOW_LARGE_FILES" != "1" ]; then
echo -e "${YELLOW}Checking for large files...${NO_COLOR}"
LARGE_FILES_FOUND=0
# Loop through staged files
for file in $(git diff --cached --name-only); do
if [ -f "$file" ]; then
FILE_SIZE=$(du -k "$file" | cut -f1) # Get file size in kilobytes
if [ "$FILE_SIZE" -gt "$MAX_FILE_SIZE" ]; then
echo -e "${RED}Error: File '${file}' exceeds the ${MAX_FILE_SIZE}KB size limit.${NO_COLOR}"
LARGE_FILES_FOUND=1
fi
fi
done
if [ "$LARGE_FILES_FOUND" -eq 1 ]; then
echo -e "${YELLOW}Please remove or reduce the size of large files before committing.${NO_COLOR}"
echo -e "${YELLOW}You can unstage a large file using:${NO_COLOR}"
echo -e "${GREEN}git reset HEAD <file>${NO_COLOR}"
echo -e "${YELLOW}If you really want to commit large files, run:${NO_COLOR}"
echo -e "${GREEN}GIT_ALLOW_LARGE_FILES=1 git commit -m '<Commit msg>'${NO_COLOR}"
exit 1
fi
fi
exit 0