001package org.apache.maven.scm.provider.perforce.command; 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.log.ScmLogger; 023import org.apache.maven.scm.provider.perforce.PerforceScmProvider; 024import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; 025import org.codehaus.plexus.util.IOUtil; 026import org.codehaus.plexus.util.cli.CommandLineException; 027import org.codehaus.plexus.util.cli.Commandline; 028 029import java.io.BufferedReader; 030import java.io.File; 031import java.io.IOException; 032import java.io.InputStreamReader; 033 034/** 035 * Encapsulates the 'p4 where' command which can be very useful in determining 036 * a file's location within the depot. Use <code>getDepotLocation(String path)</code> to query 037 * the depot location for a particular file. The data from p4 where looks like this: 038 * <p/> 039 * <pre> 040 * p4 where pom.xml 041 * //depot/modules/fabric/trunk/pom.xml //mikeperham-dt/depot/modules/fabric/trunk/pom.xml 042 * d:\perforce\depot\modules\fabric\trunk\pom.xml 043 * </pre> 044 * 045 * @author mperham 046 */ 047public class PerforceWhereCommand 048{ 049 private ScmLogger logger = null; 050 051 private PerforceScmProviderRepository repo = null; 052 053 public PerforceWhereCommand( ScmLogger log, PerforceScmProviderRepository repos ) 054 { 055 logger = log; 056 repo = repos; 057 } 058 059 public String getDepotLocation( File file ) 060 { 061 return getDepotLocation( file.getAbsolutePath() ); 062 } 063 064 /** 065 * @param filepath an absolute file path 066 * @return the absolute location of the given file within the Perforce repository or null if the file 067 * does not exist in a mapping within the current clientspec. 068 */ 069 public String getDepotLocation( String filepath ) 070 { 071 if ( !PerforceScmProvider.isLive() ) 072 { 073 return null; 074 } 075 076 InputStreamReader isReader = null; 077 InputStreamReader isReaderErr = null; 078 try 079 { 080 Commandline command = PerforceScmProvider.createP4Command( repo, null ); 081 command.createArg().setValue( "where" ); 082 command.createArg().setValue( filepath ); 083 if ( logger.isDebugEnabled() ) 084 { 085 logger.debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) ); 086 } 087 Process proc = command.execute(); 088 isReader = new InputStreamReader( proc.getInputStream() ); 089 isReaderErr = new InputStreamReader( proc.getErrorStream() ); 090 BufferedReader br = new BufferedReader( isReader ); 091 BufferedReader brErr = new BufferedReader( isReaderErr ); 092 String line; 093 String path = null; 094 while ( ( line = br.readLine() ) != null ) 095 { 096 if ( line.indexOf( "not in client view" ) != -1 ) 097 { 098 // uh oh, something bad is happening 099 if ( logger.isErrorEnabled() ) 100 { 101 logger.error( line ); 102 } 103 return null; 104 } 105 if ( line.indexOf( "is not under" ) != -1 ) 106 { 107 // uh oh, something bad is happening 108 if ( logger.isErrorEnabled() ) 109 { 110 logger.error( line ); 111 } 112 return null; 113 } 114 115 if ( logger.isDebugEnabled() ) 116 { 117 logger.debug( line ); 118 } 119 // verify that "//" appears twice in the line 120 path = line.substring( 0, line.lastIndexOf( "//" ) - 1 ); 121 } 122 // Check for errors 123 while ( ( line = brErr.readLine() ) != null ) 124 { 125 if ( line.indexOf( "not in client view" ) != -1 ) 126 { 127 // uh oh, something bad is happening 128 if ( logger.isErrorEnabled() ) 129 { 130 logger.error( line ); 131 } 132 return null; 133 } 134 if ( line.indexOf( "is not under" ) != -1 ) 135 { 136 // uh oh, something bad is happening 137 if ( logger.isErrorEnabled() ) 138 { 139 logger.error( line ); 140 } 141 return null; 142 } 143 144 if ( logger.isDebugEnabled() ) 145 { 146 logger.debug( line ); 147 } 148 } 149 150 return path; 151 } 152 catch ( CommandLineException e ) 153 { 154 if ( logger.isErrorEnabled() ) 155 { 156 logger.error( e ); 157 } 158 throw new RuntimeException( e.getLocalizedMessage() ); 159 } 160 catch ( IOException e ) 161 { 162 if ( logger.isErrorEnabled() ) 163 { 164 logger.error( e ); 165 } 166 throw new RuntimeException( e.getLocalizedMessage() ); 167 } 168 finally 169 { 170 IOUtil.close( isReader ); 171 IOUtil.close( isReaderErr ); 172 } 173 } 174}