view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SubRepo.java @ 615:7ea82d063131 Gaya-8.0.x

TW-30589 fix create temp dir race condition
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Thu, 01 Aug 2013 11:53:48 +0400
parents 109d7d3cdc8f
children 03a544f9eae1
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());
      return subrepoAbsUrl.toString();
    } catch (URISyntaxException e) {
      return parentRepoUrl + url();
    } catch (IllegalArgumentException e) {
      return url();
    }
  }

  @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;
  }
}