1 22 package org.netbeans.lib.cvsclient.command.update; 23 24 import java.io.*; 25 import java.text.*; 26 import java.util.*; 27 28 import org.netbeans.lib.cvsclient.*; 29 import org.netbeans.lib.cvsclient.file.FileUtils; 30 import org.netbeans.lib.cvsclient.admin.*; 31 import org.netbeans.lib.cvsclient.command.*; 32 import org.netbeans.lib.cvsclient.connection.*; 33 import org.netbeans.lib.cvsclient.event.*; 34 import org.netbeans.lib.cvsclient.request.*; 35 36 42 public class UpdateCommand extends BasicCommand 43 implements TemporaryFileCreator { 44 45 private static final String RENAME_FORMAT = "{0}/.#{1}.{2}"; private static final Object [] FORMAT_PARAMETER = new Object [3]; 49 54 private final Set emptyDirectories = new HashSet(); 55 59 private boolean buildDirectories; 60 61 65 private boolean cleanCopy; 66 67 71 private boolean pruneDirectories; 72 73 77 private boolean pipeToOutput; 78 79 82 private boolean resetStickyOnes; 83 84 88 private boolean useHeadIfNotFound; 89 90 93 private String updateByDate; 94 95 98 private String updateByRevision; 99 100 104 private KeywordSubstitutionOptions keywordSubst; 105 106 111 private String mergeRevision1; 112 113 119 private String mergeRevision2; 120 121 124 public UpdateCommand() { 125 resetCVSCommand(); 127 } 128 129 134 public Builder createBuilder(EventManager eventManager) { 135 if (isPipeToOutput()) { 136 return new PipedFilesBuilder(eventManager, this, this); 137 } 138 return new UpdateBuilder(eventManager, getLocalDirectory()); 139 } 140 141 145 protected void sendEntryAndModifiedRequests(Entry entry, File file) { 146 if (isCleanCopy() && file != null && entry != null) { 147 if (!isPipeToOutput()) { 148 FORMAT_PARAMETER[0] = file.getParent(); 149 FORMAT_PARAMETER[1] = file.getName(); 150 FORMAT_PARAMETER[2] = entry.getRevision(); 151 152 String filename = MessageFormat.format(RENAME_FORMAT, FORMAT_PARAMETER); 153 try { 154 FileUtils.copyFile(file, new File(filename)); 155 } catch (IOException e) { 156 } 158 } 159 file = null; 160 } 161 super.sendEntryAndModifiedRequests(entry, file); 162 } 163 164 168 public void setBuildDirectories(boolean buildDirectories) { 169 this.buildDirectories = buildDirectories; 170 } 171 172 176 public boolean isBuildDirectories() { 177 return buildDirectories; 178 } 179 180 185 public void setCleanCopy(boolean cleanCopy) { 186 this.cleanCopy = cleanCopy; 187 } 188 189 192 public boolean isCleanCopy() { 193 return cleanCopy; 194 } 195 196 200 public void setPruneDirectories(boolean pruneDirectories) { 201 this.pruneDirectories = pruneDirectories; 202 } 203 204 209 public boolean isPruneDirectories() { 210 return pruneDirectories; 211 } 212 213 219 public void execute(ClientServices client, EventManager eventManager) 220 throws CommandException, AuthenticationException { 221 client.ensureConnection(); 222 223 super.execute(client, eventManager); 224 225 emptyDirectories.clear(); 226 227 try { 228 if (!isRecursive()) { 231 requests.add(1, new ArgumentRequest("-l")); } 233 if (isBuildDirectories()) { 234 requests.add(1, new ArgumentRequest("-d")); } 236 if (isCleanCopy() && !isPipeToOutput()) { 237 requests.add(1, new ArgumentRequest("-C")); } 239 if (isPipeToOutput()) { 240 requests.add(1, new ArgumentRequest("-p")); } 242 if (isResetStickyOnes()) { 243 requests.add(1, new ArgumentRequest("-A")); } 245 if (isUseHeadIfNotFound()) { 246 requests.add(1, new ArgumentRequest("-f")); } 248 if (getUpdateByDate() != null) { 249 requests.add(1, new ArgumentRequest("-D")); requests.add(2, new ArgumentRequest(getUpdateByDate())); 251 } 252 else if (getUpdateByRevision() != null) { 253 requests.add(1, new ArgumentRequest("-r")); requests.add(2, new ArgumentRequest(getUpdateByRevision())); 255 } 256 if (getMergeRevision1() != null) { 257 requests.add(1, new ArgumentRequest("-j")); requests.add(2, new ArgumentRequest(getMergeRevision1())); 259 260 if (getMergeRevision2() != null) { 261 requests.add(3, new ArgumentRequest("-j")); requests.add(4, new ArgumentRequest(getMergeRevision2())); 263 } 264 } 265 if (getKeywordSubst() != null) { 266 requests.add(1, new ArgumentRequest("-k")); requests.add(2, new ArgumentRequest(getKeywordSubst().toString())); 268 } 269 requests.add(1, new ArgumentRequest("-u")); 271 addRequestForWorkingDirectory(client); 272 addArgumentRequests(); 273 addRequest(CommandRequest.UPDATE); 274 275 if (isPipeToOutput() && (getUpdateByRevision() != null || getUpdateByDate() != null)) { 278 ListIterator it = requests.listIterator(); 279 while (it.hasNext()) { 280 Object req = it.next(); 281 if (req instanceof EntryRequest) { 282 EntryRequest eReq = (EntryRequest)req; 283 Entry entry = eReq.getEntry(); 284 if (entry.getRevision().startsWith("-")) { entry.setRevision(entry.getRevision().substring(1)); 286 } 287 it.set(new EntryRequest(entry)); 288 it.add(new UnchangedRequest(entry.getName())); 289 } 290 } 291 } 292 client.processRequests(requests); 294 if (pruneDirectories && (getGlobalOptions() == null || !getGlobalOptions().isDoNoChanges())) { 295 pruneEmptyDirectories(client); 296 } 297 } 298 catch (CommandException ex) { 299 throw ex; 300 } 301 catch (EOFException ex) { 302 throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); } 304 catch (Exception ex) { 305 throw new CommandException(ex, ex.getLocalizedMessage()); 306 } 307 finally { 308 requests.clear(); 309 } 310 } 311 312 316 public boolean isPipeToOutput() { 317 return pipeToOutput; 318 } 319 320 324 public void setPipeToOutput(boolean pipeToOutput) { 325 this.pipeToOutput = pipeToOutput; 326 } 327 328 332 public boolean isResetStickyOnes() { 333 return resetStickyOnes; 334 } 335 336 340 public void setResetStickyOnes(boolean resetStickyOnes) { 341 this.resetStickyOnes = resetStickyOnes; 342 } 343 344 348 public boolean isUseHeadIfNotFound() { 349 return useHeadIfNotFound; 350 } 351 352 356 public void setUseHeadIfNotFound(boolean useHeadIfNotFound) { 357 this.useHeadIfNotFound = useHeadIfNotFound; 358 } 359 360 364 public String getUpdateByDate() { 365 return updateByDate; 366 } 367 368 372 public void setUpdateByDate(String updateByDate) { 373 this.updateByDate = getTrimmedString(updateByDate); 374 } 375 376 380 public String getUpdateByRevision() { 381 return updateByRevision; 382 } 383 384 388 public void setUpdateByRevision(String updateByRevision) { 389 this.updateByRevision = getTrimmedString(updateByRevision); 390 } 391 392 396 public KeywordSubstitutionOptions getKeywordSubst() { 397 return keywordSubst; 398 } 399 400 404 public void setKeywordSubst(KeywordSubstitutionOptions keywordSubst) { 405 this.keywordSubst = keywordSubst; 406 } 407 408 411 public File createTempFile(String filename) throws IOException { 412 File temp = File.createTempFile("cvs", ".dff", getGlobalOptions().getTempDir()); return temp; 414 } 415 416 423 public String getCVSCommand() { 424 StringBuffer toReturn = new StringBuffer ("update "); toReturn.append(getCVSArguments()); 426 File[] files = getFiles(); 427 if (files != null) { 428 for (int index = 0; index < files.length; index++) { 429 toReturn.append(files[index].getName()); 430 toReturn.append(' '); 431 } 432 } 433 return toReturn.toString(); 434 } 435 436 440 public String getCVSArguments() { 441 StringBuffer toReturn = new StringBuffer (""); if (isPipeToOutput()) { 443 toReturn.append("-p "); } 445 if (isCleanCopy()) { 446 toReturn.append("-C "); } 448 if (!isRecursive()) { 449 toReturn.append("-l "); } 451 if (isBuildDirectories()) { 452 toReturn.append("-d "); } 454 if (isPruneDirectories()) { 455 toReturn.append("-P "); } 457 if (isResetStickyOnes()) { 458 toReturn.append("-A "); } 460 if (isUseHeadIfNotFound()) { 461 toReturn.append("-f "); } 463 if (getKeywordSubst() != null) { 464 toReturn.append("-k"); toReturn.append(getKeywordSubst().toString()); 466 toReturn.append(' '); 467 } 468 if (getUpdateByRevision() != null) { 469 toReturn.append("-r "); toReturn.append(getUpdateByRevision()); 471 toReturn.append(' '); 472 } 473 if (getUpdateByDate() != null) { 474 toReturn.append("-D "); toReturn.append(getUpdateByDate()); 476 toReturn.append(' '); 477 } 478 if (getMergeRevision1() != null) { 479 toReturn.append("-j "); toReturn.append(getMergeRevision1()); 481 toReturn.append(' '); 482 483 if (getMergeRevision2() != null) { 484 toReturn.append("-j "); toReturn.append(getMergeRevision2()); 486 toReturn.append(' '); 487 } 488 } 489 return toReturn.toString(); 490 } 491 492 496 public boolean setCVSCommand(char opt, String optArg) { 497 if (opt == 'R') { 498 setRecursive(true); 499 } 500 else if (opt == 'C') { 501 setCleanCopy(true); 502 } 503 else if (opt == 'l') { 504 setRecursive(false); 505 } 506 else if (opt == 'd') { 507 setBuildDirectories(true); 508 } 509 else if (opt == 'P') { 510 setPruneDirectories(true); 511 } 512 else if (opt == 'A') { 513 setResetStickyOnes(true); 514 } 515 else if (opt == 'f') { 516 setUseHeadIfNotFound(true); 517 } 518 else if (opt == 'D') { 519 setUpdateByDate(optArg.trim()); 520 } 521 else if (opt == 'r') { 522 setUpdateByRevision(optArg.trim()); 523 } 524 else if (opt == 'k') { 525 KeywordSubstitutionOptions keywordSubst = 526 KeywordSubstitutionOptions.findKeywordSubstOption(optArg); 527 setKeywordSubst(keywordSubst); 528 } 529 else if (opt == 'p') { 530 setPipeToOutput(true); 531 } 532 else if (opt == 'j') { 533 if (getMergeRevision1() == null) { 534 setMergeRevision1(optArg); 535 } 536 else { 537 setMergeRevision2(optArg); 538 } 539 } 540 else { 541 return false; 543 } 544 return true; 545 } 546 547 552 public void resetCVSCommand() { 553 setRecursive(true); 554 setCleanCopy(false); 555 setBuildDirectories(false); 556 setPruneDirectories(false); 557 setResetStickyOnes(false); 558 setUseHeadIfNotFound(false); 559 setUpdateByDate(null); 560 setUpdateByRevision(null); 561 setKeywordSubst(null); 562 setPipeToOutput(false); 563 setMergeRevision1(null); 564 setMergeRevision2(null); 565 } 566 567 573 public void messageSent(MessageEvent e) { 574 super.messageSent(e); 575 if (!pruneDirectories) { 578 return; 579 } 580 581 final String relativePath = CommandUtils.getExaminedDirectory(e.getMessage(), 582 UpdateBuilder.EXAM_DIR); 583 if (relativePath == null) { 584 return; 585 } 586 587 if (relativePath.equals(".")) { return; 590 } 591 592 emptyDirectories.add(new File(getLocalDirectory(), relativePath)); 593 } 594 595 599 private boolean pruneEmptyDirectory(File directory, ClientServices client) 600 throws IOException { 601 final File[] contents = directory.listFiles(); 602 603 if (contents == null) { 605 return true; 606 } 607 608 for (int i = 0; i < contents.length; i++) { 609 if (contents[i].isFile()) { 610 return false; 611 } 612 613 if (contents[i].getName().equals("CVS")) { 615 continue; 617 } 618 619 if (!pruneEmptyDirectory(contents[i], client)) { 620 return false; 621 } 622 } 623 624 if (new File(directory, "CVS/Entries").isFile() && new File(directory, "CVS/Repository").isFile()) { 627 final File adminDir = new File(directory, "CVS"); for (Iterator i = clientServices.getEntries(directory); i.hasNext(); ) { 630 Entry entry = (Entry) i.next(); 631 if (entry.getName() != null && entry.isUserFileToBeRemoved()) return false; 632 } 633 deleteRecursively(adminDir); 634 directory.delete(); 635 if (!client.exists(directory)) client.removeEntry(directory); 637 return true; 638 } 639 640 return false; 641 } 642 643 648 private void deleteRecursively(File dir) { 649 File [] files = dir.listFiles(); 650 for (int i = 0; i < files.length; i++) { 651 File file = files[i]; 652 if (file.isDirectory()) { 653 deleteRecursively(file); 654 } else { 655 file.delete(); 656 } 657 } 658 dir.delete(); 659 } 660 661 664 private void pruneEmptyDirectories(ClientServices client) 665 throws IOException { 666 for (Iterator it = emptyDirectories.iterator(); it.hasNext();) { 667 final File dir = (File)it.next(); 668 if (dir.exists()) { 671 pruneEmptyDirectory(dir, client); 672 } 673 } 674 emptyDirectories.clear(); 675 } 676 677 680 public String getOptString() { 681 return "RCnldPAfD:r:pj:k:"; } 683 684 687 public String getMergeRevision1() { 688 return mergeRevision1; 689 } 690 691 694 public void setMergeRevision1(String mergeRevision1) { 695 this.mergeRevision1 = getTrimmedString(mergeRevision1); 696 } 697 698 701 public String getMergeRevision2() { 702 return mergeRevision2; 703 } 704 705 708 public void setMergeRevision2(String mergeRevision2) { 709 this.mergeRevision2 = getTrimmedString(mergeRevision2); 710 } 711 } 712 | Popular Tags |