Skip to content

Commit

Permalink
Merge pull request #56 from greg0ire/make
Browse files Browse the repository at this point in the history
Add support for make
  • Loading branch information
greg0ire committed May 17, 2018
2 parents 0a609bc + 7bb5562 commit 7dd1194
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/hooks/make.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Description

Runs one or several make targets on pre-commit, or on pre-push, or both.

# Activation

```sh
git config --add hooks.enabled-plugins make
```

# Configuration

By default, the `test` target will be run. To change the target:

```sh
git config hooks.make.target "my_target my_other_target"
```

By default, `make` is only run on pre-push, but pre-commit is also supported.
To change this, you can specify a list of hooks to run `make` on:

```sh
git config hooks.make.on "pre-commit pre-push"
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pages:
- [index.md, Home]
- [usage.md, Usage]
- [hooks/junk-checker.md, 'Hooks', 'Junk checker']
- [hooks/make.md, 'Hooks', 'Make']
- [hooks/syntax-checker.md, 'Hooks', 'Syntax checker']
- [hooks/work-in-progress.md, 'Hooks', 'Work In Progress']
- [hooks/php/composer.md, 'Hooks', 'PHP : Composer']
Expand Down
10 changes: 10 additions & 0 deletions template/hooks/make/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# shellcheck source=../git_config_wrapper.sh
. "$GIT_DIR/hooks/git_config_wrapper.sh"

get_hook_config make on hooks optional "pre-push"

if echo "$hooks"|grep --quiet pre-commit > /dev/null
then
. "$GIT_DIR/hooks/make/run-make"
fi
11 changes: 11 additions & 0 deletions template/hooks/make/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
# shellcheck source=../git_config_wrapper.sh
gitDir=$(git rev-parse --git-dir)
. "$gitDir/hooks/git_config_wrapper.sh"

get_hook_config make on hooks optional "pre-push"

if echo "$hooks"|grep --quiet pre-push > /dev/null
then
. "$gitDir/hooks/make/run-make"
fi
8 changes: 8 additions & 0 deletions template/hooks/make/run-make
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# shellcheck source=../git_config_wrapper.sh
gitDir=$(git rev-parse --git-dir)
. "$gitDir/hooks/git_config_wrapper.sh"

get_hook_config make target target optional test

make --directory="$(git rev-parse --show-toplevel)" $target
4 changes: 2 additions & 2 deletions template/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ for enabled_plugin in $(git config --get-all hooks.enabled-plugins)
do
if [ -f "$GIT_DIR/hooks/$enabled_plugin/pre-commit" ]
then
"$GIT_DIR/hooks/$enabled_plugin/pre-commit"
if [ $? -ne 0 ]

if ! "$GIT_DIR/hooks/$enabled_plugin/pre-commit"
then
exit 1
fi
Expand Down
57 changes: 57 additions & 0 deletions tests/make_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh

testExitsWithCodeGreaterWhenMakeFails()
{
initRepo
cat > Makefile <<MAKEFILE
my_target:
false
MAKEFILE
git add Makefile
git commit --message "Let's commit the Makefile" 1> /dev/null 2> "${stderrF}"
rtrn=$?
assertEquals "Make failure was not detected" 1 $rtrn
}

testItSupportsSeveralTargets()
{
initRepo
cat << MAKEFILE > Makefile
my_target:
echo "my_target" >> foo/result
my_other_target:
echo "my_other_target" >> foo/result
MAKEFILE
git config hooks.make.target "my_target my_other_target"
git add Makefile
mkdir foo
cd foo || exit 1
git commit --message "Let's commit the Makefile" 1> /dev/null 2> "${stderrF}"
rtrn=$?
assertEquals "Make ran successfully" 0 $rtrn
assertTrue 'First target was not run' "grep my_target result"
assertTrue 'Second target was not run' "grep my_other_target result"
}

initRepo()
{
cd "$testRepo" || exit 1
git init --quiet .
git config hooks.enabled-plugins make
git config hooks.make.target my_target
git config hooks.make.on "pre-commit pre-push"
}

oneTimeSetUp()
{
outputDir="${SHUNIT_TMPDIR}/output"
mkdir "${outputDir}"
stderrF="${outputDir}/stderr"

testRepo=$SHUNIT_TMPDIR/test_repo
mkdir --parents "$testRepo"
}

[ -n "${ZSH_VERSION:-}" ] && SHUNIT_PARENT=$0
. "$(which shunit2)"

0 comments on commit 7dd1194

Please sign in to comment.