1 package org.apache.maven.scm.provider.perforce.command.checkout;
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.ScmFile;
23 import org.apache.maven.scm.ScmFileStatus;
24 import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
25 import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
26 import org.codehaus.plexus.util.cli.StreamConsumer;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 /**
34 * @author Mike Perham
35 * @version $Id: PerforceChangeLogConsumer.java 331276 2005-11-07 15:04:54Z
36 * evenisse $
37 */
38 public class PerforceCheckOutConsumer
39 extends AbstractPerforceConsumer
40 implements StreamConsumer
41 {
42 public static final int STATE_CLIENTSPEC = 0;
43
44 public static final int STATE_NORMAL = 1;
45
46 public static final int STATE_ERROR = 2;
47
48 private int currentState = STATE_CLIENTSPEC;
49
50 private Pattern fileRegexp = Pattern.compile( "([^#]+)#\\d+ - ([a-z]+)" );
51
52 private List<ScmFile> checkedout = new ArrayList<ScmFile>();
53
54 private String repo = null;
55
56 private String specname = null;
57
58 public PerforceCheckOutConsumer( String clientspec, String repoPath )
59 {
60 repo = repoPath;
61 specname = clientspec;
62 }
63
64 /*
65 * Client mperham-mikeperham-dt-maven saved.
66 */
67 /*
68 * //depot/modules/cordoba/runtime-ear/.j2ee#1 - deleted as
69 * d:\perforce\depot\modules\cordoba\runtime-ear\.j2ee
70 * //depot/modules/cordoba/runtime-ear/.project#1 - deleted as
71 * d:\perforce\depot\modules\cordoba\runtime-ear\.project
72 * //depot/modules/cordoba/runtime-ear/.runtime#1 - deleted as
73 * d:\perforce\depot\modules\cordoba\runtime-ear\.runtime
74 * //depot/modules/cordoba/runtime-ear/Foo.java#1 - deleted as
75 * d:\perforce\depot\modules\cordoba\runtime-ear\Foo.java
76 * //depot/modules/cordoba/runtime-ear/META-INF/.modulemaps#1 - deleted as
77 * d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\.modulemaps
78 * //depot/modules/cordoba/runtime-ear/META-INF/application.xml#1 - deleted
79 * as d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\application.xml
80 * //depot/modules/cordoba/runtime-ear/pom.xml#4 - deleted as
81 * d:\perforce\depot\modules\cordoba\runtime-ear\pom.xml
82 */
83 /*
84 * Invalid changelist/client/label/date '@somelabel'.
85 */
86 /** {@inheritDoc} */
87 public void consumeLine( String line )
88 {
89 if ( currentState == STATE_CLIENTSPEC
90 && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
91 + " not changed." ) ) )
92 {
93 currentState = STATE_NORMAL;
94 return;
95 }
96
97 // Handle case where the clientspec is current
98 if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
99 {
100 return;
101 }
102
103 Matcher matcher;
104 if ( currentState != STATE_ERROR && ( matcher = fileRegexp.matcher( line ) ).find() )
105 {
106 String location = matcher.group( 1 );
107 if ( location.startsWith( repo ) )
108 {
109 location = location.substring( repo.length() + 1 );
110 }
111 ScmFileStatus status = PerforceVerbMapper.toStatus( matcher.group( 2 ) );
112 if ( status != null )
113 {
114 // there are cases where Perforce prints out something but the file did not
115 // actually change (especially when force syncing). Those files will have
116 // a null status.
117 checkedout.add( new ScmFile( location, status ) );
118 }
119 return;
120 }
121
122 error( line );
123 }
124
125 private void error( String line )
126 {
127 currentState = STATE_ERROR;
128 output.println( line );
129 }
130
131 public boolean isSuccess()
132 {
133 return currentState == STATE_NORMAL;
134 }
135
136 public List<ScmFile> getCheckedout()
137 {
138 return checkedout;
139 }
140 }