You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅在Git暂存PHP文件存在时执行Makefile的cs-rector命令

解决方案

你遇到的问题是混淆了Makefile的Make解析语法和Shell执行语法,以下是两种可行的实现方式:

方式一:使用Make的条件判断

在Make解析阶段先获取待处理文件列表,再通过Make的ifneq条件判断决定是否执行Rector命令:

cs-rector: ##@coding-style Refactor code following the PHP version standards staged php files
	@echo "${BLUE}Running rector on git staged files ${RESET}"
	$(eval FILES := $(shell git diff --name-only --cached --diff-filter=d | grep "\.php"))
ifneq ($(FILES),)
	@$(call run-php-script, ./vendor/bin/rector process $(FILES))
endif

注意事项:

  • ifneq和endif不能缩进,它们是Make的指令,不属于Shell命令块
  • 用$(FILES)复用获取到的文件列表,避免重复执行git命令

方式二:使用Shell的条件判断

直接在Shell命令层面完成判断,无需依赖Make的语法:

cs-rector: ##@coding-style Refactor code following the PHP version standards staged php files
	@echo "${BLUE}Running rector on git staged files ${RESET}"
	@FILES=$$(git diff --name-only --cached --diff-filter=d | grep "\.php"); \
	if [ -n "$$FILES" ]; then \
		$(call run-php-script, ./vendor/bin/rector process $$FILES); \
	fi

注意事项:

  • Shell变量前要加$$转义,避免被Make解析
  • 多行Shell命令需要用\换行连接,保持在同一个Shell进程中执行

你之前写法的错误原因

  • 把Make的ifneq条件判断放在了Shell命令的缩进块中,这会被当作Shell命令执行,导致语法错误
  • 混用了Make变量定义和Shell的export命令,两种语法不能在同一逻辑块中直接混用

内容的提问来源于stack exchange,提问作者YannPl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 05:16:38