From 925964134707d93c9d3e80deb82c16e1af783068 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 7 Oct 2011 15:58:42 +0200 Subject: [PATCH] fix git hook exception handling --- .../java/sonia/scm/web/GitReceiveHook.java | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java index 9005706e8a..6735a72ba0 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java @@ -78,6 +78,9 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook /** Field description */ public static final String FILE_HOOK_PRE_RECEIVE = "pre-receive"; + /** Field description */ + public static final String PREFIX_MSG = "[SCM] "; + /** the logger for GitReceiveHook */ private static final Logger logger = LoggerFactory.getLogger(GitReceiveHook.class); @@ -130,13 +133,18 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook * Method description * * + * + * + * + * @param rpack + * @param rc * @param hook * @param oldId * @param newId * @param refName */ - private void executeFileHook(File hook, ObjectId oldId, ObjectId newId, - String refName) + private void executeFileHook(ReceivePack rpack, ReceiveCommand rc, File hook, + ObjectId oldId, ObjectId newId, String refName) { final Command cmd = new SimpleCommand(hook.getAbsolutePath(), getId(oldId), getId(newId), Util.nonNull(refName)); @@ -162,9 +170,12 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook } } } - else if (logger.isErrorEnabled()) + else { - logger.error("failed to execute file hook"); + if (logger.isErrorEnabled()) + { + logger.error("failed to execute file hook"); + } String out = result.getOutput(); @@ -172,6 +183,8 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook { logger.error(out); } + + sendError(rpack, rc, out); } } catch (IOException ex) @@ -181,15 +194,21 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook } /** - * Method description + * Method description, ccurred * * + * + * + * + * @param rpack + * @param rc * @param directory * @param oldId * @param newId * @param type */ - private void fireHookEvent(File directory, ObjectId oldId, ObjectId newId, + private void fireHookEvent(ReceivePack rpack, ReceiveCommand rc, + File directory, ObjectId oldId, ObjectId newId, RepositoryHookType type) { try @@ -205,6 +224,15 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook { logger.error("repository could not be found", ex); } + catch (Exception ex) + { + if (logger.isWarnEnabled()) + { + logger.warn("execption occurred during hook execution", ex); + } + + sendError(rpack, rc, ex.getMessage()); + } } /** @@ -251,14 +279,29 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook if (hookScript != null) { - executeFileHook(hookScript, oldId, newId, rc.getRefName()); + executeFileHook(rpack, rc, hookScript, oldId, newId, rc.getRefName()); } - fireHookEvent(directory, oldId, newId, type); + fireHookEvent(rpack, rc, directory, oldId, newId, type); } } } + /** + * Method description + * + * + * + * @param rpack + * @param rc + * @param message + */ + private void sendError(ReceivePack rpack, ReceiveCommand rc, String message) + { + rpack.sendError(PREFIX_MSG.concat(Util.nonNull(message))); + rc.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON); + } + //~--- get methods ---------------------------------------------------------- /**