1 | # pre-git
|
2 |
|
3 | > Important git hooks implemented using Nodejs for your project.
|
4 |
|
5 | [![NPM][pre-git-icon]][pre-git-url]
|
6 |
|
7 | [![Build status][pre-git-ci-image]][pre-git-ci-url]
|
8 | [![Build Status][snapci-image]][snapci-url]
|
9 | [![dependencies][pre-git-dependencies-image]][pre-git-dependencies-url]
|
10 | [![devdependencies][pre-git-devdependencies-image]][pre-git-devdependencies-url]
|
11 | [![endorse][endorse-image]][endorse-url]
|
12 | [![semantic-release][semantic-image] ][semantic-url]
|
13 | [![manpm](https://img.shields.io/badge/manpm-%E2%9C%93-3399ff.svg)](https://github.com/bahmutov/manpm)
|
14 |
|
15 | [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
16 | [semantic-url]: https://github.com/semantic-release/semantic-release
|
17 |
|
18 | ## Why?
|
19 |
|
20 | You can easily run the `pre-commit` and `pre-push` Git hooks to
|
21 | [avoid breaking the local master branch](http://glebbahmutov.com/blog/never-break-master-by-accident/)
|
22 | or the [remote master](http://glebbahmutov.com/blog/never-break-remote-master-again/).
|
23 |
|
24 | ### Installation
|
25 |
|
26 | It's advised to install this module as `devDependency` in your `package.json`
|
27 | file so it doesn't get installed on production servers. Run:
|
28 |
|
29 | ```
|
30 | npm install pre-git --save-dev
|
31 | ```
|
32 |
|
33 | ### Configuration
|
34 |
|
35 | Specify commands to run on *commit* and on *push* in your package.json under `config > pre-git`
|
36 | object.
|
37 |
|
38 | ```json
|
39 | "scripts": {
|
40 | "test": "node-qunit *.js"
|
41 | },
|
42 | "config": {
|
43 | "pre-git": {
|
44 | "commit-msg": "simple",
|
45 | "pre-commit": [
|
46 | "grunt jshint"
|
47 | ],
|
48 | "post-commit": "git status",
|
49 | "pre-push": [
|
50 | "rm -rf node_modules",
|
51 | "npm install",
|
52 | "grunt build",
|
53 | "grunt test"
|
54 | ],
|
55 | "post-checkout": "npm install",
|
56 | "post-merge": "npm install"
|
57 | }
|
58 | }
|
59 | ```
|
60 |
|
61 | If there are older settings like `pre-commit`, etc, you will have to move
|
62 | them to the `config > pre-git` object manually.
|
63 |
|
64 | Related project: [post-merge-make](https://github.com/bahmutov/post-merge-make)
|
65 | runs `make post-merge` after pull or merge.
|
66 |
|
67 | ## Windows
|
68 |
|
69 | Thanks to [ybiquitous](https://github.com/ybiquitous) for
|
70 | [adding support](https://github.com/bahmutov/pre-git/pull/72) for Windows.
|
71 |
|
72 | * Git Bash (Git for Windows): work fine!
|
73 | * Command prompt: work if sh.exe in PATH (e.g. `set PATH=C:\Program Files\Git\bin;%PATH%`)
|
74 |
|
75 | ## Details
|
76 |
|
77 | You can always skip pre-commit hook (but not pre-push hook!) by using `-n` option
|
78 |
|
79 | git commit -m "done, don't check me" -n
|
80 |
|
81 | You can skip the pre-push hook using `--no-verify` option
|
82 |
|
83 | To run just the hook (for example to test what it does), execute
|
84 |
|
85 | ```bash
|
86 | .git/hooks/pre-commit
|
87 | .git/hooks/pre-push
|
88 | ```
|
89 |
|
90 | Since there might be no changes to push, you can force the `pre-commit` hook to execute
|
91 | using `-f` or `--force` argument
|
92 |
|
93 | ```bash
|
94 | .git/hooks/pre-commit -f
|
95 | ```
|
96 |
|
97 | ## Validating commit message
|
98 |
|
99 | By default, this package will install both the message validator
|
100 | and the message format helper. You can disable the validation
|
101 | by removing the below command.
|
102 |
|
103 | ```json
|
104 | "config": {
|
105 | "pre-git": {
|
106 | "commit-msg": "conventional"
|
107 | }
|
108 | }
|
109 | ```
|
110 |
|
111 | When you run `git commit -m "message ..."` the hook will enforce the default style
|
112 | `type(scope): message ...`. To better form the message, there is a CLI wizard
|
113 | installed based on [commitizen](https://www.npmjs.com/package/commitizen) project,
|
114 | very well shown in the tutorial
|
115 | [video](https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-writing-conventional-commits-with-commitizen). The commit wizard is available under "node_modules/.bin/commit-wizard" link
|
116 | and can be started from command line
|
117 |
|
118 | node --harmony node_modules/.bin/commit-wizard
|
119 |
|
120 | or via a script
|
121 |
|
122 | ```json
|
123 | "scripts": {
|
124 | "commit": "commit-wizard"
|
125 | }
|
126 | ```
|
127 |
|
128 | Then run
|
129 |
|
130 | npm run commit
|
131 |
|
132 | The wizard will look something like this
|
133 |
|
134 | ```
|
135 | ? Select the type of change that you're committing:
|
136 | feat: A new feature
|
137 | ❯ fix: A bug fix
|
138 | chore: Changes to the build process or auxiliary tools
|
139 | and libraries such as documentation generation
|
140 | ```
|
141 |
|
142 | There are 2 plugins included with `pre-git`:
|
143 |
|
144 | * "conventional" follows AngularJS log convention,
|
145 | see [conventional-commit-message](https://github.com/bahmutov/conventional-commit-message)
|
146 | * "simple" only allows "feat", "fix" and "chore" commit types,
|
147 | see [simple-commit-message](https://github.com/bahmutov/simple-commit-message)
|
148 |
|
149 |
|
150 | I recommend sticking with "simple" unless you need more release types.
|
151 |
|
152 | ## Development
|
153 |
|
154 | In order to locally test this package, from another git repo execute the install script
|
155 | using `-f` or `--force` argument. For example
|
156 |
|
157 | ```
|
158 | $ node ../pre-git/src/install.js -f
|
159 | pre-git 0.7.2 in /Users/kensho/git/test-git-hooks
|
160 | /Users/kensho/git/test-git-hooks
|
161 | read target package from /Users/kensho/git/test-git-hooks/package.json
|
162 | added empty command list for hook commit-msg
|
163 | added empty command list for hook pre-commit
|
164 | added empty command list for hook pre-push
|
165 | added empty command list for hook post-commit
|
166 | added empty command list for hook post-merge
|
167 | saving updated files /Users/kensho/git/test-git-hooks/package.json
|
168 | ```
|
169 |
|
170 | I am using a small project [test-pre-git](https://github.com/bahmutov/test-pre-git)
|
171 | as a test playground for these hooks.
|
172 |
|
173 | ## Debugging
|
174 |
|
175 | If a hook does not behave as expected, you can see verbose log messages by setting
|
176 | the environment variable `DEBUG=pre-git` when running a git command. For example, to
|
177 | see the messages when committing
|
178 |
|
179 | DEBUG=pre-git git commit -m "a message"
|
180 | running pre-commit script
|
181 | pre-git pre-commit +0ms arguments [ 'node',
|
182 | '/instant-vdom-todo/node_modules/.bin/pre-commit' ]
|
183 | pre-commit No changes detected, bailing out.
|
184 | pre-git found package in folder +0ms /instant-vdom-todo
|
185 | pre-git commit message wizard name from +5ms
|
186 | ...
|
187 |
|
188 | This should provide plenty of information to debug the hooks
|
189 |
|
190 | ## Local development
|
191 |
|
192 | To execute commit message validation, run `npm run demo-commit-message` and vary the
|
193 | test message to see how the hook validates it.
|
194 |
|
195 | ## Recent contributors
|
196 |
|
197 | * [ybiquitous](https://github.com/ybiquitous)
|
198 | * [Marko Schulz](https://github.com/datenreisender)
|
199 |
|
200 | ### Small print
|
201 |
|
202 | Author: Gleb Bahmutov © 2014
|
203 |
|
204 | * [@bahmutov](https://twitter.com/bahmutov)
|
205 | * [glebbahmutov.com](http://glebbahmutov.com)
|
206 | * [blog](http://glebbahmutov.com/blog)
|
207 |
|
208 | License: MIT - do anything with the code, but don't blame me if it does not work.
|
209 |
|
210 | Spread the word: tweet, star on github, etc.
|
211 |
|
212 | Support: if you find any problems with this module, email / tweet /
|
213 | [open issue](https://github.com/bahmutov/pre-git/issues?state=open) on Github
|
214 |
|
215 | [snapci-image]: https://snap-ci.com/bahmutov/pre-git/branch/master/build_image
|
216 | [snapci-url]: https://snap-ci.com/bahmutov/pre-git/branch/master
|
217 | [pre-git-icon]: https://nodei.co/npm/pre-git.svg?downloads=true
|
218 | [pre-git-url]: https://npmjs.org/package/pre-git
|
219 | [pre-git-ci-image]: https://travis-ci.org/bahmutov/pre-git.svg?branch=master
|
220 | [pre-git-ci-url]: https://travis-ci.org/bahmutov/pre-git
|
221 | [pre-git-dependencies-image]: https://david-dm.org/bahmutov/pre-git.svg
|
222 | [pre-git-dependencies-url]: https://david-dm.org/bahmutov/pre-git
|
223 | [pre-git-devdependencies-image]: https://david-dm.org/bahmutov/pre-git/dev-status.svg
|
224 | [pre-git-devdependencies-url]: https://david-dm.org/bahmutov/pre-git#info=devDependencies
|
225 | [endorse-image]: https://api.coderwall.com/bahmutov/endorsecount.png
|
226 | [endorse-url]: https://coderwall.com/bahmutov
|