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 java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import org.apache.maven.scm.ChangeFile;
28  import org.apache.maven.scm.ChangeSet;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.log.ScmLogger;
31  import org.apache.maven.scm.util.AbstractConsumer;
32  
33  /**
34   * Parse the tagged output from "p4 describe -s [change] [change] [...]".
35   *
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   * @author Olivier Lamy
38   *
39   */
40  public class PerforceDescribeConsumer
41      extends AbstractConsumer
42  {
43      
44      private List<ChangeSet> entries = new ArrayList<ChangeSet>();
45  
46      /**
47       * State machine constant: expecting revision
48       */
49      private static final int GET_REVISION = 1;
50  
51      /**
52       * State machine constant: eat the first blank line
53       */
54      private static final int GET_COMMENT_BEGIN = 2;
55  
56      /**
57       * State machine constant: expecting comments
58       */
59      private static final int GET_COMMENT = 3;
60  
61      /**
62       * State machine constant: expecting "Affected files"
63       */
64      private static final int GET_AFFECTED_FILES = 4;
65  
66      /**
67       * State machine constant: expecting blank line
68       */
69      private static final int GET_FILES_BEGIN = 5;
70  
71      /**
72       * State machine constant: expecting files
73       */
74      private static final int GET_FILE = 6;
75  
76      /**
77       * Current status of the parser
78       */
79      private int status = GET_REVISION;
80  
81      /**
82       * The current log entry being processed by the parser
83       */
84      @SuppressWarnings( "unused" )
85      private String currentRevision;
86  
87      /**
88       * The current log entry being processed by the parser
89       */
90      private ChangeSet currentChange;
91  
92      /**
93       * the current file being processed by the parser
94       */
95      private String currentFile;
96  
97      /**
98       * The location of files within the Perforce depot that we are processing
99       * e.g. //depot/projects/foo/bar
100      */
101     private String repoPath;
102 
103     private String userDatePattern;
104 
105     /**
106      * The regular expression used to match header lines
107      */
108     private static final Pattern REVISION_PATTERN = Pattern.compile( "^Change (\\d+) " + // changelist number
109         "by (.*)@[^ ]+ " + // author
110         "on (.*)" ); // date
111     /**
112      * The comment section ends with a blank line
113      */
114     private static final String COMMENT_DELIMITER = "";
115     /**
116      * The changelist ends with a blank line
117      */
118     private static final String CHANGELIST_DELIMITER = "";
119 
120     /**
121      * The regular expression used to match file paths
122      */
123     private static final Pattern FILE_PATTERN = Pattern.compile( "^\\.\\.\\. (.*)#(\\d+) " );
124 
125     public PerforceDescribeConsumer( String repoPath, String userDatePattern, ScmLogger logger )
126     {
127         super( logger );
128 
129         this.repoPath = repoPath;
130         this.userDatePattern = userDatePattern;
131     }
132 
133     // ----------------------------------------------------------------------
134     //
135     // ----------------------------------------------------------------------
136 
137     public List<ChangeSet> getModifications() throws ScmException
138     {
139         return entries;
140     }
141 
142     // ----------------------------------------------------------------------
143     // StreamConsumer Implementation
144     // ----------------------------------------------------------------------
145 
146     /** {@inheritDoc} */
147     public void consumeLine( String line )
148     {
149         switch ( status )
150         {
151             case GET_REVISION:
152                 processGetRevision( line );
153                 break;
154             case GET_COMMENT_BEGIN:
155                 status = GET_COMMENT;
156                 break;
157             case GET_COMMENT:
158                 processGetComment( line );
159                 break;
160             case GET_AFFECTED_FILES:
161                 processGetAffectedFiles( line );
162                 break;
163             case GET_FILES_BEGIN:
164                 status = GET_FILE;
165                 break;
166             case GET_FILE:
167                 processGetFile( line );
168                 break;
169             default:
170                 throw new IllegalStateException( "Unknown state: " + status );
171         }
172     }
173 
174     // ----------------------------------------------------------------------
175     //
176     // ----------------------------------------------------------------------
177 
178     /**
179      * Add a change log entry to the list (if it's not already there)
180      * with the given file.
181      *
182      * @param entry a {@link ChangeSet} to be added to the list if another
183      *              with the same key (p4 change number) doesn't exist already.
184      * @param file  a {@link ChangeFile} to be added to the entry
185      */
186     private void addEntry( ChangeSet entry, ChangeFile file )
187     {
188         entry.addFile( file );
189     }
190 
191     /**
192      * Each file matches the fileRegexp.
193      *
194      * @param line A line of text from the Perforce log output
195      */
196     private void processGetFile( String line )
197     {
198         if ( line.equals( CHANGELIST_DELIMITER ) )
199         {
200             entries.add( 0, currentChange );
201             status = GET_REVISION;
202             return;
203         }
204 
205         Matcher matcher = FILE_PATTERN.matcher( line );
206         if ( !matcher.find() )
207         {
208             return;
209         }
210 
211         currentFile = matcher.group( 1 );
212 
213         // Although Perforce allows files to be submitted anywhere in the
214         // repository in a single changelist, we're only concerned about the
215         // local files.
216         if ( currentFile.startsWith( repoPath ) )
217         {
218             currentFile = currentFile.substring( repoPath.length() + 1 );
219             addEntry( currentChange, new ChangeFile( currentFile, matcher.group( 2 ) ) );
220         }
221     }
222 
223     /**
224      * Most of the relevant info is on the revision line matching the
225      * 'pattern' string.
226      *
227      * @param line A line of text from the perforce log output
228      */
229     private void processGetRevision( String line )
230     {
231         Matcher matcher = REVISION_PATTERN.matcher( line );
232         if ( !matcher.find() )
233         {
234             return;
235         }
236         currentChange = new ChangeSet();
237         currentRevision = matcher.group( 1 );
238         currentChange.setAuthor( matcher.group( 2 ) );
239         currentChange.setDate( matcher.group( 3 ), userDatePattern );
240 
241         status = GET_COMMENT_BEGIN;
242     }
243 
244     /**
245      * Process the current input line in the GET_COMMENT state.  This
246      * state gathers all of the comments that are part of a log entry.
247      *
248      * @param line a line of text from the perforce log output
249      */
250     private void processGetComment( String line )
251     {
252         if ( line.equals( COMMENT_DELIMITER ) )
253         {
254             status = GET_AFFECTED_FILES;
255         }
256         else
257         {
258             // remove prepended tab
259             currentChange.setComment( currentChange.getComment() + line.substring( 1 ) + "\n" );
260         }
261     }
262 
263     /**
264      * Process the current input line in the GET_COMMENT state.  This
265      * state gathers all of the comments that are part of a log entry.
266      *
267      * @param line a line of text from the perforce log output
268      */
269     private void processGetAffectedFiles( String line )
270     {
271         if ( !line.equals( "Affected files ..." ) )
272         {
273             return;
274         }
275         status = GET_FILES_BEGIN;
276     }
277 }