1 package org.apache.maven.scm.provider.perforce.command;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41
42
43
44
45
46
47
48 public class PerforceWhereCommand
49 {
50 private ScmLogger logger = null;
51
52 private PerforceScmProviderRepository repo = null;
53
54 public PerforceWhereCommand( ScmLogger log, PerforceScmProviderRepository repos )
55 {
56 logger = log;
57 repo = repos;
58 }
59
60 public String getDepotLocation( File file )
61 {
62 return getDepotLocation( file.getAbsolutePath() );
63 }
64
65
66
67
68
69
70 public String getDepotLocation( String filepath )
71 {
72 if ( !PerforceScmProvider.isLive() )
73 {
74 return null;
75 }
76
77 InputStreamReader isReader = null;
78 InputStreamReader isReaderErr = null;
79 try
80 {
81 Commandline command = PerforceScmProvider.createP4Command( repo, null );
82 command.createArg().setValue( "where" );
83 command.createArg().setValue( filepath );
84 if ( logger.isDebugEnabled() )
85 {
86 logger.debug( PerforceScmProvider.clean( "Executing: " + command.toString() ) );
87 }
88 Process proc = command.execute();
89 isReader = new InputStreamReader( proc.getInputStream() );
90 isReaderErr = new InputStreamReader( proc.getErrorStream() );
91 BufferedReader br = new BufferedReader( isReader );
92 BufferedReader brErr = new BufferedReader( isReaderErr );
93 String line;
94 String path = null;
95 while ( ( line = br.readLine() ) != null )
96 {
97 if ( line.indexOf( "not in client view" ) != -1 )
98 {
99
100 if ( logger.isErrorEnabled() )
101 {
102 logger.error( line );
103 }
104 return null;
105 }
106 if ( line.indexOf( "is not under" ) != -1 )
107 {
108
109 if ( logger.isErrorEnabled() )
110 {
111 logger.error( line );
112 }
113 return null;
114 }
115
116 if ( logger.isDebugEnabled() )
117 {
118 logger.debug( line );
119 }
120
121 path = line.substring( 0, line.lastIndexOf( "//" ) - 1 );
122 }
123
124 while ( ( line = brErr.readLine() ) != null )
125 {
126 if ( line.indexOf( "not in client view" ) != -1 )
127 {
128
129 if ( logger.isErrorEnabled() )
130 {
131 logger.error( line );
132 }
133 return null;
134 }
135 if ( line.indexOf( "is not under" ) != -1 )
136 {
137
138 if ( logger.isErrorEnabled() )
139 {
140 logger.error( line );
141 }
142 return null;
143 }
144
145 if ( logger.isDebugEnabled() )
146 {
147 logger.debug( line );
148 }
149 }
150
151 return path;
152 }
153 catch ( CommandLineException e )
154 {
155 if ( logger.isErrorEnabled() )
156 {
157 logger.error( e );
158 }
159 throw new RuntimeException( e.getLocalizedMessage() );
160 }
161 catch ( IOException e )
162 {
163 if ( logger.isErrorEnabled() )
164 {
165 logger.error( e );
166 }
167 throw new RuntimeException( e.getLocalizedMessage() );
168 }
169 finally
170 {
171 IOUtil.close( isReader );
172 IOUtil.close( isReaderErr );
173 }
174 }
175 }