view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java @ 338:d31d7c81b637 Eluru-6.5.x

Support subrepositories while building clean patch. Use 'hg clone' instead of 'hg archive' if repository contains subrepositories.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 02 Dec 2011 15:12:49 +0300
parents f5092b982bdb
children b799355b4016 e8f0eb6d4ca4
line wrap: on
line source
/*
 * Copyright 2000-2011 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.command;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Collections;
import java.util.Set;

/**
 * @author pavel
 */
public class BaseCommand {
  private Settings mySettings;
  private String myWorkDirectory;

  public BaseCommand(@NotNull final Settings settings, @NotNull File workingDir) {
    mySettings = settings;
    myWorkDirectory = workingDir.getAbsolutePath();
  }


  public Settings getSettings() {
    return mySettings;
  }

  /**
   * Sets new working directory, by default working directory is taken from the Settings#getLocalRepositoryDir
   * @param workDirectory work dir
   */
  public void setWorkDirectory(final String workDirectory) {
    myWorkDirectory = workDirectory;
  }

  public String getWorkDirectory() {
    return myWorkDirectory;
  }

  protected GeneralCommandLine createCommandLine() {
    GeneralCommandLine cli = createCL();
    cli.setWorkDirectory(myWorkDirectory);
    cli.setPassParentEnvs(true);
    return cli;
  }

  protected GeneralCommandLine createCL() {
    GeneralCommandLine cl = new EscapingCommandLineDecorator();
    setupExecutable(cl);
    return cl;
  }

  /**
   * Escape its parameters
   */
  private static class EscapingCommandLineDecorator extends GeneralCommandLine {
    @Override
    public void addParameter(@NotNull String parameter) {
      String escaped = escape(parameter);
      super.addParameter(escaped);
    }

    private String escape(String s) {
      return StringUtil.escapeQuotesIfWindows(s);
    }
  }

  /**
   * Since mercurial 1.7 on Windows the only file inside '<mercurial_install_dir>/bin' is 'hg.cmd'
   * which run hg.exe placed in the parent dir. GeneralCommandLine will not find hg.cmd, in the
   * case when $PATH contains <mercurial_install_dir>/bin and doesn't contain <mercurial_install_dir>
   * and hg executable is set to 'hg'. To fix it - run hg using windows shell which expand
   * hg to hg.cmd correctly.
   */
  private void setupExecutable(GeneralCommandLine cli) {
    if (SystemInfo.isWindows && getSettings().getHgCommandPath().equals("hg")) {
      setupCmd(cli);
    } else {
      setupHg(cli);
    }
  }

  private void setupCmd(GeneralCommandLine cli) {
    cli.setExePath("cmd");
    cli.addParameter("/c");
    cli.addParameter("hg");
  }

  private void setupHg(GeneralCommandLine cli) {
    cli.setExePath(getSettings().getHgCommandPath());
  }

  protected ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
    return CommandUtil.runCommand(cli, getPrivateData());
  }

  protected ExecResult runCommand(@NotNull GeneralCommandLine cli, boolean logErrorsInDebug) throws VcsException {
    return CommandUtil.runCommand(cli, CommandUtil.DEFAULT_COMMAND_TIMEOUT_SEC, getPrivateData(), logErrorsInDebug);
  }

  protected ExecResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
    return CommandUtil.runCommand(cli, executionTimeout, getPrivateData());
  }

  protected void failIfNotEmptyStdErr(@NotNull GeneralCommandLine cli, @NotNull ExecResult res) throws VcsException {
    if (!StringUtil.isEmpty(res.getStderr())) {
      CommandUtil.commandFailed(cli.getCommandLineString(), res);
    }
  }

  public Set<String> getPrivateData() {
    return Collections.singleton(mySettings.getPassword());
  }
}