view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/LogCommand.java @ 86:948a35b430e1 Darjeeling-5.0.x

report parent changesets
author Pavel.Sher
date Tue, 08 Dec 2009 19:49:28 +0300
parents 99e757f2527b
children 5d9c34cb543a
line wrap: on
line source
/*
 * Copyright 2000-2007 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 SUMMARY_PREFIX = "summary:";

  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("-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;                  
    while (lineNum < lines.length) {
      String line = lines[lineNum];
      lineNum++;

      if (line.startsWith(CHANGESET_PREFIX)) {
        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(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(SUMMARY_PREFIX)) {
        String summary = line.substring(SUMMARY_PREFIX.length()).trim();
        current.setSummary(summary);

        continue;
      }
    }

    return result;
  }

}