View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.unedit;
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.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.command.unedit.AbstractUnEditCommand;
26  import org.apache.maven.scm.command.unedit.UnEditScmResult;
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.repository.PerforceScmProviderRepository;
31  import org.codehaus.plexus.util.cli.CommandLineException;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  import java.util.List;
37  
38  /**
39   * @author Mike Perham
40   * @author Olivier Lamy
41   *
42   */
43  public class PerforceUnEditCommand
44      extends AbstractUnEditCommand
45      implements PerforceCommand
46  {
47  
48      /** {@inheritDoc} */
49      protected ScmResult executeUnEditCommand( ScmProviderRepository repo, ScmFileSet files )
50          throws ScmException
51      {
52          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
53          PerforceUnEditConsumer consumer = new PerforceUnEditConsumer();
54          try
55          {
56              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
57              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
58  
59              if ( exitCode != 0 )
60              {
61                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
62  
63                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
64                  msg.append( '\n' );
65                  msg.append( "Command line was:" + cmdLine );
66  
67                  throw new CommandLineException( msg.toString() );
68              }
69          }
70          catch ( CommandLineException e )
71          {
72              if ( getLogger().isErrorEnabled() )
73              {
74                  getLogger().error( "CommandLineException " + e.getMessage(), e );
75              }
76          }
77  
78          if ( consumer.isSuccess() )
79          {
80              return new UnEditScmResult( cl.toString(), consumer.getEdits() );
81          }
82  
83          return new UnEditScmResult( cl.toString(), "Unable to revert", consumer.getOutput(), consumer.isSuccess() );
84      }
85  
86      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
87                                                   ScmFileSet files )
88      {
89          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
90  
91          command.createArg().setValue( "revert" );
92  
93          List<File> fs = files.getFileList();
94          for ( File file : fs )
95          {
96              command.createArg().setValue( file.getName() );
97          }
98          return command;
99      }
100 }