【问题标题】:Pre-commit hook in IntelliJ IDEAIntelliJ IDEA 中的预提交挂钩
【发布时间】:2017-07-23 10:34:58
【问题描述】:

我想在 IntelliJ IDEA(社区版)中创建一个预提交钩子,用于创建为提交选择的所有文件的列表并将其转储到 xml 文件中。我正在使用团队服务 VCS Git 存储库。

我也安装了 Pre-Commit 插件。我不确定“pre-commit-hook.sh”文件中的哪些代码能够实现这一点。有人可以帮我解决这个问题吗?

【问题讨论】:

标签: intellij-idea pre-commit-hook


【解决方案1】:

要在 pre-commit 挂钩中将正在提交的文件扩展名更改为 xml,您可以使用以下 shell 脚本:

#!/bin/sh

# find the staged files
for Sfile in $(git diff --name-only --cached)
do
{
  mv "$Sfile" "${Sfile%.*}.xml"
  git add ${Sfile%.*}.xml
}
done
# find the unstaged files
for USfile in $(git diff --name-only)
do
{
  mv "$USfile" "${USfile%.*}.xml"
  git add ${USfile%.*}.xml
}
done

【讨论】:

    【解决方案2】:

    由于插件的描述不是很有帮助,有我的解决方案让 Windows 在提交之前运行所有测试:

    • 在项目根文件夹中创建一个名为 pre-commit-hook.bat 的文件
    • 创建一个 .py 文件,例如prehook_runner.py 包含您要在提交之前执行的 python 代码。就我而言,这是对测试运行者的调用:PATH_TO_ENV\python.exe PATH_TO_FILE/prehook_runner.py。这将由 bat 文件调用,因此请确保它在命令提示符中运行。
    • 将你想要运行的代码 pre-commit 放在 prehook_runner.py 中。我的代码(origin):

      import sys
      import unittest
      
      test_suite = unittest.defaultTestLoader.discover('.\\Test', 'test*.py')
      test_runner = unittest.TextTestRunner(resultclass=unittest.TextTestResult)
      result = test_runner.run(test_suite)
      if not result.wasSuccessful():
          print('')
          print('')
          print('SOME TESTS FAILED. You can still commit, but be aware.')
          print('')
          print('')
          print('')
      sys.exit(not result.wasSuccessful())
      

    如果测试失败,这将生成一个弹出窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-23
      • 2019-06-02
      • 1970-01-01
      • 2014-07-26
      • 2013-03-20
      • 1970-01-01
      • 2015-07-08
      • 2020-10-18
      相关资源
      最近更新 更多