-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-infer
executable file
Β·163 lines (135 loc) Β· 2.84 KB
/
git-infer
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env bash
set -uo pipefail
usage() {
cat <<EOF
git infer:
Commit changes with an automatically generated message
(from https://github.com/moondewio/git-infer)
USAGE
git infer [-a] [-A] [-h]
-a Include all tracked files
-A Include all files, even untracked ones
-h Show this help message
EOF
}
while getopts ":aAh" opt; do
case $opt in
a)
git add -u
;;
A)
git add `git rev-parse --show-toplevel`
;;
h)
usage
exit
;;
esac
done
STATUS=$(git status --porcelain | grep -v '^ ' | grep -v '^?')
COMMIT_MSG=$(awk '
BEGIN {
updatedCount = 0;
addedCount = 0;
removedCount = 0;
renamedCount = 0;
}
$1 == "M" {
updated[updatedCount] = substr($0, 4);
updatedCount += 1
}
$1 == "A" || $1 == "??" {
added[addedCount] = substr($0, 4);
addedCount += 1
}
$1 == "D" {
removed[removedCount] = substr($0, 4);
removedCount += 1
}
$1 == "R" {
renamed[renamedCount] = substr($0, 4);
renamedCount += 1
}
END {
if (updatedCount > 0) {
updatedMsg = "";
for (i = 0; i + 1 < updatedCount; i++) {
updatedMsg = updatedMsg updated[i] ", "
};
updatedMsg = updatedMsg updated[updatedCount - 1]
updatedMsg = "Modify " updatedMsg
}
if (addedCount > 0) {
addedMsg = "";
for (i = 0; i + 1 < addedCount; i++) {
addedMsg = addedMsg added[i] ", "
};
addedMsg = addedMsg added[addedCount - 1]
addedMsg = "Add " addedMsg
}
if (removedCount > 0) {
removedMsg = "";
for (i = 0; i + 1 < removedCount; i++) {
removedMsg = removedMsg removed[i] ", "
};
removedMsg = removedMsg removed[removedCount - 1]
removedMsg = "Remove " removedMsg
}
if (renamedCount > 0) {
renamedMsg = "";
for (i = 0; i + 1 < renamedCount; i++) {
renamedMsg = renamedMsg renamed[i] ", "
};
renamedMsg = renamedMsg renamed[renamedCount - 1]
renamedMsg = "Rename " renamedMsg
}
changesCount = 0;
if (updatedCount > 0) {
changesCount += 1
}
if (addedCount > 0) {
changesCount += 1
}
if (removedCount > 0) {
changesCount += 1
}
if (renamedCount > 0) {
changesCount += 1
}
if (changesCount > 1) {
printf "Several changes\n\n"
if (updatedCount > 0) {
print updatedMsg
}
if (addedCount > 0) {
print addedMsg
}
if (removedCount > 0) {
print removedMsg
}
if (renamedCount > 0) {
print renamedMsg
}
}
if (changesCount == 1) {
if (updatedCount > 0) {
print updatedMsg
}
if (addedCount > 0) {
print addedMsg
}
if (removedCount > 0) {
print removedMsg
}
if (renamedCount > 0) {
print renamedMsg
}
}
}
' <<< "$STATUS")
if [ -z "${COMMIT_MSG}" ]
then
echo "git infer: Nothing added, updated, removed or renamed"
exit 1
fi
git commit -m "${COMMIT_MSG}"