2012-11-06 27 views
5

Ho bisogno di scrivere un hook di pre-commit Git in Java, che verificherebbe se il codice commesso dallo sviluppatore è formattato secondo uno specifico formattatore di codice eclissi prima di eseguirlo effettivamente, altrimenti lo rifiuta dal commit. È possibile scrivere il gancio di pre-commit in Java?Scrivi GIT pre-commit hook in java?

risposta

5

L'idea è di chiamare uno script che a sua volta chiama il tuo programma java (controllando il formato).

È possibile see here an example written in python, che chiama 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) 

Questo other example calls directly ant, al fine di eseguire uno script formica (che a sua volta chiama una suite di test JUnit Java)

#!/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

È possibile scrivere il gancio in qualsiasi lingua che può essere compreso dalla shell, con l'interprete correttamente configurato (bash, Python, Perl), ecc

Ma, perché non scrivere il codice Java formattatore in java, e invocarlo dal gancio di pre-commit.