Create base

This commit is contained in:
femsci 2022-04-18 17:50:40 +02:00
commit 88629c9467
Signed by: femsci
GPG key ID: 08F7911F0E650C67
60 changed files with 312 additions and 0 deletions

9
.gitignore vendored Normal file
View file

@ -0,0 +1,9 @@
.vscode
**/.DS_Store
*.swp
*.*~
data/*
!data/.gitkeep
custom.d/*/*
!custom.d/*/.gitkeep

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 nya
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

26
README.md Normal file
View file

@ -0,0 +1,26 @@
# pinch
*A pinch of Git ~~pinches~~ hooks*
Pinch is a global Git hook manager. Its main use is to establish a modular hook system.
## Requirements
- A mostly POSIX-compatible OS
- Bash >= 5.0
- Busybox, Coreutils or alternatives
- Git >= 2.9
## Installation
To install pinch, clone this repository and execute **[install.sh](install.sh)**. If there is different global hook configuration, pinch will remember it.
Similarly, in order to uninstall pinch, execute **[uninstall.sh](uninstall.sh)**. Previous configuration (if there was any) will be restored.
## Debug mode
In order to enable debug messages, set `PINCH_DEBUG` to `1` as an environment variable before invoking Git hooks.
## License
pinch is a free and open-source program distributed under the terms of the [MIT license](https://opensource.org/licenses/MIT).

4
custom.d/README Normal file
View file

@ -0,0 +1,4 @@
This directory (custom.d) is dedicated to custom hooks.
In order to add a custom hook, insert the file to a subdirectory bearing the respective category.
All hidden files (starting with a dot) and files not marked as executable are ignored.
The contents of the subdirectories (except for .gitkeep) are not tracked and will be preserved during project updates.

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

0
custom.d/update/.gitkeep Normal file
View file

0
data/.gitkeep Normal file
View file

52
hooks/_init.sh Executable file
View file

@ -0,0 +1,52 @@
#!/usr/bin/env bash
HOOK_ARGS=$@
HOOK_PATH=$(realpath $0)
HOOK_PATH=${HOOK_PATH%/*}
export HOOK_PATH
err_trap() {
echo "Error: $BASH_COMMAND"
}
if [[ $PINCH_DEBUG -eq 1 ]]; then
pdebug() {
echo -e "\033[33m(DEBUG)\033[0m $@"
}
else
pdebug() {
:
}
fi
pinfo() {
echo -e "\033[32m=>\033[0m $@"
}
export -f pdebug
export -f err_trap
trap err_trap ERR
exec_custom() {
local hooks_path=$HOOK_PATH/../custom.d/$1
for hook in $(ls $hooks_path); do
if [ -x $hooks_path/$hook ]; then
pdebug "Executing custom hook $hooks_path/$hook"
$hooks_path/$hook $HOOK_ARGS
fi
done
}
exec_all() {
for file in $(ls $1 | sort); do
if [ -x $1/$file ]; then
pdebug "Executing hook $1/$file"
$1/$file $HOOK_ARGS
fi
done
}
pdebug "Hook type: $(basename $0)"

6
hooks/applypatch-msg Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/applypatch-msg.d
exec_custom applypatch-msg

View file

6
hooks/commit-msg Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/commit-msg.d
exec_custom commit-msg

View file

6
hooks/post-applypatch Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-applypatch.d
exec_custom post-applypatch

View file

6
hooks/post-checkout Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-checkout.d
exec_custom post-checkout

View file

6
hooks/post-commit Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-commit.d
exec_custom post-commit

View file

6
hooks/post-merge Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-merge.d
exec_custom post-merge

View file

6
hooks/post-receive Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-receive.d
exec_custom post-receive

View file

6
hooks/post-rewrite Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-rewrite.d
exec_custom post-rewrite

View file

6
hooks/post-update Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/post-update.d
exec_custom post-update

View file

6
hooks/pre-applypatch Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-applypatch.d
exec_custom pre-applypatch

View file

6
hooks/pre-auto-gc Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-auto-gc.d
exec_custom pre-auto-gc

View file

6
hooks/pre-commit Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-commit.d
exec_custom pre-commit

View file

6
hooks/pre-push Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-push.d
exec_custom pre-push

View file

6
hooks/pre-rebase Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-rebase.d
exec_custom pre-rebase

View file

6
hooks/pre-receive Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/pre-receive.d
exec_custom pre-receive

View file

6
hooks/prepare-commit-msg Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/prepare-commit-msg.d
exec_custom prepare-commit-msg

View file

6
hooks/update Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
. "${0%/*}/_init.sh"
exec_all $HOOK_PATH/update.d
exec_custom update

0
hooks/update.d/.gitkeep Normal file
View file

22
install.sh Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
current_path=$(realpath $0)
hook_base=${current_path%/*}
if [[ -e $hook_base/data/install ]]; then
echo "Pinch already installed" >&2
exit 1
fi
old_cfg=$(git config --global --get core.hooksPath)
hooks_path=$hook_base/hooks
if [ ! $? -eq 0 ]; then
echo $old_cfg >$hook_base/data/old_cfg
fi
git config --global core.hooksPath $hooks_path
echo $(date +%s) >$hook_base/data/install
echo "Pinch installed."

55
tools/populate.sh Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env bash
#
# This script is used to populate hook directories
# Use flag -c to clean the hook directories
# If hook files are already present, they will be overwritten
#
# Add or remove in order to generate different hooks
# For available hooks, see githooks(5)
hooktypes="applypatch-msg \
pre-applypatch \
post-applypatch \
pre-commit \
prepare-commit-msg \
commit-msg \
post-commit \
pre-rebase \
post-checkout \
post-merge \
pre-receive \
update \
post-receive \
post-update \
pre-auto-gc \
post-rewrite \
pre-push"
path=$(realpath $0)
path=${path%/*}/..
# Clean the hook directories if -c flag is present
if [[ $1 == "-c" ]]; then
find $path/hooks -mindepth 1 -maxdepth 1 -not -name "_init.sh" -exec rm -r {} +
find $path/custom.d -mindepth 1 -maxdepth 1 -not -name "README" -exec rm -r {} +
else
populate_hook() {
echo "#!/usr/bin/env bash
. \"\${0%/*}/_init.sh\"
exec_all \$HOOK_PATH/$1.d
exec_custom $1
" >$path/hooks/$1
chmod +x $path/hooks/$1
}
for hooktype in $hooktypes; do
mkdir -p $path/custom.d/$hooktype
touch $path/custom.d/$hooktype/.gitkeep
mkdir -p $path/hooks/$hooktype.d
touch $path/hooks/$hooktype.d/.gitkeep
populate_hook $hooktype
done
fi

21
uninstall.sh Executable file
View file

@ -0,0 +1,21 @@
#!/usr/bin/env bash
current_path=$(realpath $0)
hook_base=${current_path%/*}
if [[ ! -e $hook_base/data/install ]]; then
echo "Pinch not installed. Consider installing pinch using install.sh..." >&2
exit 1
fi
if [[ -r $hook_base/data/old_cfg ]]; then
old_cfg=$(cat $hook_base/data/old_cfg)
git config --global core.hooksPath $old_cfg
rm $hook_base/data/old_cfg
else
git config --global --unset core.hooksPath
fi
rm $hook_base/data/install
echo "Pinch uninstalled..."