View Javadoc

1   package org.apache.maven.scm.provider.perforce.command.status;
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.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.command.status.AbstractStatusCommand;
26  import org.apache.maven.scm.command.status.StatusScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
29  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
30  import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
31  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
32  import org.apache.regexp.RE;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  import java.io.File;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * @author Mike Perham
44   * @version $Id: PerforceStatusCommand.java 1306867 2012-03-29 13:45:10Z olamy $
45   */
46  public class PerforceStatusCommand
47      extends AbstractStatusCommand
48      implements PerforceCommand
49  {
50      private String actualLocation;
51  
52      /** {@inheritDoc} */
53      protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet files )
54          throws ScmException
55      {
56          PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
57          actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() );
58          PerforceStatusConsumer consumer = new PerforceStatusConsumer();
59          Commandline command = readOpened( prepo, files, consumer );
60  
61          if ( consumer.isSuccess() )
62          {
63              List<ScmFile> scmfiles = createResults( actualLocation, consumer );
64              return new StatusScmResult( command.toString(), scmfiles );
65          }
66  
67          return new StatusScmResult( command.toString(), "Unable to get status", consumer
68                  .getOutput(), consumer.isSuccess() );
69      }
70  
71      public static List<ScmFile> createResults( String repoPath, PerforceStatusConsumer consumer )
72      {
73          List<ScmFile> results = new ArrayList<ScmFile>();
74          List<String> files = consumer.getDepotfiles();
75          RE re = new RE( "([^#]+)#\\d+ - ([^ ]+) .*" );
76          for ( Iterator<String> it = files.iterator(); it.hasNext(); )
77          {
78              String filepath = it.next();
79              if ( !re.match( filepath ) )
80              {
81                  System.err.println( "Skipping " + filepath );
82                  continue;
83              }
84              String path = re.getParen( 1 );
85              String verb = re.getParen( 2 );
86  
87              ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper
88                  .toStatus( verb ) );
89              results.add( scmfile );
90          }
91          return results;
92      }
93  
94      private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files,
95                                      PerforceStatusConsumer consumer )
96      {
97          Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation );
98          try
99          {
100             if ( getLogger().isDebugEnabled() )
101             {
102                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
103             }
104 
105             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
106             int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
107 
108             if ( exitCode != 0 )
109             {
110                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
111 
112                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
113                 msg.append( '\n' );
114                 msg.append( "Command line was:" + cmdLine );
115 
116                 throw new CommandLineException( msg.toString() );
117             }
118         }
119         catch ( CommandLineException e )
120         {
121             if ( getLogger().isErrorEnabled() )
122             {
123                 getLogger().error( "CommandLineException " + e.getMessage(), e );
124             }
125         }
126 
127         return cl;
128     }
129 
130     public static Commandline createOpenedCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
131                                                        String location )
132     {
133         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
134         command.createArg().setValue( "opened" );
135         command.createArg().setValue( PerforceScmProvider.getCanonicalRepoPath( location ) );
136         return command;
137     }
138 }