├── .gitignore ├── .pre-commit-config.yaml ├── .pre-commit-hooks.yaml ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── pre_commit_hooks └── shell-lint.sh └── test ├── .pre-commit-config.yaml ├── init.sh └── test.sh /.gitignore: -------------------------------------------------------------------------------- 1 | test/.pre-commit-config.yaml 2 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | - repo: /Users/detailyang/art/opensource/personal/pre-commit-shell 2 | sha: 1b26bf757e6d5b9bfed2566339c6464f44779678 3 | hooks: 4 | - id: shell-lint 5 | exclude: test 6 | -------------------------------------------------------------------------------- /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | - id: shell-lint 2 | name: Shell Syntax Check 3 | description: Check Shell Syntax on ALL staged files with user friendly messages and colors 4 | entry: pre_commit_hooks/shell-lint.sh 5 | language: script 6 | types: [shell] 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: true 2 | dist: trusty 3 | before_install: 4 | - sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe" 5 | - sudo apt-get update 6 | - sudo apt-get install shellcheck 7 | - sudo pip install pre-commit 8 | - sudo git config --global user.email "detailyang@gmail.com" 9 | - sudo git config --global user.name "detailyang" 10 | script: 11 | - export PATH=/home/travis/.cabal/bin:$PATH 12 | - sudo git config --global user.email "detailyang@gmail.com" 13 | - sudo git config --global user.name "detailyang" 14 | - make test 15 | deploy: 16 | provider: releases 17 | api_key: $CI_USER_TOKEN 18 | skip_cleanup: true 19 | on: 20 | tags: true 21 | all_branches: true 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 detailyang 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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | 3 | test: 4 | bash test/init.sh 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pre-commit-shell 2 | ![Branch master](https://img.shields.io/badge/branch-master-brightgreen.svg?style=flat-square)[![Build](https://api.travis-ci.org/detailyang/pre-commit-shell.svg)](https://travis-ci.org/detailyang/pre-commit-shell)[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/detailyang/pre-commit-shell/master/LICENSE)[![release](https://img.shields.io/github/release/detailyang/pre-commit-shell.svg)](https://github.com/detailyang/pre-commit-shell/releases) 3 | 4 | pre-commit-shell is a [pre-commit](https://github.com/pre-commit/pre-commit) component, which wrapper [shellcheck](https://www.shellcheck.net/) to check shell scripts:) 5 | 6 | Table of Contents 7 | ----------------- 8 | 9 | * [Requirements](#requirements) 10 | * [Install](#install) 11 | * [Contributing](#contributing) 12 | * [License](#license) 13 | * [Author](#author) 14 | 15 | Requirements 16 | ------------ 17 | pre-commit-shell requires the following to run: 18 | 19 | * [pre-commit](http://pre-commit.com) 20 | * [shellcheck](https://www.shellcheck.net/) 21 | 22 | 23 | Install 24 | --------- 25 | 26 | 1. create .pre-commit-config.yaml in you git project 27 | 2. pre-commit install 28 | 3. enjoy it 29 | 30 | example .pre-commit-config.yaml as following: 31 | 32 | ```yaml 33 | - repo: git://github.com/detailyang/pre-commit-shell 34 | rev: v1.0.6 35 | hooks: 36 | - id: shell-lint 37 | args: [--format=json] 38 | ``` 39 | Contributing 40 | ------------ 41 | 42 | To contribute to pre-commit-shell, clone this repo locally and commit your code on a separate branch. 43 | 44 | 45 | Author 46 | ------ 47 | 48 | > GitHub [@detailyang](https://github.com/detailyang) 49 | 50 | 51 | License 52 | ------- 53 | 54 | pre-commit-shell is licensed under the [MIT](https://github.com/detailyang/pre-commit-shell/blob/master/LICENSE) license. 55 | -------------------------------------------------------------------------------- /pre_commit_hooks/shell-lint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -o errexit 4 | set -o pipefail 5 | set -o nounset 6 | 7 | DEBUG=${DEBUG:=0} 8 | [[ "$DEBUG" = "1" ]] && set -o xtrace 9 | 10 | if ! command which shellcheck &>/dev/null; then 11 | >&2 echo 'shellcheck command not found' 12 | exit 1 13 | fi 14 | 15 | shellcheck "$@" 16 | -------------------------------------------------------------------------------- /test/.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | - repo: https://github.com/detailyang/pre-commit-shell 2 | sha: 1.0.2 3 | hooks: 4 | - id: shell-lint 5 | -------------------------------------------------------------------------------- /test/init.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | which shellcheck &> /dev/null 4 | if [[ $? != 0 ]]; then 5 | echo "are you sure you have installed shellcheck?" 6 | exit 1 7 | fi 8 | 9 | cat << EOS > .pre-commit-config.yaml 10 | - repo: $(pwd) 11 | sha: $(git rev-parse HEAD) 12 | hooks: 13 | - id: shell-lint 14 | EOS 15 | 16 | tmpdir=$(mktemp -t pre-commit-shell.XXXXXX -d) 17 | cp test/test.sh "$tmpdir" 18 | cp test/.pre-commit-config.yaml "$tmpdir" 19 | pushd "$tmpdir" 20 | pwd 21 | git init 22 | git config user.email "detailyang@gmail.com" 23 | git config user.name "detailyang" 24 | pre-commit install 25 | git add .pre-commit-config.yaml; git commit -a -m "init test case" 26 | git add . --all 27 | tmpfile=$(mktemp -t pre-commit-shell.XXX) 28 | git commit -a -m "let begin test" &> "$tmpfile" 29 | popd 30 | rm -rf "$tmpdir" 31 | 32 | function passed() { 33 | echo "$@" 34 | 35 | return 0 36 | } 37 | 38 | function failed() { 39 | echo "$@" 40 | exit 255 41 | } 42 | 43 | grep --quiet "SC2115" $tmpfile && passed "SC2115 PASSED" || failed "SC2115 FAILED" 44 | grep --quiet "SC2086" $tmpfile && passed "SC2086 PASSED" || failed "SC2086 FAILED" 45 | grep --quiet "SC2034" $tmpfile && passed "SC2034 PASSED" || failed "SC2034 FAILED" 46 | -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # should trigger -- SC2115 4 | rm -rf "$abcd/" 5 | 6 | # should trigger -- SC2086 7 | [ $0 == $1 ] 8 | 9 | # should trigger -- SC1068 10 | var = 42 11 | --------------------------------------------------------------------------------