Commits

Sebastian Sdorra committed a2b5684

do not fire hook event, if no new id is specified

  • Participants
  • Parent commits c2852fd
  • Branches issue-242

Comments (0)

Files changed (2)

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

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.collect.Lists;
+
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevSort;
    */
   private List<Changeset> fetchChangesets()
   {
-    List<Changeset> result = new ArrayList<Changeset>();
+    List<Changeset> result = Lists.newArrayList();
+    GitChangesetConverter converter = null;
+    RevWalk walk = null;
+    org.eclipse.jgit.lib.Repository repository = null;
 
-    if (newId != null)
+    try
     {
-      GitChangesetConverter converter = null;
-      RevWalk walk = null;
-      org.eclipse.jgit.lib.Repository repository = null;
+      repository = GitUtil.open(directory);
+      converter = new GitChangesetConverter(repository, GitUtil.ID_LENGTH);
+      walk = new RevWalk(repository);
+      walk.reset();
+      walk.sort(RevSort.NONE);
+      walk.markStart(walk.parseCommit(newId));
 
-      try
+      if (oldId != null)
       {
-        repository = GitUtil.open(directory);
-        converter = new GitChangesetConverter(repository, GitUtil.ID_LENGTH);
-        walk = new RevWalk(repository);
-        walk.reset();
-        walk.sort(RevSort.NONE);
-        walk.markStart(walk.parseCommit(newId));
+        walk.markUninteresting(walk.parseCommit(oldId));
+      }
 
-        if (oldId != null)
+      RevCommit commit = walk.next();
+
+      while (commit != null)
+      {
+        Changeset changeset = converter.createChangeset(commit);
+
+        if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch))
         {
-          walk.markUninteresting(walk.parseCommit(oldId));
+          if (logger.isTraceEnabled())
+          {
+            logger.trace("set branch to current default branch {}", branch);
+          }
+
+          changeset.getBranches().add(branch);
         }
 
-        RevCommit commit = walk.next();
+        result.add(changeset);
+        commit = walk.next();
+      }
+    }
+    catch (IOException ex)
+    {
+      logger.error("could not fetch changesets", ex);
+    }
+    finally
+    {
+      IOUtil.close(converter);
+      GitUtil.release(walk);
+      GitUtil.close(repository);
 
-        while (commit != null)
-        {
-          Changeset changeset = converter.createChangeset(commit);
-
-          if (changeset.getBranches().isEmpty() && Util.isNotEmpty(branch))
-          {
-            if (logger.isTraceEnabled())
-            {
-              logger.trace("set branch to current default branch {}", branch);
-            }
-
-            changeset.getBranches().add(branch);
-          }
-
-          result.add(changeset);
-          commit = walk.next();
-        }
-      }
-      catch (IOException ex)
-      {
-        logger.error("could not fetch changesets", ex);
-      }
-      finally
-      {
-        IOUtil.close(converter);
-        GitUtil.release(walk);
-        GitUtil.close(repository);
-      }
     }
 
     return result;

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

         }
 
         ObjectId newId = rc.getNewId();
-        ObjectId oldId = null;
 
-        if (isUpdateCommand(rc))
+        if (newId != null)
         {
-          oldId = rc.getOldId();
-
-          if (logger.isTraceEnabled())
-          {
-            logger.trace(
-              "handle update receive command from commit '{}' to '{}'",
-              oldId.getName(), newId.getName());
-          }
+          onReceive(rpack, rc, newId, type);
         }
-        else if (logger.isTraceEnabled())
+        else if (logger.isWarnEnabled())
         {
-          logger.trace("handle receive command for commit '{}'",
-            newId.getName());
+          logger.warn("received hook event without new id");
         }
 
-        File directory = rpack.getRepository().getDirectory();
-        String scriptName = null;
-
-        if (type == RepositoryHookType.POST_RECEIVE)
-        {
-          scriptName = FILE_HOOK_POST_RECEIVE;
-        }
-        else if (type == RepositoryHookType.PRE_RECEIVE)
-        {
-          scriptName = FILE_HOOK_PRE_RECEIVE;
-        }
-
-        if (scriptName != null)
-        {
-          File hookScript = getHookScript(directory, scriptName);
-
-          if (hookScript != null)
-          {
-            executeFileHook(rpack, rc, directory, hookScript, oldId, newId,
-              rc.getRefName());
-          }
-        }
-
-        fireHookEvent(rpack, rc, directory, oldId, newId, type);
       }
       else if (logger.isTraceEnabled())
       {
    * Method description
    *
    *
+   * @param rpack
+   * @param rc
+   * @param newId
+   * @param type
+   */
+  private void onReceive(ReceivePack rpack, ReceiveCommand rc, ObjectId newId,
+    RepositoryHookType type)
+  {
+    ObjectId oldId = null;
+
+    if (isUpdateCommand(rc))
+    {
+      oldId = rc.getOldId();
+
+      if (logger.isTraceEnabled())
+      {
+        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());
+    }
+
+    File directory = rpack.getRepository().getDirectory();
+    String scriptName = null;
+
+    if (type == RepositoryHookType.POST_RECEIVE)
+    {
+      scriptName = FILE_HOOK_POST_RECEIVE;
+    }
+    else if (type == RepositoryHookType.PRE_RECEIVE)
+    {
+      scriptName = FILE_HOOK_PRE_RECEIVE;
+    }
+
+    if (scriptName != null)
+    {
+      File hookScript = getHookScript(directory, scriptName);
+
+      if (hookScript != null)
+      {
+        executeFileHook(rpack, rc, directory, hookScript, oldId, newId,
+          rc.getRefName());
+      }
+    }
+
+    fireHookEvent(rpack, rc, directory, oldId, newId, type);
+  }
+
+  /**
+   * Method description
+   *
+   *
    *
    * @param rpack
    * @param rc