首页 > 解决方案 > Makefile 目标检查干净的 git diff

问题描述

我正在尝试编写一个Makefile目标,以检查在运行另一个目标后运行它的 git 存储库中是否没有未提交的更改。

这是我到目前为止所拥有的:

check-git-clean: other-target
ifneq ($(shell git diff-index --quiet HEAD; echo $$?), 0)
    $(error "There are uncomitted changes after running make other-target")
endif

但是,我遇到的是,如果other-target未提交的原因发生变化,ifneq则不会捕获它。另一方面,如果在我运行之前make check-git-clean已经有未提交的更改,那么ifneq确实会捕获它。

所以,在某种程度上,它几乎就像ifneq是在“之前”运行,make other-target但我得到的 CLI 输出(回显)的顺序是正确的。

我想知道我怎样才能做到这一点。

标签: bashgitmakefile

解决方案


我可以推荐git diff --exit-code直接使用吗?

.PHONY: check-git-clean
check-git-clean: other-target
    git diff --quiet

或者

.PHONY: check-git-clean
check-git-clean: other-target
    git diff-index --quiet HEAD

推荐阅读