UNPKG

7.28 kBMarkdownView Raw
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
20You 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/)
22or the [remote master](http://glebbahmutov.com/blog/never-break-remote-master-again/).
23
24### Installation
25
26It's advised to install this module as `devDependency` in your `package.json`
27file so it doesn't get installed on production servers. Run:
28
29```
30npm install pre-git --save-dev
31```
32
33### Configuration
34
35Specify commands to run on *commit* and on *push* in your package.json under `config > pre-git`
36object.
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
61If there are older settings like `pre-commit`, etc, you will have to move
62them to the `config > pre-git` object manually.
63
64Related project: [post-merge-make](https://github.com/bahmutov/post-merge-make)
65runs `make post-merge` after pull or merge.
66
67## Windows
68
69Thanks 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
77You 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
81You can skip the pre-push hook using `--no-verify` option
82
83To 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
90Since there might be no changes to push, you can force the `pre-commit` hook to execute
91using `-f` or `--force` argument
92
93```bash
94.git/hooks/pre-commit -f
95```
96
97## Validating commit message
98
99By default, this package will install both the message validator
100and the message format helper. You can disable the validation
101by removing the below command.
102
103```json
104"config": {
105 "pre-git": {
106 "commit-msg": "conventional"
107 }
108}
109```
110
111When 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
113installed based on [commitizen](https://www.npmjs.com/package/commitizen) project,
114very 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
116and can be started from command line
117
118 node --harmony node_modules/.bin/commit-wizard
119
120or via a script
121
122```json
123"scripts": {
124 "commit": "commit-wizard"
125}
126```
127
128Then run
129
130 npm run commit
131
132The 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
142There 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
150I recommend sticking with "simple" unless you need more release types.
151
152## Development
153
154In order to locally test this package, from another git repo execute the install script
155using `-f` or `--force` argument. For example
156
157```
158$ node ../pre-git/src/install.js -f
159pre-git 0.7.2 in /Users/kensho/git/test-git-hooks
160/Users/kensho/git/test-git-hooks
161read target package from /Users/kensho/git/test-git-hooks/package.json
162added empty command list for hook commit-msg
163added empty command list for hook pre-commit
164added empty command list for hook pre-push
165added empty command list for hook post-commit
166added empty command list for hook post-merge
167saving updated files /Users/kensho/git/test-git-hooks/package.json
168```
169
170I am using a small project [test-pre-git](https://github.com/bahmutov/test-pre-git)
171as a test playground for these hooks.
172
173## Debugging
174
175If a hook does not behave as expected, you can see verbose log messages by setting
176the environment variable `DEBUG=pre-git` when running a git command. For example, to
177see 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
188This should provide plenty of information to debug the hooks
189
190## Local development
191
192To execute commit message validation, run `npm run demo-commit-message` and vary the
193test 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
202Author: Gleb Bahmutov © 2014
203
204* [@bahmutov](https://twitter.com/bahmutov)
205* [glebbahmutov.com](http://glebbahmutov.com)
206* [blog](http://glebbahmutov.com/blog)
207
208License: MIT - do anything with the code, but don't blame me if it does not work.
209
210Spread the word: tweet, star on github, etc.
211
212Support: 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