From 667b48903628cb6b13df9a054eb4988051e65fdf Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Tue, 27 Sep 2011 21:19:40 +0200 Subject: [PATCH] implement pre receive hook for git repositories --- .../repository/GitRepositoryHookEvent.java | 25 +++++++++--- .../java/sonia/scm/web/GitReceiveHook.java | 40 +++++++++++++------ 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryHookEvent.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryHookEvent.java index 59201f117e..1d0a811012 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryHookEvent.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryHookEvent.java @@ -65,18 +65,28 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent private static final Logger logger = LoggerFactory.getLogger(GitRepositoryHookEvent.class); - //~--- get methods ---------------------------------------------------------- + //~--- constructors --------------------------------------------------------- - public GitRepositoryHookEvent(File directory, - ObjectId newId, ObjectId oldId) + /** + * Constructs ... + * + * + * @param directory + * @param newId + * @param oldId + * @param type + */ + public GitRepositoryHookEvent(File directory, ObjectId newId, ObjectId oldId, + RepositoryHookType type) { this.directory = directory; this.newId = newId; this.oldId = oldId; + this.type = type; } - - + //~--- get methods ---------------------------------------------------------- + /** * Method description * @@ -103,7 +113,7 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent @Override public RepositoryHookType getType() { - return RepositoryHookType.POST_RECEIVE; + return type; } //~--- methods -------------------------------------------------------------- @@ -174,4 +184,7 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent /** Field description */ private ObjectId oldId; + + /** Field description */ + private RepositoryHookType type; } 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 2c330e51fb..9005706e8a 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 @@ -49,6 +49,7 @@ import sonia.scm.io.CommandResult; import sonia.scm.io.SimpleCommand; import sonia.scm.repository.GitRepositoryHandler; import sonia.scm.repository.GitRepositoryHookEvent; +import sonia.scm.repository.RepositoryHookType; import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryNotFoundException; import sonia.scm.util.IOUtil; @@ -107,7 +108,7 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook public void onPostReceive(ReceivePack rpack, Collection receiveCommands) { - onReceive(rpack, receiveCommands, FILE_HOOK_POST_RECEIVE, true); + onReceive(rpack, receiveCommands, RepositoryHookType.POST_RECEIVE); } /** @@ -122,7 +123,7 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook public void onPreReceive(ReceivePack rpack, Collection receiveCommands) { - onReceive(rpack, receiveCommands, FILE_HOOK_PRE_RECEIVE, false); + onReceive(rpack, receiveCommands, RepositoryHookType.PRE_RECEIVE); } /** @@ -186,14 +187,16 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook * @param directory * @param oldId * @param newId + * @param type */ - private void fireHookEvent(File directory, ObjectId oldId, ObjectId newId) + private void fireHookEvent(File directory, ObjectId oldId, ObjectId newId, + RepositoryHookType type) { try { String repositoryName = directory.getName(); GitRepositoryHookEvent e = new GitRepositoryHookEvent(directory, newId, - oldId); + oldId, type); repositoryManager.fireHookEvent(GitRepositoryHandler.TYPE_NAME, repositoryName, e); @@ -210,16 +213,19 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook * * @param rpack * @param receiveCommands - * @param hook - * @param fireApiHook + * @param type */ private void onReceive(ReceivePack rpack, Collection receiveCommands, - String hook, boolean fireApiHook) + RepositoryHookType type) { for (ReceiveCommand rc : receiveCommands) { - if (rc.getResult() == ReceiveCommand.Result.OK) + if (((RepositoryHookType.PRE_RECEIVE == type) + && (rc.getResult() + == ReceiveCommand.Result.NOT_ATTEMPTED)) || ((RepositoryHookType + .POST_RECEIVE == type) && (rc.getResult() + == ReceiveCommand.Result.OK))) { ObjectId newId = rc.getNewId(); ObjectId oldId = null; @@ -230,17 +236,25 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook } File directory = rpack.getRepository().getDirectory(); - File hookScript = getHookScript(directory, hook); + String scriptName = null; + + if (type == RepositoryHookType.POST_RECEIVE) + { + scriptName = FILE_HOOK_POST_RECEIVE; + } + else if (type == RepositoryHookType.PRE_RECEIVE) + { + scriptName = FILE_HOOK_PRE_RECEIVE; + } + + File hookScript = getHookScript(directory, scriptName); if (hookScript != null) { executeFileHook(hookScript, oldId, newId, rc.getRefName()); } - if (fireApiHook) - { - fireHookEvent(directory, oldId, newId); - } + fireHookEvent(directory, oldId, newId, type); } } }