view mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/Util.java @ 1068:2d8aab94494f Lakhnau-2020.2.x release-86063

TW-70693
author victory.bedrosova
date Thu, 18 Mar 2021 18:30:34 +0100
parents 7bf4d943d5bb
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.TempFiles;
import jetbrains.buildServer.util.FileUtil;
import com.intellij.openapi.util.SystemInfo;
import jetbrains.buildServer.vcs.CheckoutRules;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import jetbrains.buildServer.vcs.patches.PatchBuilderImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

/**
 * @author dmitry.neverov
 */
public final class Util {

  private Util() {}

  public static String getHgPath() throws IOException {
    String providedHg = System.getenv(Constants.HG_PATH_ENV);
    if (providedHg != null)
      return providedHg;
    if (SystemInfo.isWindows)
      return new File("mercurial-tests/testData/bin/hg.exe").getAbsolutePath();
    return "hg"; //assume it is somethere in the $PATH
  }


  public static File copyRepository(@NotNull TempFiles tempFiles, @NotNull String repPath) throws IOException {
    File tempDir = tempFiles.createTempDir();
    copyRepository(new File(repPath), tempDir);
    return tempDir;
  }

  public static void copyRepository(@NotNull File src, @NotNull File dst) throws IOException {
    FileUtil.copyDir(src, dst);
    if (new File(dst, "hg").isDirectory())
      FileUtil.rename(new File(dst, "hg"), new File(dst, ".hg"));
  }

  public static ByteArrayOutputStream buildPatch(@NotNull MercurialVcsSupport vcs,
                                                 @NotNull VcsRoot vcsRoot,
                                                 @Nullable String from,
                                                 @NotNull String to,
                                                 @NotNull CheckoutRules rules) throws IOException, VcsException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    PatchBuilderImpl builder = new PatchBuilderImpl(output);
    vcs.buildPatch(vcsRoot, from, to, builder, rules);
    builder.close();
    return output;
  }
}