Skip to content

Add help to scripts #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 82 additions & 61 deletions conventional-commit/conventional-commit
Original file line number Diff line number Diff line change
@@ -27,31 +27,42 @@
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============================================================================
# Git `commit-msg` hook that looks for patterns the branch name
# and uses the matches as prefix or suffix of every commit title.
#
# This is useful if you have convention to use issue number in the branch,
# and want to add it automatically to the commit title.
# For example, you have branch like `FOO-9999-add-hook`, on adding the commit `Add Hook`,
# the hook may add suffix `Add Hook (FOO-9999)` or prefix `[FOO-9999] Add Hook`.
#
# Note: you can use `commit --no-verify` to skip `commit-msg` hooks.
#
# Example of config:
# ~~~
# [conventional-commit]
# excluded-branch = master
# excluded-branch = development
# prefix-pattern = "FOO-[0-9]+"
# suffix-pattern = "FOO-[0-9]+"
# ~~~

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

CMD="conventional-commit"
VERSION="1.0.0"
HOME="https://github.com/slavcodev/git-hooks-scripts"
ABOUT="${FCG}Conventional commit title${NC} version ${FCY}$VERSION${NC}"
HELP="$ABOUT

Git 'commit-msg' hook that looks for patterns the branch name
and uses the matches as prefix or suffix of every commit title.

This is useful if you have convention to use issue number in the branch,
and want to add it automatically to the commit title.
For example, you have branch like 'FOO-9999-add-hook', on adding the commit 'Add Hook,
the hook may add suffix 'Add Hook (FOO-9999)' or prefix '[FOO-9999] Add Hook'.

Note: you can use 'commit --no-verify' to skip 'commit-msg' hooks.

${FCY}Example of config:${NC}
${FCS}~~~
[${CMD}]
excluded-branch = master
excluded-branch = development
prefix-pattern = "FOO-[0-9]+"
suffix-pattern = "FOO-[0-9]+"
~~~${NC}

$HOME

> Enjoy coding ❤️"

report_start() {
echo "${FCG}$@${NC}"
}
@@ -64,63 +75,73 @@ report_done() {
[[ $# > 0 ]] && echo "${FCG}✓ $@${NC}" || echo "${FCG}✓ Done${NC}"
}

prefix_pattern=$(git config --get conventional-commit.prefix-pattern)
suffix_pattern=$(git config --get conventional-commit.suffix-pattern)
main() {
prefix_pattern=$(git config --get conventional-commit.prefix-pattern)
suffix_pattern=$(git config --get conventional-commit.suffix-pattern)

if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
exit
fi
if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
exit
fi

branch_name=$(git rev-parse --abbrev-ref HEAD)

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

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

if [[ -z ${excluded_branches} ]]; then
excluded_branches="master"
fi
for excluded_branch in ${excluded_branches} ; do
if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then
exit
fi
done

for excluded_branch in ${excluded_branches} ; do
if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then
commit_file=$1
commit_title=$(head -n 1 "${commit_file}")

if [[ -z "${commit_title}" ]]; then
exit
fi
done

commit_file=$1
commit_title=$(head -n 1 "${commit_file}")

if [[ -z "${commit_title}" ]]; then
exit
fi
prefix=$(echo "${branch_name}" | grep -Eo "${prefix_pattern}")
suffix=$(echo "${branch_name}" | grep -Eo "${suffix_pattern}")

prefix=$(echo "${branch_name}" | grep -Eo "${prefix_pattern}")
suffix=$(echo "${branch_name}" | grep -Eo "${suffix_pattern}")
if [[ -z "${prefix}" && -z "${suffix}" ]]; then
exit
fi

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

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

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

if [[ -z "${prefix_exists}" ]]; then
echo "Adding prefix '${prefix}' to the commit title..."
sed -i -e 's/'"${commit_title}"'/'"[${prefix}] ${commit_title}"'/g' "${commit_file}"
else
echo "Prefix '${prefix}' already exists, skipping..."
fi
if [[ -z "${suffix_exists}" ]]; then
echo "Adding suffix '${suffix}' to the commit title..."
sed -i -e 's/'"${commit_title}"'/'"${commit_title} (${suffix})"'/g' "${commit_file}"
else
echo "Suffix '${suffix}' already exists, skipping..."
fi

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

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

report_done
exit $?
}

exit $?
case "$1" in
-h|--help|-? )
echo "${HELP}"
;;
* )
main
esac
63 changes: 45 additions & 18 deletions disallow-commits/disallow-commits
Original file line number Diff line number Diff line change
@@ -27,28 +27,55 @@
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# =============================================================================
# The `pre-commit` hook to disallow commits to some branches.
# Useful for example to disallow commits directly to master branch.
# Example of config:
# ~~~
# [disallow-commits]
# branch = master
# branch = development
# ~~~

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

branch_name="$(git rev-parse --abbrev-ref HEAD)"
closed_branches=$(git config --get-all disallow-commits.branch)
CMD="disallow-commits"
VERSION="1.0.0"
HOME="https://github.com/slavcodev/git-hooks-scripts"
ABOUT="${FCG}Branch protection${NC} version ${FCY}$VERSION${NC}"
HELP="$ABOUT

if [[ -z ${closed_branches} ]]; then
closed_branches="master"
fi
The 'pre-commit' hook to disallow commits into some branches.
Useful for example to disallow commits directly to master branch.

for closed_branch in ${closed_branches} ; do
if [[ "${branch_name}" =~ "${closed_branch}" ]]; then
echo "${FCR}You can't commit directly to ${closed_branch} branch${NC}"
exit 1
Multiple branches are supported.

${FCY}Example of config:${NC}
${FCS}~~~
[${CMD}]
branch = master
branch = development
~~~${NC}

$HOME

> Enjoy coding ❤️"

main() {
branch_name="$(git rev-parse --abbrev-ref HEAD)"
closed_branches=$(git config --get-all disallow-commits.branch)

if [[ -z ${closed_branches} ]]; then
closed_branches="master"
fi
done

for closed_branch in ${closed_branches} ; do
if [[ "${branch_name}" =~ "${closed_branch}" ]]; then
echo "${FCR}You can't commit directly to ${closed_branch} branch${NC}"
exit 1
fi
done
}

case "$1" in
-h|--help|-? )
echo "${HELP}"
;;
* )
main
esac
Loading