As of Java 11, you can now run un-compiled main class files using the java command.
$ java Hook.java
If you are using a Unix based operating system (MacOS or Linux for example), you can strip off the .java
and add a shebang to the top line like so:
#!/your/path/to/bin/java --source 11
public class Hook {
public static void main(String[] args) {
System.out.println("No committing please.");
System.exit(1);
}
}
then you can simply execute it the same way you would with any other script file.
$ ./Hook
If you rename the file pre-commit
, and then move it into your .git/hooks
directory, you now have a working Java Git Hook.
Note: You may be able to get this to work on Windows using Cygwin or Git Bash or similar terminal emulators. However, shebangs do not handle spaces very well so you may need to create. I tested that this works by moving a simlink or similar for your Java executablecopy of java into a directory without spaces and it works fine.