1 22 package org.netbeans.lib.cvsclient.command.commit; 23 24 import java.io.*; 25 import java.util.*; 26 27 import org.netbeans.lib.cvsclient.*; 28 import org.netbeans.lib.cvsclient.admin.*; 29 import org.netbeans.lib.cvsclient.command.*; 30 import org.netbeans.lib.cvsclient.connection.*; 31 import org.netbeans.lib.cvsclient.event.*; 32 import org.netbeans.lib.cvsclient.request.*; 33 34 38 public class CommitCommand extends BasicCommand { 39 43 private final List argumentRequests = new LinkedList(); 44 45 48 private String message; 49 50 54 private boolean forceCommit; 55 56 59 private String logMessageFromFile; 60 61 64 private boolean noModuleProgram; 65 66 67 private String toRevisionOrBranch; 68 69 72 public CommitCommand() { 73 resetCVSCommand(); 74 } 75 76 79 public String getMessage() { 80 return message; 81 } 82 83 86 public void setMessage(String message) { 87 this.message = message; 88 } 89 90 94 public boolean isForceCommit() { 95 return forceCommit; 96 } 97 98 101 public void setForceCommit(boolean forceCommit) { 102 this.forceCommit = forceCommit; 103 } 104 105 112 protected void addRequestsForDirectory(File directory) 113 throws IOException { 114 if (!directory.exists()) { 115 return; 116 } 117 String dir = getRelativeToLocalPathInUnixStyle(directory); 121 122 try { 123 String repository = clientServices.getRepositoryForDirectory( 124 directory.getAbsolutePath()); 125 requests.add(new DirectoryRequest(dir, repository)); 126 String tag = clientServices.getStickyTagForDirectory(directory); 127 if (tag != null) { 128 requests.add(new StickyRequest(tag)); 129 } 130 } 131 catch (IOException ex) { 132 System.err.println("An error occurred reading the respository " + 133 "for the directory " + dir + ": " + ex); 134 ex.printStackTrace(); 135 } 136 137 Set set = clientServices.getAllFiles(directory); 141 142 final File[] files = directory.listFiles(); 145 146 set.addAll(Arrays.asList(files)); 149 150 List subdirectories = null; 151 if (isRecursive()) { 152 subdirectories = new LinkedList(); 153 } 154 155 for (Iterator it = set.iterator(); it.hasNext();) { 156 File file = (File)it.next(); 157 if (file.getName().equals("CVS")) { continue; 159 } 160 161 try { 162 final Entry entry = clientServices.getEntry(file); 163 if (entry == null) { 166 continue; 167 } 168 169 if (file.isFile()) { 172 sendEntryAndModifiedRequests(entry, file); 173 } 174 else if (isRecursive() && file.isDirectory()) { 175 File cvsSubDir = new File(file, "CVS"); if (cvsSubDir.exists()) { 177 subdirectories.add(file); 178 } 179 } 180 } 181 catch (IOException ex) { 182 System.err.println("An error occurred getting the " + 183 "Entry for file " + file + ": " + ex); 184 ex.printStackTrace(); 185 } 186 } 187 188 if (isRecursive()) { 189 for (Iterator it = subdirectories.iterator(); it.hasNext();) { 190 File subdirectory = (File)it.next(); 191 addRequestsForDirectory(subdirectory); 192 } 193 } 194 } 195 196 202 protected void addRequestsForFile(File file) 203 throws IOException { 204 final File parentDirectory = file.getParentFile(); 205 String dir = getRelativeToLocalPathInUnixStyle(parentDirectory); 208 209 try { 210 requests.add(new DirectoryRequest(dir, clientServices. 212 getRepositoryForDirectory(parentDirectory. 213 getAbsolutePath()))); 214 String tag = clientServices.getStickyTagForDirectory(parentDirectory); 215 if (tag != null) { 216 requests.add(new StickyRequest(tag)); 217 } 218 } 219 catch (IOException ex) { 220 System.err.println("An error occurred reading the respository " + 221 "for the directory " + dir + ": " + ex); 222 ex.printStackTrace(); 223 } 224 225 try { 226 final Entry entry = clientServices.getEntry(file); 227 if (entry != null) { 230 sendEntryAndModifiedRequests(entry, file); 231 } 232 } 233 catch (IOException ex) { 234 System.err.println("An error occurred getting the Entry " + 235 "for file " + file + ": " + ex); 236 ex.printStackTrace(); 237 } 238 } 239 240 245 protected boolean doesCheckFileTime() { 246 return !isForceCommit(); 247 } 248 249 255 public void execute(ClientServices client, EventManager em) 256 throws CommandException, AuthenticationException { 257 client.ensureConnection(); 258 259 super.execute(client, em); 260 261 try { 262 if (isForceCommit()) { 264 requests.add(1, new ArgumentRequest("-f")); if (isRecursive()) { 266 requests.add(1, new ArgumentRequest("-R")); } 268 } 269 if (isNoModuleProgram()) { 270 requests.add(1, new ArgumentRequest("-n")); } 272 if (getToRevisionOrBranch() != null) { 273 requests.add(1, new ArgumentRequest("-r")); requests.add(2, new ArgumentRequest(getToRevisionOrBranch())); 275 } 276 277 String message = getMessage(); 279 if (getLogMessageFromFile() != null) { 280 message = loadLogFile(getLogMessageFromFile()); 281 } 282 if (message != null) { 283 message = message.trim(); 284 } 285 if (message == null 286 || message.length() == 0) { 287 message = "no message"; } 289 addMessageRequest(message); 290 291 addRequestForWorkingDirectory(client); 292 requests.addAll(argumentRequests); 293 argumentRequests.clear(); addArgumentRequests(); 295 requests.add(CommandRequest.COMMIT); 296 297 client.processRequests(requests); 298 } 299 catch (CommandException ex) { 300 throw ex; 301 } 302 catch (Exception ex) { 303 throw new CommandException(ex, ex.getLocalizedMessage()); 304 } 305 finally { 306 requests.clear(); 307 } 308 } 309 310 protected void addArgumentRequests() { 311 if (isForceCommit()) { 312 Iterator it = requests.iterator(); 313 String directory = ""; 314 List args = new LinkedList(); 315 while (it.hasNext()) { 316 Object req = it.next(); 317 if (req instanceof org.netbeans.lib.cvsclient.request.DirectoryRequest) { 318 org.netbeans.lib.cvsclient.request.DirectoryRequest dirReq = (org.netbeans.lib.cvsclient.request.DirectoryRequest)req; 319 directory = dirReq.getLocalDirectory(); 321 } 322 else if (req instanceof org.netbeans.lib.cvsclient.request.EntryRequest) { 323 org.netbeans.lib.cvsclient.request.EntryRequest entReq = (org.netbeans.lib.cvsclient.request.EntryRequest)req; 324 String argument = null; 325 if (directory.length() == 0) { 326 argument = entReq.getEntry().getName(); 327 } 328 else { 329 argument = directory + '/' + entReq.getEntry().getName(); 330 } 331 args.add(new ArgumentRequest(argument)); 332 } 333 } 334 it = args.iterator(); 335 while (it.hasNext()) { 336 requests.add(it.next()); 337 } 338 } 339 else { 340 super.addArgumentRequests(); 341 } 342 } 343 344 349 public String getCVSCommand() { 350 StringBuffer toReturn = new StringBuffer ("commit "); toReturn.append(getCVSArguments()); 352 File[] files = getFiles(); 353 if (files != null) { 354 for (int index = 0; index < files.length; index++) { 355 toReturn.append(files[index].getName() + " "); } 357 } 358 return toReturn.toString(); 359 } 360 361 366 public boolean setCVSCommand(char opt, String optArg) { 367 if (opt == 'm') { 368 setMessage(optArg); 369 } 370 else if (opt == 'l') { 371 setRecursive(false); 372 } 373 else if (opt == 'R') { 374 setRecursive(true); 375 } 376 else if (opt == 'f') { 377 setForceCommit(true); 378 } 379 else if (opt == 'F') { 380 setLogMessageFromFile(optArg); 381 } 382 else if (opt == 'r') { 383 setToRevisionOrBranch(optArg); 384 } 385 else if (opt == 'n') { 386 setNoModuleProgram(true); 387 } 388 else { 389 return false; 390 } 391 return true; 392 } 393 394 397 public String getOptString() { 398 return "m:flRnF:r:"; } 400 401 406 public Builder createBuilder(EventManager eventMan) { 407 return new CommitBuilder(eventMan, getLocalDirectory(), clientServices.getRepository()); 408 } 409 410 414 private void addMessageRequest(String message) { 415 requests.add(new ArgumentRequest("-m")); StringTokenizer token = new StringTokenizer(message, "\n", false); boolean first = true; 418 while (token.hasMoreTokens()) { 419 if (first) { 420 requests.add(new ArgumentRequest(token.nextToken())); 421 first = false; 422 } 423 else { 424 requests.add(new ArgumentxRequest(token.nextToken())); 425 } 426 } 427 } 428 429 432 public String getLogMessageFromFile() { 433 return logMessageFromFile; 434 } 435 436 439 public void setLogMessageFromFile(String logMessageFromFile) { 440 this.logMessageFromFile = logMessageFromFile; 441 } 442 443 446 public boolean isNoModuleProgram() { 447 return noModuleProgram; 448 } 449 450 453 public void setNoModuleProgram(boolean noModuleProgram) { 454 this.noModuleProgram = noModuleProgram; 455 } 456 457 460 public String getToRevisionOrBranch() { 461 return toRevisionOrBranch; 462 } 463 464 467 public void setToRevisionOrBranch(String toRevBranch) { 468 this.toRevisionOrBranch = toRevBranch; 469 } 470 471 private String loadLogFile(String fileName) 472 throws CommandException { 473 StringBuffer buffer = new StringBuffer (); 474 BufferedReader reader = null; 475 try { 476 reader = new BufferedReader(new FileReader(fileName)); 477 String line; 478 while ((line = reader.readLine()) != null) { 479 buffer.append(line + "\n"); } 481 } 482 catch (FileNotFoundException ex) { 483 throw new CommandException(ex, 484 CommandException.getLocalMessage("CommitCommand.logInfoFileNotExists", new Object []{fileName})); } 486 catch (IOException ex) { 487 throw new CommandException(ex, 488 CommandException.getLocalMessage("CommitCommand.errorReadingLogFile", new Object []{fileName})); } 490 finally { 491 if (reader != null) { 492 try { 493 reader.close(); 494 } 495 catch (IOException exc) { 496 } 497 } 498 } 499 return buffer.toString(); 500 } 501 502 507 public void resetCVSCommand() { 508 setMessage(null); 509 setRecursive(true); 510 setForceCommit(false); 511 setLogMessageFromFile(null); 512 setNoModuleProgram(false); 513 setToRevisionOrBranch(null); 514 } 515 516 520 public String getCVSArguments() { 521 StringBuffer toReturn = new StringBuffer (); 522 if (!isRecursive()) { 523 toReturn.append("-l "); } 525 if (isForceCommit()) { 526 toReturn.append("-f "); if (isRecursive()) { 528 toReturn.append("-R "); 529 } 530 } 531 if (isNoModuleProgram()) { 532 toReturn.append("-n "); } 534 if (getToRevisionOrBranch() != null) { 535 toReturn.append("-r "); toReturn.append(getToRevisionOrBranch() + " "); } 538 if (getLogMessageFromFile() != null) { 539 toReturn.append("-F "); toReturn.append(getLogMessageFromFile()); 541 toReturn.append(" "); } 543 if (getMessage() != null) { 544 toReturn.append("-m \""); toReturn.append(getMessage()); 546 toReturn.append("\" "); } 548 return toReturn.toString(); 549 } 550 } 551 | Popular Tags |