-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.sh
executable file
·62 lines (55 loc) · 2.03 KB
/
test.sh
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
#!/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=$1
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 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##*.}"
# File should be removed if filename or extension type has been specified in .ccignore
if [[ "${IGNORED[*]}" =~ "${filename}" || "${IGNORED[*]}" =~ "${extension}" ]]; then
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/^/- /')
echo -n "$comments"