Skip to content

Commit 510e932

Browse files
authored
Add help to scripts (#7)
* Refactor motivation * Refactor php-cs-fixer * Refactor disallow-commits * Refactor conventional-commit * Refactor files-watcher * Make code and help consitent
1 parent f5b4994 commit 510e932

File tree

5 files changed

+299
-189
lines changed

5 files changed

+299
-189
lines changed

conventional-commit/conventional-commit

+82-61
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,42 @@
2727
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2828
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
# =============================================================================
30-
# Git `commit-msg` hook that looks for patterns the branch name
31-
# and uses the matches as prefix or suffix of every commit title.
32-
#
33-
# This is useful if you have convention to use issue number in the branch,
34-
# and want to add it automatically to the commit title.
35-
# For example, you have branch like `FOO-9999-add-hook`, on adding the commit `Add Hook`,
36-
# the hook may add suffix `Add Hook (FOO-9999)` or prefix `[FOO-9999] Add Hook`.
37-
#
38-
# Note: you can use `commit --no-verify` to skip `commit-msg` hooks.
39-
#
40-
# Example of config:
41-
# ~~~
42-
# [conventional-commit]
43-
# excluded-branch = master
44-
# excluded-branch = development
45-
# prefix-pattern = "FOO-[0-9]+"
46-
# suffix-pattern = "FOO-[0-9]+"
47-
# ~~~
4830

4931
FCR='\033[1;31m' # Red
5032
FCG='\033[1;32m' # Green
5133
FCY='\033[0;33m' # Yellow
5234
FCS='\033[0;37m' # Light gray (silver)
5335
NC='\033[0m'
5436

37+
CMD="conventional-commit"
38+
VERSION="1.0.0"
39+
HOME="https://github.com/slavcodev/git-hooks-scripts"
40+
ABOUT="${FCG}Conventional commit title${NC} version ${FCY}$VERSION${NC}"
41+
HELP="$ABOUT
42+
43+
Git 'commit-msg' hook that looks for patterns the branch name
44+
and uses the matches as prefix or suffix of every commit title.
45+
46+
This is useful if you have convention to use issue number in the branch,
47+
and want to add it automatically to the commit title.
48+
For example, you have branch like 'FOO-9999-add-hook', on adding the commit 'Add Hook,
49+
the hook may add suffix 'Add Hook (FOO-9999)' or prefix '[FOO-9999] Add Hook'.
50+
51+
Note: you can use 'commit --no-verify' to skip 'commit-msg' hooks.
52+
53+
${FCY}Example of config:${NC}
54+
${FCS}~~~
55+
[${CMD}]
56+
excluded-branch = master
57+
excluded-branch = development
58+
prefix-pattern = "FOO-[0-9]+"
59+
suffix-pattern = "FOO-[0-9]+"
60+
~~~${NC}
61+
62+
$HOME
63+
64+
> Enjoy coding ❤️"
65+
5566
report_start() {
5667
echo "${FCG}$@${NC}"
5768
}
@@ -64,63 +75,73 @@ report_done() {
6475
[[ $# > 0 ]] && echo "${FCG}$@${NC}" || echo "${FCG}✓ Done${NC}"
6576
}
6677

67-
prefix_pattern=$(git config --get conventional-commit.prefix-pattern)
68-
suffix_pattern=$(git config --get conventional-commit.suffix-pattern)
78+
main() {
79+
prefix_pattern=$(git config --get conventional-commit.prefix-pattern)
80+
suffix_pattern=$(git config --get conventional-commit.suffix-pattern)
6981

70-
if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
71-
exit
72-
fi
82+
if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
83+
exit
84+
fi
85+
86+
branch_name=$(git rev-parse --abbrev-ref HEAD)
7387

74-
branch_name=$(git rev-parse --abbrev-ref HEAD)
88+
excluded_branches=$(git config --get-all conventional-commit.excluded-branch)
7589

76-
excluded_branches=$(git config --get-all conventional-commit.excluded-branch)
90+
if [[ -z ${excluded_branches} ]]; then
91+
excluded_branches="master"
92+
fi
7793

78-
if [[ -z ${excluded_branches} ]]; then
79-
excluded_branches="master"
80-
fi
94+
for excluded_branch in ${excluded_branches} ; do
95+
if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then
96+
exit
97+
fi
98+
done
8199

82-
for excluded_branch in ${excluded_branches} ; do
83-
if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then
100+
commit_file=$1
101+
commit_title=$(head -n 1 "${commit_file}")
102+
103+
if [[ -z "${commit_title}" ]]; then
84104
exit
85105
fi
86-
done
87-
88-
commit_file=$1
89-
commit_title=$(head -n 1 "${commit_file}")
90106

91-
if [[ -z "${commit_title}" ]]; then
92-
exit
93-
fi
107+
prefix=$(echo "${branch_name}" | grep -Eo "${prefix_pattern}")
108+
suffix=$(echo "${branch_name}" | grep -Eo "${suffix_pattern}")
94109

95-
prefix=$(echo "${branch_name}" | grep -Eo "${prefix_pattern}")
96-
suffix=$(echo "${branch_name}" | grep -Eo "${suffix_pattern}")
110+
if [[ -z "${prefix}" && -z "${suffix}" ]]; then
111+
exit
112+
fi
97113

98-
if [[ -z "${prefix}" && -z "${suffix}" ]]; then
99-
exit
100-
fi
114+
prefix_exists=$(echo "${commit_title}" | grep -o "^\[${prefix}\] ")
115+
suffix_exists=$(echo "${commit_title}" | grep -o " (${suffix})$")
101116

102-
prefix_exists=$(echo "${commit_title}" | grep -o "^\[${prefix}\] ")
103-
suffix_exists=$(echo "${commit_title}" | grep -o " (${suffix})$")
117+
commit_title=${commit_title#*]} # remove prefix ending in "."
118+
commit_title=${commit_title%(*} # remove suffix starting with "."
104119

105-
commit_title=${commit_title#*]} # remove prefix ending in "."
106-
commit_title=${commit_title%(*} # remove suffix starting with "."
120+
if [[ -z "${prefix_exists}" ]]; then
121+
echo "Adding prefix '${prefix}' to the commit title..."
122+
sed -i -e 's/'"${commit_title}"'/'"[${prefix}] ${commit_title}"'/g' "${commit_file}"
123+
else
124+
echo "Prefix '${prefix}' already exists, skipping..."
125+
fi
107126

108-
if [[ -z "${prefix_exists}" ]]; then
109-
echo "Adding prefix '${prefix}' to the commit title..."
110-
sed -i -e 's/'"${commit_title}"'/'"[${prefix}] ${commit_title}"'/g' "${commit_file}"
111-
else
112-
echo "Prefix '${prefix}' already exists, skipping..."
113-
fi
127+
if [[ -z "${suffix_exists}" ]]; then
128+
echo "Adding suffix '${suffix}' to the commit title..."
129+
sed -i -e 's/'"${commit_title}"'/'"${commit_title} (${suffix})"'/g' "${commit_file}"
130+
else
131+
echo "Suffix '${suffix}' already exists, skipping..."
132+
fi
114133

115-
if [[ -z "${suffix_exists}" ]]; then
116-
echo "Adding suffix '${suffix}' to the commit title..."
117-
sed -i -e 's/'"${commit_title}"'/'"${commit_title} (${suffix})"'/g' "${commit_file}"
118-
else
119-
echo "Suffix '${suffix}' already exists, skipping..."
120-
fi
134+
echo "Title was changed to '$(head -n 1 "${commit_file}")'"
121135

122-
echo "Title was changed to '$(head -n 1 "${commit_file}")'"
136+
report_done
123137

124-
report_done
138+
exit $?
139+
}
125140

126-
exit $?
141+
case "$1" in
142+
-h|--help|-? )
143+
echo "${HELP}"
144+
;;
145+
* )
146+
main
147+
esac

disallow-commits/disallow-commits

+45-18
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,55 @@
2727
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
2828
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
# =============================================================================
30-
# The `pre-commit` hook to disallow commits to some branches.
31-
# Useful for example to disallow commits directly to master branch.
32-
# Example of config:
33-
# ~~~
34-
# [disallow-commits]
35-
# branch = master
36-
# branch = development
37-
# ~~~
3830

3931
FCR='\033[1;31m' # Red
32+
FCG='\033[1;32m' # Green
33+
FCY='\033[0;33m' # Yellow
34+
FCS='\033[0;37m' # Light gray (silver)
4035
NC='\033[0m'
4136

42-
branch_name="$(git rev-parse --abbrev-ref HEAD)"
43-
closed_branches=$(git config --get-all disallow-commits.branch)
37+
CMD="disallow-commits"
38+
VERSION="1.0.0"
39+
HOME="https://github.com/slavcodev/git-hooks-scripts"
40+
ABOUT="${FCG}Branch protection${NC} version ${FCY}$VERSION${NC}"
41+
HELP="$ABOUT
4442
45-
if [[ -z ${closed_branches} ]]; then
46-
closed_branches="master"
47-
fi
43+
The 'pre-commit' hook to disallow commits into some branches.
44+
Useful for example to disallow commits directly to master branch.
4845
49-
for closed_branch in ${closed_branches} ; do
50-
if [[ "${branch_name}" =~ "${closed_branch}" ]]; then
51-
echo "${FCR}You can't commit directly to ${closed_branch} branch${NC}"
52-
exit 1
46+
Multiple branches are supported.
47+
48+
${FCY}Example of config:${NC}
49+
${FCS}~~~
50+
[${CMD}]
51+
branch = master
52+
branch = development
53+
~~~${NC}
54+
55+
$HOME
56+
57+
> Enjoy coding ❤️"
58+
59+
main() {
60+
branch_name="$(git rev-parse --abbrev-ref HEAD)"
61+
closed_branches=$(git config --get-all disallow-commits.branch)
62+
63+
if [[ -z ${closed_branches} ]]; then
64+
closed_branches="master"
5365
fi
54-
done
66+
67+
for closed_branch in ${closed_branches} ; do
68+
if [[ "${branch_name}" =~ "${closed_branch}" ]]; then
69+
echo "${FCR}You can't commit directly to ${closed_branch} branch${NC}"
70+
exit 1
71+
fi
72+
done
73+
}
74+
75+
case "$1" in
76+
-h|--help|-? )
77+
echo "${HELP}"
78+
;;
79+
* )
80+
main
81+
esac

0 commit comments

Comments
 (0)