view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java @ 719:59a3b5ef91a9

drop legacy
author eugene.petrenko@jetbrains.com
date Mon, 13 Jan 2014 19:42:22 +0100
parents 0e21f908b10d
children 31a1aca3305c
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 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, myCommandSettings.setFailWhenStderrNotEmpty(true));
    String output = res.getStdout().trim();
    if (myShowBranch) {
      return output;
    } else {
      return output.contains(" ") ? output.substring(0, output.indexOf(" ")) : output;
    }
  }
}