001package org.apache.maven.scm.provider.perforce.command.unedit; 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.ScmFileSet; 024import org.apache.maven.scm.ScmResult; 025import org.apache.maven.scm.command.unedit.AbstractUnEditCommand; 026import org.apache.maven.scm.command.unedit.UnEditScmResult; 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.repository.PerforceScmProviderRepository; 031import org.codehaus.plexus.util.cli.CommandLineException; 032import org.codehaus.plexus.util.cli.CommandLineUtils; 033import org.codehaus.plexus.util.cli.Commandline; 034 035import java.io.File; 036import java.util.List; 037 038/** 039 * @author Mike Perham 040 * @author Olivier Lamy 041 * 042 */ 043public class PerforceUnEditCommand 044 extends AbstractUnEditCommand 045 implements PerforceCommand 046{ 047 048 /** {@inheritDoc} */ 049 protected ScmResult executeUnEditCommand( ScmProviderRepository repo, ScmFileSet files ) 050 throws ScmException 051 { 052 Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files ); 053 PerforceUnEditConsumer consumer = new PerforceUnEditConsumer(); 054 try 055 { 056 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); 057 int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err ); 058 059 if ( exitCode != 0 ) 060 { 061 String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); 062 063 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); 064 msg.append( '\n' ); 065 msg.append( "Command line was:" + cmdLine ); 066 067 throw new CommandLineException( msg.toString() ); 068 } 069 } 070 catch ( CommandLineException e ) 071 { 072 if ( getLogger().isErrorEnabled() ) 073 { 074 getLogger().error( "CommandLineException " + e.getMessage(), e ); 075 } 076 } 077 078 if ( consumer.isSuccess() ) 079 { 080 return new UnEditScmResult( cl.toString(), consumer.getEdits() ); 081 } 082 083 return new UnEditScmResult( cl.toString(), "Unable to revert", consumer.getOutput(), consumer.isSuccess() ); 084 } 085 086 public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory, 087 ScmFileSet files ) 088 { 089 Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory ); 090 091 command.createArg().setValue( "revert" ); 092 093 List<File> fs = files.getFileList(); 094 for ( File file : fs ) 095 { 096 command.createArg().setValue( file.getName() ); 097 } 098 return command; 099 } 100}