view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java @ 349:e0464f11206c

TW-19698 Handle unrelated repositories When repository becames unrelated - clone it in different directory on the server. When changes are collected and any of revisions is from unrelated repository - return empty changes collection.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Thu, 12 Jan 2012 18:21:07 +0400
parents 4a49a0baf30b
children 061e5f3a6bad
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.util.StringUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author pavel
 */
public class BaseCommand {

  private final String myHgPath;
  private final File myWorkDirectory;

  public BaseCommand(@NotNull final String hgPath, @NotNull File workingDir) {
    myHgPath = hgPath;
    myWorkDirectory = workingDir;
  }


  public File getWorkDirectory() {
    return myWorkDirectory;
  }

  protected GeneralCommandLine createCommandLine() {
    GeneralCommandLine cli = createCL();
    cli.setWorkDirectory(myWorkDirectory.getAbsolutePath());
    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.
   * @param cli command line in which to setup hg executable
   */
  private void setupExecutable(GeneralCommandLine cli) {
    if (SystemInfo.isWindows && myHgPath.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(myHgPath);
  }

  protected CommandResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
    return CommandUtil.runCommand(cli, Collections.<String>emptySet());
  }

  protected CommandResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
    return CommandUtil.runCommand(cli, executionTimeout, Collections.<String>emptySet());
  }

  protected void failIfNotEmptyStdErr(@NotNull CommandResult res) throws VcsException {
    if (!StringUtil.isEmpty(res.getStderr())) {
      CommandUtil.commandFailed(res);
    }
  }
}