1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.clearcase;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
23 | import org.apache.tools.ant.types.Commandline;
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Performs ClearCase checkout.
|
---|
28 | *
|
---|
29 | * <p>
|
---|
30 | * The following attributes are interpreted:
|
---|
31 | * <table border="1">
|
---|
32 | * <tr>
|
---|
33 | * <th>Attribute</th>
|
---|
34 | * <th>Values</th>
|
---|
35 | * <th>Required</th>
|
---|
36 | * </tr>
|
---|
37 | * <tr>
|
---|
38 | * <td>viewpath</td>
|
---|
39 | * <td>Path to the ClearCase view file or directory that the command will operate on</td>
|
---|
40 | * <td>No</td>
|
---|
41 | * <tr>
|
---|
42 | * <tr>
|
---|
43 | * <td>reserved</td>
|
---|
44 | * <td>Specifies whether to check out the file as reserved or not</td>
|
---|
45 | * <td>Yes</td>
|
---|
46 | * <tr>
|
---|
47 | * <tr>
|
---|
48 | * <td>out</td>
|
---|
49 | * <td>Creates a writable file under a different filename</td>
|
---|
50 | * <td>No</td>
|
---|
51 | * <tr>
|
---|
52 | * <tr>
|
---|
53 | * <td>nodata</td>
|
---|
54 | * <td>Checks out the file but does not create an editable file containing its data</td>
|
---|
55 | * <td>No</td>
|
---|
56 | * <tr>
|
---|
57 | * <tr>
|
---|
58 | * <td>branch</td>
|
---|
59 | * <td>Specify a branch to check out the file to</td>
|
---|
60 | * <td>No</td>
|
---|
61 | * <tr>
|
---|
62 | * <tr>
|
---|
63 | * <td>version</td>
|
---|
64 | * <td>Allows checkout of a version other than main latest</td>
|
---|
65 | * <td>No</td>
|
---|
66 | * <tr>
|
---|
67 | * <tr>
|
---|
68 | * <td>nowarn</td>
|
---|
69 | * <td>Suppress warning messages</td>
|
---|
70 | * <td>No</td>
|
---|
71 | * <tr>
|
---|
72 | * <tr>
|
---|
73 | * <td>comment</td>
|
---|
74 | * <td>Specify a comment. Only one of comment or cfile may be used.</td>
|
---|
75 | * <td>No</td>
|
---|
76 | * <tr>
|
---|
77 | * <tr>
|
---|
78 | * <td>commentfile</td>
|
---|
79 | * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
|
---|
80 | * <td>No</td>
|
---|
81 | * <tr>
|
---|
82 | * <tr>
|
---|
83 | * <td>notco</td>
|
---|
84 | * <td>Fail if it's already checked out to the current view. Set to false to ignore it.</td>
|
---|
85 | * <td>No</td>
|
---|
86 | * <tr>
|
---|
87 | * <tr>
|
---|
88 | * <td>failonerr</td>
|
---|
89 | * <td>Throw an exception if the command fails. Default is true</td>
|
---|
90 | * <td>No</td>
|
---|
91 | * <tr>
|
---|
92 | * </table>
|
---|
93 | *
|
---|
94 | */
|
---|
95 | public class CCCheckout extends ClearCase {
|
---|
96 | private boolean mReserved = true;
|
---|
97 | private String mOut = null;
|
---|
98 | private boolean mNdata = false;
|
---|
99 | private String mBranch = null;
|
---|
100 | private boolean mVersion = false;
|
---|
101 | private boolean mNwarn = false;
|
---|
102 | private String mComment = null;
|
---|
103 | private String mCfile = null;
|
---|
104 | private boolean mNotco = true;
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Executes the task.
|
---|
108 | * <p>
|
---|
109 | * Builds a command line to execute cleartool and then calls Exec's run method
|
---|
110 | * to execute the command line.
|
---|
111 | * @throws BuildException if the command fails and failonerr is set to true
|
---|
112 | */
|
---|
113 | public void execute() throws BuildException {
|
---|
114 | Commandline commandLine = new Commandline();
|
---|
115 | Project aProj = getProject();
|
---|
116 | int result = 0;
|
---|
117 |
|
---|
118 | // Default the viewpath to basedir if it is not specified
|
---|
119 | if (getViewPath() == null) {
|
---|
120 | setViewPath(aProj.getBaseDir().getPath());
|
---|
121 | }
|
---|
122 |
|
---|
123 | // build the command line from what we got the format is
|
---|
124 | // cleartool checkout [options...] [viewpath ...]
|
---|
125 | // as specified in the CLEARTOOL.EXE help
|
---|
126 | commandLine.setExecutable(getClearToolCommand());
|
---|
127 | commandLine.createArgument().setValue(COMMAND_CHECKOUT);
|
---|
128 |
|
---|
129 | checkOptions(commandLine);
|
---|
130 | /*
|
---|
131 | * If configured to not care about whether the element is
|
---|
132 | * already checked out to the current view.
|
---|
133 | * Then check to see if it is checked out.
|
---|
134 | */
|
---|
135 | if (!getNotco() && lsCheckout()) {
|
---|
136 | getProject().log("Already checked out in this view: "
|
---|
137 | + getViewPathBasename(), Project.MSG_VERBOSE);
|
---|
138 | return;
|
---|
139 | }
|
---|
140 | if (!getFailOnErr()) {
|
---|
141 | getProject().log("Ignoring any errors that occur for: "
|
---|
142 | + getViewPathBasename(), Project.MSG_VERBOSE);
|
---|
143 | }
|
---|
144 | result = run(commandLine);
|
---|
145 | if (Execute.isFailure(result) && getFailOnErr()) {
|
---|
146 | String msg = "Failed executing: " + commandLine.toString();
|
---|
147 | throw new BuildException(msg, getLocation());
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Check to see if the element is checked out in the current view.
|
---|
153 | */
|
---|
154 | private boolean lsCheckout() {
|
---|
155 | Commandline cmdl = new Commandline();
|
---|
156 | String result;
|
---|
157 |
|
---|
158 | // build the command line from what we got the format is
|
---|
159 | // cleartool lsco [options...] [viewpath ...]
|
---|
160 | // as specified in the CLEARTOOL.EXE help
|
---|
161 | cmdl.setExecutable(getClearToolCommand());
|
---|
162 | cmdl.createArgument().setValue(COMMAND_LSCO);
|
---|
163 | cmdl.createArgument().setValue("-cview");
|
---|
164 | cmdl.createArgument().setValue("-short");
|
---|
165 | cmdl.createArgument().setValue("-d");
|
---|
166 | // viewpath
|
---|
167 | cmdl.createArgument().setValue(getViewPath());
|
---|
168 |
|
---|
169 | result = runS(cmdl);
|
---|
170 |
|
---|
171 | // System.out.println( "lsCheckout: " + result );
|
---|
172 |
|
---|
173 | return (result != null && result.length() > 0) ? true : false;
|
---|
174 | }
|
---|
175 | /**
|
---|
176 | * Check the command line options.
|
---|
177 | */
|
---|
178 | private void checkOptions(Commandline cmd) {
|
---|
179 | // ClearCase items
|
---|
180 | if (getReserved()) {
|
---|
181 | // -reserved
|
---|
182 | cmd.createArgument().setValue(FLAG_RESERVED);
|
---|
183 | } else {
|
---|
184 | // -unreserved
|
---|
185 | cmd.createArgument().setValue(FLAG_UNRESERVED);
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (getOut() != null) {
|
---|
189 | // -out
|
---|
190 | getOutCommand(cmd);
|
---|
191 | } else {
|
---|
192 | if (getNoData()) {
|
---|
193 | // -ndata
|
---|
194 | cmd.createArgument().setValue(FLAG_NODATA);
|
---|
195 | }
|
---|
196 |
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (getBranch() != null) {
|
---|
200 | // -branch
|
---|
201 | getBranchCommand(cmd);
|
---|
202 | } else {
|
---|
203 | if (getVersion()) {
|
---|
204 | // -version
|
---|
205 | cmd.createArgument().setValue(FLAG_VERSION);
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (getNoWarn()) {
|
---|
211 | // -nwarn
|
---|
212 | cmd.createArgument().setValue(FLAG_NOWARN);
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (getComment() != null) {
|
---|
216 | // -c
|
---|
217 | getCommentCommand(cmd);
|
---|
218 | } else {
|
---|
219 | if (getCommentFile() != null) {
|
---|
220 | // -cfile
|
---|
221 | getCommentFileCommand(cmd);
|
---|
222 | } else {
|
---|
223 | cmd.createArgument().setValue(FLAG_NOCOMMENT);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | // viewpath
|
---|
228 | cmd.createArgument().setValue(getViewPath());
|
---|
229 |
|
---|
230 | // Print out info about the notco option
|
---|
231 | // System.out.println( "Notco: " + (getNotco() ? "yes" : "no") );
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * If true, checks out the file as reserved.
|
---|
236 | *
|
---|
237 | * @param reserved the status to set the flag to
|
---|
238 | */
|
---|
239 | public void setReserved(boolean reserved) {
|
---|
240 | mReserved = reserved;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Get reserved flag status
|
---|
245 | *
|
---|
246 | * @return boolean containing status of reserved flag
|
---|
247 | */
|
---|
248 | public boolean getReserved() {
|
---|
249 | return mReserved;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * If true, checkout fails if the element is already checked out to the current view.
|
---|
254 | *
|
---|
255 | * @param notco the status to set the flag to
|
---|
256 | * @since ant 1.6.1
|
---|
257 | */
|
---|
258 | public void setNotco(boolean notco) {
|
---|
259 | mNotco = notco;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Get notco flag status
|
---|
264 | *
|
---|
265 | * @return boolean containing status of notco flag
|
---|
266 | * @since ant 1.6.1
|
---|
267 | */
|
---|
268 | public boolean getNotco() {
|
---|
269 | return mNotco;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Creates a writable file under a different filename.
|
---|
275 | *
|
---|
276 | * @param outf the path to the out file
|
---|
277 | */
|
---|
278 | public void setOut(String outf) {
|
---|
279 | mOut = outf;
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Get out file
|
---|
284 | *
|
---|
285 | * @return String containing the path to the out file
|
---|
286 | */
|
---|
287 | public String getOut() {
|
---|
288 | return mOut;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * If true, checks out the file but does not create an
|
---|
293 | * editable file containing its data.
|
---|
294 | *
|
---|
295 | * @param ndata the status to set the flag to
|
---|
296 | */
|
---|
297 | public void setNoData(boolean ndata) {
|
---|
298 | mNdata = ndata;
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Get nodata flag status
|
---|
303 | *
|
---|
304 | * @return boolean containing status of ndata flag
|
---|
305 | */
|
---|
306 | public boolean getNoData() {
|
---|
307 | return mNdata;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Specify a branch to check out the file to.
|
---|
312 | *
|
---|
313 | * @param branch the name of the branch
|
---|
314 | */
|
---|
315 | public void setBranch(String branch) {
|
---|
316 | mBranch = branch;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Get branch name
|
---|
321 | *
|
---|
322 | * @return String containing the name of the branch
|
---|
323 | */
|
---|
324 | public String getBranch() {
|
---|
325 | return mBranch;
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * If true, allows checkout of a version other than main latest.
|
---|
330 | *
|
---|
331 | * @param version the status to set the flag to
|
---|
332 | */
|
---|
333 | public void setVersion(boolean version) {
|
---|
334 | mVersion = version;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Get version flag status
|
---|
339 | *
|
---|
340 | * @return boolean containing status of version flag
|
---|
341 | */
|
---|
342 | public boolean getVersion() {
|
---|
343 | return mVersion;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * If true, warning messages are suppressed.
|
---|
348 | *
|
---|
349 | * @param nwarn the status to set the flag to
|
---|
350 | */
|
---|
351 | public void setNoWarn(boolean nwarn) {
|
---|
352 | mNwarn = nwarn;
|
---|
353 | }
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Get nowarn flag status
|
---|
357 | *
|
---|
358 | * @return boolean containing status of nwarn flag
|
---|
359 | */
|
---|
360 | public boolean getNoWarn() {
|
---|
361 | return mNwarn;
|
---|
362 | }
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Sets the comment string.
|
---|
366 | *
|
---|
367 | * @param comment the comment string
|
---|
368 | */
|
---|
369 | public void setComment(String comment) {
|
---|
370 | mComment = comment;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Get comment string
|
---|
375 | *
|
---|
376 | * @return String containing the comment
|
---|
377 | */
|
---|
378 | public String getComment() {
|
---|
379 | return mComment;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Specifies a file containing a comment.
|
---|
384 | *
|
---|
385 | * @param cfile the path to the comment file
|
---|
386 | */
|
---|
387 | public void setCommentFile(String cfile) {
|
---|
388 | mCfile = cfile;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Get comment file
|
---|
393 | *
|
---|
394 | * @return String containing the path to the comment file
|
---|
395 | */
|
---|
396 | public String getCommentFile() {
|
---|
397 | return mCfile;
|
---|
398 | }
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Get the 'out' command
|
---|
402 | *
|
---|
403 | * @param cmd containing the command line string with or
|
---|
404 | * without the out flag and path appended
|
---|
405 | */
|
---|
406 | private void getOutCommand(Commandline cmd) {
|
---|
407 | if (getOut() != null) {
|
---|
408 | /* Had to make two separate commands here because if a space is
|
---|
409 | inserted between the flag and the value, it is treated as a
|
---|
410 | Windows filename with a space and it is enclosed in double
|
---|
411 | quotes ("). This breaks clearcase.
|
---|
412 | */
|
---|
413 | cmd.createArgument().setValue(FLAG_OUT);
|
---|
414 | cmd.createArgument().setValue(getOut());
|
---|
415 | }
|
---|
416 | }
|
---|
417 |
|
---|
418 | /**
|
---|
419 | * Get the 'branch' command
|
---|
420 | *
|
---|
421 | * @param cmd containing the command line string with or
|
---|
422 | without the branch flag and name appended
|
---|
423 | */
|
---|
424 | private void getBranchCommand(Commandline cmd) {
|
---|
425 | if (getBranch() != null) {
|
---|
426 | /* Had to make two separate commands here because if a space is
|
---|
427 | inserted between the flag and the value, it is treated as a
|
---|
428 | Windows filename with a space and it is enclosed in double
|
---|
429 | quotes ("). This breaks clearcase.
|
---|
430 | */
|
---|
431 | cmd.createArgument().setValue(FLAG_BRANCH);
|
---|
432 | cmd.createArgument().setValue(getBranch());
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Get the 'comment' command
|
---|
439 | *
|
---|
440 | * @param cmd containing the command line string with or
|
---|
441 | * without the comment flag and string appended
|
---|
442 | */
|
---|
443 | private void getCommentCommand(Commandline cmd) {
|
---|
444 | if (getComment() != null) {
|
---|
445 | /* Had to make two separate commands here because if a space is
|
---|
446 | inserted between the flag and the value, it is treated as a
|
---|
447 | Windows filename with a space and it is enclosed in double
|
---|
448 | quotes ("). This breaks clearcase.
|
---|
449 | */
|
---|
450 | cmd.createArgument().setValue(FLAG_COMMENT);
|
---|
451 | cmd.createArgument().setValue(getComment());
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * Get the 'cfile' command
|
---|
457 | *
|
---|
458 | * @param cmd containing the command line string with or
|
---|
459 | * without the cfile flag and file appended
|
---|
460 | */
|
---|
461 | private void getCommentFileCommand(Commandline cmd) {
|
---|
462 | if (getCommentFile() != null) {
|
---|
463 | /* Had to make two separate commands here because if a space is
|
---|
464 | inserted between the flag and the value, it is treated as a
|
---|
465 | Windows filename with a space and it is enclosed in double
|
---|
466 | quotes ("). This breaks clearcase.
|
---|
467 | */
|
---|
468 | cmd.createArgument().setValue(FLAG_COMMENTFILE);
|
---|
469 | cmd.createArgument().setValue(getCommentFile());
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * -reserved flag -- check out the file as reserved
|
---|
475 | */
|
---|
476 | public static final String FLAG_RESERVED = "-reserved";
|
---|
477 | /**
|
---|
478 | * -reserved flag -- check out the file as unreserved
|
---|
479 | */
|
---|
480 | public static final String FLAG_UNRESERVED = "-unreserved";
|
---|
481 | /**
|
---|
482 | * -out flag -- create a writable file under a different filename
|
---|
483 | */
|
---|
484 | public static final String FLAG_OUT = "-out";
|
---|
485 | /**
|
---|
486 | * -ndata flag -- checks out the file but does not create an editable file containing its data
|
---|
487 | */
|
---|
488 | public static final String FLAG_NODATA = "-ndata";
|
---|
489 | /**
|
---|
490 | * -branch flag -- checks out the file on a specified branch
|
---|
491 | */
|
---|
492 | public static final String FLAG_BRANCH = "-branch";
|
---|
493 | /**
|
---|
494 | * -version flag -- allows checkout of a version that is not main latest
|
---|
495 | */
|
---|
496 | public static final String FLAG_VERSION = "-version";
|
---|
497 | /**
|
---|
498 | * -nwarn flag -- suppresses warning messages
|
---|
499 | */
|
---|
500 | public static final String FLAG_NOWARN = "-nwarn";
|
---|
501 | /**
|
---|
502 | * -c flag -- comment to attach to the file
|
---|
503 | */
|
---|
504 | public static final String FLAG_COMMENT = "-c";
|
---|
505 | /**
|
---|
506 | * -cfile flag -- file containing a comment to attach to the file
|
---|
507 | */
|
---|
508 | public static final String FLAG_COMMENTFILE = "-cfile";
|
---|
509 | /**
|
---|
510 | * -nc flag -- no comment is specified
|
---|
511 | */
|
---|
512 | public static final String FLAG_NOCOMMENT = "-nc";
|
---|
513 |
|
---|
514 | }
|
---|
515 |
|
---|