pre-commit 在python项目中新建hooks

hooks的新建分为两种情况:远程和本地

一、新建的hooks放在git上,使用时远程下载安装使用参考:https://github.com/pre-commit/pre-commit

1、创建一个文件夹,将你的.py文件放进去

 

 2、创建setup.py、setup.cfg文件,配置setup.cfg文件

[options]
packages = find:
python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*

[options.entry_points]
console_scripts =
    check-commit-message = pre_commit_hooks.check_commit_message:main
    check-case-conflict = pre_commit_hooks.check_case_conflict:main

3、创建.pre-commit-hooks.yaml文件,并定义相关内容

复制代码
-   id: check-case-conflict
    name: Check for case conflicts
    description: Check for files that would conflict in case-insensitive filesystems
    entry: check-case-conflict
    language: python
-   id: check-commit-message
    name: Check commit message
    description: Check commit message.
    entry: check-commit-message
    language: python
    types: [python]
复制代码

4、使用:在.pre-commit-config.yaml

default_stages: [commit]
repos:
  - repo: https://github.com/yingzi113/pre-commit-hooks
    rev: 5863e162f1bed1f63eeb716e77d622ff8e3d9af9
    hooks:
    - id: check-case-conflict
    - id: check-commit-message

注:要想拿到rev后面的代码,执行

 pre-commit autoupdate

二、本地创建hooks

第一种里创建的文件和配置都需要,但同第一种有两处不同

1、需要安装setup.py

 python setup.py build

 python setup.py install

2、使用时reop为local,不需要rev

- repo: local
    hooks:
    - id: check-commit-message
      name: Check commit message
      description: This is deprecated, use pre-commit/mirrors-autopep8 instead.
      entry: check-commit-message
      language: system
      types: [python]

 

posted @   yingzi__block  阅读(723)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示