#git-history #git #history #clean #git-commit #linting #lint

app clean_git_history

一个 Git 历史代码检查工具,确保其保持清洁,对于那些相比于合并和压缩提交更喜欢变基和快速前进的用户。

3 个版本

0.1.2 2021年10月22日
0.1.1 2021年8月26日
0.1.0 2021年8月10日

#1972开发工具

AGPL-3.0

37KB
487 代码行

Clean Git History

crates.io pipeline status Conventional Commits License: AGPL v3

一个 Git 历史代码检查工具,确保其保持清洁,对于那些相比于合并和压缩提交更喜欢变基和快速前进的用户。

内容

用法

Clean History 在仓库历史中操作一系列 Git 提交。要指定提交范围,可以使用 --from-commit-hash <commit-hash>--from-reference <reference> 参数。提交范围从指定的提交开始,直到包括 HEAD

唯一必需的参数是 --from-commit-hash <commit-hash>--from-reference <reference>

用法 - 额外参数

可以通过额外的命令行标志来修改代码检查的内容和方式。

标志
--ignore-merge-commits 如果启用了此标志,则忽略任何 Git 合并提交;否则,合并提交的存在将导致代码检查失败。

用法 - Git 环境变量

在查找仓库时,会尊重 Git 环境变量。当设置 $GIT_DIR 时,它具有优先权,Clean History 将开始在 $GIT_DIR 指定的目录中查找仓库。当未设置 $GIT_DIR 时,Clean History 将从当前目录开始查找仓库。

用法 - 记录

使用crate pretty_env_loggerlog 来提供日志功能。可以通过环境变量 RUST_LOG 来设置日志级别。更多详细文档请见 https://crates.io/crates/pretty_env_logger

CICD 示例

GitLab CI Rust 项目示例

通过 Cargo

有关通过Cargo安装的更多详细信息,请参阅 通过Cargo编译

注意 - 此示例下载了最新版本的 0.*

clean-git-history-checking:
  stage: clean-git-history-checking
  image: rust
  before_script:
    - cargo install clean_git_history --version ^0
  script:
    # Check all the commits in the branch.
    - /usr/local/cargo/bin/clean_git_history --from-reference "origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
  rules:
    - if: $CI_MERGE_REQUEST_ID

通过二进制下载

有关二进制下载的更多详细信息,请参阅 下载二进制

注意 - 此示例下载了版本 0.1.1

clean-git-history-checking:
  stage: clean-git-history-checking
  image: rust
  before_script:
    - wget -q -O tmp.zip "https://gitlab.com/DeveloperC/clean_git_history/-/jobs/artifacts/0.1.1/download?job=release-binary-compiling-x86_64-linux-musl" && unzip tmp.zip && rm tmp.zip
  script:
    # Check all the commits in the branch.
    - ./clean_git_history --from-reference "origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
  rules:
    - if: $CI_MERGE_REQUEST_ID

Git Hooks Rust 项目示例

一个示例 pre-push Git钩子,用于在将项目推送到远程服务器之前检查项目历史记录是否清洁。

#!/usr/bin/env bash

set -o errexit
set -o pipefail

root_commit_hash=$(git rev-list --max-parents=0 HEAD)
"/home/${USER}/.cargo/bin/clean_git_history"  --from-commit-hash "$root_commit_hash"

下载二进制文件

提供静态链接编译的二进制文件供下载。访问https://gitlab.com/DeveloperC/clean_git_history/-/releases页面查看所有版本,发布说明中包含各种架构的二进制下载链接。

如果不信任提供的二进制文件,可以选择自己编译,然后将其提供给远程下载,以便CICD等可以下载。

通过本地仓库编译

在本地检出代码仓库,切换到仓库目录,然后通过Cargo构建。使用 --release 标志可以生成优化的二进制文件,但编译时间会更长。

git clone [email protected]:DeveloperC/clean_git_history.git
cd clean_git_history/
cargo build --release

编译的二进制文件位于 target/release/clean_git_history

通过 Cargo 编译

Cargo是Rust包管理器,install 子命令从crates.io 拉取,然后本地编译二进制文件,并将编译的二进制文件放置在 $HOME/.cargo/bin/clean_git_history

cargo install clean_git_history

默认情况下,它会在执行时安装最新版本。您可以使用 --version 参数指定要安装的特定版本。对于CICD等特定环境,您可能需要锁定版本。

例如:

cargo install clean_git_history --version 0.1.1

您可以选择指定主版本或次版本,而不是锁定到特定版本。

例如:

cargo install clean_git_history --version ^0

将下载最新的 0.* 版本,无论是 0.2.2 还是 0.7.0

单元测试

单元测试套件有多个参数化测试,使用Cargo来设置和运行所有单元测试。

cargo test

问题/功能请求

要报告问题或请求新功能,请使用 https://gitlab.com/DeveloperC/clean_git_history/-/issues

依赖项

~13–22MB
~382K SLoC