001package org.apache.maven.scm.provider.perforce.command.status;
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.ScmException;
023import org.apache.maven.scm.ScmFile;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.command.status.AbstractStatusCommand;
026import org.apache.maven.scm.command.status.StatusScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
029import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
030import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
031import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineException;
033import org.codehaus.plexus.util.cli.CommandLineUtils;
034import org.codehaus.plexus.util.cli.Commandline;
035
036import java.io.File;
037import java.util.ArrayList;
038import java.util.Iterator;
039import java.util.List;
040import java.util.regex.Matcher;
041import java.util.regex.Pattern;
042
043/**
044 * @author Mike Perham
045 *
046 */
047public class PerforceStatusCommand
048    extends AbstractStatusCommand
049    implements PerforceCommand
050{
051    private String actualLocation;
052
053    /** {@inheritDoc} */
054    protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet files )
055        throws ScmException
056    {
057        PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
058        actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() );
059        PerforceStatusConsumer consumer = new PerforceStatusConsumer();
060        Commandline command = readOpened( prepo, files, consumer );
061
062        if ( consumer.isSuccess() )
063        {
064            List<ScmFile> scmfiles = createResults( actualLocation, consumer );
065            return new StatusScmResult( command.toString(), scmfiles );
066        }
067
068        return new StatusScmResult( command.toString(), "Unable to get status", consumer
069                .getOutput(), consumer.isSuccess() );
070    }
071
072    public static List<ScmFile> createResults( String repoPath, PerforceStatusConsumer consumer )
073    {
074        List<ScmFile> results = new ArrayList<ScmFile>();
075        List<String> files = consumer.getDepotfiles();
076        Pattern re = Pattern.compile( "([^#]+)#\\d+ - ([^ ]+) .*" );
077        for ( Iterator<String> it = files.iterator(); it.hasNext(); )
078        {
079            String filepath = it.next();
080            Matcher matcher = re.matcher( filepath );
081            if ( !matcher.matches() )
082            {
083                System.err.println( "Skipping " + filepath );
084                continue;
085            }
086            String path = matcher.group( 1 );
087            String verb = matcher.group( 2 );
088
089            ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper
090                .toStatus( verb ) );
091            results.add( scmfile );
092        }
093        return results;
094    }
095
096    private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files,
097                                    PerforceStatusConsumer consumer )
098    {
099        Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation );
100        try
101        {
102            if ( getLogger().isDebugEnabled() )
103            {
104                getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
105            }
106
107            CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
108            int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
109
110            if ( exitCode != 0 )
111            {
112                String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
113
114                StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
115                msg.append( '\n' );
116                msg.append( "Command line was:" + cmdLine );
117
118                throw new CommandLineException( msg.toString() );
119            }
120        }
121        catch ( CommandLineException e )
122        {
123            if ( getLogger().isErrorEnabled() )
124            {
125                getLogger().error( "CommandLineException " + e.getMessage(), e );
126            }
127        }
128
129        return cl;
130    }
131
132    public static Commandline createOpenedCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
133                                                       String location )
134    {
135        Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
136        command.createArg().setValue( "opened" );
137        command.createArg().setValue( PerforceScmProvider.getCanonicalRepoPath( location ) );
138        return command;
139    }
140}