Skip to content

Commit

Permalink
#6 Change main to run command as external file
Browse files Browse the repository at this point in the history
  • Loading branch information
askirmas committed Nov 29, 2019
1 parent a9101fa commit de02545
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
16 changes: 16 additions & 0 deletions hooks/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
MY_DIR=$(dirname "$(realpath "$0")")
HOOKS_PATH="${1:-git-hooks}"

echo -n "$HOOKS_PATH" > "$MY_DIR/hooks_dir"

git config --local core.hooksPath "$MY_DIR"
CORE=$(git config --local --get core.hooksPath)
if [ "$CORE" == "$MY_DIR" ]
then
echo "Wrapped git hooks in folder '$HOOKS_PATH'"
exit 0
else
echo "Something went wrong. Run manually: git config --local core.hooksPath \"$MY_DIR\""
exit 1
fi
16 changes: 0 additions & 16 deletions hooks/init.sh

This file was deleted.

12 changes: 3 additions & 9 deletions main.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#!/bin/bash
MY_DIR=$(dirname "$(realpath "$0")")

case "$1" in
--pre-commit)
$MY_DIR/hooks/pre-commit
;;
*)
$MY_DIR/hooks/init.sh "$@"
;;
esac
CMD="$1"; shift
test -e "$MY_DIR/hooks/$CMD" && ($MY_DIR/hooks/$CMD $@; exit $?)
test -e "$MY_DIR/scripts/$CMD.sh" && ($MY_DIR/scripts/$CMD.sh $@; exit $?)
exit $?
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"test": "./test.sh",
"release": "np",
"postinstall": "./main.sh"
"postinstall": "./main.sh init"
},
"repository": {
"type": "git",
Expand Down
7 changes: 3 additions & 4 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
reset=\\033[0m

HOOKS=$(cat hooks/hooks_dir)
./main.sh $HOOKS
./main.sh init $HOOKS

_it() {
echo -e "\033[1;30;43m TEST \033[0m \033[1;4m$test$reset"
Expand Down Expand Up @@ -37,11 +37,10 @@ test="first commit without hooks"
_it; _commit
[ "$?" == 0 ] || _failed

$MY_DIR/main.sh init $HOOKS
HOOKS_CONFIG=$(git config --get core.hooksPath | sed -e 's/^\([A-Z]\):/\/\1/' | tr '[:upper:]' '[:lower:]')
test="init"
_it
$MY_DIR/main.sh $HOOKS

HOOKS_CONFIG=$(git config --get core.hooksPath | sed -e 's/^\([A-Z]\):/\/\1/' | tr '[:upper:]' '[:lower:]')
[ "$HOOKS_CONFIG" == $(echo "$MY_DIR/hooks" | tr '[:upper:]' '[:lower:]') ] \
|| _failed "$HOOKS_CONFIG != $MY_DIR/hooks"

Expand Down

0 comments on commit de02545

Please sign in to comment.