├── .travis.yml
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── docs
└── logo.png
└── src
├── post-merge
└── composer_install.sh
├── pre-commit
├── check_working_on_master.sh
├── php_cs_fixer.sh
└── phpunit.sh
└── prepare-commit-msg
└── ticket_number.sh
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: bash
2 |
3 | git:
4 | depth: 1
5 |
6 | # faster builds on new travis setup not using sudo
7 | sudo: false
8 |
9 | # show spellcheck version
10 | before_script:
11 | - shellcheck --version
12 |
13 | script:
14 | # Fail if any of these files have warnings
15 | - shellcheck ./src/*/*.sh
16 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Contributing
2 | ============
3 |
4 | We welcome any contributing!
5 |
6 | Pull requests
7 | -------------
8 |
9 | You must follow the next rules to contribute to this project:
10 |
11 | - **Create feature branches** - You must create a new branch for a feature. We don't accept pull requests from master branch.
12 |
13 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
14 |
15 | - **Only English** - Please use English language in pull requests.
16 |
17 | - **Readme** - You should add your script to README.md with a short description and symlink creating command example.
18 |
19 | Spellcheck
20 | ----------
21 |
22 | Before creating pull request you can check your script with [www.shellcheck.net](https://www.shellcheck.net/) and fix issues.
23 |
24 | Happy coding :)
25 | ---------------
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2019 - 2024, Volodymyr Kupriienko
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | * Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | * Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
Useful scripts for Git Hooks
6 |
7 |
8 | [](https://travis-ci.org/greeflas/git-hooks)
9 |
10 | ### prepare-commit-msg
11 |
12 | * [ticket_number.sh](src/prepare-commit-msg/ticket_number.sh) - this script adds ticket number to commits
13 | in feature branch. It gets ticket number from branch name. Branch name should have following format `EXAMPLE-305/some_feature`.
14 |
15 | For example if first commit is named `Implements some feature` this script will update it with ticket number and it will look like `EXAMPLE-305 Implements some feature`.
16 |
17 | Also you can set `EACH_COMMIT` variable to `false` if you want add ticket number only for the first commit.
18 |
19 | > Symlink: ln -s ~/tools/git-hooks/src/prepare-commit-msg/ticket_number.sh .git/hooks/prepare-commit-msg
20 |
21 | ### pre-commit
22 |
23 | * [php_cs_fixer.sh](src/pre-commit/php_cs_fixer.sh) - this script runs PHP-CS-Fixer before commit for fixing code style.
24 |
25 | > Symlink: ln -s ~/tools/git-hooks/src/pre-commit/php_cs_fixer.sh .git/hooks/pre-commit
26 |
27 | * [phpunit.sh](src/pre-commit/phpunit.sh) - this script runs PHPUnit before commit for running tests.
28 |
29 | > Symlink: ln -s ~/tools/git-hooks/src/pre-commit/phpunit.sh .git/hooks/pre-commit
30 |
31 | * [check_working_on_master.sh](src/pre-commit/check_working_on_master.sh) - this script helps to warn if you make commit on the master branch.
32 |
33 | > Symlink: ln -s ~/tools/git-hooks/src/pre-commit/check_working_on_master.sh .git/hooks/pre-commit
34 |
35 | ### post-merge
36 |
37 | * [composer_install.sh](src/post-merge/composer_install.sh) - this script installs composer packages specified in
38 | composer.json or composer.lock (if it presents) file after git pull or git merge.
39 |
40 | > Symlink: ln -s ~/tools/git-hooks/src/post-merge/composer_install.sh .git/hooks/post-merge
41 |
42 | Installation
43 | ------------
44 |
45 | 1. Pull this repo to some place in your machine
46 |
47 | `$ git clone https://github.com/greeflas/git-hooks.git`
48 |
49 | 2. Create a symlink for needed script in your project `.git/hooks` directory
50 |
51 | `$ ln -s ~/tools/git-hooks/src/prepare-commit-msg/ticket_number.sh .git/hooks/prepare-commit-msg`
52 |
53 | Contributing
54 | ------------
55 |
56 | For information about contributing please read [CONTRIBUTING.md](CONTRIBUTING.md).
57 |
58 | License
59 | -------
60 |
61 | [](LICENSE)
62 |
63 | This project is released under the terms of the BSD-3-Clause [license](LICENSE).
64 |
65 | Copyright (c) 2019 - 2024, Volodymyr Kupriienko
66 |
67 |
--------------------------------------------------------------------------------
/docs/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/greeflas/git-hooks/e856a834cd72c41bfbec8efdcfe46fc259a249db/docs/logo.png
--------------------------------------------------------------------------------
/src/post-merge/composer_install.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -e
4 |
5 | command -v composer > /dev/null 2>&1
6 | COMPOSER=$?
7 |
8 | if [[ $COMPOSER -ne 0 ]];
9 | then
10 | echo "Composer is not installed!"
11 | exit 1
12 | fi
13 |
14 | composer install
15 |
--------------------------------------------------------------------------------
/src/pre-commit/check_working_on_master.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -e
4 |
5 | BRANCH_MASTER='master'
6 | YELLOW='\033[0;33m'
7 | CO='\033[0m'
8 |
9 | current_branch=$(git rev-parse --abbrev-ref HEAD)
10 |
11 | if [[ $current_branch == "$BRANCH_MASTER" ]];
12 | then
13 | printf "\n$%s!!!WARNING!!! Now you are working on the master branch.\n%s" "$YELLOW" "$CO"
14 | exec < /dev/tty
15 | while :
16 | do
17 | read -pr "Are you really want to continue? [y/n] " answer
18 | case "$answer" in
19 | y|yes)
20 | printf "Commited successfully.\n"
21 | exit 0
22 | ;;
23 | n|no)
24 | echo "Commit was canceled."
25 | exit 1
26 | ;;
27 | *)
28 | echo "Enter something y|yes or n|no."
29 | ;;
30 | esac
31 | done
32 | fi
33 |
--------------------------------------------------------------------------------
/src/pre-commit/php_cs_fixer.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | if [[ ! -d vendor/friendsofphp/php-cs-fixer ]];
4 | then
5 | echo "PHP-CS-Fixer is not installed! You can install package with following command"
6 | echo "composer require --dev friendsofphp/php-cs-fixer"
7 | exit 1
8 | fi
9 |
10 | vendor/bin/php-cs-fixer fix --diff
11 |
12 | git add --all
13 |
--------------------------------------------------------------------------------
/src/pre-commit/phpunit.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | PACKAGE=phpunit/phpunit
4 |
5 | if [[ ! -d vendor/$PACKAGE ]];
6 | then
7 | echo "PHPUnit is not installed! You can install package with following command"
8 | echo "composer require --dev $PACKAGE"
9 | exit 1
10 | fi
11 |
12 | vendor/bin/phpunit
13 |
--------------------------------------------------------------------------------
/src/prepare-commit-msg/ticket_number.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -e
4 |
5 | # Append ticket number to each commit or for first only
6 | EACH_COMMIT=true
7 |
8 | base_commit_hash=$(git log origin/master -n 1 --format=%H)
9 | parent_commit_hash=$(git log -n 1 --format=%H)
10 | ticket=$(git rev-parse --abbrev-ref HEAD | cut -f 1 -d '/')
11 |
12 | if [[ $EACH_COMMIT || $base_commit_hash == "$parent_commit_hash" ]];
13 | then
14 | original_message=$(cat "$1")
15 |
16 | if [[ $original_message != *$ticket* ]];
17 | then
18 | echo "$ticket $original_message" > "$1"
19 | fi
20 | fi
21 |
--------------------------------------------------------------------------------