├── .eslintrc.json ├── .gitignore ├── .jscsrc ├── LICENSE ├── README.md ├── custom-hooks ├── commit-msg ├── post-checkout ├── post-commit ├── pre-commit ├── pre-rebase └── prepare-commit-msg ├── package.json ├── src └── index.js └── test └── test.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "node": true, 4 | "commonjs": true, 5 | "es6": true, 6 | "mocha": true 7 | }, 8 | "extends": "eslint:recommended", 9 | "parserOptions": { 10 | "sourceType": "module" 11 | }, 12 | "rules": { 13 | "indent": [ 14 | "error", 15 | 2 16 | ], 17 | "linebreak-style": [ 18 | "error", 19 | "unix" 20 | ], 21 | "quotes": [ 22 | "error", 23 | "single" 24 | ], 25 | "semi": [ 26 | "error", 27 | "always" 28 | ] 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Dependency directories 2 | node_modules 3 | yarn.lock 4 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "airbnb", 3 | "disallowMultipleLineBreaks": null, 4 | "requireSemicolons": true 5 | } 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Elizabeth Mabishi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # git-hooks 2 | An introduction to git hooks. 3 | 4 | Examples for the following git-hooks are present in this repository. 5 | 1. commit-msg 6 | 2. post-checkout 7 | 3. post-commit 8 | 4. pre-commit 9 | 5. pre-rebase 10 | 6. prepare-commit-msg 11 | -------------------------------------------------------------------------------- /custom-hooks/commit-msg: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Validates whether commit message is of a certain format. 4 | # Aborts commit if message is unsatisfactory 5 | 6 | # Standard commit from Pivotal Tracker [#135316555:Feature]Create Kafka Audit Trail 7 | commit_standard_regex='\[#[0-9]{9,}:[a-z]{3,}\]:[a-z].+|merge' 8 | error_message="Aborting commit. Please ensure your commit message meets the 9 | standard requirement. '[#StoryID:CommitType]Commit Message' 10 | Use '[#135316555:Feature]Create Kafka Audit Trail' for reference" 11 | 12 | 13 | if ! grep -iqE "$commit_standard_regex" "$1"; then 14 | echo "$error_message" >&2 15 | exit 1 16 | fi 17 | -------------------------------------------------------------------------------- /custom-hooks/post-checkout: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Executed immediately after a git checkout 4 | repository_name=`basename `git rev-parse --show-toplevel`` 5 | current_branch=`git rev-parse --abbrev-ref HEAD` 6 | present_working_directory=`pwd` 7 | requirements=`ls | grep 'requirements.txt' ` 8 | 9 | echo "Stashing changes previously made before pulling remote branch" 10 | git stash 11 | 12 | echo 13 | 14 | echo "Pulling remote branch ....." 15 | git pull origin $current_branch 16 | 17 | echo 18 | 19 | echo "Installing nodeJS dependencies ....." 20 | npm install 21 | 22 | echo 23 | 24 | echo "Installing yarn package ....." 25 | npm install yarn 26 | echo "Yarning dependencies ......" 27 | yarn 28 | 29 | echo 30 | 31 | # Only do this if you find a requirements.txt file at the root of the project 32 | if [ $present_working_directory == $repository_name ] && [ $requirements == 'requirements.txt']; then 33 | echo "Creating virtualenvironments for project ......." 34 | source `which virtualenv` 35 | echo 36 | mkvirtualenv $repository_name/$current_branch 37 | workon $repository_name/$current_branch 38 | echo "Installing python dependencies ......." 39 | pip install -r requirements.txt 40 | fi 41 | -------------------------------------------------------------------------------- /custom-hooks/post-commit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | say Congratulations! You\'ve just made a commit! Time for a break. 4 | -------------------------------------------------------------------------------- /custom-hooks/pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Exits with non zero status if tests fail or linting errors exist 4 | num_of_failures=`mocha -R json | grep failures -m 1 | awk '{print $2}' | sed 's/[,]/''/'` 5 | 6 | errors=`jscs -r inline ./test/test.js` 7 | num_of_linting_errors=`jscs -r junit ./test/test.js | grep failures -m 1 | awk '{print $4}' | sed 's/failures=/''/' | sed s/">"/''/ | sed s/\"/''/ | sed s/\"/''/` 8 | 9 | if [ $num_of_failures != '0' ]; then 10 | echo "$num_of_failures tests have failed. You cannot commit until all tests pass. 11 | Commit exiting with a non-zero status." 12 | exit 1 13 | fi 14 | 15 | if [ $num_of_linting_errors != '0' ]; then 16 | echo "Linting errors present. $errors" 17 | exit 1 18 | fi 19 | -------------------------------------------------------------------------------- /custom-hooks/pre-rebase: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "Dangerous to perform rebase. Aborting rebase." 4 | exit 1 5 | 6 | # current_branch=`git branch | grep -o '\*.*' | sed s/*/''/ | sed s/' '/''/` 7 | # 8 | # if [ $current_branch == "master" ] && [ $1 == "master" ]; then 9 | # echo "Cannot perform a rebase on the master branch. 10 | # Please checkout to another branch. Aborting rebase." 11 | # exit 1 12 | # fi 13 | 14 | 15 | # if [ $1 == "master"] || [ $2 == "master"]; then 16 | # echo "Cannot perform rebase on master. Aborting rebase." 17 | # exit 1 18 | # fi 19 | c 20 | -------------------------------------------------------------------------------- /custom-hooks/prepare-commit-msg: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Result will be output in place of the default commit message on running git commit 4 | 5 | current_branch=`git rev-parse --abbrev-ref HEAD` 6 | 7 | echo "#$current_branch Commit messages should be of the form [#StoryID:CommitType]Commit Message. Use [#135316555:Feature]Create Kafka Audit Trail for reference" > $1 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "git-hooks", 3 | "version": "1.0.0", 4 | "description": "A git-hooks primer", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/andela-emabishi/git-hooks.git" 12 | }, 13 | "keywords": [ 14 | "mocha", 15 | "chai", 16 | "expect" 17 | ], 18 | "author": "Elizabeth Mabishi", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/andela-emabishi/git-hooks/issues" 22 | }, 23 | "homepage": "https://github.com/andela-emabishi/git-hooks#readme", 24 | "devDependencies": { 25 | "chai": "^3.5.0", 26 | "jscs": "^3.0.7", 27 | "mocha": "^3.2.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | String.prototype.hasVowels = function hasVowels() { 2 | // Returns true if a vowel exists in the input string. Returns false otherwise. 3 | const vowels = new RegExp('[aeiou]', 'i'); 4 | return vowels.test(this); 5 | }; 6 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const expect = require('chai').expect; 2 | require('../src/index'); 3 | 4 | 5 | describe('Test hasVowels', () => { 6 | it('should return false if the string has no vowels', () => { 7 | expect('N VWLS'.hasVowels()).to.equal(false); 8 | }); 9 | it('should return true if the string has vowels', () => { 10 | expect('No vowels'.hasVowels()).to.equal(true); 11 | 12 | // Introduce failing test 13 | // expect('Has vowels'.hasVowels()).to.equal(false); 14 | }); 15 | }); 16 | --------------------------------------------------------------------------------