-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg-generator.sh
executable file
·125 lines (100 loc) · 2.91 KB
/
commit-msg-generator.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
#set -x # echo on
# Usage:
# $ ./commit-msg-generator.sh -p=~/repo
# $ ./commit-msg-generator.sh --projects=~/repo
# $ ./commit-msg-generator.sh --default
function buildFilesListString {
local -n files_list=$1
local len=${#files_list[@]}
for (( i=0; i<len; i++ )); do
file_name="${files_list[$i]}"
if (( i < len-1 )); then
echo -n "'$file_name', "
else
echo -n "'$file_name'"
fi
done
}
projects_dir=.
for i in "$@"; do
case $i in
-p=*|--projects=*)
projects_dir="${i#*=}"
eval projects_dir=$projects_dir
shift # past argument=value
;;
--default)
projects_dir=.
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
cd "$projects_dir"
pwd
git for-each-ref --format='%(refname:short)' refs/heads/ | while read branch_name; do
#echo "branch_name = $branch_name"
for commit_id in $(git rev-list "$branch_name"); do
#echo "commit_id = $commit_id"
declare -a all_files=()
SAVEIFS=$IFS # Save current IFS
IFS=$'\n' # Change IFS to new line.
all_files=( $(git show --pretty="" --name-status "$commit_id") )
IFS=$SAVEIFS # Restore IFS
declare -a all=()
declare -a added=()
declare -a modified=()
declare -a deleted=()
for (( i=0; i<${#all_files[@]}; i++ )); do
file_name_and_status="${all_files[$i]}"
#echo "file_name_and_status = $file_name_and_status"
file_status=$(echo "$file_name_and_status" | awk -F"\t" '{ print $1 }')
file_name=$(echo "$file_name_and_status" | awk -F"\t" '{ print $2 }')
#echo "file_name = $file_name, file_status = $file_status"
all+=( "$file_name" )
case "$file_status" in
('A')
added+=( "$file_name" )
#echo "added[-1] = ${added[-1]}"
#echo "added = ${added[@]}"
;;
('M')
modified+=( "$file_name" )
#echo "modified[-1] = ${modified[-1]}"
#echo "modified = ${modified[@]}"
;;
('D')
deleted+=( "$file_name" )
#echo "deleted[-1] = ${deleted[-1]}"
#echo "deleted = ${deleted[@]}"
;;
esac
done
commit_message=""
if [ -n "$all" ]; then
commit_message="FILES: $(buildFilesListString all).\n"
fi
if [ -n "$added" ]; then
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
commit_message="${commit_message}ADDED: $(buildFilesListString added)."
fi
if [ -n "$modified" ]; then
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
commit_message="${commit_message}MODIFIED: $(buildFilesListString modified)."
fi
if [ -n "$deleted" ]; then
if [ -n "$commit_message" ]; then commit_message="${commit_message}\n"; fi
commit_message="${commit_message}DELETED: $(buildFilesListString deleted)."
fi
#echo "$commit_message"
#echo "//---------------------------------------------------------------------"
git filter-branch -f --msg-filter \
"if test \$GIT_COMMIT = '$commit_id'
then
echo \"$commit_message\"; else cat
fi"
done
done