Commits

Sebastian Sdorra committed 15e8f7b

use javahg to retrieve changesets from a mercurial hook

  • Participants
  • Parent commits ac9737f

Comments (0)

Files changed (2)

scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/HgRepositoryHookEvent.java

 
 package sonia.scm.repository;
 
+//~--- non-JDK imports --------------------------------------------------------
+
+import com.google.common.io.Closeables;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import sonia.scm.repository.spi.HgCommandContext;
+import sonia.scm.repository.spi.javahg.HgLogChangesetCommand;
+
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.File;
   /** Field description */
   public static final String REV_TIP = "tip";
 
+  /**
+   * the logger for HgRepositoryHookEvent
+   */
+  private static final Logger logger =
+    LoggerFactory.getLogger(HgRepositoryHookEvent.class);
+
   //~--- constructors ---------------------------------------------------------
 
   /**
    * @param type
    */
   public HgRepositoryHookEvent(HgRepositoryHandler handler,
-                               String repositoryName, String startRev,
-                               RepositoryHookType type)
+    String repositoryName, String startRev, RepositoryHookType type)
   {
     this.handler = handler;
     this.repositoryName = repositoryName;
   {
     if (changesets == null)
     {
+      HgCommandContext context = null;
+
       try
       {
-        ChangesetPagingResult result =
-          createChangesetViewer().getChangesets(startRev, REV_TIP);
+        context = createCommandContext();
 
-        if (result != null)
-        {
-          changesets = result.getChangesets();
-        }
+        HgLogChangesetCommand cmd = HgLogChangesetCommand.on(context.open(),
+                                      handler.getConfig());
+
+        changesets = cmd.rev(startRev.concat(":").concat(REV_TIP)).execute();
       }
       catch (Exception ex)
       {
-        throw new RuntimeException("could not load changesets", ex);
+        logger.error("could not retrieve changesets", ex);
+      }
+      finally
+      {
+        Closeables.closeQuietly(context);
       }
     }
 
    *
    * @return
    */
-  private HgChangesetViewer createChangesetViewer()
+  private HgCommandContext createCommandContext()
   {
     File directory = handler.getConfig().getRepositoryDirectory();
     File repositoryDirectory = new File(directory, repositoryName);
     // use HG_PENDING only for pre receive hooks
     boolean pending = type == RepositoryHookType.PRE_RECEIVE;
 
-    return handler.getChangesetViewer(repositoryDirectory,
-                                      new HgContext(pending));
+    Repository repository = getRepository();
+
+    return new HgCommandContext(handler.getConfig(), repository,
+      repositoryDirectory, pending);
   }
 
   //~--- fields ---------------------------------------------------------------

scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/repository/spi/HgCommandContext.java

    * @param repository
    * @param directory
    */
-  HgCommandContext(HgConfig config, sonia.scm.repository.Repository repository,
-    File directory)
+  public HgCommandContext(HgConfig config,
+    sonia.scm.repository.Repository repository, File directory)
+  {
+    this(config, repository, directory, false);
+  }
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param config
+   * @param repository
+   * @param directory
+   * @param pending
+   */
+  public HgCommandContext(HgConfig config,
+    sonia.scm.repository.Repository repository, File directory, boolean pending)
   {
     this.config = config;
     this.directory = directory;
-    encoding = repository.getProperty(PROPERTY_ENCODING);
+    this.encoding = repository.getProperty(PROPERTY_ENCODING);
+    this.pending = pending;
 
     if (Strings.isNullOrEmpty(encoding))
     {
         RepositoryConfiguration.DEFAULT;
 
       repoConfiguration.addExtension(HgFileviewExtension.class);
+      repoConfiguration.setEnablePendingChangesets(pending);
 
       try
       {
   private String encoding;
 
   /** Field description */
+  private boolean pending;
+
+  /** Field description */
   private Repository repository;
 }