view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java @ 979:2b1bd4bca6ad Indore-2017.2.x

TW-50054 support custom clone path whitelist
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Wed, 24 Jan 2018 13:49:01 +0100
parents 38adef4f1b8f
children 6b69c7a12f76
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.command;

import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;

/**
 * @author Pavel.Sher
 *         Date: 16.07.2008
 */
public class IdentifyCommand extends AuthCommand {

  private boolean myInLocalRepository = false;
  private ChangeSet myChangeSet;
  private Integer myRevisionNumber;
  private AuthSettings myAuthSettings;
  private String myRepositoryUrl;
  private String myNamedRevision;
  private boolean myShowBranch = false;

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

  public IdentifyCommand namedRevision(@NotNull String name) {
    myNamedRevision = name;
    return this;
  }

  public IdentifyCommand revision(@NotNull String revision) {
    myChangeSet = new ChangeSet(revision);
    return this;
  }

  public IdentifyCommand revision(@NotNull ChangeSet cset) {
    myChangeSet = cset;
    return this;
  }

  public IdentifyCommand inLocalRepository() {
    myInLocalRepository = true;
    return this;
  }

  public IdentifyCommand revisionNumber(int revisionNumber) {
    myRevisionNumber = revisionNumber;
    return this;
  }

  public IdentifyCommand withAuthSettings(@NotNull AuthSettings authSettings) {
    myAuthSettings = authSettings;
    return this;
  }

  public IdentifyCommand repository(@NotNull String repositoryUrl) {
    myRepositoryUrl = repositoryUrl;
    return this;
  }

  public IdentifyCommand showBranch() {
    myShowBranch = true;
    return this;
  }

  public String call() throws VcsException {
    MercurialCommandLine cli = createCL();
    cli.addParameter("identify");
    if (myInLocalRepository) {
      cli.setWorkDirectory(this.getWorkDirectory().getAbsolutePath());
    } else {
      cli.addParameter(myAuthSettings.getRepositoryUrlWithCredentials(myRepositoryUrl));
    }
    if (myShowBranch) {
      cli.addParameter("-b");
    }
    if (myChangeSet != null) {
      cli.addParameter("--rev");
      cli.addParameter(myChangeSet.getId());
    } else if (myRevisionNumber != null) {
      cli.addParameter("--rev");
      cli.addParameter(myRevisionNumber.toString());
    } else if (myNamedRevision != null) {
      cli.addParameter("--rev");
      cli.addParameter(myNamedRevision);
    }
    CommandResult res = runCommand(cli);
    String output = res.getRawStdout().trim();
    if (myShowBranch) {
      return output;
    } else {
      return output.contains(" ") ? output.substring(0, output.indexOf(" ")) : output;
    }
  }

  @NotNull
  @Override
  protected String getDescription() {
    StringBuilder result = new StringBuilder("hg id");
    if (myChangeSet != null) {
      result.append(" --rev ").append(myChangeSet.getId());
    } else if (myRevisionNumber != null) {
      result.append(" --rev ").append(myRevisionNumber.toString());
    } else if (myNamedRevision != null) {
      result.append(" --rev ").append(myNamedRevision);
    }
    return result.toString();
  }
}