view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SubRepo.java @ 733:5211baefb5ec

Handle windows paths in subrepo resolving
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Thu, 16 Jan 2014 13:56:33 +0100
parents 31a1aca3305c
children a75aae928f79
line wrap: on
line source
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.WrongSubrepoUrlException;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
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;
    myRevision = revision;

    if (url.startsWith("[svn]")) {
      myVcsType = VcsType.svn;
      myUrl = url.substring(5);
    } else if (url.startsWith("[git]")) {
      myVcsType = VcsType.git;
      myUrl = url.substring(5);
    } else {
      myVcsType = VcsType.hg;
      myUrl = url;
    }
  }

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

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

  @NotNull
  public String revision() {
    return HgRepo.shortId(myRevision);
  }

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

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

  @NotNull
  public String resolveUrl(@NotNull String parentRepoUrl) throws WrongSubrepoUrlException {
    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 (Exception e) {
      File parentRepoDir = new File(parentRepoUrl);
      if (parentRepoDir.isDirectory()) {//handle windows paths
        try {
          return FileUtil.resolvePath(parentRepoDir, url()).getCanonicalPath();
        } catch (IOException e1) {
          throw new WrongSubrepoUrlException(parentRepoUrl, url(), e);
        }
      }
      throw new WrongSubrepoUrlException(parentRepoUrl, url(), e);
    }
  }

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

  public static enum VcsType {
    hg(Constants.VCS_NAME), git("jetbrains.git"), svn("svn")
    ;

    private final String myVcsPluginName;

    VcsType(@NotNull String vcsPluginName) {
      myVcsPluginName = vcsPluginName;
    }

    @NotNull
    public String getVcsPluginName() {
      return myVcsPluginName;
    }

  }

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