irasin / code_template

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

code_template

a template repo for c++/python/doc by irasin

dependency

pre-commit的使用

  1. 通过python的pip安装
pip install pre-commit
pip install clang-format ## 安装clang-format,因为我通常是会用到c++开发,可以用clang-format来格式化代码
pip install cpplint ## 安装cpplint,因为我通常是会用到c++开发,可以用cpplint来检查代码风格
  1. 在repo根目录安装pre-commit的hook
pre-commit install

此时在.git/hooks下会生成一个pre-commit文件,可以看到其内容是调用.pre-commit-config.yaml中的配置

#!/bin/sh
#!/usr/bin/env bash
# File generated by pre-commit: https://pre-commit.com
# ID: 138fd403232d2ddd5efb44317e38bf03

# start templated
INSTALL_PYTHON='C:\Users\edad8\anaconda3\python.exe'
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated

HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")

if [ -x "$INSTALL_PYTHON" ]; then
    exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
    exec pre-commit "${ARGS[@]}"
else
    echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
    exit 1
fi
  1. 在repo根目录新建.pre-commit-config.yaml,并写入所需配置,可参考官网

顶层有一个参数名为repos,repos中每一个元素为repo,代表一个代码库,可以是远程的git链接,也可以是代表当前库,代表当前库时用local表示。每一个repo中有一个或者多个hook,每个hook代表一个任务。

# 该config文件为该项目的pre-commit的配置文件,用于指定该项目可以执行的git hooks

# 这是pre-commit的全局配置之一,false表示不会遇到第一个错误就停止
fail_fast: false

repos:
# hook所在的仓库
- repo: https://github.com/pre-commit/pre-commit-hooks
  # 仓库的版本,可以直接用tag或者分支,但分支是容易发生变化的
  # 如果使用分支,则会在第一次安装之后不自动更新
  # 通过 `pre-commit autoupdate`指令可以将tag更新到默认分支的最新tag
  rev: v4.0.1
  # 仓库中的hook id
  hooks:
  # 定义的hook脚本,在repo的.pre-commit-hooks.yaml中定义
  - id: check-added-large-files
  # 移除尾部空格符
  - id: trailing-whitespace
    # 传入参数,不处理makedown
    args: [--markdown-linebreak-ext=md]
  # 检查是否含有合并冲突符号
  - id: check-merge-conflict
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
  rev: v2.0.0
  hooks:
  - id: pretty-format-yaml
    # https://github.com/macisamuele/language-formatters-pre-commit-hooks/blob/v2.0.0/language_formatters_pre_commit_hooks/pretty_format_yaml.py
    # hook脚本需要的参数,可以在该hook脚本文件中看到
    args: [--autofix, --indent, '2']

关于local的使用

如果自己定义一些脚本来执行hook的化,repo的值需定义为local,并且在.pre-commit-config.yaml中定义hook的脚本路径

-   repo: local
    -   id: hook_id
        name: hook_name
        description: 'hook description'
        # 脚本路径
        entry: bash /path/to/hook.sh # 执行的脚本,这里可以用shell写,也可以用python
        language: system
        files: \.(py|c|cc|cxx|cpp|cl|cu|h|hpp|hxx)$ ## 可以指定文件,也可以指定文件夹
        exclude: ^(ci/*|doc/*/third_parth/*/tools/*|.clang-format)
        pass_finenames: fasle

About


Languages

Language:C++ 51.1%Language:Python 48.9%