view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/SubRepo.java @ 1124:a14a2f7e74d8 development/2024.03.x tip

2024.03.x branch is created
author Nadia Burnasheva <nadezhda.burnasheva@jetbrains.com>
date Thu, 15 Feb 2024 11:33:35 +0100
parents 10dc26b32c35
children
line wrap: on
line source
/*
 * Copyright 2000-2018 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).trim();
    } else if (url.startsWith("[git]")) {
      myVcsType = VcsType.git;
      myUrl = url.substring(5).trim();
    } else {
      myVcsType = VcsType.hg;
      myUrl = url.trim();
    }
  }

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

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

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

  public String fullRevision() {
    return myRevision;
  }

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

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

  @NotNull
  public String resolveUrl(@NotNull final String parentRepoUrl) throws WrongSubrepoUrlException {
    try {
      URI parentURI = parentRepoUrl.endsWith("/") ? new URI(parentRepoUrl) : 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, FileUtil.normalizeSeparator(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 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;
  }
}