Prática de Shadowing: The easy way to keep your repos tidy. - Aprenda a falar inglês com vídeo

Carregando...
1
Hello and welcome.
2
I'm James Murphy and today we're talking about PreCommit, which is a really cool tool that you can use to keep your codebase tidy
3
and catch some simple mistakes before things ever get committed into Git.
4
It happens.
5
There's a new person on your team that doesn't really know about PEP8, or you just forget to run your code formatter, or you just make a typo in a config file somewhere.
6
But at the time, you don't really notice, so you just go ahead and git commit.
7
Then either something breaks, or just the quality of your codebase has gone down just a little bit.
8
Well, a bad config you can fix, and code you can just reformat.
9
But still, wouldn't it be better if you could just catch those errors before the commit happens?
10
That's what pre-commit is for.
11
Here's the story of pre-commit.
12
Basically, git has a notion of what are called commit hooks.
13
These are things that happen either before you commit or just after you commit or when you push or when you pull, things like that.
14
Git will store this and a whole bunch of other information in the .git folder inside a directory.
15
Depending on your editor, this folder might be hidden, so just look out for that.
16
You might have to unhide it.
17
This hooks folder is where git stores all these different hooks.
18
The ones you see here are just samples.
19
If you were to get rid of that .sample on the end of the file name, then git would actually run that action at that corresponding So if I have a file named pre-commit,
20
then Git will run that script right before a commit happens.
21
But you don't need to go out and waste your time writing something like this yourself.
22
A lot of really good scripts have already been written.
23
The pre-commit project just makes it really easy to pick
24
and choose things that you want to check for your own code base.
25
Let's start simple.
26
All I want to do is make sure that before I commit anything, I run my code formatter on it.
27
Of course, I can just run the code formatter before I commit, but the point is I shouldn't have to remember to do that.
28
Why not just make it automatic?
29
Well, it's super simple to set up.
30
Open up your terminal and just pip install pre-commit.
31
Then, at the root of your project, just make a new file called .precommitconfig.yaml.
32
Then, it's just this easy to add automatic code formatting on every commit.
33
You specify precommit hooks by just giving a git repository.
34
In this case, I'm using GitHub, but you can actually use any git repository, even a local one.
35
You specify the git tag that it's going to use, and then some information about what command to run and what arguments to give it.
36
In this case, I'll be using black to do the actual code formatting.
37
Then you use pre-commit to install the githooks into your githooks folder.
38
Now if we go and look in our githooks folder, we see that we have this new pre-commit file.
39
Alright, now let's add our files to git, commit, and see what happens.
40
As you can see, it ran black and the commit failed because some modifications were made, and it's not going to commit anything automatically.
41
I can go and manually inspect the hello file and see that everything looks good, or just compare the diff like I normally would before a commit.
42
Note that these modifications to the hello file were made after the file was staged.
43
That means I need to git add this file again, or if something went wrong and I don't like the changes that were automatically made,
44
then I can git restore the file to go back to the way it was before.
45
In this case, I'm happy with the changes, so I will add and then try to commit again.
46
This time, since everything passed, the commit went through.
47
Note that some graphical editors like this one, PyCharm, have built-in Git support and they will automatically add tracked files when you try to commit.
48
So if that's the case then it might look more like this.
49
Here I have a spurious space in there and I'm going to go ahead and try and commit.
50
Then I try to commit and I see the commit failed and I can go to the console to see what errors.
51
I see that it deleted the extra space
52
and then I can just go ahead and click commit again and then this time it will succeed.
53
So what else can you do besides automatically formatting your code?
54
Well you can write your own commit hooks so you can pretty much do whatever you want, but there are a ton of really good ones already written on this website here.
55
Here are just a few of my favorites.
56
You can delete trailing whitespace, make sure a file is either empty or ends with a new line, make sure that any yaml files have valid yaml in them,
57
make sure you're not accidentally committing any really big files into your repository, and check for any spurious breakpoints that you forgot to delete before committing.
58
You can also run your linter like flake 8, you can reorder and standardize the order that your imports happen in, you can check for outdated syntax and replace it with its modern equivalent,
59
and this would also be a good place to run your type checker, like MyPy.
60
Pre-commit is most popular in the Python world, but it's not something that's just limited to Python.
61
There are premade scripts for doing things like running clang-tidy or clang-format, or some other code formatter for a different language.
62
You don't have to reinstall anything if you change the YAML file, just go ahead and commit.
63
We see the commit failed because of a MyPy error, telling us that we passed the wrong kind of argument to this function f.
64
Also note that most hooks will only run if an applicable file actually changed.
65
So it's not going to be reformatting code that it already formatted and hasn't changed.
66
Finally, there's the question of safety.
67
How safe is it to automatically run programs on your source code before committing them?
68
Well, this ultimately goes down to the project and whether or not you trust the project to do the right thing.
69
Of course, these little scripts can be written by anyone, so if they put something malicious in, then you're going to be out of luck.
70
You can fix a specific version or even a specific hash in the config file.
71
This helps mitigate things like what if this project gets hacked and an attacker uploads something malicious.
72
But as always, only use projects that you really trust.
73
As far as accidental errors go though, like what if black accidentally changed the meaning of the code?
74
Well, that's something that I haven't actually run into.
75
If something did go wrong, it would really be up to you to catch it in the git diff or by running tests.
76
Of course, for an accidental mistake, you can always revert using the git restore command that I talked about earlier.
77
Well, that's all I've got.
78
I hope this makes your repos better.

Sobre esta lição

Você está praticando inglês com "The easy way to keep your repos tidy." usando a técnica de Shadowing.

O que é a Técnica de Shadowing?

Shadowing é uma técnica de aprendizado de idiomas com base científica, originalmente desenvolvida para o treinamento de intérpretes profissionais. O método é simples, mas poderoso: você ouve áudio em inglês nativo e repete imediatamente em voz alta — como uma sombra seguindo o falante com 1-2 segundos de atraso. Pesquisas mostram melhora significativa na precisão da pronúncia, entonação, ritmo, sons conectados, compreensão auditiva e fluência na fala.