View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.changelog;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ChangeFile;
23  import org.apache.maven.scm.ChangeSet;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.log.ScmLogger;
26  import org.apache.maven.scm.util.AbstractConsumer;
27  
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.LinkedHashMap;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.regex.Matcher;
34  import java.util.regex.Pattern;
35  
36  /**
37   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
38   *
39   */
40  public class PerforceChangeLogConsumer
41      extends AbstractConsumer
42  {
43      /**
44       * Date formatter for perforce timestamp
45       */
46      private static final String PERFORCE_TIMESTAMP_PATTERN = "yyyy/MM/dd HH:mm:ss";
47  
48      private List<ChangeSet> entries = new ArrayList<ChangeSet>();
49  
50      /**
51       * State machine constant: expecting revision and/or file information
52       */
53      private static final int GET_REVISION = 1;
54  
55      /**
56       * State machine constant: eat the first blank line
57       */
58      private static final int GET_COMMENT_BEGIN = 2;
59  
60      /**
61       * State machine constant: expecting comments
62       */
63      private static final int GET_COMMENT = 3;
64  
65      /**
66       * The comment section ends with a blank line
67       */
68      private static final String COMMENT_DELIMITER = "";
69  
70      /**
71       * A file line begins with two slashes
72       */
73      private static final String FILE_BEGIN_TOKEN = "//";
74  
75      /**
76       * Current status of the parser
77       */
78      private int status = GET_REVISION;
79  
80      /**
81       * The current log entry being processed by the parser
82       */
83      private ChangeSet currentChange;
84  
85      /**
86       * the current file being processed by the parser
87       */
88      private String currentFile;
89  
90      /**
91       * The location of files within the Perforce depot that we are processing
92       * e.g. //depot/projects/foo/bar
93       */
94      private String repoPath;
95  
96      private Date startDate;
97  
98      private Date endDate;
99  
100     private String userDatePattern;
101 
102     /**
103      * The regular expression used to match header lines
104      */
105     private static final Pattern PATTERN = Pattern.compile( "^\\.\\.\\. #(\\d+) " + // revision number
106         "change (\\d+) .* " + // changelist number
107         "on (.*) " + // date
108         "by (.*)@" ); // author
109 
110     public PerforceChangeLogConsumer( String path, Date startDate, Date endDate, String userDatePattern,
111                                       ScmLogger logger )
112     {
113         super( logger );
114 
115         this.startDate = startDate;
116         this.endDate = endDate;
117         this.userDatePattern = userDatePattern;
118         this.repoPath = path;
119     }
120 
121     // ----------------------------------------------------------------------
122     //
123     // ----------------------------------------------------------------------
124 
125     public List<ChangeSet> getModifications() throws ScmException
126     {
127     	
128     	// Here there are one entry for each couple (changelist,file). We merge
129     	// entries to have only one entry per changelist
130     	
131     	// Date > ChangeSet
132         Map<Date,ChangeSet> groupedEntries = new LinkedHashMap<Date,ChangeSet>();
133         for ( int i = 0; i < entries.size(); i++ )
134         {
135             ChangeSet cs = (ChangeSet) entries.get( i );
136             ChangeSet hit = (ChangeSet) groupedEntries.get( cs.getDate() );
137             if ( hit != null )
138             {
139                 if ( cs.getFiles().size() != 1 )
140                 {
141                     throw new ScmException( "Merge of entries failed. Bad entry size: " + cs.getFiles().size() );
142                 }
143                 hit.addFile( (ChangeFile) cs.getFiles().get( 0 ) );
144             }
145             else
146             {
147                 groupedEntries.put( cs.getDate(), cs );
148             }
149         }
150 
151         List<ChangeSet> result = new ArrayList<ChangeSet>();
152         result.addAll( groupedEntries.values() );
153 
154         return result;
155     	
156     }
157 
158     // ----------------------------------------------------------------------
159     // StreamConsumer Implementation
160     // ----------------------------------------------------------------------
161 
162     /** {@inheritDoc} */
163     public void consumeLine( String line )
164     {
165         switch ( status )
166         {
167             case GET_REVISION:
168                 processGetRevision( line );
169                 break;
170             case GET_COMMENT_BEGIN:
171                 status = GET_COMMENT;
172                 break;
173             case GET_COMMENT:
174                 processGetComment( line );
175                 break;
176             default:
177                 throw new IllegalStateException( "Unknown state: " + status );
178         }
179     }
180 
181     // ----------------------------------------------------------------------
182     //
183     // ----------------------------------------------------------------------
184 
185     /**
186      * Add a change log entry to the list (if it's not already there)
187      * with the given file.
188      *
189      * @param entry a {@link ChangeSet} to be added to the list if another
190      *              with the same key (p4 change number) doesn't exist already.
191      * @param file  a {@link ChangeFile} to be added to the entry
192      */
193     private void addEntry( ChangeSet entry, ChangeFile file )
194     {
195         // ----------------------------------------------------------------------
196         // Check that we are inside the requested date range
197         // ----------------------------------------------------------------------
198 
199         if ( startDate != null && entry.getDate().before( startDate ) )
200         {
201             return;
202         }
203 
204         if ( endDate != null && entry.getDate().after( endDate ) )
205         {
206             return;
207         }
208 
209         // ----------------------------------------------------------------------
210         //
211         // ----------------------------------------------------------------------
212 
213         entry.addFile( file );
214 
215         entries.add( entry );
216     }
217 
218     /**
219      * Most of the relevant info is on the revision line matching the
220      * 'pattern' string.
221      *
222      * @param line A line of text from the perforce log output
223      */
224     private void processGetRevision( String line )
225     {
226         if ( line.startsWith( FILE_BEGIN_TOKEN ) )
227         {
228             currentFile = line.substring( repoPath.length() + 1 );
229             return;
230         }
231 
232         Matcher matcher = PATTERN.matcher( line );
233         if ( !matcher.find() )
234         {
235             return;
236         }
237 
238         currentChange = new ChangeSet();
239         currentChange.setRevision( matcher.group( 1 ));
240         currentChange.setDate( parseDate( matcher.group( 3 ), userDatePattern, PERFORCE_TIMESTAMP_PATTERN ) );
241         currentChange.setAuthor( matcher.group( 4 ) );
242 
243         status = GET_COMMENT_BEGIN;
244     }
245 
246     /**
247      * Process the current input line in the GET_COMMENT state.  This
248      * state gathers all of the comments that are part of a log entry.
249      *
250      * @param line a line of text from the perforce log output
251      */
252     private void processGetComment( String line )
253     {
254         if ( line.equals( COMMENT_DELIMITER ) )
255         {
256             addEntry( currentChange, new ChangeFile( currentFile, currentChange.getRevision() ) );
257 
258             status = GET_REVISION;
259         }
260         else
261         {
262             currentChange.setComment( currentChange.getComment() + line + "\n" );
263         }
264     }
265 }