-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit.html
More file actions
773 lines (708 loc) · 23.4 KB
/
Copy pathpre-commit.html
File metadata and controls
773 lines (708 loc) · 23.4 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PRE Commits by Adam</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/black.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>PRE-COMMIT</h1>
<h2>od zera do <strike>bohatera</strike> 0.1</h2>
</section>
<section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>What are <b>pre-commits</b>?</h2>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>What are <b>pre-commits</b>?</h2>
<p>Pre-Commits is a part of bigger concept named <b>Git Hooks</b></p>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>What are <b>pre-commits</b>?</h2>
<p>Pre-Commits is a part of bigger concept named <b>Git Hooks</b></p>
<p>
<b>Git Hooks</b> are events triggered by actions like commit, merge, pull, etc...
</p>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Hooks are stored in folder:
`.git/hooks`
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Hooks are stored in folder:
`.git/hooks`
You can find there the following files:
```sh
applypatch-msg.sample
commit-msg.sample
fsmonitor-watchman.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-merge-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
push-to-checkout.sample
update.sample
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Hooks are stored in folder:
`.git/hooks`
These are just samples, but...
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Hooks are stored in folder:
`.git/hooks`
These are just samples, but...
As you can see there's a lot of it. Each of it can be configured separately and utilized up to your requirements!
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Hooks are stored in folder:
`.git/hooks`
These are just samples, but...
As you can see there's a lot of it. Each of it can be configured separately and utilized up to your requirements!
Despite it's a big range, we will focus on pre-commits only :)
</script>
</section>
<section data-markdown>
<script type="text/template">
Actions defined in git hooks are run by Python.
"Which python?" you may ask.
It appears that it's the Virtual Env located in `.env` folder hosted by your project directory
```sh
$ ll -a
total 9432
drwxr-xr-x 45 adam.szorcz staff 1,4K 6 mar 10:23 .
drwxr-xr-x 50 adam.szorcz staff 1,6K 6 lut 14:09 ..
-rw-r--r-- 1 adam.szorcz staff 81B 16 wrz 2021 .dockerignore
drwxr-xr-x 8 adam.szorcz staff 256B 11 sie 2021 .env
drwxr-xr-x 15 adam.szorcz staff 480B 10 mar 07:36 .git
drwxr-xr-x 4 adam.szorcz staff 128B 20 sie 2021 .github
-rw-r--r-- 1 adam.szorcz staff 1,3K 28 wrz 2021 .gitignore
drwxr-xr-x 9 adam.szorcz staff 288B 27 lip 2021 .idea
...
```
</script>
</section>
</section>
<section>
<section>
<h2>Let's set a project</h2>
</section>
<section>
But first
<h2>Prerequisites</h2>
VS Code should have Python Extension Pack installed
<img src="img/python_extension_pack.png" alt="">
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
1. Let's setup git repo
```sh
$ git init
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
1. Let's setup git repo
```sh
$ git init
```
2. Let's create conda environment (with this command)
```sh
$ conda create -n prezka-env python=3.9
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
1. Let's setup git repo
2. Let's create conda environment
```sh
$ conda create -n prezka-env python=3.9
```
3. Let's create `environment.yml` file
```sh
$ conda env export > ./environment.yml
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
1. Let's setup git repo
2. Let's create conda environment
3. Let's create `environment.yml` file
We get this:
```yaml
name: prezka-env
channels:
- defaults
dependencies:
- ca-certificates=2023.01.10=hecd8cb5_0
- certifi=2022.12.7=py39hecd8cb5_0
- cffi=1.15.1=py39hc55c11b_0
... And many more ...
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
1. Let's setup git repo
2. Let's create conda environment
3. Let's create `environment.yml` file
We can (but do not have to) remove dependencies of `python` and `pip` to get this:
```yaml
name: prezka-env
channels:
- defaults
dependencies:
- pip=23.0.1=py39hecd8cb5_0
- python=3.9.0=h88f2d9e_2
```
The rest of the packages will be pulled as dependencies to `python` and `pip`
</script>
</section>
</section>
<section>
<section>
<h2>Let's install pre-commits</h2>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
We install pre-commits using the following command:
```sh
conda install pre-commit
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
We install pre-commits using the following command:
```sh
conda install pre-commit
```
It is good to remember to add it to the `environment.yml` file:
```sh
- pre-commit=2.20.0
```
Next developer will have the same version of pre-commit
<small><small>and the same problems</small></small>
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
We install pre-commits using the following command:
```sh
conda install pre-commit
```
It is good to remember to add it to the `environment.yml` file:
```sh
- pre-commit=2.20.0
```
Next developer will have the same version of pre-commit
Finally the verification
```sh
$ pre-commit -V
pre-commit 2.20.0
```
</script>
</section>
</section>
<section>
<section>
<h2>Let's setup pre-commits for repo</h2>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Let's create sample configuration file.
The configuration is stored in `.pre-commit-config.yaml` file.
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Let's create sample configuration file.
The configuration is stored in `.pre-commit-config.yaml` file.
We can create it using this command
```
$ pre-commit smaple-config > .pre-commit-config.yaml
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Let's create sample configuration file.
```yaml
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Now it is enough to connect the configuration file with .git repository.
```sh
$ pre-commit install
```
It will create new file in `.git/hooks` folder.
From now on each change in the configuration file will be *single point of truth* for pre-commits.
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Now it is enough to connect the configuration file with .git repository.
```sh
$ pre-commit install
```
It will create new file in `.git/hooks` folder.
From now on each change in the configuration file will be *single point of truth* for pre-commits.
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
## Short recap
* Pre-Commits - it is a part of Git Hooks concept
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
## Short recap
* Pre-Commits - it is a part of Git Hooks concept
* Pre-Commits are checks/actions run right before `git commit` command
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
## Short recap
* Pre-Commits - it is a part of Git Hooks concept
* Pre-Commits are checks/actions run right before `git commit` command
* For that purpose python virtual env is stored
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
## Short recap
* Pre-Commits - it is a part of Git Hooks concept
* Pre-Commits are checks/actions run right before `git commit` command
* For that purpose python virtual env is stored
* `.pre_commit_config.yaml` is a configuraiton file
</script>
</section>
</section>
<section>
<section>
Paint it <h2>black</h2>
</section>
<section>
<a href="https://github.com/psf/black">BLACK</a> is the uncompromising Python code formatter.
</section>
<section>
To add black to configuration of pre-commits add the following lines to `.pre-commit-config.yaml`:
</section>
<section>
<pre><code class="language-yaml" data-line-numbers="12-17">
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3.9
</code></pre>
</section>
<section>
<h3>
NOTE
</h3>
It hasn't been added to our conda environment yet!!! It is available in virutal env related to
pre-commits only
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
To add black to our environment, let's add this line to environment.yaml file:
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
To add black to our environment, let's add this line to environment.yaml file:
<pre>
<code class="language-yaml" data-line-numbers="9">
name: prezka-env
channels:
- defaults
dependencies:
- pip=23.0.1=py39hecd8cb5_0
- python=3.9.0=h88f2d9e_2
- pre-commit=2.20.0
- black=22.6.0
</code>
</pre>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h3>VS Code and black</h3>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h3>VS Code and black</h3>
Open <b>Settings UI</b> and search for <b>python formatting provider</b>
<img src="img/python_formatting_provider.png" alt="">
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h3>VS Code and black</h3>
Good practice is to format on save:
<img src="img/format_on_save.png" alt="">
</section>
</section>
<section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>Flake8</h2>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>Flake8</h2>
<a href="https://flake8.pycqa.org/en/latest/">Flake8</a> is a popular lint wrapper for python. Under
the hood, it runs three other tools and combines their results: pep8 for checking style. pyflakes
for checking syntax. mccabe for checking complexity.
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h2>Flake8</h2>
How to add flake8 to pre-commits?
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<pre><code class="language-yaml" data-line-numbers="17-22">
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3.9
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
</code></pre>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h3>
NOTE
</h3>
It hasn't been added to our conda environment yet!!! It is available in virutal env related to
pre-commits only
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
It hasn't been added to our conda environment yet!!! It is available in
pre-commits only
<script type="text/template">
To add flake8 to our environment, let's add this line to `environment.yaml` file:
</script>
</section>
<section>
<pre>
<code class="language-yaml" data-line-numbers="10">
name: prezka-env
channels:
- defaults
dependencies:
- pip=23.0.1=py39hecd8cb5_0
- python=3.9.0=h88f2d9e_2
- pre-commit=2.20.0
- black=22.6.0
- flake8=6.0.0
</code>
</pre>
</section>
<section>
To enable flake8 in VS Code it is enough to enable it:
<img src="img/flake8_vscode.png" alt="">
</section>
</section>
<section>
<section data-auto-animate data-auto-animate-unmatched="fade">
Hail
<h2>MyPy</h2>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
Hail
<h2>MyPy</h2>
<a href="https://mypy-lang.org/">Mypy</a> is an optional static type checker for Python
</section>
<section>
How to add mypy to pre-commits?
</section>
<section>
<pre><code class="language-yaml" data-line-numbers="21-27">
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3.9
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.981
hooks:
- id: mypy
exclude: ^tests/
args: [--strict]
</code></pre>
</section>
<section>
You can configure mypy in configuraiton file name mypy.ini located in your project directory:
<pre><code class="language_yaml">
[mypy]
python_version = 3.9
disallow_any_generics = True
disallow_untyped_defs = True
warn_return_any = True
warn_unused_configs = True
strict_optional = True
ignore_missing_imports = True
[mypy-tests.*]
ignore_errors = True
</code></pre>
</section>
<section data-auto-animate data-auto-animate-unmatched="fade">
<h3>
NOTE
</h3>
It hasn't been added to our conda environment yet!!! It is available in virutal env related to
pre-commits only
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
It hasn't been added to our conda environment yet!!! It is available in virutal env related to
pre-commits only
<script type="text/template">
To add mypy to our environment, let's add this line to `environment.yaml` file:
</script>
</section>
<section>
<pre>
<code class="language-yaml" data-line-numbers="11">
name: prezka-env
channels:
- defaults
dependencies:
- pip=23.0.1=py39hecd8cb5_0
- python=3.9.0=h88f2d9e_2
- pre-commit=2.20.0
- black=22.6.0
- flake8=6.0.0
- mypy=0.981
</code>
</pre>
</section>
<section>
To enable mypy in VS Code it is enough to enable it:
<img src="img/mypy_vscode.png" alt="">
</section>
</section>
<section>
<section>
<h2>More about pre-commits</h2>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
Let's move back a bit.
</script>
</section>
<section>
<pre><code class="language-yaml" data-line-numbers="8-11">
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
language_version: python3.9
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
</code></pre>
</section>
<section>
These are hooks available "out of the box"
You can find much more of them <a href="https://pre-commit.com/hooks.html">here</a>
</section>
<section>
As you might have noticed earlier, <b>pre-commits</b> can be used with many languages!!!
Why?
Because
<div>
<ul>
<li>trailing-whitespace</li>
<li>end-of-file-fixer</li>
<li>check-yaml</li>
<li>check-added-large-files</li>
</ul>
</div>
are important in all the programming languages you use
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
What if you want to run your pre-commits before commit?
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
What if you want to run your pre-commits before commit?
```sh
$ pre-commit run --all-files
```
or
```sh
$ pre-commit run --files ./main.py
```
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
When there is a reason why you want to commit without running specified check
</script>
</section>
<section data-markdown data-auto-animate data-auto-animate-unmatched="fade">
<script type="text/template">
When there is a reason why you want to commit without running specified check
```sh
$ SKIP=flake8 git commit -m "foo"
```
</script>
</section>
<section>
If you want to become git hooks ninja go to
<a href="https://pre-commit.com/index.html#advanced">Advanced Features</a>
</section>
</section>
<section>
<section>
<h2>Examples of configuration</h2>
</section>
<section>
You can change max line length for flake8 and for black:
<pre><code class="language-yaml" data-line-numbers="6">
- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
args: [--max-line-length=131]
</code></pre>
</section>
<section>
You can store mypy's result into a file:
<pre><code class="language-yaml" data-line-numbers="6">
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.812
hooks:
- id: mypy
args: [--junit-xml, test-reports/junit-mypy.xml, --ignore-missing-imports]
</code></pre>
</section>
<section>
Let's not forget about mypy's own configuration :)
<pre><code class="language_yaml">
[mypy]
python_version = 3.9
disallow_any_generics = True
disallow_untyped_defs = True
warn_return_any = True
warn_unused_configs = True
strict_optional = True
ignore_missing_imports = True
[mypy-tests.*]
ignore_errors = True
</code></pre>
</section>
</section>
<section>
<h2>Pysznej kawusi!</h2>
<h3>Udanego weekendu!</h3>
<h4>życzy</h4>
Adam
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/zoom/zoom.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/search/search.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// Also available as an ES module, see:
// https://revealjs.com/initialization/
Reveal.initialize({
controls: true,
progress: true,
center: true,
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [RevealZoom, RevealNotes, RevealSearch, RevealMarkdown, RevealHighlight]
});
</script>
</body>
</html>