1 package org.apache.maven.scm.provider.perforce.command.changelog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37
38
39
40 public class PerforceDescribeConsumer
41 extends AbstractConsumer
42 {
43
44 private List<ChangeSet> entries = new ArrayList<ChangeSet>();
45
46
47
48
49 private static final int GET_REVISION = 1;
50
51
52
53
54 private static final int GET_COMMENT_BEGIN = 2;
55
56
57
58
59 private static final int GET_COMMENT = 3;
60
61
62
63
64 private static final int GET_AFFECTED_FILES = 4;
65
66
67
68
69 private static final int GET_FILES_BEGIN = 5;
70
71
72
73
74 private static final int GET_FILE = 6;
75
76
77
78
79 private int status = GET_REVISION;
80
81
82
83
84 @SuppressWarnings( "unused" )
85 private String currentRevision;
86
87
88
89
90 private ChangeSet currentChange;
91
92
93
94
95 private String currentFile;
96
97
98
99
100
101 private String repoPath;
102
103 private String userDatePattern;
104
105
106
107
108 private static final Pattern REVISION_PATTERN = Pattern.compile( "^Change (\\d+) " +
109 "by (.*)@[^ ]+ " +
110 "on (.*)" );
111
112
113
114 private static final String COMMENT_DELIMITER = "";
115
116
117
118 private static final String CHANGELIST_DELIMITER = "";
119
120
121
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
144
145
146
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
180
181
182
183
184
185
186 private void addEntry( ChangeSet entry, ChangeFile file )
187 {
188 entry.addFile( file );
189 }
190
191
192
193
194
195
196 private void processGetFile( String line )
197 {
198 if ( line.equals( CHANGELIST_DELIMITER ) ) {
199 entries.add( 0, currentChange );
200 status = GET_REVISION;
201 return;
202 }
203
204 Matcher matcher = FILE_PATTERN.matcher( line );
205 if ( !matcher.find() )
206 {
207 return;
208 }
209
210 currentFile = matcher.group( 1 );
211
212
213
214
215 if( currentFile.startsWith( repoPath ) ) {
216 currentFile = currentFile.substring( repoPath.length() + 1 );
217 addEntry( currentChange, new ChangeFile( currentFile, matcher.group( 2 ) ) );
218 }
219 }
220
221
222
223
224
225
226
227 private void processGetRevision( String line )
228 {
229 Matcher matcher = REVISION_PATTERN.matcher( line );
230 if ( !matcher.find() )
231 {
232 return;
233 }
234 currentChange = new ChangeSet();
235 currentRevision = matcher.group( 1 );
236 currentChange.setAuthor( matcher.group( 2 ) );
237 currentChange.setDate( matcher.group( 3 ), userDatePattern );
238
239 status = GET_COMMENT_BEGIN;
240 }
241
242
243
244
245
246
247
248 private void processGetComment( String line )
249 {
250 if ( line.equals( COMMENT_DELIMITER ) )
251 {
252 status = GET_AFFECTED_FILES;
253 }
254 else
255 {
256
257 currentChange.setComment( currentChange.getComment() + line.substring(1) + "\n" );
258 }
259 }
260
261
262
263
264
265
266
267 private void processGetAffectedFiles( String line )
268 {
269 if ( !line.equals( "Affected files ..." ) )
270 {
271 return;
272 }
273 status = GET_FILES_BEGIN;
274 }
275 }