Package org.eclipse.jgit.hooks
Class GitHook<T>
- java.lang.Object
-
- org.eclipse.jgit.hooks.GitHook<T>
-
- Type Parameters:
T- the return type which is expected fromcall()
- All Implemented Interfaces:
Callable<T>
- Direct Known Subclasses:
CommitMsgHook,PostCommitHook,PreCommitHook,PrePushHook
public abstract class GitHook<T> extends Object implements Callable<T>
Git can fire off custom scripts when certain important actions occur. These custom scripts are called "hooks". There are two groups of hooks: client-side (that run on local operations such as committing and merging), and server-side (that run on network operations such as receiving pushed commits). This is the abstract super-class of the different hook implementations in JGit.- Since:
- 5.11
- See Also:
- Git Hooks on the git-scm official site
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedGitHook(Repository repo, OutputStream outputStream)Constructor for GitHook.protectedGitHook(Repository repo, OutputStream outputStream, OutputStream errorStream)Constructor for GitHook
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Tcall()protected voiddoRun()Runs the hook, without performing any validity checks.protected OutputStreamgetErrorStream()Get error streamabstract StringgetHookName()Get name of the hookprotected OutputStreamgetOutputStream()Get output streamprotected String[]getParameters()Override this method when needed to provide relevant parameters to the underlying hook script.protected RepositorygetRepository()Get the repositoryprotected StringgetStdinArgs()Override to provide relevant arguments via stdin to the underlying hook script.protected voidhandleError(String message, ProcessResult result)Process that the hook exited with an error.booleanisNativeHookPresent()Check whether a 'native' (i.e.
-
-
-
Constructor Detail
-
GitHook
protected GitHook(Repository repo, OutputStream outputStream)
Constructor for GitHook.This constructor will use stderr for the error stream.
- Parameters:
repo- aRepositoryobject.outputStream- The output stream the hook must use.nullis allowed, in which case the hook will useSystem.out.
-
GitHook
protected GitHook(Repository repo, OutputStream outputStream, OutputStream errorStream)
Constructor for GitHook- Parameters:
repo- aRepositoryobject.outputStream- The output stream the hook must use.nullis allowed, in which case the hook will useSystem.out.errorStream- The error stream the hook must use.nullis allowed, in which case the hook will useSystem.err.
-
-
Method Detail
-
call
public abstract T call() throws IOException, AbortedByHookException
Run the hook.
- Specified by:
callin interfaceCallable<T>- Throws:
IOExceptionAbortedByHookException
-
getHookName
public abstract String getHookName()
Get name of the hook- Returns:
- The name of the hook, which must not be
null.
-
getRepository
protected Repository getRepository()
Get the repository- Returns:
- The repository.
-
getParameters
protected String[] getParameters()
Override this method when needed to provide relevant parameters to the underlying hook script. The default implementation returns an empty array.- Returns:
- The parameters the hook receives.
-
getStdinArgs
protected String getStdinArgs()
Override to provide relevant arguments via stdin to the underlying hook script. The default implementation returnsnull.- Returns:
- The parameters the hook receives.
-
getOutputStream
protected OutputStream getOutputStream()
Get output stream- Returns:
- The output stream the hook must use. Never
null,System.outis returned by default.
-
getErrorStream
protected OutputStream getErrorStream()
Get error stream- Returns:
- The error stream the hook must use. Never
null,System.erris returned by default.
-
doRun
protected void doRun() throws AbortedByHookException, IOExceptionRuns the hook, without performing any validity checks.- Throws:
AbortedByHookException- If the underlying hook script exited with non-zero.IOException- if an IO error occurred
-
handleError
protected void handleError(String message, ProcessResult result) throws AbortedByHookException
Process that the hook exited with an error. This default implementation throws anAbortedByHookException. Hooks which need a different behavior can overwrite this method.- Parameters:
message- error messageresult- The process result of the hook- Throws:
AbortedByHookException- When the hook should be aborted- Since:
- 5.11
-
isNativeHookPresent
public boolean isNativeHookPresent()
Check whether a 'native' (i.e. script) hook is installed in the repository.- Returns:
- whether a native hook script is installed in the repository.
- Since:
- 4.11
-
-