如何使“git描述”提及是否存在局部变化?

发布于 2024-08-16 07:27:11 字数 56 浏览 9 评论 0原文

如何在脚本中检查本地更改是否存在?也许与gitdescribe结合使用?

How can I check, in a script, if local changes are present? Perhaps in combination with git describe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄凉少年不暖心 2024-08-23 07:27:11

从 git 1.6.6 开始,git-describe 接受了 --dirty 选项。如果您的工作树中有未提交的更改,那么您将得到如下输出:

$ git describe --dirty
1.0.2-2-g215081f-dirty

Since git 1.6.6, git-describe has accepted a --dirty option. If you have uncommitted changes in your working tree then you'll get output like this:

$ git describe --dirty
1.0.2-2-g215081f-dirty
想你只要分分秒秒 2024-08-23 07:27:11

您需要确保满足以下两个属性:

  1. HEAD 和索引缓存之间没有差异

    git diff-index --cached HEAD

  2. 索引和工作树之间没有区别:

    git diff-files

两个命令都采用 - -quiet 参数,将根据是否存在差异设置退出代码(从 gi​​t 1.4 之后的某个时间开始)。如果您需要使其在 git 1.4 上运行,则需要运行不带 --quiet 的命令并检查它们是否产生任何输出。

注意: git diff 是一个陶瓷命令,因此不应在脚本中使用。请改用上面的管道命令。

示例 shell 代码,取自我的 git_version.sh 脚本< /a>:

git_dirty=yes
# git-1.4 does not understand "git-diff-files --quiet"
# git-1.4 does not understand "git-diff-index --cached --quiet HEAD"
if [ "x$($GIT diff-files)" = "x" ] && [ "x$($GIT diff-index --cached HEAD)" = "x" ]; then
    git_dirty=no
fi

如果你可以要求 git 版本 >= 1.5, if git diff-files --quiet && git diff-index --quiet --cached HEAD;那么可以代替上面的比较。

注意:此解决方案(与 Antony 的交互式 git diff HEAD --quiet 完全相同)仅发现相对于 HEAD 的本地更改。然而,本地提交也可以被认为是本地更改,并且自然不会出现在与 HEAD 的任何差异中。您需要检查gitdescribe使用的SHA1值来检测HEAD是否来自您认为不是本地更改的一组提交。

You will need to make sure that both the two following properties are met:

  1. That there are no differences between HEAD and the index cache

    git diff-index --cached HEAD

  2. That there are no differences between the index and the working tree:

    git diff-files

Both commands take a --quiet parameter which will set the exit code according to whether there are differences or not (starting some time after git 1.4). If you need to make it work on git 1.4, you need to run the commands without --quiet and check whether they produce any output.

Note: git diff is a porcelain command, and thus should not be used in scripts. Use above plumbing commands instead.

Example shell code, taken from my git_version.sh script:

git_dirty=yes
# git-1.4 does not understand "git-diff-files --quiet"
# git-1.4 does not understand "git-diff-index --cached --quiet HEAD"
if [ "x$($GIT diff-files)" = "x" ] && [ "x$($GIT diff-index --cached HEAD)" = "x" ]; then
    git_dirty=no
fi

If you can require a git version >= 1.5, if git diff-files --quiet && git diff-index --quiet --cached HEAD; then can replace above comparison.

Note: This solution (exactly like Antony's interactive git diff HEAD --quiet) only discovers local changes relative to HEAD. However, local commits can arguable also be considered local changes, and naturally will not show up in any diff against HEAD. You will need to check the SHA1 value git describe uses to detect whether HEAD is from a set of commits you consider not to be local changes.

本王不退位尔等都是臣 2024-08-23 07:27:11

如果有更改,git diff --quiet 返回退出状态 1,如果没有,则返回 0。

请记住,它将显示分阶段更改和工作目录之间的差异。
如果您对 HEAD 和工作目录之间的更改感兴趣,您应该使用 git diff HEAD --quiet 。

--quiet 意味着 --exit-code

git diff --quiet returns with exit status 1 if there're changes and 0 if there're not.

Keep in mind that it will show diff between staged changes and the working dir.
If you're interested in changes between your HEAD and working dir, you should use git diff HEAD --quiet.

--quiet implies --exit-code.

眼眸里的快感 2024-08-23 07:27:11

如果没有本地更改,git status 将以非零状态退出。

但我不明白你的意思“与 git describe 结合”。

git status exits with non-zero status if there is no local changes.

But I don't understand what you mean "in combination with git describe".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文