Commits

Sebastian Sdorra committed 7d49d09

retrieve branch informations from receive command

  • Participants
  • Parent commits df2d77f
  • Branches issue-235

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;
 import org.slf4j.LoggerFactory;
 
 import sonia.scm.util.IOUtil;
+import sonia.scm.util.Util;
 
 //~--- JDK imports ------------------------------------------------------------
 
    *
    *
    * @param directory
+   * @param ref
    * @param newId
    * @param oldId
    * @param type
    */
-  public GitRepositoryHookEvent(File directory, ObjectId newId, ObjectId oldId,
-                                RepositoryHookType type)
+  public GitRepositoryHookEvent(File directory, Ref ref, ObjectId newId,
+    ObjectId oldId, RepositoryHookType type)
   {
     this.directory = directory;
+    this.defaultBranch = GitUtil.getBranch(ref);
     this.newId = newId;
     this.oldId = oldId;
     this.type = type;
 
         while (commit != null)
         {
-          result.add(converter.createChangeset(commit));
+          Changeset changeset = converter.createChangeset(commit);
+
+          if (changeset.getBranches().isEmpty()
+            && Util.isNotEmpty(defaultBranch))
+          {
+            if (logger.isTraceEnabled())
+            {
+              logger.trace("set branch to current default branch {}",
+                defaultBranch);
+            }
+
+            changeset.getBranches().add(defaultBranch);
+          }
+
+          result.add(changeset);
           commit = walk.next();
         }
       }
   private List<Changeset> changesets;
 
   /** Field description */
+  private String defaultBranch;
+
+  /** Field description */
   private File directory;
 
   /** Field description */

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

   {
     String branch = null;
 
-    String name = ref.getName();
+    if (ref != null)
+    {
 
-    if (name.startsWith(PREFIX_HEADS))
-    {
-      branch = name.substring(PREFIX_HEADS.length());
+      String name = ref.getName();
+
+      if (name.startsWith(PREFIX_HEADS))
+      {
+        branch = name.substring(PREFIX_HEADS.length());
+      }
+
     }
 
     return branch;
   }
-  
+
   /**
    * Method description
    *
     throws IOException
   {
     ObjectId branchId = null;
-    if ( ! branchName.startsWith(REF_HEAD) ){
+
+    if (!branchName.startsWith(REF_HEAD))
+    {
       branchName = PREFIX_HEADS.concat(branchName);
     }
-    
+
     Ref ref = repo.getRef(branchName);
 
     if (ref != null)

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

    * @param handler
    */
   public GitReceiveHook(RepositoryManager repositoryManager,
-                        GitRepositoryHandler handler)
+    GitRepositoryHandler handler)
   {
     this.repositoryManager = repositoryManager;
     this.handler = handler;
    */
   @Override
   public void onPostReceive(ReceivePack rpack,
-                            Collection<ReceiveCommand> receiveCommands)
+    Collection<ReceiveCommand> receiveCommands)
   {
     onReceive(rpack, receiveCommands, RepositoryHookType.POST_RECEIVE);
   }
    */
   @Override
   public void onPreReceive(ReceivePack rpack,
-                           Collection<ReceiveCommand> receiveCommands)
+    Collection<ReceiveCommand> receiveCommands)
   {
     onReceive(rpack, receiveCommands, RepositoryHookType.PRE_RECEIVE);
   }
    * @param refName
    */
   private void executeFileHook(ReceivePack rpack, ReceiveCommand rc,
-                               File repositoryDirectory, File hook,
-                               ObjectId oldId, ObjectId newId, String refName)
+    File repositoryDirectory, File hook, ObjectId oldId, ObjectId newId,
+    String refName)
   {
     final Command cmd = new SimpleCommand(hook.getAbsolutePath(), getId(oldId),
                           getId(newId), Util.nonNull(refName));
   }
 
   /**
-   * Method description, ccurred
-   *
-   *
-   *
-   *
+   * Method description, occurred
    *
    * @param rpack
    * @param rc
    * @param type
    */
   private void fireHookEvent(ReceivePack rpack, ReceiveCommand rc,
-                             File directory, ObjectId oldId, ObjectId newId,
-                             RepositoryHookType type)
+    File directory, ObjectId oldId, ObjectId newId, RepositoryHookType type)
   {
     try
     {
       String repositoryName = RepositoryUtil.getRepositoryName(handler,
                                 directory);
-      GitRepositoryHookEvent e = new GitRepositoryHookEvent(directory, newId,
-                                   oldId, type);
+      GitRepositoryHookEvent e = new GitRepositoryHookEvent(directory,
+                                   rc.getRef(), newId, oldId, type);
 
       repositoryManager.fireHookEvent(GitRepositoryHandler.TYPE_NAME,
-                                      repositoryName, e);
+        repositoryName, e);
     }
     catch (RepositoryNotFoundException ex)
     {
    * @param type
    */
   private void onReceive(ReceivePack rpack,
-                         Collection<ReceiveCommand> receiveCommands,
-                         RepositoryHookType type)
+    Collection<ReceiveCommand> receiveCommands, RepositoryHookType type)
   {
     for (ReceiveCommand rc : receiveCommands)
     {
       if (((RepositoryHookType.PRE_RECEIVE == type)
-           && (rc.getResult()
-               == ReceiveCommand.Result.NOT_ATTEMPTED)) || ((RepositoryHookType
-                 .POST_RECEIVE == type) && (rc.getResult()
-                   == ReceiveCommand.Result.OK)))
+        && (rc.getResult()
+          == ReceiveCommand.Result.NOT_ATTEMPTED)) || ((RepositoryHookType
+            .POST_RECEIVE == type) && (rc.getResult()
+              == ReceiveCommand.Result.OK)))
       {
         ObjectId newId = rc.getNewId();
         ObjectId oldId = null;
           if (hookScript != null)
           {
             executeFileHook(rpack, rc, directory, hookScript, oldId, newId,
-                            rc.getRefName());
+              rc.getRefName());
           }
         }
 
    */
   private File getHookScript(File directory, String name)
   {
-    File baseFile =
-      new File(directory,
-               FILE_HOOKDIRECTORY.concat(File.separator).concat(name));
+    File baseFile = new File(directory,
+                      FILE_HOOKDIRECTORY.concat(File.separator).concat(name));
 
     return IOUtil.getScript(baseFile);
   }
   private boolean isUpdateCommand(ReceiveCommand rc)
   {
     return (rc.getType() == ReceiveCommand.Type.UPDATE)
-           || (rc.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
+      || (rc.getType() == ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
   }
 
   //~--- fields ---------------------------------------------------------------