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