1. Sebastian Sdorra
  2. scm-manager

Commits

Sebastian Sdorra  committed 03aa6e4

implementing HgRepositoryManager

  • Participants
  • Parent commits 462dac2
  • Branches default

Comments (0)

Files changed (4)

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

View file
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package sonia.scm.repository;
+
+import java.util.Collection;
+import sonia.scm.group.Group;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+public class HgPermissionBuilder
+{
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param permissions
+   */
+  public HgPermissionBuilder(Collection<Permission> permissions)
+  {
+    buildPermissions(permissions);
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  public String getReadPermission()
+  {
+    return read.toString();
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  public String getWritePermission()
+  {
+    return write.toString();
+  }
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param permissions
+   */
+  private void buildPermissions(Collection<Permission> permissions)
+  {
+    boolean firstRead = true;
+    boolean firstWrite = true;
+
+    for (Permission permission : permissions)
+    {
+      String name = permission.getName();
+
+      if (permission.isGroupPermission())
+      {
+        name = createGroupString(name);
+      }
+
+      if (permission.isReadable())
+      {
+        if (!firstRead)
+        {
+          read.append(", ");
+        }
+        else
+        {
+          firstRead = false;
+        }
+
+        read.append(name);
+      }
+
+      if (permission.isWriteable())
+      {
+        if (!firstWrite)
+        {
+          write.append(", ");
+        }
+        else
+        {
+          firstWrite = false;
+        }
+
+        write.append(name);
+      }
+    }
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param name
+   *
+   * @return
+   */
+  private String createGroupString(String name)
+  {
+    StringBuilder result = new StringBuilder();
+
+    result.append("__").append(name).append(", ");
+
+    Group group = /*SCMContext.getContext().getGroupManager().get(name);*/ null;
+
+    if (group != null)
+    {
+      Collection<String> memerbers = group.getMembers();
+
+      if (memerbers != null)
+      {
+        for (String member : memerbers)
+        {
+          result.append(member).append(", ");
+        }
+      }
+    }
+
+    result.append("__").append(name);
+
+    return result.toString();
+  }
+
+  //~--- fields ---------------------------------------------------------------
+
+  /** Field description */
+  private StringBuilder read = new StringBuilder();
+
+  /** Field description */
+  private StringBuilder write = new StringBuilder();
+}

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

View file
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package sonia.scm.repository;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import sonia.scm.util.Util;
+
+/**
+ *
+ * @author Sebastian Sdorra
+ */
+public class HgPermissionReader
+{
+
+  /**
+   * Method description
+   *
+   *
+   * @param read
+   * @param write
+   *
+   * @return
+   */
+  public List<Permission> readPermissions(String read, String write)
+  {
+    List<String> readPerms = createPermList(read);
+    List<String> writePerms = createPermList(write);
+
+    return createPermissionList(readPerms, writePerms);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param perms
+   *
+   * @return
+   */
+  private List<String> createGroups(List<String> perms)
+  {
+    List<String> groups = new ArrayList<String>();
+    boolean start = false;
+    Iterator<String> permIt = perms.iterator();
+
+    while (permIt.hasNext())
+    {
+      String perm = permIt.next();
+
+      if (perm.startsWith("__"))
+      {
+        if (start)
+        {
+          start = false;
+        }
+        else
+        {
+          start = true;
+          groups.add(perm.substring(2));
+        }
+
+        permIt.remove();
+      }
+      else if (start)
+      {
+        permIt.remove();
+      }
+    }
+
+    return groups;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param value
+   *
+   * @return
+   */
+  private List<String> createPermList(String value)
+  {
+    List<String> perms = new ArrayList<String>();
+
+    if (Util.isNotEmpty(value))
+    {
+      String[] values = value.split(",");
+
+      for (String name : values)
+      {
+        perms.add(name.trim());
+      }
+    }
+
+    return perms;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param readPerms
+   * @param writePerms
+   *
+   * @return
+   */
+  private List<Permission> createPermissionList(List<String> readPerms,
+          List<String> writePerms)
+  {
+    List<Permission> permissions = new ArrayList<Permission>();
+    List<String> readGroups = createGroups(readPerms);
+    List<String> writeGroups = createGroups(writePerms);
+
+    createPermissions(permissions, readPerms, writePerms, false);
+    createPermissions(permissions, readGroups, writeGroups, true);
+
+    return permissions;
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param permissions
+   * @param readPermissions
+   * @param writePermissions
+   * @param group
+   */
+  private void createPermissions(List<Permission> permissions,
+                                 List<String> readPermissions,
+                                 List<String> writePermissions, boolean group)
+  {
+    for (String readPerm : readPermissions)
+    {
+      boolean write = false;
+
+      if (writePermissions.contains(readPerm))
+      {
+        write = true;
+        writePermissions.remove(readPerm);
+      }
+
+      permissions.add(new Permission(readPerm, true, write, group));
+    }
+
+    for (String writePerm : writePermissions)
+    {
+      permissions.add(new Permission(writePerm, false, true, group));
+    }
+  }
+}

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

View file
 //~--- non-JDK imports --------------------------------------------------------
 
 import sonia.scm.SCMContextProvider;
+import sonia.scm.io.CommandResult;
+import sonia.scm.io.ExtendedCommand;
+import sonia.scm.io.INIConfiguration;
+import sonia.scm.io.INIConfigurationReader;
+import sonia.scm.io.INIConfigurationWriter;
+import sonia.scm.io.INISection;
 import sonia.scm.util.AssertUtil;
+import sonia.scm.util.Util;
 
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.File;
 import java.io.IOException;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.bind.JAXB;
 
   public static final String CONFIG_FILE =
     "config".concat(File.separator).concat("hg.xml");
 
+  /** Field description */
+  private static final Logger logger =
+    Logger.getLogger(HgRepositoryManager.class.getName());
+
   //~--- methods --------------------------------------------------------------
 
   /**
   public void create(Repository repository)
           throws RepositoryException, IOException
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    File directory = getDirectory(repository);
+
+    if (directory.exists())
+    {
+      throw new RepositoryAllreadyExistExeption();
+    }
+
+    ExtendedCommand command = new ExtendedCommand(config.getHgBinary(), "init",
+                                directory.getPath());
+    CommandResult result = command.execute();
+
+    if (!result.isSuccessfull())
+    {
+      StringBuilder msg = new StringBuilder("hg exit with error ");
+
+      msg.append(result.getReturnCode()).append(" and message: '");
+      msg.append(result.getOutput()).append("'");
+
+      throw new RepositoryException(msg.toString());
+    }
+
+    repository.setType(TYPE_NAME);
+
+    File hgDirectory = new File(directory, ".hg");
+
+    writeHgrc(repository, hgDirectory);
+    fireEvent(repository, RepositoryEvent.CREATE);
   }
 
   /**
   public void delete(Repository repository)
           throws RepositoryException, IOException
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    File directory = getDirectory(repository);
+
+    if (new File(directory, ".hg").exists())
+    {
+      Util.delete(directory);
+      fireEvent(repository, RepositoryEvent.DELETE);
+    }
+    else
+    {
+      throw new RepositoryException("repository does not exists");
+    }
   }
 
   /**
   public void modify(Repository repository)
           throws RepositoryException, IOException
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    File directory = getDirectory(repository);
+    File hgDirectory = new File(directory, ".hg");
+
+    if (hgDirectory.exists())
+    {
+      writeHgrc(repository, hgDirectory);
+      fireEvent(repository, RepositoryEvent.MODIFY);
+    }
   }
 
   /**
   public void refresh(Repository repository)
           throws RepositoryException, IOException
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    File directory = getDirectory(repository);
+    File hgDirectory = new File(directory, ".hg");
+
+    if (hgDirectory.exists())
+    {
+      readHgrc(repository, hgDirectory);
+    }
   }
 
   //~--- get methods ----------------------------------------------------------
   @Override
   public Repository get(String name)
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    Repository repository = null;
+    File directory = getDirectory(name);
+
+    if (directory.exists() && directory.isDirectory())
+    {
+      File hgDirectory = new File(directory, ".hg");
+
+      if (hgDirectory.exists() && hgDirectory.isDirectory())
+      {
+        repository = new Repository(TYPE_NAME, name);
+        readHgrc(repository, hgDirectory);
+      }
+    }
+
+    return repository;
   }
 
   /**
   @Override
   public Collection<Repository> getAll()
   {
-    throw new UnsupportedOperationException("Not supported yet.");
+    List<Repository> repositories = new ArrayList<Repository>();
+    String[] repositoryNames = config.getRepositoryDirectory().list();
+
+    if ((repositoryNames != null) && (repositoryNames.length > 0))
+    {
+      for (String repositoryName : repositoryNames)
+      {
+        Repository repository = get(repositoryName);
+
+        if (repository != null)
+        {
+          repositories.add(repository);
+        }
+      }
+    }
+
+    return repositories;
   }
 
   /**
     return config != null;
   }
 
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param section
+   * @param permissions
+   */
+  private void appendPermission(INISection section,
+                                Collection<Permission> permissions)
+  {
+    HgPermissionBuilder builder = new HgPermissionBuilder(permissions);
+
+    section.setParameter("allow_read", builder.getReadPermission());
+    section.setParameter("allow_push", builder.getWritePermission());
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param repository
+   * @param hgDirectory
+   */
+  private void readHgrc(Repository repository, File hgDirectory)
+  {
+    File hgrc = new File(hgDirectory, "hgrc");
+
+    if (hgrc.exists() && hgrc.isFile())
+    {
+      try
+      {
+        INIConfiguration iniConfig = new INIConfigurationReader().read(hgrc);
+        INISection section = iniConfig.getSection("web");
+
+        if (section != null)
+        {
+          repository.setDescription(section.getParameter("description"));
+          repository.setContact(section.getParameter("contact"));
+
+          String read = section.getParameter("allow_read");
+          String write = section.getParameter("allow_push");
+
+          if ((read != null) || (write != null))
+          {
+            if (read == null)
+            {
+              read = "";
+            }
+
+            if (write == null)
+            {
+              write = "";
+            }
+
+            List<Permission> permissions =
+              new HgPermissionReader().readPermissions(read, write);
+
+            repository.setPermissions(permissions);
+          }
+        }
+      }
+      catch (IOException ex)
+      {
+        logger.log(Level.SEVERE, null, ex);
+      }
+    }
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param repository
+   * @param hgDirectory
+   *
+   * @throws IOException
+   */
+  private void writeHgrc(Repository repository, File hgDirectory)
+          throws IOException
+  {
+    INISection section = new INISection("web");
+
+    section.setParameter("name", repository.getName());
+
+    String description = repository.getDescription();
+
+    if (Util.isNotEmpty(description))
+    {
+      section.setParameter("description", description);
+    }
+
+    String contact = repository.getContact();
+
+    if (Util.isNotEmpty(contact))
+    {
+      section.setParameter("contact", contact);
+    }
+
+    Collection<Permission> permissions = repository.getPermissions();
+
+    if (permissions != null)
+    {
+      appendPermission(section, permissions);
+    }
+
+    INIConfiguration iniConfig = new INIConfiguration();
+
+    iniConfig.addSection(section);
+
+    File hgrc = new File(hgDirectory, "hgrc");
+    INIConfigurationWriter writer = new INIConfigurationWriter();
+
+    writer.write(iniConfig, hgrc);
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param name
+   *
+   * @return
+   */
+  private File getDirectory(String name)
+  {
+    return new File(config.getRepositoryDirectory(), name);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param repository
+   *
+   * @return
+   */
+  private File getDirectory(Repository repository)
+  {
+    return getDirectory(repository.getName());
+  }
+
   //~--- fields ---------------------------------------------------------------
 
   /** Field description */

File scm-core/src/main/java/sonia/scm/util/Util.java

View file
 //~--- JDK imports ------------------------------------------------------------
 
 import java.io.Closeable;
+import java.io.File;
 import java.io.IOException;
 
 import java.util.Collection;
     }
   }
 
+  /**
+   *   Method description
+   *
+   *
+   *   @param file
+   *
+   *   @throws IOException
+   */
+  public static void delete(File file) throws IOException
+  {
+    if (file.isDirectory())
+    {
+      File[] children = file.listFiles();
+
+      if (children != null)
+      {
+        for (File child : children)
+        {
+          delete(child);
+        }
+      }
+    }
+
+    if (!file.delete())
+    {
+      throw new IOException("could not delete file ".concat(file.getPath()));
+    }
+  }
+
   //~--- get methods ----------------------------------------------------------
 
   /**