View Javadoc

1   package org.apache.maven.scm.provider.perforce.command.update;
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.CommandParameter;
23  import org.apache.maven.scm.CommandParameters;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
28  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
30  import org.apache.maven.scm.command.update.UpdateScmResult;
31  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
34  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
35  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
36  import org.apache.maven.scm.provider.perforce.command.changelog.PerforceChangeLogCommand;
37  import org.apache.maven.scm.provider.perforce.command.checkout.PerforceCheckOutCommand;
38  
39  import org.codehaus.plexus.util.cli.CommandLineException;
40  import org.codehaus.plexus.util.cli.CommandLineUtils;
41  import org.codehaus.plexus.util.cli.Commandline;
42  
43  import java.io.File;
44  
45  /**
46   * @author Mike Perham
47   *
48   */
49  public class PerforceUpdateCommand
50      extends AbstractUpdateCommand
51      implements PerforceCommand
52  {
53      /** {@inheritDoc} */
54      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet files,
55                                                      ScmVersion scmVersion )
56          throws ScmException
57      {
58          // In Perforce, there is no difference between update and checkout.
59          // Here we just run the checkout command and map the result onto an
60          // UpdateScmResult.
61          PerforceCheckOutCommand command = new PerforceCheckOutCommand();
62          command.setLogger( getLogger() );
63          CommandParameters params = new CommandParameters();
64          params.setScmVersion( CommandParameter.SCM_VERSION, scmVersion );
65  
66          CheckOutScmResult cosr = (CheckOutScmResult) command.execute( repo, files, params );
67          if ( !cosr.isSuccess() )
68          {
69              return new UpdateScmResult( cosr.getCommandLine(), cosr.getProviderMessage(), cosr.getCommandOutput(),
70                                          false );
71          }
72  
73          PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
74          String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, files.getBasedir() );
75          Commandline cl = createCommandLine( p4repo, files.getBasedir(), clientspec );
76  
77          @SuppressWarnings( "unused" )
78          String location = PerforceScmProvider.getRepoPath( getLogger(), p4repo, files.getBasedir() );
79          PerforceHaveConsumer consumer =
80              new PerforceHaveConsumer( getLogger() );
81  
82          try
83          {
84              if ( getLogger().isDebugEnabled() )
85              {
86                  getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
87              }
88  
89              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
90              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
91  
92              if ( exitCode != 0 )
93              {
94                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
95  
96                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
97                  msg.append( '\n' );
98                  msg.append( "Command line was:" + cmdLine );
99  
100                 throw new CommandLineException( msg.toString() );
101             }
102         }
103         catch ( CommandLineException e )
104         {
105             if ( getLogger().isErrorEnabled() )
106             {
107                 getLogger().error( "CommandLineException " + e.getMessage(), e );
108             }
109         }
110 
111         return new UpdateScmResultWithRevision( cosr.getCommandLine(), cosr.getCheckedOutFiles(),
112                                                 String.valueOf( consumer.getHave() ) );
113     }
114 
115     /** {@inheritDoc} */
116     protected ChangeLogCommand getChangeLogCommand()
117     {
118         PerforceChangeLogCommand command = new PerforceChangeLogCommand();
119         command.setLogger( getLogger() );
120         return command;
121     }
122 
123     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
124                                                  String clientspec )
125     {
126         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
127 
128         if ( clientspec != null )
129         {
130             command.createArg().setValue( "-c" );
131             command.createArg().setValue( clientspec );
132         }
133         command.createArg().setValue( "changes" );
134         command.createArg().setValue( "-m1" );
135         command.createArg().setValue( "-ssubmitted" );
136         command.createArg().setValue( "//" + clientspec + "/...#have" );
137 
138 	return command;
139     }
140 }