You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm-commits@maven.apache.org by ev...@apache.org on 2003/12/06 17:55:19 UTC

cvs commit: maven-scm/generic-scm-providers/perforce/src/java/org/apache/maven/genericscm/provider/perforce/command/changelog PerforceChangeLogCommand.java PerforceChangeLogConsumer.java

evenisse    2003/12/06 08:55:19

  Modified:    generic-scm-providers/perforce project.xml
               generic-scm-providers/starteam/src/java/org/apache/maven/genericscm/provider/starteam/command/changelog
                        StarteamChangeLogCommand.java
                        StarteamChangeLogConsumer.java
  Added:       generic-scm-providers/perforce/src/java/org/apache/maven/genericscm/provider/perforce/command/changelog
                        PerforceChangeLogCommand.java
                        PerforceChangeLogConsumer.java
  Log:
  Add the ChangeLog command
  
  Revision  Changes    Path
  1.2       +5 -0      maven-scm/generic-scm-providers/perforce/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/maven-scm/generic-scm-providers/perforce/project.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- project.xml	6 Dec 2003 14:25:58 -0000	1.1
  +++ project.xml	6 Dec 2003 16:55:19 -0000	1.2
  @@ -37,6 +37,11 @@
         <artifactId>genericscm-api</artifactId>
         <version>1.0-beta1-SNAPSHOT</version>
       </dependency>
  +    <dependency>
  +      <groupId>regexp</groupId>
  +      <artifactId>regexp</artifactId>
  +      <version>1.2</version>
  +    </dependency>
     </dependencies>
   
     <build>
  
  
  
  1.4       +6 -1      maven-scm/generic-scm-providers/starteam/src/java/org/apache/maven/genericscm/provider/starteam/command/changelog/StarteamChangeLogCommand.java
  
  Index: StarteamChangeLogCommand.java
  ===================================================================
  RCS file: /home/cvs/maven-scm/generic-scm-providers/starteam/src/java/org/apache/maven/genericscm/provider/starteam/command/changelog/StarteamChangeLogCommand.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StarteamChangeLogCommand.java	5 Dec 2003 18:10:36 -0000	1.3
  +++ StarteamChangeLogCommand.java	6 Dec 2003 16:55:19 -0000	1.4
  @@ -121,6 +121,11 @@
        */
       public void setRange(int numDays)
       {
  +        if (consumer != null && consumer instanceof StarteamChangeLogConsumer)
  +        {
  +            ((StarteamChangeLogConsumer)consumer).setDateRange(numDays);
  +        }
  +        
           setStartDate(
               new Date(
                   System.currentTimeMillis()
  
  
  
  1.2       +4 -6      maven-scm/generic-scm-providers/starteam/src/java/org/apache/maven/genericscm/provider/starteam/command/changelog/StarteamChangeLogConsumer.java
  
  Index: StarteamChangeLogConsumer.java
  ===================================================================
  RCS file: /home/cvs/maven-scm/generic-scm-providers/starteam/src/java/org/apache/maven/genericscm/provider/starteam/command/changelog/StarteamChangeLogConsumer.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- StarteamChangeLogConsumer.java	3 Dec 2003 21:23:49 -0000	1.1
  +++ StarteamChangeLogConsumer.java	6 Dec 2003 16:55:19 -0000	1.2
  @@ -370,15 +370,13 @@
        * Set the beforeDate and toDate member based on the number of days
        * obtained from the ChangeLog.
        *
  -     * @param numDaysString The number of days of log output to
  +     * @param numDays The number of days of log output to
        * generate.
        */
  -    private void setDateRange(String numDaysString)
  +    public void setDateRange(int numDays)
       {
  -        int days = Integer.parseInt(numDaysString);
  -
           beforeDate = new Date(
  -            System.currentTimeMillis() - (long) days * 24 * 60 * 60 * 1000);
  +            System.currentTimeMillis() - (long) numDays * 24 * 60 * 60 * 1000);
           toDate = new Date(
               System.currentTimeMillis() + (long) 1 * 24 * 60 * 60 * 1000);
       }
  
  
  
  1.1                  maven-scm/generic-scm-providers/perforce/src/java/org/apache/maven/genericscm/provider/perforce/command/changelog/PerforceChangeLogCommand.java
  
  Index: PerforceChangeLogCommand.java
  ===================================================================
  package org.apache.maven.genericscm.provider.perforce.command.changelog;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-userName documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import org.apache.maven.genericscm.ScmException;
  import org.apache.maven.genericscm.command.changelog.ChangeLogCommand;
  import org.apache.maven.genericscm.command.changelog.ChangeLogConsumer;
  import org.apache.maven.genericscm.provider.perforce.command.AbstractPerforceCommand;
  import org.apache.maven.genericscm.provider.perforce.repository.PerforceRepository;
  import org.apache.maven.genericscm.util.Commandline;
  import org.apache.maven.genericscm.util.StreamConsumer;
  
  /**
   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
   * @version $Id: PerforceChangeLogCommand.java,v 1.1 2003/12/06 16:55:19 evenisse Exp $
   */
  public class PerforceChangeLogCommand
      extends AbstractPerforceCommand
      implements ChangeLogCommand
  {
  	private ChangeLogConsumer consumer;
      private Date startDate;
      private Date endDate;
  
      public PerforceChangeLogCommand() throws ScmException
      {
          setConsumer(new PerforceChangeLogConsumer());
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogCommand#setStartDate(java.util.Date)
       */
      public void setStartDate(Date startDate)
      {
          this.startDate = startDate;
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogCommand#getStartDate()
       */
      public Date getStartDate()
      {
          return startDate;
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogCommand#setEndDate(java.util.Date)
       */
      public void setEndDate(Date endDate)
      {
          this.endDate = endDate;
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogCommand#getEndDate()
       */
      public Date getEndDate()
      {
          return endDate;
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogCommand#setRange(int)
       */
      public void setRange(int numDays)
      {
          if (consumer != null && consumer instanceof PerforceChangeLogConsumer)
          {
              ((PerforceChangeLogConsumer)consumer).setDateRange(numDays);
          }
          
          setStartDate(
              new Date(
                  System.currentTimeMillis()
                      - (long)numDays * 24 * 60 * 60 * 1000));
          setEndDate(
              new Date(
                  System.currentTimeMillis() + (long)1 * 24 * 60 * 60 * 1000));
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.Command#getName()
       */
      public String getName()
      {
          return NAME;
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.Command#getDisplayName()
       */
      public String getDisplayName() throws Exception
      {
          return "ChangeLog";
      }
  
      public Commandline getCommandLine() throws ScmException
      {
          Commandline command = new Commandline();
          command.setExecutable("p4");
          
  		if (getWorkingDirectory() != null)
  		{
  			command.setWorkingDirectory(getWorkingDirectory());
  		}
  		
  		PerforceRepository repo = (PerforceRepository)getRepository();
          if (repo.getHost() != null
              && repo.getPort() != null) {
              String p4port = repo.getHost() + ":" + repo.getPort();
              command.createArgument().setValue("-p");
              command.createArgument().setValue(p4port);
          }
          command.createArgument().setValue("filelog");
          command.createArgument().setValue("-tl");
          command.createArgument().setValue(repo.getPath());
          
          return command;
      }
      
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.Command#setConsumer(org.apache.maven.genericscm.util.StreamConsumer)
       */
      public void setConsumer(StreamConsumer consumer) throws ScmException
      {
  		if (consumer instanceof ChangeLogConsumer)
  		{
  			this.consumer = (ChangeLogConsumer)consumer;
  		}
  		else
  		{
  			throw new ScmException("Unsupported consumer for this command");
  		}
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.Command#getConsumer()
       */
      public StreamConsumer getConsumer()
      {
          return consumer;
      }
  }
  
  
  1.1                  maven-scm/generic-scm-providers/perforce/src/java/org/apache/maven/genericscm/provider/perforce/command/changelog/PerforceChangeLogConsumer.java
  
  Index: PerforceChangeLogConsumer.java
  ===================================================================
  package org.apache.maven.genericscm.provider.perforce.command.changelog;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-userName documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Maven" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Maven", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  import java.text.SimpleDateFormat;
  import java.text.ParseException;
  import java.util.Date;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Collections;
  import java.util.Map;
  import java.util.TreeMap;
  
  import org.apache.maven.genericscm.command.changelog.ChangeLogConsumer;
  import org.apache.maven.genericscm.command.changelog.ChangeLogEntry;
  import org.apache.maven.genericscm.command.changelog.ChangeLogFile;
  
  import org.apache.regexp.RE;
  import org.apache.regexp.RESyntaxException;
  
  /**
   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
   * @version $Id: PerforceChangeLogConsumer.java,v 1.1 2003/12/06 16:55:19 evenisse Exp $
   */
  public class PerforceChangeLogConsumer implements ChangeLogConsumer
  {
      /** Date formatter for perforce timestamp */
      private static final SimpleDateFormat PERFORCE_TIMESTAMP =
          new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  
      /**
       * RCS entries, in reverse changelist number order
       */
      private Map entries = new TreeMap(Collections.reverseOrder());
  
      /** State machine constant: expecting revision and/or file information */
      private static final int GET_REVISION = 1;
  
      /** State machine constant: eat the first blank line */
      private static final int GET_COMMENT_BEGIN = 2;
  
      /** State machine constant: expecting comments */
      private static final int GET_COMMENT = 3;
  
      /** The comment section ends with a blank line */
      private static final String COMMENT_DELIMITER = "";
  
      /** A file line begins with two slashes */
      private static final String FILE_BEGIN_TOKEN = "//";
  
      /** Current status of the parser */
      private int status = GET_REVISION;
      
      /** The current log entry being processed by the parser */
      private ChangeLogEntry currentLogEntry;
  
      /** the current file being processed by the parser */
      private String currentFile;
      
      /** The regular expression used to match header lines */
      private RE revisionRegexp;
  
      /** the before date */
      private Date beforeDate;
  
      /** the to date */
      private Date toDate;
  
      private static final String pattern =
          "^\\.\\.\\. #(\\d+) " +           // revision number
          "change (\\d+) .* " +             // changelist number
          "on (.*) " +                      // date 
          "by (.*)@";                       // author
  
      /** 
       * Default constructor.
       */
      public PerforceChangeLogConsumer()
      {
          try
          {
              revisionRegexp = new RE(pattern);
          }
          catch (RESyntaxException ignored) {
              //LOG.error("Could not create regexp to parse perforce log file", ignored);
          }
      }
      
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.command.changelog.ChangeLogConsumer#getModifications()
       */
      public Collection getModifications()
      {
          return entries.values();
      }
  
      /* (non-Javadoc)
       * @see org.apache.maven.genericscm.util.StreamConsumer#consumeLine(java.lang.String)
       */
      public void consumeLine(String line)
      {
          switch (status)
          {
              case GET_REVISION:
                  processGetRevision(line);
                  break;
              case GET_COMMENT_BEGIN:
                  status = GET_COMMENT;
                  break;
              case GET_COMMENT:
                  processGetComment(line);
                  break;
              default:
                  throw new IllegalStateException("Unknown state: " + status);
          }
      }
      
      /**
       * Add a change log entry to the list (if it's not already there)
       * with the given file.
       * @param entry a {@link ChangeLogEntry} to be added to the list if another
       *      with the same key (p4 change number) doesn't exist already.
       * @param file a {@link ChangeLogFile} to be added to the entry
       */
      private void addEntry(ChangeLogEntry entry, ChangeLogFile file)
      {
          System.out.println();
          System.out.println(entry.toXML());
          if (beforeDate != null) {
              if (entry.getDate().before(beforeDate) || entry.getDate().after(toDate)) {
                  return;
              }
          }
          Integer key = new Integer(revisionRegexp.getParen(2));
          if (!entries.containsKey(key)) {
              entry.addFile(file);
              entries.put(key, entry);
          } else {
              ChangeLogEntry existingEntry = (ChangeLogEntry) entries.get(key);
              existingEntry.addFile(file);
          }
      }
   
      /**
       * Most of the relevant info is on the revision line matching the
       * 'pattern' string.
       *
       * @param line A line of text from the perforce log output
       */
      private void processGetRevision(String line) 
      {
          if (line.startsWith(FILE_BEGIN_TOKEN)) {
              currentFile = line;
              return;
          }
          
          if (!revisionRegexp.match(line)) {
              return;
          }
  
          currentLogEntry = new ChangeLogEntry();
          currentLogEntry.setDate(parseDate(revisionRegexp.getParen(3)));
          currentLogEntry.setAuthor(revisionRegexp.getParen(4));
  
          status = GET_COMMENT_BEGIN;
      }
  
      /**
       * Process the current input line in the GET_COMMENT state.  This
       * state gathers all of the comments that are part of a log entry.
       *
       * @param line a line of text from the perforce log output
       */
      private void processGetComment(String line)
      {
          if (line.equals(COMMENT_DELIMITER)) {
              addEntry(currentLogEntry, new ChangeLogFile(currentFile, revisionRegexp.getParen(1)));
              status = GET_REVISION;
          } else {
              currentLogEntry.setComment(currentLogEntry.getComment() + line + "\n");
          }
      }
  
      /** 
       * Converts the date timestamp from the perforce output into a date
       * object.
       * 
       * @return A date representing the timestamp of the log entry.
       */
      private Date parseDate(String date)
      {
          try
          {
              return PERFORCE_TIMESTAMP.parse(date);
          }
          catch (ParseException e) {
              //LOG.error("ParseException Caught", e);
              return null;        
          }
      }
      
      /**
       * Set the beforeDate and toDate member based on the number of days
       * obtained from the ChangeLog.
       *
       * @param numDays The number of days of log output to
       * generate.
       */
      public void setDateRange(int numDays)
      {
          beforeDate = new Date(
              System.currentTimeMillis() - (long) numDays * 24 * 60 * 60 * 1000);
          toDate = new Date(
              System.currentTimeMillis() + (long) 1 * 24 * 60 * 60 * 1000);
      }
  }