From 8afcd04a092b3235ace6c683ae8733e3d03cb4ee Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Wed, 2 Feb 2022 16:19:03 +0100 Subject: [PATCH 1/2] feat: githooks --- .githooks/pre-push | 59 ++++++++++++++++++++++++++++++++++++++++++++++ setup-repo.sh | 5 ++++ 2 files changed, 64 insertions(+) create mode 100755 .githooks/pre-push create mode 100755 setup-repo.sh diff --git a/.githooks/pre-push b/.githooks/pre-push new file mode 100755 index 0000000..d751255 --- /dev/null +++ b/.githooks/pre-push @@ -0,0 +1,59 @@ +#!/bin/bash + +# @link https://gist.github.com/mattscilipoti/8424018 +# +# Called by "git push" after it has checked the remote status, +# but before anything has been pushed. +# +# If this script exits with a non-zero status nothing will be pushed. +# +# Steps to install, from the root directory of your repo... +# 1. Copy the file into your repo at `.git/hooks/pre-push` +# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` +# 3. Or, use `rake hooks:pre_push` to install +# +# Try a push to master, you should get a message `*** [Policy] Never push code directly to...` +# +# The commands below will not be allowed... +# `git push origin master` +# `git push --force origin master` +# `git push --delete origin master` + + +protected_branch='master' + +policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! (Prevented with pre-push hook.)\n\n" + +current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') + +push_command=$(ps -ocommand= -p $PPID) + +is_destructive='force|delete|\-f' + +will_remove_protected_branch=':'$protected_branch + +do_exit(){ + echo -e $policy + exit 1 +} + +if [[ $push_command =~ $is_destructive ]] && [ $current_branch = $protected_branch ]; then + do_exit +fi + +if [[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]; then + do_exit +fi + +if [[ $push_command =~ $will_remove_protected_branch ]]; then + do_exit +fi + +# Prevent ALL pushes to protected_branch +if [[ $push_command =~ $protected_branch ]] || [ $current_branch = $protected_branch ]; then + do_exit +fi + +unset do_exit + +exit 0 diff --git a/setup-repo.sh b/setup-repo.sh new file mode 100755 index 0000000..20ca20f --- /dev/null +++ b/setup-repo.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cp .githooks/* .git/hooks + +echo "hooks have been copied" \ No newline at end of file From f71e76be5d3908cf5dcf83ec39e2ffce03b6d714 Mon Sep 17 00:00:00 2001 From: Filip Rojek Date: Wed, 2 Feb 2022 16:20:24 +0100 Subject: [PATCH 2/2] feat: githooks --- src/skeletons/express-ts/.githooks/pre-push | 59 +++++++++++++++++++++ src/skeletons/express-ts/setup-repo.sh | 5 ++ 2 files changed, 64 insertions(+) create mode 100755 src/skeletons/express-ts/.githooks/pre-push create mode 100755 src/skeletons/express-ts/setup-repo.sh diff --git a/src/skeletons/express-ts/.githooks/pre-push b/src/skeletons/express-ts/.githooks/pre-push new file mode 100755 index 0000000..d751255 --- /dev/null +++ b/src/skeletons/express-ts/.githooks/pre-push @@ -0,0 +1,59 @@ +#!/bin/bash + +# @link https://gist.github.com/mattscilipoti/8424018 +# +# Called by "git push" after it has checked the remote status, +# but before anything has been pushed. +# +# If this script exits with a non-zero status nothing will be pushed. +# +# Steps to install, from the root directory of your repo... +# 1. Copy the file into your repo at `.git/hooks/pre-push` +# 2. Set executable permissions, run `chmod +x .git/hooks/pre-push` +# 3. Or, use `rake hooks:pre_push` to install +# +# Try a push to master, you should get a message `*** [Policy] Never push code directly to...` +# +# The commands below will not be allowed... +# `git push origin master` +# `git push --force origin master` +# `git push --delete origin master` + + +protected_branch='master' + +policy="\n\n[Policy] Never push code directly to the "$protected_branch" branch! (Prevented with pre-push hook.)\n\n" + +current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') + +push_command=$(ps -ocommand= -p $PPID) + +is_destructive='force|delete|\-f' + +will_remove_protected_branch=':'$protected_branch + +do_exit(){ + echo -e $policy + exit 1 +} + +if [[ $push_command =~ $is_destructive ]] && [ $current_branch = $protected_branch ]; then + do_exit +fi + +if [[ $push_command =~ $is_destructive ]] && [[ $push_command =~ $protected_branch ]]; then + do_exit +fi + +if [[ $push_command =~ $will_remove_protected_branch ]]; then + do_exit +fi + +# Prevent ALL pushes to protected_branch +if [[ $push_command =~ $protected_branch ]] || [ $current_branch = $protected_branch ]; then + do_exit +fi + +unset do_exit + +exit 0 diff --git a/src/skeletons/express-ts/setup-repo.sh b/src/skeletons/express-ts/setup-repo.sh new file mode 100755 index 0000000..20ca20f --- /dev/null +++ b/src/skeletons/express-ts/setup-repo.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cp .githooks/* .git/hooks + +echo "hooks have been copied" \ No newline at end of file