쉐도잉 연습: The easy way to keep your repos tidy. - 영상으로 영어 말하기 배우기
로딩 중...
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.
이 레슨에 대해
"The easy way to keep your repos tidy."으로 쉐도잉 기법을 사용해 영어를 연습합니다.
매일 15~30분 꾸준히 연습하면 IELTS 스피킹에 대한 자신감이 길러집니다.
쉐도잉이란? 영어 실력을 빠르게 키우는 과학적 방법
쉐도잉(Shadowing)은 원래 전문 통역사 훈련을 위해 개발된 언어 학습 기법으로, 다언어 학자인 Dr. Alexander Arguelles에 의해 대중화된 방법입니다. 핵심 원리는 간단하지만 매우 강력합니다: 원어민의 영어를 들으면서 1~2초의 짧은 지연으로 즉시 소리 내어 따라 말하는 것——마치 '그림자(shadow)'처럼 화자를 따라가는 것입니다. 문법 공부나 수동적인 청취와 달리, 쉐도잉은 뇌와 입 근육이 동시에 실시간으로 영어를 처리하고 재현하도록 훈련합니다. 연구에 따르면 이 방법은 발음 정확도, 억양, 리듬, 연음, 청취력, 말하기 유창성을 크게 향상시킵니다. IELTS 스피킹 준비와 자연스러운 영어 소통을 원하는 분들에게 특히 효과적입니다.