Commits

Sebastian Sdorra committed c2852fd

improve trace logging for git repository hooks

Comments (0)

Files changed (3)

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitRepositoryHookEvent.java

 //~--- 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;
    *
    * @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 ----------------------------------------------------------
         {
           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);
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */
-  private List<Changeset> changesets;
+  private String branch;
 
   /** Field description */
-  private String defaultBranch;
+  private List<Changeset> changesets;
 
   /** Field description */
   private File directory;

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java

 
     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;
    * 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
+   *
+   *
    * @param repo
    *
    * @return

scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitReceiveHook.java

 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;
       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);
       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);
   private void onReceive(ReceivePack rpack,
     Collection<ReceiveCommand> receiveCommands, RepositoryHookType type)
   {
-    if ( logger.isTraceEnabled() )
+    if (logger.isTraceEnabled())
     {
       logger.trace("received git hook, type={}", type);
     }
+
     for (ReceiveCommand rc : receiveCommands)
     {
       if (isReceiveable(rc, type))
 
           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();
    * 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
-   *
-   *
    * @param rc
    * @param type
    *