001package org.apache.maven.scm.provider.perforce.command.checkout;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileStatus;
024import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
025import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
026import org.codehaus.plexus.util.cli.StreamConsumer;
027
028import java.util.ArrayList;
029import java.util.List;
030import java.util.regex.Matcher;
031import java.util.regex.Pattern;
032
033/**
034 * @author Mike Perham
035 */
036public class PerforceCheckOutConsumer
037    extends AbstractPerforceConsumer
038    implements StreamConsumer
039{
040    public static final int STATE_CLIENTSPEC = 0;
041
042    public static final int STATE_NORMAL = 1;
043
044    public static final int STATE_ERROR = 2;
045
046    private int currentState = STATE_CLIENTSPEC;
047
048    private Pattern fileRegexp = Pattern.compile( "([^#]+)#\\d+ - ([a-z]+)" );
049
050    private List<ScmFile> checkedout = new ArrayList<ScmFile>();
051
052    private String repo = null;
053
054    private String specname = null;
055
056    public PerforceCheckOutConsumer( String clientspec, String repoPath )
057    {
058        repo = repoPath;
059        specname = clientspec;
060    }
061
062    /*
063     * Client mperham-mikeperham-dt-maven saved.
064     */
065    /*
066     * //depot/modules/cordoba/runtime-ear/.j2ee#1 - deleted as
067     * d:\perforce\depot\modules\cordoba\runtime-ear\.j2ee
068     * //depot/modules/cordoba/runtime-ear/.project#1 - deleted as
069     * d:\perforce\depot\modules\cordoba\runtime-ear\.project
070     * //depot/modules/cordoba/runtime-ear/.runtime#1 - deleted as
071     * d:\perforce\depot\modules\cordoba\runtime-ear\.runtime
072     * //depot/modules/cordoba/runtime-ear/Foo.java#1 - deleted as
073     * d:\perforce\depot\modules\cordoba\runtime-ear\Foo.java
074     * //depot/modules/cordoba/runtime-ear/META-INF/.modulemaps#1 - deleted as
075     * d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\.modulemaps
076     * //depot/modules/cordoba/runtime-ear/META-INF/application.xml#1 - deleted
077     * as d:\perforce\depot\modules\cordoba\runtime-ear\META-INF\application.xml
078     * //depot/modules/cordoba/runtime-ear/pom.xml#4 - deleted as
079     * d:\perforce\depot\modules\cordoba\runtime-ear\pom.xml
080     */
081    /*
082     * Invalid changelist/client/label/date '@somelabel'.
083     */
084    /** {@inheritDoc} */
085    public void consumeLine( String line )
086    {
087        if ( currentState == STATE_CLIENTSPEC
088            && ( line.startsWith( "Client " + specname + " saved." ) || line.startsWith( "Client " + specname
089                + " not changed." ) ) )
090        {
091            currentState = STATE_NORMAL;
092            return;
093        }
094
095        // Handle case where the clientspec is current
096        if ( currentState == STATE_NORMAL && line.indexOf( "ile(s) up-to-date" ) != -1 )
097        {
098            return;
099        }
100
101        Matcher matcher;
102        if ( currentState != STATE_ERROR && ( matcher = fileRegexp.matcher( line ) ).find() )
103        {
104            String location = matcher.group( 1 );
105            if ( location.startsWith( repo ) )
106            {
107                location = location.substring( repo.length() + 1 );
108            }
109            ScmFileStatus status = PerforceVerbMapper.toStatus( matcher.group( 2 ) );
110            if ( status != null )
111            {
112                // there are cases where Perforce prints out something but the file did not
113                // actually change (especially when force syncing).  Those files will have
114                // a null status.
115                checkedout.add( new ScmFile( location, status ) );
116            }
117            return;
118        }
119
120        error( line );
121    }
122
123    private void error( String line )
124    {
125        currentState = STATE_ERROR;
126        output.println( line );
127    }
128
129    public boolean isSuccess()
130    {
131        return currentState == STATE_NORMAL;
132    }
133
134    public List<ScmFile> getCheckedout()
135    {
136        return checkedout;
137    }
138}