1 | |
package org.apache.maven.scm.provider.vss.commands.checkin; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.maven.scm.ScmException; |
28 | |
import org.apache.maven.scm.ScmFile; |
29 | |
import org.apache.maven.scm.ScmFileSet; |
30 | |
import org.apache.maven.scm.ScmVersion; |
31 | |
import org.apache.maven.scm.command.checkin.AbstractCheckInCommand; |
32 | |
import org.apache.maven.scm.command.checkin.CheckInScmResult; |
33 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
34 | |
import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils; |
35 | |
import org.apache.maven.scm.provider.vss.commands.VssConstants; |
36 | |
import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository; |
37 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
38 | |
import org.codehaus.plexus.util.cli.Commandline; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class VssCheckInCommand |
48 | |
extends AbstractCheckInCommand |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repository, ScmFileSet fileSet, |
59 | |
String message, ScmVersion scmVersion ) |
60 | |
throws ScmException |
61 | |
{ |
62 | 0 | if ( getLogger().isDebugEnabled() ) |
63 | |
{ |
64 | 0 | getLogger().debug( "executing checkin command..." ); |
65 | |
} |
66 | |
|
67 | 0 | VssScmProviderRepository repo = (VssScmProviderRepository) repository; |
68 | |
|
69 | 0 | List<Commandline> commandLines = buildCmdLine( repo, fileSet, scmVersion ); |
70 | |
|
71 | 0 | VssCheckInConsumer consumer = new VssCheckInConsumer( repo, getLogger() ); |
72 | |
|
73 | |
|
74 | 0 | CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); |
75 | |
|
76 | |
int exitCode; |
77 | |
|
78 | 0 | StringBuilder sb = new StringBuilder(); |
79 | 0 | for ( Commandline cl : commandLines ) |
80 | |
{ |
81 | |
|
82 | 0 | if ( getLogger().isDebugEnabled() ) |
83 | |
{ |
84 | 0 | getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() ); |
85 | |
} |
86 | |
|
87 | 0 | exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() ); |
88 | |
|
89 | 0 | if ( exitCode != 0 ) |
90 | |
{ |
91 | 0 | String error = stderr.getOutput(); |
92 | |
|
93 | 0 | if ( getLogger().isDebugEnabled() ) |
94 | |
{ |
95 | 0 | getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" ); |
96 | |
} |
97 | 0 | if ( error.indexOf( "A writable copy of" ) < 0 ) |
98 | |
{ |
99 | 0 | return new CheckInScmResult( cl.toString(), "The vss command failed.", error, false ); |
100 | |
} |
101 | |
|
102 | 0 | if ( getLogger().isWarnEnabled() ) |
103 | |
{ |
104 | 0 | getLogger().warn( error ); |
105 | |
} |
106 | 0 | } |
107 | |
|
108 | |
} |
109 | 0 | return new CheckInScmResult( sb.toString(), new ArrayList<ScmFile>() ); |
110 | |
} |
111 | |
|
112 | |
public List<Commandline> buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version ) |
113 | |
throws ScmException |
114 | |
{ |
115 | |
|
116 | 0 | List<File> files = fileSet.getFileList(); |
117 | 0 | List<Commandline> commands = new ArrayList<Commandline>(); |
118 | |
|
119 | 0 | if ( files.size() > 0 ) |
120 | |
{ |
121 | |
|
122 | |
String base; |
123 | |
try |
124 | |
{ |
125 | 0 | base = fileSet.getBasedir().getCanonicalPath(); |
126 | |
} |
127 | 0 | catch ( IOException e ) |
128 | |
{ |
129 | 0 | throw new ScmException( "Invalid canonical path", e ); |
130 | 0 | } |
131 | |
|
132 | 0 | for ( File file : files ) |
133 | |
{ |
134 | |
|
135 | 0 | Commandline command = new Commandline(); |
136 | |
|
137 | |
try |
138 | |
{ |
139 | 0 | command.addSystemEnvironment(); |
140 | |
} |
141 | 0 | catch ( Exception e ) |
142 | |
{ |
143 | 0 | throw new ScmException( "Can't add system environment.", e ); |
144 | 0 | } |
145 | |
|
146 | 0 | command.addEnvironment( "SSDIR", repo.getVssdir() ); |
147 | |
|
148 | 0 | String ssDir = VssCommandLineUtils.getSsDir(); |
149 | |
|
150 | 0 | command.setExecutable( ssDir + VssConstants.SS_EXE ); |
151 | |
|
152 | 0 | command.createArg().setValue( VssConstants.COMMAND_CHECKIN ); |
153 | |
|
154 | |
String absolute; |
155 | |
try |
156 | |
{ |
157 | 0 | absolute = file.getCanonicalPath(); |
158 | |
String relative; |
159 | 0 | int index = absolute.indexOf( base ); |
160 | 0 | if ( index >= 0 ) |
161 | |
{ |
162 | 0 | relative = absolute.substring( index + base.length() ); |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | 0 | relative = file.getPath(); |
167 | |
} |
168 | |
|
169 | 0 | relative = relative.replace( '\\', '/' ); |
170 | |
|
171 | 0 | if ( !relative.startsWith( "/" ) ) |
172 | |
{ |
173 | 0 | relative = '/' + relative; |
174 | |
} |
175 | |
|
176 | 0 | String relativeFolder = relative.substring( 0, relative.lastIndexOf( '/' ) ); |
177 | |
|
178 | 0 | command.setWorkingDirectory( new File( fileSet.getBasedir().getAbsolutePath() + File.separatorChar |
179 | |
+ relativeFolder ).getCanonicalPath() ); |
180 | |
|
181 | 0 | command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() + relative ); |
182 | |
} |
183 | 0 | catch ( IOException e ) |
184 | |
{ |
185 | 0 | throw new ScmException( "Invalid canonical path", e ); |
186 | 0 | } |
187 | |
|
188 | |
|
189 | 0 | if ( repo.getUserPassword() != null ) |
190 | |
{ |
191 | 0 | command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() ); |
192 | |
} |
193 | |
|
194 | |
|
195 | 0 | command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF ); |
196 | |
|
197 | |
|
198 | 0 | command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE ); |
199 | |
|
200 | 0 | commands.add( command ); |
201 | |
|
202 | 0 | } |
203 | |
|
204 | 0 | } |
205 | |
else |
206 | |
{ |
207 | 0 | Commandline command = new Commandline(); |
208 | |
|
209 | 0 | command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() ); |
210 | |
|
211 | |
try |
212 | |
{ |
213 | 0 | command.addSystemEnvironment(); |
214 | |
} |
215 | 0 | catch ( Exception e ) |
216 | |
{ |
217 | 0 | throw new ScmException( "Can't add system environment.", e ); |
218 | 0 | } |
219 | |
|
220 | 0 | command.addEnvironment( "SSDIR", repo.getVssdir() ); |
221 | |
|
222 | 0 | String ssDir = VssCommandLineUtils.getSsDir(); |
223 | |
|
224 | 0 | command.setExecutable( ssDir + VssConstants.SS_EXE ); |
225 | |
|
226 | 0 | command.createArg().setValue( VssConstants.COMMAND_CHECKIN ); |
227 | |
|
228 | 0 | command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() ); |
229 | |
|
230 | 0 | command.createArg().setValue( VssConstants.FLAG_RECURSION ); |
231 | |
|
232 | |
|
233 | 0 | if ( repo.getUserPassword() != null ) |
234 | |
{ |
235 | 0 | command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() ); |
236 | |
} |
237 | |
|
238 | |
|
239 | 0 | command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF ); |
240 | |
|
241 | |
|
242 | 0 | command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE ); |
243 | |
|
244 | 0 | commands.add( command ); |
245 | |
|
246 | |
} |
247 | |
|
248 | 0 | return commands; |
249 | |
|
250 | |
} |
251 | |
} |