view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SubRepo.java @ 717:c37903906fad

add new test
author eugene.petrenko@jetbrains.com
date Mon, 13 Jan 2014 19:20:39 +0100
parents 03a544f9eae1
children d1469a7cc038 9e4c8cd92838
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import org.jetbrains.annotations.NotNull;

import java.net.URI;
import java.net.URISyntaxException;

/**
* @author dmitry.neverov
*/
public class SubRepo {
  private final String myPath;
  private final String myUrl;
  private final String myRevision;
  private final VcsType myVcsType;

  public SubRepo(@NotNull String path, @NotNull String url, @NotNull String revision) {
    myPath = path;
    myUrl = url;
    myRevision = revision;
    myVcsType = parseVcsType();
  }

  @NotNull
  public String path() {
    return myPath;
  }

  @NotNull
  public String url() {
    return myUrl;
  }

  @NotNull
  public String revision() {
    if (myRevision.length() > 12)
      return myRevision.substring(0, 12);
    return myRevision;
  }

  @NotNull
  public VcsType vcsType() {
    return myVcsType;
  }

  public boolean hasDifferentUrlThan(@NotNull SubRepo other) {
    return !myUrl.equals(other.url());
  }

  public String resolveUrl(@NotNull String parentRepoUrl) throws URISyntaxException {
    if (!parentRepoUrl.endsWith("/"))
      parentRepoUrl = parentRepoUrl + "/";
    try {
      URI parentURI = new URI(parentRepoUrl);
      URI subrepoAbsUrl = parentURI.resolve(url());
      if (isSsh(subrepoAbsUrl) && isPathFromRoot(parentURI))
        return getUrlWithPathFromRoot(subrepoAbsUrl);
      return subrepoAbsUrl.toString();
    } catch (URISyntaxException e) {
      return parentRepoUrl + url();
    } catch (IllegalArgumentException e) {
      return url();
    }
  }

  private boolean isSsh(@NotNull URI uri) {
    return "ssh".equals(uri.getScheme());
  }

  private boolean isPathFromRoot(@NotNull URI uri) {
    return uri.getPath() != null && uri.getPath().startsWith("//");
  }

  @NotNull
  private String getUrlWithPathFromRoot(@NotNull URI uri) throws URISyntaxException {
    return new URI(uri.getScheme(),
            uri.getUserInfo(),
            uri.getHost(),
            uri.getPort(),
            "/" + uri.getPath(),
            uri.getQuery(),
            uri.getFragment()).toString();
  }

  @Override
  public String toString() {
    return myPath + " = " + myUrl + "#" + myRevision;
  }

  private VcsType parseVcsType() {
    if (myUrl.startsWith("[svn]"))
      return VcsType.svn;
    if (myUrl.startsWith("[git]"))
      return VcsType.git;
    return VcsType.hg;
  }

  public static enum VcsType {
    hg, git, svn
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (!(o instanceof SubRepo))
      return false;

    SubRepo subRepo = (SubRepo) o;

    if (!myPath.equals(subRepo.myPath))
      return false;
    if (!myRevision.equals(subRepo.myRevision))
      return false;
    if (!myUrl.equals(subRepo.myUrl))
      return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = myPath.hashCode();
    result = 31 * result + myUrl.hashCode();
    result = 31 * result + myRevision.hashCode();
    return result;
  }
}