Browse Source

Post commit hook failure should not cause commit failure

As the post commit hook is run after a commit is finished, it can not
abort the commit and the exit code of this hook should not have any
effect.

This can be achieved by not throwing a AbortedByHookException exception.
The stderr output is not lost thanks to contributions for bug 553471.

Bug: 553428
Change-Id: I451a76e04103e632ff44e045561c5a41f7b7d558
Signed-off-by: Tim Neumann <Tim.Neumann@advantest.com>
Signed-off-by: Fabian Pfaff <fabian.pfaff@vogella.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v5.11.0.202102240950-m3
Tim Neumann 4 years ago
parent
commit
15a38e5b4f

+ 22
- 4
org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java View File

@@ -170,13 +170,31 @@ public abstract class GitHook<T> implements Callable<T> {
getParameters(), getOutputStream(), stderrStream,
getStdinArgs());
if (result.isExecutedWithError()) {
throw new AbortedByHookException(
new String(errorByteArray.toByteArray(),
Charset.defaultCharset().name()),
getHookName(), result.getExitCode());
handleError(new String(errorByteArray.toByteArray(),
Charset.defaultCharset().name()), result);
}
}

/**
* Process that the hook exited with an error. This default implementation
* throws an {@link AbortedByHookException }. Hooks which need a different
* behavior can overwrite this method.
*
* @param message
* error message
* @param result
* The process result of the hook
* @throws AbortedByHookException
* When the hook should be aborted
* @since 5.11
*/
protected void handleError(String message,
final ProcessResult result)
throws AbortedByHookException {
throw new AbortedByHookException(message, getHookName(),
result.getExitCode());
}

/**
* Check whether a 'native' (i.e. script) hook is installed in the
* repository.

+ 13
- 0
org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java View File

@@ -14,6 +14,7 @@ import java.io.PrintStream;

import org.eclipse.jgit.api.errors.AbortedByHookException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.ProcessResult;

/**
* The <code>post-commit</code> hook implementation. This hook is run after the
@@ -73,4 +74,16 @@ public class PostCommitHook extends GitHook<Void> {
return NAME;
}


/**
* Overwrites the default implementation to never throw an
* {@link AbortedByHookException}, as the commit has already been done and
* the exit code of the post-commit hook has no effect.
*/
@Override
protected void handleError(String message, ProcessResult result)
throws AbortedByHookException {
// Do nothing as the exit code of the post-commit hook has no effect.
}

}

Loading…
Cancel
Save