view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ArchiveCommand.java @ 781:3cd449e58f70 Gaya-8.1.x

Hg arch and cat commands can require an authentication in case of largefiles
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Wed, 16 Apr 2014 18:12:04 +0200
parents 31a1aca3305c
children f0327df85b9d
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.
 */

/*
 * 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 jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ArchiveCommand extends AuthCommand {
  private final static int MAX_CMD_LEN = 900;

  private File myDestination;
  private String myToId;
  private String myType = "files";
  private List<String> myIncludeRules = new ArrayList<String>();

  public ArchiveCommand(@NotNull CommandSettings commandSettings,
                        @NotNull String hgPath,
                        @NotNull File workingDir,
                        @NotNull AuthSettings authSettings) {
    super(commandSettings, hgPath, workingDir, authSettings);
  }

  public ArchiveCommand destination(@NotNull File destination) {
    myDestination = destination;
    return this;
  }

  public ArchiveCommand revision(@NotNull ChangeSet cset) {
    myToId = cset.getId();
    return this;
  }

  public ArchiveCommand type(@NotNull String type) {
    myType = type;
    return this;
  }

  public boolean addIncludeRule(@NotNull String rule) {
    MercurialCommandLine cmd = createCmd();
    int cmdSize = cmd.getCommandLineString().length();
    if (cmdSize + rule.length() + (myIncludeRules.isEmpty() ? 0 : "-I ".length()) > MAX_CMD_LEN)
      return false;
    myIncludeRules.add(rule);
    return true;
  }

  public void call() throws VcsException {
    if (myDestination == null)
      throw new IllegalStateException("Destination dir must be specified");
    MercurialCommandLine cli = createCmd();

    runCommand(cli, myCommandSettings.setFailWhenStderrNotEmpty(true));
    deleteHgArchival();
  }

  private MercurialCommandLine createCmd() {
    MercurialCommandLine cli = createCommandLine();
    addHttpAuthParams(cli);
    cli.addParameter("archive");
    setType(cli);
    setRevision(cli);
    setDestination(cli);
    addIncludeRules(cli);
    return cli;
  }

  private void addIncludeRules(@NotNull MercurialCommandLine cli) {
    if (myIncludeRules.isEmpty())
      return;
    cli.addParameter("-I");
    cli.addParameters(myIncludeRules);
  }

  private void setDestination(GeneralCommandLine cli) {
    cli.addParameter(myDestination.getAbsolutePath());
  }

  private void setRevision(GeneralCommandLine cli) {
    cli.addParameter("-r");
    if (myToId != null) {
      cli.addParameter(myToId);
    } else {
      cli.addParameter("tip");
    }
  }

  private void setType(GeneralCommandLine cli) {
    cli.addParameter("-t");
    cli.addParameter(myType);
  }

  /**
   * hg archive generates .hg_archival.txt, delete it since original repository do not have such file
   */
  private void deleteHgArchival() {
    if (myDestination == null)
      throw new IllegalStateException("Destination dir must be specified");
    File hg_arhival = new File(myDestination, ".hg_archival.txt");
    FileUtil.delete(hg_arhival);
  }
}