1 18 package org.apache.tools.ant.taskdefs.optional.starteam; 19 20 import com.starbase.starteam.Folder; 21 import com.starbase.starteam.Item; 22 import com.starbase.starteam.Status; 23 import com.starbase.starteam.View; 24 import com.starbase.starteam.ViewConfiguration; 25 import java.io.IOException ; 26 import java.io.File ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 32 33 51 public class StarTeamCheckout extends TreeBasedTask { 52 53 56 private boolean createDirs = true; 57 58 62 private boolean deleteUncontrolled = true; 63 64 70 private boolean convertEOL = true; 71 72 73 79 public void setCreateWorkingDirs(boolean value) { 80 this.createDirs = value; 81 } 82 83 88 public void setDeleteUncontrolled(boolean value) { 89 this.deleteUncontrolled = value; 90 } 91 92 98 public void setConvertEOL(boolean value) { 99 this.convertEOL = value; 100 } 101 102 107 public void setLabel(String label) { 108 _setLabel(label); 109 } 110 111 120 private int lockStatus = Item.LockType.UNCHANGED; 121 122 128 public void setLocked(boolean v) throws BuildException { 129 setLockStatus(v, Item.LockType.EXCLUSIVE); 130 } 131 132 133 139 public void setUnlocked(boolean v) throws BuildException { 140 setLockStatus(v, Item.LockType.UNLOCKED); 141 } 142 143 private void setLockStatus(boolean v, int newStatus) 144 throws BuildException { 145 if (v) { 146 if (this.lockStatus == Item.LockType.UNCHANGED) { 147 this.lockStatus = newStatus; 148 } else if (this.lockStatus != newStatus) { 149 throw new BuildException( 150 "Error: cannot set locked and unlocked both true."); 151 } 152 } 153 } 154 155 160 private boolean useRepositoryTimeStamp = false; 161 162 170 public void setUseRepositoryTimeStamp(boolean useRepositoryTimeStamp) { 171 this.useRepositoryTimeStamp = useRepositoryTimeStamp; 172 } 173 174 179 public boolean getUseRepositoryTimeStamp() { 180 return this.useRepositoryTimeStamp; 181 } 182 183 190 public void setAsOfDate(String asOfDateParam) { 191 _setAsOfDate(asOfDateParam); 192 } 193 194 203 public void setAsOfDateFormat(String asOfDateFormat) { 204 _setAsOfDateFormat(asOfDateFormat); 205 } 206 207 218 protected View createSnapshotView(View raw) throws BuildException { 219 220 int labelID = getLabelID(raw); 221 222 if (this.isUsingViewLabel()) { 225 return new View(raw, ViewConfiguration.createFromLabel(labelID)); 226 } else if (this.isUsingRevisionLabel()) { 229 return raw; 230 } 231 View view = getViewConfiguredByDate(raw); 233 if (view != null) { 234 return view; 235 } else { 237 return new View(raw, ViewConfiguration.createTip()); 238 } 239 } 240 241 242 249 protected void testPreconditions() throws BuildException { 250 if (this.isUsingRevisionLabel() && this.createDirs) { 251 log("Ignoring createworkingdirs while using a revision label." 252 + " Folders will be created only as needed.", 253 Project.MSG_WARN); 254 this.createDirs = false; 255 } 256 if (lockStatus != Item.LockType.UNCHANGED) { 257 boolean lockStatusBad = false; 258 if (null != getLabel()) { 259 log("Neither locked nor unlocked may be true" 260 + " when checking out a labeled version.", 261 Project.MSG_ERR); 262 lockStatusBad = true; 263 } else if (null != getAsOfDate()) { 264 log("Neither locked nor unlocked may be true" 265 + " when checking out by date.", 266 Project.MSG_ERR); 267 lockStatusBad = true; 268 } 269 if (lockStatusBad) { 270 throw new BuildException( 271 "Lock status may not be changed" 272 + " when checking out a non-current version."); 273 } 274 } 275 if (null != getLabel() && null != getAsOfDate()) { 276 throw new BuildException( 277 "Both label and asOfDate specified. " 278 + "Unable to process request."); 279 } 280 281 } 282 283 293 294 protected void logOperationDescription( 295 Folder starteamrootFolder, java.io.File targetrootFolder) { 296 log((this.isRecursive() ? "Recursive" : "Non-recursive") 297 + " Checkout from: " + starteamrootFolder.getFolderHierarchy()); 298 299 log(" Checking out to" 300 + (null == getRootLocalFolder() ? "(default): " : ": ") 301 + targetrootFolder.getAbsolutePath()); 302 303 304 logLabel(); 305 logAsOfDate(); 306 logIncludes(); 307 logExcludes(); 308 309 if (this.lockStatus == Item.LockType.EXCLUSIVE) { 310 log(" Items will be checked out with Exclusive locks."); 311 } else if (this.lockStatus == Item.LockType.UNLOCKED) { 312 log(" Items will be checked out unlocked " 313 + "(even if presently locked)."); 314 } else { 315 log(" Items will be checked out with no change in lock status."); 316 } 317 log(" Items will be checked out with " 318 + (this.useRepositoryTimeStamp ? "repository timestamps." 319 : "the current timestamp.")); 320 log(" Items will be checked out " 321 + (this.isForced() ? "regardless of" : "in accordance with") 322 + " repository status."); 323 if (this.deleteUncontrolled) { 324 log(" Local items not found in the repository will be deleted."); 325 } 326 log(" Items will be checked out " 327 + (this.convertEOL ? "using the local machine's EOL convention" 328 : "without changing the EOL convention used on the server")); 329 log(" Directories will be created" 330 + (this.createDirs ? " wherever they exist in the repository, even if empty." 331 : " only where needed to check out files.")); 332 333 } 334 335 336 345 protected void visit(Folder starteamFolder, java.io.File targetFolder) 346 throws BuildException { 347 try { 348 349 350 if (null != getRootLocalFolder()) { 351 starteamFolder.setAlternatePathFragment( 352 targetFolder.getAbsolutePath()); 353 } 354 355 if (!targetFolder.exists()) { 356 if (!this.isUsingRevisionLabel()) { 357 if (this.createDirs) { 358 if (targetFolder.mkdirs()) { 359 log("Creating folder: " + targetFolder); 360 } else { 361 throw new BuildException( 362 "Failed to create local folder " + targetFolder); 363 } 364 } 365 } 366 } 367 368 369 Folder[] foldersList = starteamFolder.getSubFolders(); 370 Item[] filesList = starteamFolder.getItems(getTypeNames().FILE); 371 372 if (this.isUsingRevisionLabel()) { 373 374 377 Hashtable labelItems = new Hashtable (filesList.length); 378 int s = filesList.length; 379 int[] ids = new int[s]; 380 for (int i = 0; i < s; i++) { 381 ids[i] = filesList[i].getItemID(); 382 labelItems.put(new Integer (ids[i]), new Integer (i)); 383 } 384 int[] foundIds = getLabelInUse().getLabeledItemIDs(ids); 385 s = foundIds.length; 386 Item[] labeledFiles = new Item[s]; 387 for (int i = 0; i < s; i++) { 388 Integer id = new Integer (foundIds[i]); 389 labeledFiles[i] = 390 filesList[((Integer ) labelItems.get(id)).intValue()]; 391 } 392 filesList = labeledFiles; 393 } 394 395 396 401 UnmatchedFileMap ufm = 402 new CheckoutMap(). 403 init(targetFolder.getAbsoluteFile(), starteamFolder); 404 405 406 407 for (int i = 0; i < foldersList.length; i++) { 408 Folder stFolder = foldersList[i]; 409 410 java.io.File subfolder = 411 new java.io.File (targetFolder, stFolder.getName()); 412 413 ufm.removeControlledItem(subfolder); 414 415 if (isRecursive()) { 416 visit(stFolder, subfolder); 417 } 418 } 419 420 for (int i = 0; i < filesList.length; i++) { 421 com.starbase.starteam.File stFile = 422 (com.starbase.starteam.File) filesList[i]; 423 processFile(stFile, targetFolder); 424 425 ufm.removeControlledItem( 426 new java.io.File (targetFolder, stFile.getName())); 427 } 428 if (this.deleteUncontrolled) { 429 ufm.processUncontrolledItems(); 430 } 431 } catch (IOException e) { 432 throw new BuildException(e); 433 } 434 } 435 436 437 444 private String describeCheckout(com.starbase.starteam.File remotefile, 445 java.io.File localFile) { 446 StringBuffer sb = new StringBuffer (); 447 sb.append(getFullRepositoryPath(remotefile)) 448 .append(" --> "); 449 if (null == localFile) { 450 sb.append(remotefile.getFullName()); 451 } else { 452 sb.append(localFile); 453 } 454 return sb.toString(); 455 } 456 private String describeCheckout(com.starbase.starteam.File remotefile) { 457 return describeCheckout(remotefile, null); 458 } 459 466 private void processFile(com.starbase.starteam.File eachFile, 467 File targetFolder) 468 throws IOException { 469 String filename = eachFile.getName(); 470 471 java.io.File localFile = new java.io.File (targetFolder, filename); 472 473 if (!shouldProcess(filename)) { 475 log("Excluding " + getFullRepositoryPath(eachFile), 476 Project.MSG_INFO); 477 return; 478 } 479 480 if (this.isUsingRevisionLabel()) { 481 if (!targetFolder.exists()) { 482 if (targetFolder.mkdirs()) { 483 log("Creating folder: " + targetFolder); 484 } else { 485 throw new BuildException( 486 "Failed to create local folder " + targetFolder); 487 } 488 } 489 boolean success = eachFile.checkoutByLabelID( 490 localFile, 491 getIDofLabelInUse(), 492 this.lockStatus, 493 !this.useRepositoryTimeStamp, 494 true, 495 false); 496 if (success) { 497 log("Checked out " + describeCheckout(eachFile, localFile)); 498 } 499 } else { 500 boolean checkout = true; 501 502 513 int fileStatus = (eachFile.getStatus()); 514 515 518 if (fileStatus == Status.MERGE 519 || fileStatus == Status.UNKNOWN) { 520 eachFile.updateStatus(true, true); 521 fileStatus = (eachFile.getStatus()); 522 } 523 524 log(eachFile.toString() + " has status of " 525 + Status.name(fileStatus), Project.MSG_DEBUG); 526 527 528 switch (fileStatus) { 529 case Status.OUTOFDATE: 530 case Status.MISSING: 531 log("Checking out: " + describeCheckout(eachFile)); 532 break; 533 default: 534 if (isForced() && fileStatus != Status.CURRENT) { 535 log("Forced checkout of " 536 + describeCheckout(eachFile) 537 + " over status " + Status.name(fileStatus)); 538 } else { 539 log("Skipping: " + getFullRepositoryPath(eachFile) 540 + " - status: " + Status.name(fileStatus)); 541 checkout = false; 542 } 543 } 544 545 if (checkout) { 546 if (!targetFolder.exists()) { 547 if (targetFolder.mkdirs()) { 548 log("Creating folder: " + targetFolder); 549 } else { 550 throw new BuildException( 551 "Failed to create local folder " + targetFolder); 552 } 553 } 554 eachFile.checkout(this.lockStatus, 555 !this.useRepositoryTimeStamp, this.convertEOL, false); 556 } 557 } 558 } 559 562 private class CheckoutMap extends UnmatchedFileMap { 563 protected boolean isActive() { 564 return StarTeamCheckout.this.deleteUncontrolled; 565 } 566 567 577 UnmatchedFileMap init(java.io.File localFolder, Folder remoteFolder) { 578 if (!localFolder.exists()) { 579 return this; 580 } 581 582 String [] localFiles = localFolder.list(); 583 if (localFiles == null) { 585 return this; 586 } 587 for (int i = 0; i < localFiles.length; i++) { 588 java.io.File localFile = 589 new java.io.File (localFolder, localFiles[i]).getAbsoluteFile(); 590 591 log("adding " + localFile + " to UnmatchedFileMap", 592 Project.MSG_DEBUG); 593 594 if (localFile.isDirectory()) { 595 this.put(localFile, ""); 596 } else { 597 this.put(localFile, ""); 598 } 599 } 600 return this; 601 } 602 603 604 605 611 void processUncontrolledItems() throws BuildException { 612 if (this.isActive()) { 613 Enumeration e = this.keys(); 614 while (e.hasMoreElements()) { 615 java.io.File local = (java.io.File ) e.nextElement(); 616 delete(local); 617 } 618 } 619 } 620 621 627 void delete(java.io.File local) { 628 if (local.isDirectory() && isRecursive()) { 631 String [] contents = local.list(); 632 for (int i = 0; i < contents.length; i++) { 633 java.io.File file = new java.io.File (local, contents[i]); 634 delete(file); 635 } 636 } 637 local.delete(); 638 log("Deleted uncontrolled item " + local.getAbsolutePath()); 639 } 640 } 641 642 643 } 644 | Popular Tags |