view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommand.java @ 117:345fe6e101b7

Fix problem with locales different from en_US. Set --style parameter for log command to default. Also remove test that use wrong revision number and doesn't check anything.
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Mon, 23 Aug 2010 20:30:17 +0400
parents 6c1cff1f61cc
children 86d5b641ef63 bcc04a74783d
line wrap: on
line source
/*
 * Copyright 2000-2010 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.diagnostic.Logger;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class LogCommand extends BaseCommand {
  private final static Logger LOG = Logger.getInstance(LogCommand.class.getName());
  private String myFromId;
  private String myToId;
  private ArrayList<String> myPaths;
  private static final String CHANGESET_PREFIX = "changeset:";
  private static final String USER_PREFIX = "user:";
  private static final String PARENT_PREFIX = "parent:";
  private static final String DATE_PREFIX = "date:";
  private static final String DATE_FORMAT = "EEE MMM d HH:mm:ss yyyy Z";
  private static final String DESCRIPTION_PREFIX = "description:";
  private static final String FILES_PREFIX = "files:";

  public LogCommand(@NotNull Settings settings) {
    super(settings);
  }

  public void setFromRevId(String id) {
    myFromId = id;
  }

  public void setToRevId(String id) {
    myToId = id;
  }

  public void setPaths(final List<String> relPaths) {
    myPaths = new ArrayList<String>(relPaths);
  }

  public List<ChangeSet> execute() throws VcsException {
    GeneralCommandLine cli = createCommandLine();
    cli.addParameter("log");
    cli.addParameter("-v");
    cli.addParameter("--style");
    cli.addParameter("default");
    cli.addParameter("-b");
    cli.addParameter(getSettings().getBranchName());
    cli.addParameter("-r");
    String from = myFromId;
    if (from == null) from = "0";
    String to = myToId;
    if (to == null) to = "tip";
    cli.addParameter(from + ":" + to);
    if (myPaths != null) {
      for (String path: myPaths) {
        cli.addParameter(path);
      }
    }

    ExecResult res = runCommand(cli);
    return parseChangeSets(res.getStdout());
  }

  public static List<ChangeSet> parseChangeSets(final String stdout) {
    List<ChangeSet> result = new ArrayList<ChangeSet>();
    String[] lines = stdout.split("\n");
    ChangeSet current = null;
    int lineNum = 0;
    boolean insideDescription = false;
    StringBuilder descr = new StringBuilder();
    while (lineNum < lines.length) {
      String line = lines[lineNum];
      lineNum++;

      if (line.startsWith(CHANGESET_PREFIX)) {
        insideDescription = false;
        if (current != null) {
          current.setDescription(descr.toString().trim());
          descr.setLength(0);
        }

        String revAndId = line.substring(CHANGESET_PREFIX.length()).trim();
        try {
          current = new ChangeSet(revAndId);
          result.add(current);
        } catch (IllegalArgumentException e) {
          LOG.warn("Unable to extract changeset id from the line: " + line);
        }

        continue;
      }

      if (current == null) continue;

      if (line.startsWith(USER_PREFIX)) {
        current.setUser(line.substring(USER_PREFIX.length()).trim());
        continue;
      }

      if (line.startsWith(FILES_PREFIX)) {
        current.setContainsFiles(true);
        continue;
      }

      if (line.startsWith(PARENT_PREFIX)) {
        String parentRev = line.substring(PARENT_PREFIX.length()).trim();
        current.addParent(new ChangeSetRevision(parentRev));
        continue;
      }

      if (line.startsWith(DATE_PREFIX)) {
        String date = line.substring(DATE_PREFIX.length()).trim();
        try {
          Date parsedDate = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH).parse(date);
          current.setTimestamp(parsedDate);
        } catch (ParseException e) {
          LOG.warn("Unable to parse date: " + date);
          current = null;
        }

        continue;
      }

      if (line.startsWith(DESCRIPTION_PREFIX)) {
        insideDescription = true;
        continue;
      }

      if (insideDescription) {
        descr.append(line).append("\n");
      }
    }

    if (insideDescription) {
      current.setDescription(descr.toString().trim());
    }

    return result;
  }

}