forked from pre-commit/pre-commit.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
339 lines (321 loc) · 19.7 KB
/
index.html
File metadata and controls
339 lines (321 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:400,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="build/main.css">
<link rel="icon" href="favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>pre-commit by Yelp</title>
</head>
<body class="page-index" data-spy="scroll" data-target=".pc-sidebar">
<header class="navbar navbar-default pc-nav" role="banner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".pc-navbar-collapse">
<span class="sr-only">Toggle navigtaion</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/" class="navbar-brand pc-navbar-brand">
<i class="logo"></i>
</a>
</div>
<nav class="navbar-collapse pc-navbar pc-navbar-collapse collapse" role="navigation">
<ul class="nav navbar-nav">
<li><a href="/">Documentation</a></li>
<li><a href="hooks.html">Supported hooks</a></li>
<li><a href="https://github.com/pre-commit/demo-repo#readme">Demo</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/pre-commit/pre-commit" class="btn btn-default">Download on Github</a></li>
</ul>
</nav>
</div>
</header>
<div class="top-shelf">
<div class="container">
<img class="pc-darwin hidden-xs hidden-sm" src="pre-commit-darwin.png">
<h1>
<a href="http://github.com/pre-commit/pre-commit">
pre-commit
<span class="nowrap">by Yelp</span>
</a>
</h1>
<p>A framework for managing and maintaining multi-language <span class="nowrap">pre-commit</span> hooks.</p>
<p><a href="https://travis-ci.org/pre-commit/pre-commit"><img src="https://camo.githubusercontent.com/ea0ef9a42d3a39bf0a55b3f5d7d7a6fa5254bd01/68747470733a2f2f7472617669732d63692e6f72672f7072652d636f6d6d69742f7072652d636f6d6d69742e7376673f6272616e63683d6d6173746572" alt="Build Status" data-canonical-src="https://travis-ci.org/pre-commit/pre-commit.svg?branch=master" style="max-width:100%;"></a>
<a href="https://coveralls.io/r/pre-commit/pre-commit"><img src="https://camo.githubusercontent.com/aabc8c9324e9f816adc399bc2b29ca98e781a71e/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7072652d636f6d6d69742f7072652d636f6d6d69742e7376673f6272616e63683d6d6173746572" alt="Coverage Status" data-canonical-src="https://img.shields.io/coveralls/pre-commit/pre-commit.svg?branch=master" style="max-width:100%;"></a></p>
<p><iframe src="http://ghbtns.com/github-btn.html?user=pre-commit&repo=pre-commit&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" width="104px" height="20"></iframe>
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://pre-commit.com" data-text="pre-commit - A framework for managing and maintaining multi-language pre-commit hooks.">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script></p>
</div>
</div>
<div class="container pc-main-content">
<div class="row">
<div class="col-sm-3 hidden-xs pc-sidebar">
<ul class="nav nav-pills nav-stacked pc-sidenav" data-spy="affix" data-offset-top="400">
<li class="active"><a href="#intro">Introduction</a></li>
<li><a href="#install">Installation</a></li>
<li><a href="#plugins">Adding plugins</a></li>
<li><a href="#usage">Usage</a></li>
<li><a href="#new-hooks">Creating new hooks</a></li>
<li><a href="#advanced">Advanced features</a></li>
<li><a href="#contributing">Contributing</a></li>
</ul>
</div>
<div class="col-sm-9">
<div id="intro">
<div class="page-header">
<h1>Introduction</h1>
</div>
<p>
At Yelp we rely heavily on pre-commit hooks to find and fix common issues before
changes are submitted for code review. We run our hooks before every commit to
automatically point out issues like missing semicolons, whitespace problems, and
testing statements in code. Automatically fixing these issues before posting
code reviews allows our code reviewer to pay attention to the architecture of a
change and not worry about trivial errors.
</p>
<p>
As we created more libraries and projects we recognized that sharing our pre
commit hooks across projects is painful. We copied and pasted bash scripts
from project to project and had to manually change the hooks to work for
different project structures.
</p>
<p>
We believe that you should always use the best industry standard linters. Some
of the best linters are written in languages that you do not use in your project
or have installed on your machine. For example scss-lint is a linter for SCSS
written in Ruby. If you’re writing a project in node you should be able to use
scss-lint as a pre-commit hook without adding a Gemfile to your project or
understanding how to get scss-lint installed.
</p>
<p>
We built pre-commit to solve our hook issues. It is a multi-language package
manager for pre-commit hooks. You specify a list of hooks you want and
pre-commit manages the installation and execution of any hook written in any
language before every commit. pre-commit is specifically designed to not require
root access. If one of your developers doesn’t have node installed but modifies
a JavaScript file, pre-commit automatically handles downloading and building node
to run jshint without root.
</p>
</div>
<div id="install">
<div class="page-header">
<h1>Installation</h1>
</div>
<p>Before you can run hooks, you need to have the pre-commit package manager installed.</p>
<p>Using pip:</p>
<pre>pip install pre-commit</pre>
<p>Non Administrative Installation:</p>
<pre>curl http://pre-commit.com/install-local.py | python</pre>
<p>System Level Install:</p>
<pre>curl https://bootstrap.pypa.io/get-pip.py | sudo python - pre-commit</pre>
<p>In a Python Project, add the following to your requirements.txt (or requirements-dev.txt):</p>
<pre>pre-commit</pre>
</div>
<div id="plugins">
<div class="page-header">
<h1>Adding pre-commit plugins to your project</h1>
</div>
<p>Once you have pre-commit installed, adding pre-commit plugins to your project is done with the <code>.pre-commit-config.yaml</code> configuration file.</p>
<p>Add a file called <code>.pre-commit-config.yaml</code> to the root of your project. The pre-commit config file describes:</p>
<table class="table table-bordered">
<tbody>
<tr>
<td><code>repo</code>, <code>sha</code></td>
<td>where to get plugins (git repos). <code>sha</code> can also be a tag.</td>
</tr>
<tr>
<td><code>id</code></td>
<td>What plugins from the repo you want to use.</td>
</tr>
<tr>
<td><code>language_version</code></td>
<td>(optional) Override the default language version for the hook. See <a href="#overriding-language-version">Advanced Features: "Overriding Language Version"</a>.</td>
</tr>
<tr>
<td><code>files</code></td>
<td>(optional) Override the default pattern for files to run on.</td>
</tr>
<tr>
<td><code>exclude</code></td>
<td>(optional) File exclude pattern.</td>
</tr>
<tr>
<td><code>args</code></td>
<td>(optional) additional parameters to pass to the hook.</td>
</tr>
</tbody>
</table>
<p>For example:</p>
<pre>
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: 82344a4055f4e103afdc31e98a46de679fe55385
hooks:
- id: trailing-whitespace
</pre>
<p>This configuration says to download the pre-commit-hooks project and run its trailing-whitespace hook.</p>
</div>
<div id="usage">
<div class="page-header">
<h1>Usage</h1>
</div>
<p>Run <code>pre-commit install</code> to install pre-commit into your git hooks. pre-commit will now run on every commit. Every time you clone a project using pre-commit running <code>pre-commit install</code> should always be the first thing you do.</p>
<p>If you want to manually run all pre-commit hooks on a repository, run <code>pre-commit run --all-files</code>. To run individual hooks use <code>pre-commit run <hook_id></code>.</p>
<p>The first time pre-commit runs on a file it will automatically download, install, and run the hook. Note that running a hook for the first time may be slow. For example: If the machine does not have node installed, pre-commit will download and build a copy of node.</p>
</div>
<div id="new-hooks">
<div class="page-header">
<h1>Creating new hooks</h1>
</div>
<p>pre-commit currently supports hooks written in JavaScript (node), Python, Ruby and system installed scripts. As long as your git repo is an installable package (gem, npm, pypi, etc.) or exposes an executable, it can be used with pre-commit. Each git repo can support as many languages/hooks as you want.</p>
<p>
An executable must satisfy the following things:
<ul>
<li>Returncode of hook must be different between success / failures (Usually 0 for success, nonzero for failure)</li>
<li>It must take filenames</li>
</ul>
</p>
<p>A git repo containing pre-commit plugins must contain a hooks.yaml file that tells pre-commit:</p>
<table class="table table-bordered">
<tbody>
<tr>
<td><code>id</code></td>
<td>The id of the hook - used in pre-commit-config.yaml</td>
</tr>
<tr>
<td><code>name</code></td>
<td>The name of the hook - shown during hook execution</td>
</tr>
<tr>
<td><code>entry</code></td>
<td>The entry point - The executable to run</td>
</tr>
<tr>
<td><code>files</code></td>
<td>The pattern of files to run on.</td>
</tr>
<tr>
<td><code>language</code></td>
<td>The language of the hook - tells pre-commit how to install the hook.</td>
</tr>
<tr>
<td><code>description</code></td>
<td>(optional) The description of the hook.</td>
</tr>
<tr>
<td><code>language_version</code></td>
<td>(optional) See <a href="#overriding-language-version">Advanced Features: "Overriding Language Version"</a>.</td>
</tr>
<tr>
<td><code>expected_return_value</code></td>
<td>(optional) Defaults to 0.</td>
</tr>
</tbody>
</table>
<p>For example:</p>
<pre>
- id: trailing-whitespace
name: Trim Trailing Whitespace
description: This hook trims trailing whitespace.
entry: trailing-whitespace-fixer
language: python
files: \.(js|rb|md|py|sh|txt|yaml|yml)$
</pre>
<h2>Supported languages</h2>
<ul>
<li><code>node</code></li>
<li><code>python</code></li>
<li><code>ruby</code></li>
<li><code>pcre</code> - "Perl Compatible Regular Expression" Specify the regex as the <code>entry</code></li>
<li><code>script</code> - A script existing inside of a repository</li>
<li><code>system</code> - Executables available at the system level</li>
</ul>
</div>
<div id="advanced">
<div class="page-header">
<h1>Advanced features</h1>
</div>
<h2>Running in migration mode</h2>
<p>By default, if you have existing hooks <code>pre-commit install</code> will install in a migration mode which runs both your existing hooks and hooks for pre-commit. To disable this behavior, simply pass <code>-f</code> / <code>--overwrite</code> to the <code>install</code> command. If you decide not to use pre-commit, <code>pre-commit uninstall</code> will restore your hooks to the state prior to installation.</p>
<h2>Temporarily disabling hooks</h2>
<p>Not all hooks are perfect so sometimes you may need to skip execution of one or more hooks. pre-commit solves this by querying a <code>SKIP</code> environment variable. The <code>SKIP</code> environment variable is a comma separated list of hook ids. This allows you to skip a single hook instead of <code>--no-verify</code>ing the entire commit.</p>
<pre>$ SKIP=flake8 git commit -m "foo"</pre>
<h2>pre-commit during commits</h2>
<p>Running hooks on unstaged changes can lead to both false-positives and false-negatives during committing. pre-commit only runs on the staged contents of files by temporarily saving the contents of your files at commit time and stashing the unstaged changes while running hooks.</p>
<h2>pre-commit during merges</h2>
<p>The biggest gripe we’ve had in the past with pre-commit hooks was during merge conflict resolution. When working on very large projects a merge often results in hundreds of committed files. I shouldn’t need to run hooks on all of these files that I didn’t even touch! This often led to running commit with <code>--no-verify</code> and allowed introduction of real bugs that hooks could have caught. pre-commit solves this by only running hooks on files that conflict or were manually edited during conflict resolution.</p>
<h2>Passing arguments to hooks</h2>
<p>Sometimes hooks require arguments to run correctly. You can pass static arguments by specifying the <code>args</code> property in your <code>.pre-commit-config.yaml</code> as follows:</p>
<pre>
- repo: git://github.com/pre-commit/pre-commit-hooks
sha: a751eb58f91d8fa70e8b87c9c95777c5a743a932
hooks:
- id: flake8
args: [--max-line-length=131]
</pre>
<p>This will pass <code>--max-line-length=131</code> to <code>flake8</code>.</p>
<h2 id="overriding-language-version">Overriding Language Version</h2>
<p>Sometimes you only want to run the hooks on a specific version of the language. For each language, they default to using the system installed language (So for example if I’m running <code>python2.6</code> and a hook specifies <code>python</code>, pre-commit will run the hook using <code>python2.6</code>). Sometimes you don’t want the default system installed version so you can override this on a per-hook basis by setting the <code>language_version</code>.</p>
<pre>
- repo: git://github.com/pre-commit/mirrors-scss-lint
sha: d7266131da322d6d76a18d6a3659f21025d9ea11
hooks:
- id: scss-lint
language_version: 1.9.3-p484
</pre>
<p>This tells pre-commit to use <code>1.9.3-p484</code> to run the <code>scss-lint</code> hook.</p>
<p>Valid values for specific languages are listed below:</p>
<ul>
<li>
python: Whatever system installed python interpreters you have. The value of this argument is passed as the <code>-p</code> to <code>virtualenv</code>.
</li>
<li>
node: See <a href="https://github.com/ekalinin/nodeenv#advanced">nodeenv</a>.
</li>
<li>
ruby: See <a href="https://github.com/sstephenson/ruby-build/tree/master/share/ruby-build">ruby-build</a>
</li>
</ul>
</div>
<div id="contributing">
<div class="page-header">
<h1>Contributing</h1>
</div>
<p>
We’re looking to grow the project and get more contributors especially to support more languages/versions. We’d also like to get the hooks.yaml files added to popular linters without maintaining forks / mirrors.
</p>
<p>Feel free to submit Bug Reports, Pull Requests, and Feature Requests.</p>
<P>When submitting a pull request, please enable travis-ci for your fork.</p>
<div class="page-header">
<h1>Contributors</h1>
</div>
<ul>
<li><a href="https://github.com/asottile">Anthony Sottile</a></li>
<li><a href="https://github.com/struys">Ken Struys</a></li>
<li><a href="https://github.com/mfnkl">Molly Finkle</a></li>
</ul>
</div>
</div>
</div>
</div>
<footer class="pc-footer">
<div class="container">
Brought to you by <a href="http://www.yelp.com">Yelp</a>. Check out some of our other <a href="http://yelp.github.io/">open source projects</a> on <a href="https://github.com/yelp/">GitHub</a>.
</div>
</footer>
<script src="bower_components/jquery/jquery.min.js"></script>
<script src="bower_components/bootstrap-sass/dist/js/bootstrap.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-53969031-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>