【问题标题】:Apply pre-commit only on the local repository?仅在本地存储库上应用预提交?
【发布时间】:2019-05-09 05:36:57
【问题描述】:

使用 Git,我有一个预提交钩子,我用 mvn clean install 填充。

我的目标是在推送到远程仓库之前测试此命令是否正确执行。

问题是这个命令适用于我的当前目录,而不是本地 git 存储库(仅包含添加的文件)。所以,有时我会因为一些我没有添加的文件而出错,而没有这些文件就没有错误。

所以我的问题是如何应用 mvn clean install 的预提交,该预提交仅适用于添加到本地 repo 的文件?

【问题讨论】:

  • 这不是很依赖钩子本身吗?
  • @MadPhysicist 你的意思是我需要更改钩子里的命令吗?
  • 我的意思是我不知道,因为你在问一些我看不到的东西。

标签: git


【解决方案1】:

预提交钩子 shell 脚本示例:https://gist.github.com/dahjelle/8ddedf0aebd488208a9a7c829f19b9e8

注意线 3:

$(git diff --cached --name-only | grep -E '\.(js|jsx)$')

这会抓取 git commit ... 管道的文件以仅过滤 .js 或 .jsx 文件。您可以修改此脚本并移除管道。

在第 5 行:用您的 mvn clean install 修改 node_modules/... 行。

根据需要修改脚本。

编辑:

这是一个可能更清楚地匹配您的用例的示例:https://eing.github.io/technology/2016/01/28/Git-Pre-Commit-Hooks-Part1/

{ echo "
git stash -q --keep-index
# Using "mvn test" to run all unit tests and run plugins to assert
#   * code coverage threshold >= 85% (using surefire, enforcer plugins)
#   * FindBugs at low threshold errors (using findbugs-maven-plugin)
#   * Checkstyle has 0 errors (using maven-checkstyle-plugin)
mvn clean install test
RESULTS=\$?
# Perform checks
git stash pop -q
if [ \$RESULTS -ne 0 ]; then
  echo Error: Commit criteria not met with one or more of the following issues,
  echo 1. Failure\(s\) in unit tests
  echo 2. Failure to meet 85% code coverage
  echo 3. Failure to meet low FindBugs threshold
  echo 4. Failure to meet 0 Checkstyle errors
  exit 1
fi
# You shall commit
exit 0"
} > pre-commit.sh
pushd .git/hooks
ln -s ../../pre-commit.sh pre-commit
chmod u+x pre-commit
popd

【讨论】:

  • 当我添加 mvn clean install 时,它会为每个添加的文件执行逗号。我只想对所有提交的文件执行一次
  • 你是如何提交的?您是一次添加所有文件还是一次添加所有文件?这个脚本可以一次完成。你的流程是什么?您是添加一个文件然后提交还是添加所有文件然后提交?
  • 这是我的过程:git add file1 file2 然后是git commit -m "update message",我希望在提交之前检查mvn clean install 是否仅在添加file1 file2 后的repo 状态下无错误地执行,而不是所有目录。然后我会添加 file3 并且我想在每次提交时重复相同的过程。
  • @devPsycho 请看看我上面更新的答案。
  • 谢谢。好吧,我想我需要一个更轻的版本。 git stash -q --keep-index mvn clean install git stash pop -q exit 0 您能否确认它检查 mvn 是否在提交的文件上正确执行?
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 2014-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多