summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Neumann <Tim.Neumann@advantest.com>2019-12-13 15:20:02 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2021-02-15 16:45:59 +0100
commit15a38e5b4f79792c8ce85c8eddd567c32350de74 (patch)
tree7dfb07a2016521567508b67fcc89f6505d1166d5
parent935c8b752b05f989c9bb734dd7a47533ecec6d37 (diff)
downloadjgit-15a38e5b4f79792c8ce85c8eddd567c32350de74.tar.gz
jgit-15a38e5b4f79792c8ce85c8eddd567c32350de74.zip
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>
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java26
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java13
2 files changed, 35 insertions, 4 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java b/org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java
index 4cbd6c2eb9..ce3ad2239a 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/hooks/GitHook.java
@@ -170,14 +170,32 @@ 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.
*
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java b/org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java
index 0b61ebea3f..b9dafcca31 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/hooks/PostCommitHook.java
@@ -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.
+ }
+
}