2012-11-06 93 views
5

我需要在Java中編寫Git預提交鉤子,它會在實際提交之前檢查開發人員提交的代碼是否根據特定的eclipse代碼格式化程序進行格式化,否則拒絕提交。是否有可能在Java中編寫預提交鉤子?在Git中編寫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,爲了執行ant腳本(這又調用Java JUnit測試套件)

#!/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中,蟒蛇,perl的)等

但是,爲什麼不寫Java代碼格式在Java中,並從預提交鉤子調用它。