frida 挂钩_您必须知道的预提交挂钩

frida 挂钩

pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface. The Python package has a plugin-system to create git pre-commit hooks automatically. It’s not only for Python projects but for any project.

预提交挂钩是版本控制系统git的一种机制。 它们使您可以在提交之前立即执行代码。 令人困惑的是,还有一个名为pre-commit的Python包,它允许您使用更简单的界面来创建和使用pre-commit挂钩。 Python软件包具有一个插件系统,可自动创建git pre-commit挂钩。 它不仅适用于Python项目,而且适用于任何项目。

After reading this article, you will know my favorite plugins for professional software development. Let’s get started!

阅读本文之后,您将了解我最喜欢的专业软件开发插件。 让我们开始吧!

提交前的基础知识 (pre-commit basics)

Install pre-commit via

通过安装预提交

pip install pre-commit

Create a .pre-commit-config.yaml file within your project. This file contains the pre-commit hooks you want to run every time before you commit. It looks like this:

在您的项目中创建一个.pre-commit-config.yaml文件。 该文件包含您要在提交之前每次运行的提交前挂钩。 看起来像这样:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: mixed-line-ending- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black

pre-commit will look in those two repositories with the specified git tags for a file called .pre-commit-hooks.yaml. Within that file can be arbitrary many hooks defined. They all need an id so that you can choose which ones you want to use. The above git-commit config would use 3 hooks.

pre-commit将在这两个具有指定git标签的存储库中查找名为.pre-commit-hooks.yaml的文件。 在该文件中可以任意定义许多钩子。 它们都需要一个id以便您可以选择要使用的id 。 上面的git-commit配置将使用3个钩子。

Finally, you need to run pre-commit install to tell pre-commit to always run for this repository.

最后,您需要运行pre-commit install来告诉pre-commit始终为此存储库运行。

Before I used it, I was worried about losing control. I want to know exactly which changes I commit. pre-commit will abort the commit if it changes anything. So you can still have a look at the code and check if the changes are reasonable. You can also choose not to run pre-commit by

在使用它之前,我担心会失去控制。 我想确切知道我所做的更改。 如果进行任何更改, pre-commit将中止提交。 因此,您仍然可以查看代码并检查更改是否合理。 您还可以选择不通过以下方式运行预提交

git commit --no-verify
Image for post
geek-and-poke under CC-BY-3.0 怪胎

文件格式 (File formatting)

Formatting files in a similar way helps readability by improving consistency and keeps git commits clean. For example, you usually don’t want trailing spaces. You want the text files to end with exactly one newline character so that some of the Linux command-line tools behave well. You want consistent newline characters between Linux ( \n ), Mac ( \rMac changed to \n 🎉) and windows ( \r\n ). My configuration for that is

以类似的方式格式化文件可以通过提高一致性来提高可读性,并保持git commit整洁。 例如,您通常不希望尾随空格。 您希望文本文件仅以一个换行符结尾,以便某些Linux命令行工具运行良好。 您希望Linux( \n ),Mac( \r - Mac更改\n n🎉)和Windows( \r\n )之间具有一致的换行符。 我的配置是

# pre-commit run --all-files
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-byte-order-marker # Forbid UTF-8 byte-order markers
# Check for files with names that would conflict on a case-insensitive
# filesystem like MacOS HFS+ or Windows FAT.
- id: check-case-conflict
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: mixed-line-ending
Image for post
imgflip.com by Martin Thoma imgflip.com创建

代码风格 (Code style)

We can write code in a lot of different ways. Many of them show almost no difference in runtime, but there are differences in readability.

我们可以用许多不同的方式编写代码。 它们中的许多在运行时上几乎没有差异,但是可读性上却有差异。

代码自动格式化 (Code Autoformatter)

Image for post
XKCD) XKCD )

Automatic code formatting has the same advantages as the file formatting. Additionally, it prevents meaningless discussions. Thus it lets you and your team focus on the important and complicated parts.

自动代码格式化与文件格式化具有相同的优势。 此外,它可以防止无意义的讨论。 因此,它使您和您的团队可以专注于重要和复杂的部分。

I love Pythons autoformatter black and mentioned it already in the article about static code analysis:

我喜欢Pythons autoformatter black,并且在有关静态代码分析的文章中已经提到过:

-   repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/asottile/blacken-docs
rev: v1.8.0
hooks:
- id: blacken-docs
additional_dependencies: [black==20.8b1]

The first one is black itself, the second one is a project which applies the black formatting to code-strings within docstrings.

第一个是黑色本身,第二个是一个将黑色格式应用于文档字符串中的代码字符串的项目。

Additionally, I want my imports to be sorted:

此外,我希望对导入进行排序:

-   repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.4.2
hooks:
- id: isort

There are autoformatters with pre-commit hooks for many languages:

有些自动格式化程序带有针对多种语言的预提交挂钩:

  • Prettier: HTML, CSS, JavaScript, GraphQL, and many more.

    更漂亮 :HTML,CSS,JavaScript,GraphQL等。

  • Clang-format: C, C++, Java, JavaScript, Objective-C, Protobuf, C#

    Clang格式 :C,C ++,Java,JavaScript,Objective-C,Protobuf,C#

  • Rustfmt: Rust

    Rustfmt :锈

现代Python (Modern Python)

pyupgrade runs over your Python code and automatically changes old-style syntax to new-style syntax. Just have a look at some examples:

pyupgrade运行您的Python代码,并自动将旧式语法更改为新式语法。 看看一些例子:

dict([(a, b) for a, b in y])  # -> {a: b for a, b in y}
class C(object): pass # -> class C: pass
from mock import patch # -> from unittest.mock import patch

Do you want it? Here you are:

你要吗? 这个给你:

-   repo: https://github.com/asottile/pyupgrade
rev: v2.7.2
hooks:
- id: pyupgrade
args: [--py36-plus]

测试您的代码 (Testing your Code)

I thought about running the unit tests automatically by pre-commit. I decided not to do that as this might take quite a while. However, there are some quick tests which are good to run automatically and every time:

我考虑过通过预提交自动运行单元测试。 我决定不这样做,因为这可能需要一段时间。 但是,有一些快速测试可以很好地自动运行并每次运行:

-   repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-ast # Is it valid Python?
# Check for debugger imports and py37+ breakpoint() calls
# in python source.
- id: debug-statements- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
hooks:
- id: mypy
args: [--ignore-missing-imports]- repo: https://gitlab.com/pycqa/flake8
rev: '3.8.3'
hooks:
- id: flake8

安全 (Security)

Checking in credentials is a pretty common mistake. Here is how you prevent it:

签入凭据是一个很常见的错误。 预防方法如下:

-   repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: detect-aws-credentials
- id: detect-private-key

杂项预提交挂钩 (Miscellaneous pre-commit hooks)

Some hooks don’t fit in the above categories but are still useful. For example, this one prevents big files from being committed:

有些钩子不属于上述类别,但仍然很有用。 例如,这可以防止提交大文件:

-   repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-added-large-files

团队合作 (Working in a Team)

The pre-commit hooks are installed locally and thus every developer could decide on their own if they want pre-commit hooks and which ones. However, I think it is helpful to provide a .pre-commit-config.yaml with plugins you recommend to execute.

预提交钩子是在本地安装的,因此每个开发人员都可以自行决定是否需要预提交钩子以及哪个。 但是,我认为为.pre-commit-config.yaml提供您建议执行的插件会有所帮助。

所有的钩子! (All the hooks!)

If you’re looking for a complete .pre-commit-config.yaml ready to use, here it is:

如果您正在寻找准备使用的完整.pre-commit-config.yaml ,则为:

# Apply to all files without commiting:
# pre-commit run --all-files
# Update this file:
# pre-commit autoupdate
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-ast
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
- id: check-yaml
- id: debug-statements
- id: detect-aws-credentials
- id: detect-private-key
- id: end-of-file-fixer
- id: trailing-whitespace
- id: mixed-line-ending
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.782
hooks:
- id: mypy
args: [--ignore-missing-imports]
- repo: https://github.com/asottile/seed-isort-config
rev: v2.2.0
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.4.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.2
hooks:
- id: pyupgrade
args: [--py36-plus]
- repo: https://github.com/asottile/blacken-docs
rev: v1.8.0
hooks:
- id: blacken-docs
additional_dependencies: [black==20.8b1]

摘要 (Summary)

I love pre-commit as it fits so well in my workflow. I just commit as usual and pre-commit does all the checks which I sometimes forget. It speeds up development because the CI/CD pipeline is just way slower than executing the same steps locally. Especially for linting, it’s an enormous time-saver to quickly run black over the code instead of committing, waiting for the CI/CD pipeline, finding an error, fixing that error locally, pushing, and waiting again for the CI/CD pipeline.

我喜欢预先提交,因为它非常适合我的工作流程。 我只是像往常一样提交,并且预先提交会执行所有我有时忘记的检查。 它加快了开发速度,因为CI / CD管道比本地执行相同的步骤要慢得多。 尤其是对于lint,要在代码上快速运行而不是提交,等待CI / CD管道,查找错误,在本地修复该错误,推送并再次等待CI / CD管道,这是一个巨大的时间节省。

Please let me know as a comment or email (info@martin-thoma.de) if there are other pre-commit hooks you like!

如果还有其他您喜欢的预提交钩子,请作为评论或电子邮件(info@martin-thoma.de)告诉我!

翻译自: https://towardsdatascience.com/pre-commit-hooks-you-must-know-ff247f5feb7e

frida 挂钩

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值