2012-11-06 5 views
5

JavaでGit pre commitフックを書く必要があります。これは、デベロッパーによってコミットされたコードが特定のEclipseコードフォーマッターに従ってフォーマットされているかどうかをチェックし、そうでない場合はコミットしません。プリコミットフックをJavaで書くことは可能ですか?JavaでGITプリコミットフックを書く?

答えて

5

アイデアは、Javaプログラムを呼び出す(フォーマットをチェックする)スクリプトを呼び出すことです。

see here an example written in pythonはjavaを呼び出します。

try: 
    # call checkstyle and print output 
    print call(['java', '-jar', checkstyle, '-c', checkstyle_config, '-r', tempdir]) 
except subprocess.CalledProcessError, ex: 
    print ex.output # print checkstyle messages 
    exit(1) 
finally: 
    # remove temporary directory 
    shutil.rmtree(tempdir) 

このother example calls directly ant、(ターンのJava JUnitテストスイートを呼び出す)Antスクリプト

#!/bin/sh 

# Run the test suite. 
# It will exit with 0 if it everything compiled and tested fine. 
ant test 
if [ $? -eq 0 ]; then 
    exit 0 
else 
    echo "Building your project or running the tests failed." 
    echo "Aborting the commit. Run with --no-verify to ignore." 
    exit 1 
fi 
を実行するために
1

あなたはなど、適切に構成されたインタプリタ(bashのやPython、Perlの)と、シェルによって理解することができる任意の言語で

をフックを書くしかし、なぜJavaでJavaコードフォーマッタを記述することはできません、プリコミットフックから呼び出します。

関連する問題