-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-commit-msg
executable file
·36 lines (26 loc) · 1.13 KB
/
prepare-commit-msg
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
#!/bin/sh
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
# The file that contains the commit log message is passed by git to this hook as the first argument
COMMIT_MSG_FILE="$1"
# Extract paths of staged files
STAGED_FILES=$(git diff --cached --name-only)
# Extract the first level directory from all staged files
DIRS=$(echo "$STAGED_FILES" | awk -F'/' '{print $1}' | uniq)
# Count the number of unique first level directories
DIR_COUNT=$(echo "$DIRS" | wc -l)
DIRS_ONELINE=$(echo "$DIRS" | awk 1 ORS=', ' | awk '$0=gensub(/^(.*), /,"\\1","g",$0)')
# If there's more than one directory or no directory, exit with an error or without any action
if [ $DIR_COUNT -gt 1 ]; then
echo "Error: Multiple first-level directories detected in staged files."
echo " Split them into multiple commits. Only one group allowed per one commit."
echo ""
read -p "Abort? Y/n: " yn
if [ "${yn:0:1}" != "n" ]; then
exit 1
fi
elif [ -z "$DIRS" ]; then
exit 0
fi
# Append the directory name to the beginning of the commit message
echo "[$DIRS_ONELINE] $(cat $COMMIT_MSG_FILE)" > $COMMIT_MSG_FILE