view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/ArchiveCommand.java @ 890:771ae1b2f0b1

More command descriptions
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 07 Nov 2014 14:30:28 +0100
parents 24a6ef9166ae
children 448ef0521954
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 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 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 addIncludePathRule(@NotNull String rule) {
    final MercurialCommandLine cmd = createCmd();

    String pathRule = "path:" + rule;
    final int cmdSize = cmd.getCommandLineLength();
    if (cmdSize + pathRule.length() + 3 + (myIncludeRules.isEmpty() ? 0 : "-I ".length()) > myCommandSettings.getMaxCommandLineSize()) {
      return false;
    }

    myIncludeRules.add(pathRule);
    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();
  }

  @NotNull
  private MercurialCommandLine createCmd() {
    final MercurialCommandLine cli = createCommandLine();

    addHttpAuthParams(cli);

    cli.addParameter("archive");
    cli.addParameter("-t");
    cli.addParameter(myType);
    cli.addParameter("-r");

    if (myToId != null) {
      cli.addParameter(myToId);
    } else {
      cli.addParameter("tip");
    }

    for (String include : myIncludeRules) {
      cli.addParameter("-I");
      cli.addParameter(include);
    }
    cli.addParameter(myDestination.getAbsolutePath());

    return cli;
  }

  /**
   * 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);
  }

  @NotNull
  @Override
  protected String getDescription() {
    return "hg archive -r " + (myToId != null ? myToId : "tip");
  }
}