1 package org.apache.maven.scm.provider.perforce.command;
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.log.ScmLogger;
23 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
24 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
25 import org.codehaus.plexus.util.IOUtil;
26 import org.codehaus.plexus.util.cli.CommandLineException;
27 import org.codehaus.plexus.util.cli.Commandline;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33
34 /**
35 * Encapsulates the 'p4 where' command which can be very useful in determining
36 * a file's location within the depot. Use <code>getDepotLocation(String path)</code> to query
37 * the depot location for a particular file. The data from p4 where looks like this:
38 * <p>
39 * <pre>
40 * p4 where pom.xml
41 * //depot/modules/fabric/trunk/pom.xml //mikeperham-dt/depot/modules/fabric/trunk/pom.xml
42 * d:\perforce\depot\modules\fabric\trunk\pom.xml
43 * </pre>
44 *
45 * @author mperham
46 */
47 public class PerforceWhereCommand
48 {
49 private ScmLogger logger = null;
50
51 private PerforceScmProviderRepository repo = null;
52
53 public PerforceWhereCommand( ScmLogger log, PerforceScmProviderRepository repos )
54 {
55 logger = log;
56 repo = repos;
57 }
58
59 public String getDepotLocation( File file )
60 {
61 return getDepotLocation( file.getAbsolutePath() );
62 }
63
64 /**
65 * @param filepath an absolute file path
66 * @return the absolute location of the given file within the Perforce repository or null if the file
67 * does not exist in a mapping within the current clientspec.
68 */
69 public String getDepotLocation( String filepath )
70 {
71 if ( !PerforceScmProvider.isLive() )
72 {
73 return null;
74 }
75
76 InputStreamReader isReader = null;
77 InputStreamReader isReaderErr = null;
78 try
79 {
80 Commandline command = PerforceScmProvider.createP4Command( repo, null );
81 command.createArg().setValue( "where" );
82 command.createArg().setValue( filepath );
83 if ( logger.isDebugEnabled() )
84 {
85 logger.debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
86 }
87 Process proc = command.execute();
88 isReader = new InputStreamReader( proc.getInputStream() );
89 isReaderErr = new InputStreamReader( proc.getErrorStream() );
90 BufferedReader br = new BufferedReader( isReader );
91 BufferedReader brErr = new BufferedReader( isReaderErr );
92 String line;
93 String path = null;
94 while ( ( line = br.readLine() ) != null )
95 {
96 if ( line.indexOf( "not in client view" ) != -1 )
97 {
98 // uh oh, something bad is happening
99 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 | IOException e )
153 {
154 if ( logger.isErrorEnabled() )
155 {
156 logger.error( e );
157 }
158 throw new RuntimeException( e.getLocalizedMessage() );
159 }
160 finally
161 {
162 IOUtil.close( isReader );
163 IOUtil.close( isReaderErr );
164 }
165 }
166 }