-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprepare-commit-msg
executable file
·78 lines (68 loc) · 2.31 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#
# Copyright (c) 2016 Jacob Young
# MIT License
#
# This script will append a list of added/removed features to a commit message.
# It reads comments following a '@commit' symbol and constructs an unordered list of them.
# After comments are read, they are removed from the file they are found
#
# Example:
# //@commit: Added new link parsing function
#
# Output (in commit message):
# - Added new link parsing funciton
# Arg 1: SHOW_LINES (T/F)
SHOW_LINES=true
IGNORED=( ".ccignore" )
# If the script is deployed to the .git directory,
# move to the repository directory so .ccignore file
# can be found
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if echo "$DIR" | grep -qs 'git'; then
cd "$( echo "$DIR" | grep -Po '^.*(?=(\.git))' )"
fi
if [ -f ".ccignore" ]; then
while IFS='' read -r line || [[ -n "$line" ]]; do
IGNORED+=( "$line" )
done < ".ccignore"
fi
if which pcregrep > /dev/null; then
grep_search="pcregrep -o2 '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
N_grep_search="pcregrep -o2 -n '((?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?)([^\r\n*\/]+)'"
else
grep_search="grep -Po '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
N_grep_search="grep -Po -n '(?:[#;*]|(?:\/{2}))(?:\s+)?@commit(?:[-:.\s]+)?\K([^\r\n*\/]+)'"
fi
IFS=$'\n' ADDED_FILES=( $(git ls-files) )
for i in ${!ADDED_FILES[@]}; do
file_path=${ADDED_FILES["$i"]}
filename=$( basename "$file_path" )
extension="${filename##*.}"
if [[ "${IGNORED[*]}" =~ "${filename}" || "${IGNORED[*]}" =~ "${extension}" ]]; then
# File should not be read - remove
unset ADDED_FILES["$i"]
fi
done
comments=""
for fn in "${ADDED_FILES[@]}"; do
if [ "$SHOW_LINES" = true ]; then
temp=$(cat "$fn" | eval "$N_grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' | awk -v fname=$fn -F':' '{ $1 ="[" fname "#" $1 "]"; print}')
else
temp=$( cat "$fn" | eval "$grep_search" | tr -s '[:space:]' | sed 's/[[:blank:]]*$//' )
fi
if [[ "$temp" != "" ]]; then
comments+="$temp"
comments+="\n"
fi
done
comments=$(echo -en "$comments" | sed -e 's/^/- /')
grep -qs "^$comments" "$1"
if [ $? -eq 1 ]; then
if [[ "$comments" == "" ]]; then
exit 0
else
# Prepend to commit file
echo -e "\n\n$comments" | cat - "$1" > temp_cmmt_msg && mv temp_cmmt_msg "$1"
fi
fi