From 2eb629054501bfa96a76553f1d8de8f1d366ba00 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Thu, 13 Sep 2012 18:56:45 +0200 Subject: [PATCH] improve trace logging for git repository hooks --- .../repository/GitRepositoryHookEvent.java | 33 +++++++++----- .../java/sonia/scm/repository/GitUtil.java | 43 ++++++++++++++++--- .../java/sonia/scm/web/GitReceiveHook.java | 37 +++++----------- 3 files changed, 72 insertions(+), 41 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 3aa4344f50..76fba12042 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 @@ -36,7 +36,6 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevSort; import org.eclipse.jgit.revwalk.RevWalk; @@ -75,18 +74,34 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent * * @param directory * @param ref + * @param refName * @param newId * @param oldId * @param type */ - public GitRepositoryHookEvent(File directory, Ref ref, ObjectId newId, + public GitRepositoryHookEvent(File directory, String refName, ObjectId newId, ObjectId oldId, RepositoryHookType type) { this.directory = directory; - this.defaultBranch = GitUtil.getBranch(ref); + this.branch = GitUtil.getBranch(refName); this.newId = newId; this.oldId = oldId; this.type = type; + + if (logger.isTraceEnabled()) + { + //J- + logger.trace( + "create hook event for branch={}, new-id={}, old-id={} and type={}", + new Object[] { + refName, + GitUtil.getId(newId), + GitUtil.getId(oldId), + type + } + ); + //J+ + } } //~--- get methods ---------------------------------------------------------- @@ -158,16 +173,14 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent { Changeset changeset = converter.createChangeset(commit); - if (changeset.getBranches().isEmpty() - && Util.isNotEmpty(defaultBranch)) + if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch)) { if (logger.isTraceEnabled()) { - logger.trace("set branch to current default branch {}", - defaultBranch); + logger.trace("set branch to current default branch {}", branch); } - changeset.getBranches().add(defaultBranch); + changeset.getBranches().add(branch); } result.add(changeset); @@ -192,10 +205,10 @@ public class GitRepositoryHookEvent extends AbstractRepositoryHookEvent //~--- fields --------------------------------------------------------------- /** Field description */ - private List changesets; + private String branch; /** Field description */ - private String defaultBranch; + private List changesets; /** Field description */ private File directory; diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java index c65536217a..895b93bbca 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java @@ -226,14 +226,27 @@ public class GitUtil if (ref != null) { + branch = getBranch(ref.getName()); + } - String name = ref.getName(); + return branch; + } - if (name.startsWith(PREFIX_HEADS)) - { - branch = name.substring(PREFIX_HEADS.length()); - } + /** + * Method description + * + * + * @param name + * + * @return + */ + public static String getBranch(String name) + { + String branch = null; + if (Util.isNotEmpty(name) && name.startsWith(PREFIX_HEADS)) + { + branch = name.substring(PREFIX_HEADS.length()); } return branch; @@ -329,6 +342,26 @@ public class GitUtil return date; } + /** + * Method description + * + * + * @param objectId + * + * @return + */ + public static String getId(ObjectId objectId) + { + String id = Util.EMPTY_STRING; + + if (objectId != null) + { + id = objectId.name(); + } + + return id; + } + /** * Method description * 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 60d8aa99af..1003055ddb 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.GitUtil; import sonia.scm.repository.RepositoryHookType; import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryNotFoundException; @@ -153,8 +154,9 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook logger.debug("execute file hook '{}' in directoy '{}'"); } - final Command cmd = new SimpleCommand(hook.getAbsolutePath(), getId(oldId), - getId(newId), Util.nonNull(refName)); + final Command cmd = new SimpleCommand(hook.getAbsolutePath(), + GitUtil.getId(oldId), GitUtil.getId(newId), + Util.nonNull(refName)); // issue-99 cmd.setWorkDirectory(repositoryDirectory); @@ -221,7 +223,7 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook String repositoryName = RepositoryUtil.getRepositoryName(handler, directory); GitRepositoryHookEvent e = new GitRepositoryHookEvent(directory, - rc.getRef(), newId, oldId, type); + rc.getRefName(), newId, oldId, type); repositoryManager.fireHookEvent(GitRepositoryHandler.TYPE_NAME, repositoryName, e); @@ -252,10 +254,11 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook private void onReceive(ReceivePack rpack, Collection receiveCommands, RepositoryHookType type) { - if ( logger.isTraceEnabled() ) + if (logger.isTraceEnabled()) { logger.trace("received git hook, type={}", type); } + for (ReceiveCommand rc : receiveCommands) { if (isReceiveable(rc, type)) @@ -282,13 +285,15 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook if (logger.isTraceEnabled()) { - logger.trace("handle update receive command from commit '{}' to '{}'", + logger.trace( + "handle update receive command from commit '{}' to '{}'", oldId.getName(), newId.getName()); } } else if (logger.isTraceEnabled()) { - logger.trace("handle receive command for commit '{}'", newId.getName()); + logger.trace("handle receive command for commit '{}'", + newId.getName()); } File directory = rpack.getRepository().getDirectory(); @@ -365,26 +370,6 @@ public class GitReceiveHook implements PreReceiveHook, PostReceiveHook return IOUtil.getScript(baseFile); } - /** - * Method description - * - * - * @param objectId - * - * @return - */ - private String getId(ObjectId objectId) - { - String id = Util.EMPTY_STRING; - - if (objectId != null) - { - id = objectId.name(); - } - - return id; - } - /** * Method description *