IntelliJ IDEA中的预提交挂钩

时间:2017-07-23 10:34:59

标签: intellij-idea pre-commit-hook

我想在IntelliJ IDEA(Community Edition)中创建一个预提交挂钩,用于创建为提交选择的所有文件的列表,并将其转储到xml文件中。我正在使用Team Services VCS Git存储库。

我也安装了Pre-Commit插件。我不确定“precommit-hook.sh”文件中的代码是什么,以便能够实现此目的。有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:1)

要在预提交挂钩中将正在提交的文件扩展名更改为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

答案 1 :(得分:0)

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

  • 在项目根文件夹
  • 中创建名为pre-commit-hook.bat的文件
  • 创建一个.py文件,例如prehook_runner.py在提交之前保存您想要执行的python代码。在我的例子中,这是对测试运行员的调用:PATH_TO_ENV\python.exe PATH_TO_FILE/prehook_runner.py。这将由bat文件调用,因此请确保它在命令提示符下运行。
  • 把代码yopu想要在prehook_runner.py中运行pre-commit。我的代码(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())
    

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