자신의 사전 커밋 후크 만들기
pre-commit
에 의해 만들어진 멋진 패키지Anthony Sottile를 사용하여 아주 기본적인 git hook을 만들 것입니다 👏👏.후크는 매우 기본적입니다. 단위 테스트가 없거나 실제 프로젝트에서 매우 기능적이지 않을 것입니다. 놀라운 것을 만드는 데 필요한 첫 번째 단계를 보여 주지만.
I recommend you using a virtual environment to follow this tutorial.
튜토리얼 색인
1. 프로젝트 구조 만들기
Please create the following structure:
.
├── test-the-hook-in-this-folder
└── the-hook
├── print_arguments
│ ├── __init__.py
│ └── main.py
├── setup.cfg
└── setup.py
-
test-the-hook-in-this-folder
: Folder in which we will test our hook by the end of the tutorial (we won't use it until step 5). -
the-hook
: Folder that contains all the files required for the package. -
print_arguments
: This is our python package (and also a spoiler of what our hook will do). -
__init__.py
: Turn the folder that contains it into a package. -
main.py
: Here is where the logic of the hook will be. -
setup.py
: Necessary for building our package. -
setup.cfg
: Describe our package.
2. 기능 생성
I will work in the
the-hook
folder for steps 2 to 4.
For creating the functionality, we only need to edit the main.py
file:
# print_arguments/main.py
import argparse
def print_arguments(arguments: list[str]):
for argument in arguments:
print(argument)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("filenames", nargs="*")
args = parser.parse_args()
print_arguments(args.filenames)
if __name__ == "__main__":
main()
Yes, our hook will print the arguments we pass to it. We can test its functionality:
python print_arguments/main.py arg1 arg2 arg3
Output:
arg1
arg2
arg3
3. 파이썬 패키지로 변환
To do this, we need to edit our 2 setup files.
setup.py:
# setup.py
from setuptools import setup
setup()
setup.cfg:
# setup.cfg
[metadata]
name = print-arguments
description = print the arguments you pass
version = 0.1.0
author = Jorge Alvarado
author_email = [email protected]
license = MIT
url = https://jorgealvarado.me
[options]
packages = find:
[options.entry_points]
console_scripts =
print-arguments = print_arguments.main:main
Important points:
-
packages = find:
: It help us find ourprint_arguments
package when packaging. -
console_scripts
: This is our entry point, with this we kind of expose our function to the world. The name of our single entry point, in this caseprint-arguments
, will be used bypre-commit
.
You can choose other values for the rest of the points.
Install the package with:
pip install .
Make sure you are in
the-hook
folder when running that command.
4. 후크로 돌립니다.
For our hook to work we need our project to be a git repository. So let's do that by running the following commands (make sure you are in the the-hook
folder):
Initialize the git repository:
git init
Add every file and commit them:
git add .; git commit -m "Create the package"
Now the hook part. First let's install pre-commit
:
pip install pre-commit
Create a .pre-commit-hooks.yaml
file inside the the-hook
folder and edit it:
# .pre-commit-hooks.yaml
- id: some-id
name: some-name
description: some description
entry: print-arguments
language: python
-
id
: The id of our hook. We will need it for testing and in the future if someone wants to use our hook, the id will be required. -
name
: The name of the hook, it is what is shown during hook execution. -
description
(optional): Description of the hook. -
entry
: The entry point, the executable to run. It has to match with the entry point name defined inside oursetup.cfg
. -
language
: The language of the hook, it tellspre-commit
how to install the hook.
👀 See a complete list of the options.
이 단계를 완료하려면 후크 항목을 커밋해야 합니다.
git add .; git commit -m "Create a hook"
5. 테스트
Time to use the test-the-hook-in-this-folder
folder. Change directory to that folder, once there, let's create a very little git project:
Create some files
touch a.py b.py
Initialize the git repository:
git init
Commit the files:
git add .; git commit -m "Create the project"
Finally, let's test our hook:
pre-commit try-repo ../the-hook some-id --verbose --all-files
Output:
some-name................................................................Passed
- hook id: some-id
- duration: 0.08s
a.py
b.py
Notice:
- We got
some-name
in the output, because that's what we defined in our.pre-commit-hooks.yaml
file. - We used
some-id
(defined in the.pre-commit-hooks.yaml
file as well) in the command to refer to our hook (we could have multiple hooks). -
a.py
andb.py
were printed. That's because we used the flag--all-files
in our command (If we didn't, the hook execution would have been skipped, because there are no new/edited files for git).
Remember that you need to be in the
test-the-hook-in-this-folder
folder for the command just used to work.
If we now add a new file to our test folder:
touch c.py
We track it with git:
git add c.py
And then we run the command without the --all-files
flag:
pre-commit try-repo ../the-hook some-id --verbose
We get:
some-name................................................................Passed
- hook id: some-id
- duration: 0.07s
c.py
As you can see, only c.py
was printed this time, because is the only new/edited file for git, so our hook was run only against that file. This is the behavior you will normally want.
Now you know how to create a pre-commit hook 🪝! Go and make cool things with this knowledge 😁.
Let me know if you create a hook or something. This is one 몇 주 전에 만들었습니다.Reference
이 문제에 관하여(자신의 사전 커밋 후크 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/jalvaradosegura/create-your-own-pre-commit-hook-3kh텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)