12个版本 (4个稳定版)
1.0.3 | 2024年2月12日 |
---|---|
1.0.2 | 2023年5月24日 |
1.0.0 | 2023年4月5日 |
0.2.4 | 2023年1月21日 |
0.1.2 | 2022年8月17日 |
#284 in 开发工具
每月下载 54 次
31KB
424 行
将git仓库信息总结到shell变量中
此工具旨在用单个命令 eval $(git-status-vars)
替换对 git
的多次调用。它特别适用于生成shell提示符。
此工具旨在在支持使用 sh
-like 字符串在任何shell中报告git信息的任何主题中通用。我在我的 个人ZSH主题 中使用它。
安装
您可以从 GitHub发布页面 下载二进制文件。只需解压缩并将文件内的文件复制到您的 $PATH
,例如 /usr/local/bin
。最常见的是
- Linux: x86-64, ARM
- macOS: Intel, Apple硅芯片
- x86-64 Windows
如果您有 cargo
,您可以直接执行 cargo install git-status-vars
从源安装,或者如果您已安装 cargo binstall
,则可以使用它 (cargo binstall git-status-vars
)。
用法
eval $(git-status-vars 2>/dev/null)
if [[ $repo_state == "NotFound" ]] ; then
return 0
fi
此命令输出关于当前仓库的多个与 sh
兼容的环境变量。通过按顺序检查以下内容,找到第一个匹配的仓库。
- 命令行参数。仓库目录或子目录可以传递到命令行中。
- 环境变量
$GIT_DIR
,就像git
一样。 - 工作目录中的
.git
目录或其父目录之一。
git-status-vars
总是输出 repo_state=
,但可以省略所有其他变量。特别是,如果找不到仓库,它将只输出 repo_state=NotFound
。
使用 git-status-vars
的示例提示函数
git_prompt () {
eval $(git-status-vars 2>/dev/null)
if [[ $repo_state == "NotFound" ]] ; then
return 0
fi
local fg_color=green
if (( $untracked_count > 0 )) ; then
fg_color=red
fi
local ref=$head_ref1_short
if [[ -z $ref ]] ; then
ref=${head_hash:0:8}
fi
print -Pn "%F{$fg_color}${ref}%f "
}
不使用 git-status-vars
的等效提示函数
git_prompt () {
setopt local_options pipefail
local untracked_count fg_color=green
untracked_count=$(git ls-files --other --exclude-standard 2>/dev/null | wc -l)
if (( $? != 0 )) ; then
# No repository
return 0
fi
local fg_color=green
if (( $untracked_count > 0 )) ; then
fg_color=red
fi
# Try for the branch or tag name, then try for the commit hash
ref=$(git symbolic-ref --short HEAD 2>/dev/null) \
|| ref="$(git show-ref --head --hash --abbrev HEAD 2>/dev/null | head -n1)"
print -Pn "%F{$fg_color}${ref}%f "
}
典型输出
~/projects/git-status-vars ❯ git-status-vars
repo_state=Clean
repo_workdir=/Users/daniel/projects/git-status-vars/
repo_empty=false
repo_bare=false
head_ref_length=1
head_ref1_name=refs/heads/main
head_ref1_short=main
head_ref1_kind=direct
head_ref1_error=''
head_hash=2df6b768e60fbf899d8c8dc4a20385f30ee5da24
head_ahead=0
head_behind=0
head_upstream_error=''
untracked_count=0
unstaged_count=0
staged_count=0
conflicted_count=0
~/projects/git-status-vars ❯ cd /
/ ❯ git-status-vars
repo_state=NotFound
性能
git-status-vars
通常比多次调用 git
更快,尽管 git
已经足够快,以至于差异通常不易察觉。在我的笔记本电脑上,git-status-vars
通常运行大约 8 毫秒,而涉及多次调用 git
的回退代码大约需要 25 毫秒。
我尚未在大仓库上测试此功能。
Rust 包
我不确定它有多有用,但可能可以从其他 Rust 代码中使用它。
目前支持的最小 Rust 版本(MSRV)是 1.64。
开发和贡献
有关向此仓库贡献、许可证、更改跟踪以及发布方式的信息,请参阅 DEVELOPMENT.md。
开发状态
这是一个稳定版本。我没有添加额外功能的计划,但如果你有想法,请提交一个 问题 或一个 拉取请求。
我将定期更新以确保它不会随着依赖项的更新而变得过时,但你不应期望有活跃的开发。
依赖项
~11MB
~271K SLoC