001 package 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 022 import org.apache.maven.scm.ScmException; 023 import org.apache.maven.scm.ScmFile; 024 import org.apache.maven.scm.ScmFileSet; 025 import org.apache.maven.scm.command.status.AbstractStatusCommand; 026 import org.apache.maven.scm.command.status.StatusScmResult; 027 import org.apache.maven.scm.provider.ScmProviderRepository; 028 import org.apache.maven.scm.provider.perforce.PerforceScmProvider; 029 import org.apache.maven.scm.provider.perforce.command.PerforceCommand; 030 import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper; 031 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; 032 import org.apache.regexp.RE; 033 import org.codehaus.plexus.util.cli.CommandLineException; 034 import org.codehaus.plexus.util.cli.CommandLineUtils; 035 import org.codehaus.plexus.util.cli.Commandline; 036 037 import java.io.File; 038 import java.util.ArrayList; 039 import java.util.Iterator; 040 import java.util.List; 041 042 /** 043 * @author Mike Perham 044 * @version $Id: PerforceStatusCommand.java 1306867 2012-03-29 13:45:10Z olamy $ 045 */ 046 public class PerforceStatusCommand 047 extends AbstractStatusCommand 048 implements PerforceCommand 049 { 050 private String actualLocation; 051 052 /** {@inheritDoc} */ 053 protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet files ) 054 throws ScmException 055 { 056 PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo; 057 actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() ); 058 PerforceStatusConsumer consumer = new PerforceStatusConsumer(); 059 Commandline command = readOpened( prepo, files, consumer ); 060 061 if ( consumer.isSuccess() ) 062 { 063 List<ScmFile> scmfiles = createResults( actualLocation, consumer ); 064 return new StatusScmResult( command.toString(), scmfiles ); 065 } 066 067 return new StatusScmResult( command.toString(), "Unable to get status", consumer 068 .getOutput(), consumer.isSuccess() ); 069 } 070 071 public static List<ScmFile> createResults( String repoPath, PerforceStatusConsumer consumer ) 072 { 073 List<ScmFile> results = new ArrayList<ScmFile>(); 074 List<String> files = consumer.getDepotfiles(); 075 RE re = new RE( "([^#]+)#\\d+ - ([^ ]+) .*" ); 076 for ( Iterator<String> it = files.iterator(); it.hasNext(); ) 077 { 078 String filepath = it.next(); 079 if ( !re.match( filepath ) ) 080 { 081 System.err.println( "Skipping " + filepath ); 082 continue; 083 } 084 String path = re.getParen( 1 ); 085 String verb = re.getParen( 2 ); 086 087 ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper 088 .toStatus( verb ) ); 089 results.add( scmfile ); 090 } 091 return results; 092 } 093 094 private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files, 095 PerforceStatusConsumer consumer ) 096 { 097 Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation ); 098 try 099 { 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 }