|
1 | | -simple-linter-git-hooks |
2 | | -======================= |
| 1 | +# simple-linter-git-hooks |
3 | 2 |
|
4 | | -A collection of simple linting scripts to use with icefox/git-hooks. |
| 3 | +A collection of simple linting templates to use with icefox/git-hooks. |
| 4 | + |
| 5 | +# Prerequisites |
| 6 | + |
| 7 | +## Required |
| 8 | + |
| 9 | +1. Download and install the [Git version control system](https://git-scm.com/). |
| 10 | + Exactly how you do this will depends on the type of computer you have, e.g.: |
| 11 | + |
| 12 | + $ brew install git # MacOS with the Homebrew package manager. |
| 13 | + $ sudo apt install git # Linux with the Advanced Package Tool package manager. |
| 14 | + $ sudo yum install git # Linux with the Yellowdog Update Manager package manager. |
| 15 | + |
| 16 | +2. Download [icefox/git-hooks](https://github.com/icefox/git-hooks) and add |
| 17 | + it's `git-hooks` script to your shell's `$PATH`, e.g.: |
| 18 | + |
| 19 | + $ git clone --recursive https://github.com/icefox/git-hooks.git |
| 20 | + $ # If you use the Bourne-again Shell (bash): |
| 21 | + $ echo "export PATH=\$PATH:$PWD/git-hooks" | tee -a $HOME/.bashrc |
| 22 | + $ # If you use the Friendly Interactive Shell (fish): |
| 23 | + $ echo "set PATH \$PATH $PWD/git-hooks" | tee -a $HOME/.config/fish/config.fish |
| 24 | + |
| 25 | +## Optional |
| 26 | + |
| 27 | +Many of the `pre-commit` hooks call other programs to perform linting on files |
| 28 | +you're about to commit. You'll need to install the appropriate program(s) for |
| 29 | +the type of file(s) you want to lint: |
| 30 | + |
| 31 | +* Syntax linters: |
| 32 | + * If you want to syntax-lint Bourne-again Shell scripts, you will need to |
| 33 | + install [GNU Bash](https://www.gnu.org/software/bash/). |
| 34 | + |
| 35 | + $ which bash # returns a path if it is installed properly. |
| 36 | + |
| 37 | + * If you want to syntax-lint Friendly Interactive Shell scripts, you will |
| 38 | + need to install the [fish shell](http://fishshell.com/). |
| 39 | + |
| 40 | + $ which fish # returns a path if it is installed properly. |
| 41 | + |
| 42 | + * If you want to syntax-lint JavaScript files, you will need to install |
| 43 | + [Node.js](https://nodejs.org/). |
| 44 | + |
| 45 | + $ which node # returns a path if it is installed properly. |
| 46 | + |
| 47 | + * If you want to syntax-lint PHP files, you will need to |
| 48 | + [install PHP](http://php.net/manual/install.php). |
| 49 | + |
| 50 | + $ which php # returns a path if it is installed properly. |
| 51 | + |
| 52 | + * If you want to syntax-lint SCSS files, you will need to install |
| 53 | + [Sass](https://github.com/sass/sass#using). |
| 54 | + |
| 55 | + $ which scss # returns a path if it is installed properly. |
| 56 | + |
| 57 | +* Style linters: |
| 58 | + * If you want to style-lint CSS files, you will need to |
| 59 | + [install CSSLint](https://github.com/CSSLint/csslint/wiki/Command-line-interface). |
| 60 | + |
| 61 | + $ which csslint # returns a path if it is installed properly. |
| 62 | + |
| 63 | + * If you want to style-lint PHP files, you will need to |
| 64 | + [install PHPCodeSniffer and the Drupal Coder sniffs](https://www.drupal.org/node/1419988). |
| 65 | + |
| 66 | + $ which phpcs # returns a path if PHPCodeSniffer is installed properly. |
| 67 | + $ phpcs -i # lists Drupal if the Drupal Coder Sniffs are installed properly. |
| 68 | + |
| 69 | + * If you want to style-lint JS files, you will need to |
| 70 | + [install ESLint](http://eslint.org/docs/user-guide/getting-started#local-installation-and-usage). |
| 71 | + |
| 72 | + $ which eslint # returns a path if it is installed properly. |
| 73 | + |
| 74 | + * If you want to style-lint Puppet files, you will need to |
| 75 | + [install puppet-lint](https://github.com/rodjek/puppet-lint#installation). |
| 76 | + |
| 77 | + $ which puppet-lint # returns a path if it is installed properly. |
| 78 | + |
| 79 | + * If you want to style-lint Ruby files, you will need to |
| 80 | + [install Rubocop](https://github.com/bbatsov/rubocop#installation). |
| 81 | + |
| 82 | + $ which rubocop # returns a path if it is installed properly. |
| 83 | + |
| 84 | + * If you want to style-lint SCSS files, you will need to |
| 85 | + [install SCSS-lint](https://github.com/brigade/scss-lint#installation). |
| 86 | + |
| 87 | + $ which scss-lint # returns a path if it is installed properly. |
| 88 | + |
| 89 | + * If you want to style-lint Bourne-again Shell scripts, you will need to |
| 90 | + [install ShellCheck](https://github.com/koalaman/shellcheck#installing). |
| 91 | + |
| 92 | + $ which shellcheck # returns a path if it is installed properly. |
| 93 | + |
| 94 | +* Best-practice linters: |
| 95 | + * If you want to best-practice-lint PHP files, you will need to |
| 96 | + [install PHPCodeSniffer and the Drupal Coder sniffs](https://www.drupal.org/node/1419988). |
| 97 | + |
| 98 | + $ which phpcs # returns a path if PHPCodeSniffer is installed properly. |
| 99 | + $ phpcs -i # lists DrupalPractice if the Drupal Coder Sniffs are installed properly. |
| 100 | + |
| 101 | +# Install |
| 102 | + |
| 103 | +1. Download this repository: |
| 104 | + |
| 105 | + $ cd $HOME/Documents/Reference # or wherever you want |
| 106 | + $ git clone --recursive https://github.com/mparker17/simple-linter-git-hooks.git |
| 107 | + |
| 108 | +# Use |
| 109 | + |
| 110 | +1. Set up `icefox/git-hooks` in the repository you want to run these linters in: |
| 111 | + |
| 112 | + $ cd /path/to/repo |
| 113 | + $ git hooks install |
| 114 | + |
| 115 | +2. Create a folder for these hooks: |
| 116 | + |
| 117 | + $ cd /path/to/repo |
| 118 | + $ mkdir git_hooks |
| 119 | + $ # or, `mkdir .githooks` if you'd prefer |
| 120 | + $ cp -r $HOME/Documents/Reference/simple-linter-git-hooks/pre-commit git_hooks/ |
| 121 | + $ # or, `cp -r $HOME/Documents/Reference/simple-linter-git-hooks/pre-commit .githooks/` if you did that in the previous step |
| 122 | + $ git hooks # will list all the hooks if you did that correctly |
| 123 | + |
| 124 | + **This project is designed to be a template you can customize**, so once you |
| 125 | + have installed these linter templates, feel free to: |
| 126 | + |
| 127 | + * delete any linters that you do not want to use, |
| 128 | + * customize any linters according to your / your team's preferences, and/or, |
| 129 | + * commit the linters to your repo for everyone on your team to use. |
| 130 | + * If the rest of your team is not comfortable with running linters in |
| 131 | + their pre-commit hooks, and/or committing them to the repository, |
| 132 | + `echo "/git_hooks" | tee -a .git/info/exclude` will tell Git to |
| 133 | + ignore them. If you want to stop ignoring them, you'll have to open |
| 134 | + the `.git/info/exclude` file and delete the `/git_hooks` line. |
| 135 | + |
| 136 | +3. Work normally, and commit your work using the command-line. |
| 137 | + |
| 138 | + The linter scripts that begin with `1-` will abort the commit if the linter |
| 139 | + returns an "unsuccessful" error code to bash (i.e.: non-zero). These linters |
| 140 | + are syntax-linters, meaning trying to compile or run the code in those files |
| 141 | + will certainly result in a fatal error. This behavior is based upon the |
| 142 | + assumption that you would never want to intentionally commit things that did |
| 143 | + that! |
| 144 | + |
| 145 | + All other linter scripts will allow the commit to happen, but will display |
| 146 | + their output, giving you the option of fixing the code and either amending |
| 147 | + the last commit, or creating a new commit with the fixes depending on your / |
| 148 | + your team's preference. |
| 149 | + |
| 150 | +# Notes |
| 151 | + |
| 152 | +**If you use a third-party GUI** (e.g.: Github Desktop, Atom, Sourcetree, Tower, |
| 153 | +etc.) **your mileage may vary**. You will need to check your GUI's documentation |
| 154 | +to determine: |
| 155 | + |
| 156 | +1. Does it actually run pre-commit hooks upon commit? |
| 157 | +2. Will it allow pre-commit hooks to abort a commit? |
| 158 | +3. Does the GUI have a way to display the messages that the pre-commit hooks |
| 159 | + spit out, *even if the commit does not fail*? |
| 160 | + |
| 161 | +Because there are so many GUIs available, and because I currently prefer to use |
| 162 | +the command-line for most (but not all) operations, I have no current plans for |
| 163 | +this project to support git GUIs. |
0 commit comments