I have written git pre-commit hook using groovy script. I am using Windows 7. Since the files located at
.git/hooks/pre-commit
are not version controlled, I have moved these pre-commit hooks to project sub-repository.
${PROJECT_REPO}/git-hooks
directory.
This is the script in file @ .git/hooks/pre-commit
#!/usr/bin/env groovy
public class PreCommitHooks
{
public static boolean CheckBannedFilesToCommit()
{
def process = "cmd /c groovy ../../git-hooks/pre-commit".execute();
process.waitFor();
if (process.exitValue())
{
return false;
}
return true;
}
}
// Create an instance of PreCommitHooks() class and execute list of hooks.
preCommitHook = new PreCommitHooks();
if (!preCommitHook.CheckBannedFilesToCommit())
{
System.exit(1);
}
else
{
System.exit(0);
}
Script in file @ git-hooks/pre-commit
#!/usr/bin/env groovy
This is pseudo code only
process all the hook info.
....
if (!bSuccess)
system.exit(1);
else
system.exit(0);
I have installed all the ${GROOVY_HOME}
and ${JAVA_HOME}
paths correctly, verified using windows command line. My scripts run perfectly when called using Windows Command line.
${PROJECT_REPO}/.git/hooks>groovy pre-commit
Since all the scripts are working, I proceeded to test my pre-commit hook using smart-git
File @ .git/hooks/pre-commit
gets executed properly, it is able to find ${GROOVY_HOME}
and ${JAVA_HOME}
paths properly. But when executing following line
def process = "cmd /c groovy ../../git-hooks/pre-commit".execute();
It fails with following error.
Error: JAVA_HOME is set to invalid directory: C:\Program Files (x86)\Java\Jdk1.8.0_51 Please set the JAVA_HOME variable in your environment to match the location of your Java installation.
Can anyone please help me resolve this error?
I am looking for a groovy script solution instead of bash script.
I ended up using windows symlink instead.
cmd /c mklink /H ${link_file_full_path} ${Target_file_full_path}
Calling a groovy script (located at $WORK_TREE
} from another groovy script located at $GIT_DIR
did not work.
I believe it must be to do with environment variables. Smartgit passes all the environment variables to first script and that information is lost when another script is called.