1 22 package org.netbeans.lib.cvsclient.commandLine.command; 23 24 import java.io.*; 25 26 import org.netbeans.lib.cvsclient.command.*; 27 import org.netbeans.lib.cvsclient.command.commit.*; 28 import org.netbeans.lib.cvsclient.commandLine.*; 29 30 34 public class commit extends AbstractCommandProvider { 35 36 public String [] getSynonyms() { 37 return new String [] { "ci", "com", "put" }; } 39 40 43 private static String getEditorProcess(String editor) { 44 if (editor == null) { 45 if (System.getProperty("os.name").startsWith("Windows")) { 46 editor = "notepad.exe"; 47 } 48 else { 49 editor = null; } 51 editor = System.getProperty("cvs.editor", editor); 52 } 53 return editor; 54 } 55 56 private static File createTempFile(File[] args, File tmpDir) throws IOException { 57 File template = null; 58 BufferedReader templateReader = null; 59 BufferedWriter writer = null; 60 try { 61 File tempFile = File.createTempFile("cvsTemplate", "txt", tmpDir); 62 writer = new BufferedWriter(new FileWriter(tempFile)); 63 64 if (args != null && args.length > 0) { 65 template = new File(args[0].getParentFile(), "CVS" + 67 File.separator + 68 "Template"); 69 if (template.exists()) { 70 templateReader = new BufferedReader( 71 new FileReader(template)); 72 73 String line = null; 74 while ((line = templateReader.readLine()) != null) { 75 writer.write(line); 76 writer.newLine(); 77 } 78 } 79 } 80 writer.write("CVS: ----------------------------------------------------------------------"); 81 writer.newLine(); 82 writer.write("CVS: Enter Log. Lines beginning with `CVS:' are removed automatically"); 83 writer.newLine(); 84 writer.write("CVS: "); 85 writer.newLine(); 86 writer.write("CVS: Committing in ."); 88 writer.newLine(); 89 writer.write("CVS: "); 90 writer.newLine(); 91 writer.write("CVS: Modified Files:"); 92 writer.newLine(); 93 if (args != null) { 94 for (int i = 0; i < args.length; i++) { 95 writer.write("CVS: " + args[i].getPath()); 97 } 98 } 99 writer.write("CVS: ----------------------------------------------------------------------"); 100 writer.newLine(); 101 return tempFile; 102 } 103 finally { 104 if (templateReader != null) { 105 templateReader.close(); 106 } 107 if (writer != null) { 108 writer.close(); 109 } 110 } 111 } 112 113 private static String createMessage(File[] args, GlobalOptions gopt) { 114 File temp = null; 115 BufferedReader reader = null; 116 try { 117 temp = createTempFile(args, gopt.getTempDir()); 118 String editorProcess = getEditorProcess(gopt.getEditor()); 124 if (editorProcess == null) return null; 125 final Process proc = Runtime.getRuntime(). 126 exec(new String [] { editorProcess, temp.getPath() }); 127 int returnCode = -1; 128 129 try { 130 returnCode = proc.waitFor(); 131 } 132 catch (InterruptedException e) { 133 } 135 136 if (returnCode != 0) { 137 return null; 138 } 139 else { 140 reader = new BufferedReader(new FileReader(temp)); 144 String line; 145 StringBuffer message = new StringBuffer ((int)temp.length()); 146 while ((line = reader.readLine()) != null) { 147 if (!line.startsWith("CVS:")) { 148 message.append(line); 149 message.append('\n'); 150 } 151 } 152 return message.toString(); 153 } 154 } 155 catch (IOException e) { 156 System.err.println("Error: " + e); 159 e.printStackTrace(); 160 return null; 161 } 162 finally { 163 try { 164 if (reader != null) { 165 reader.close(); 166 } 167 if (temp != null) { 168 temp.delete(); 169 } 170 } 171 catch (Exception e) { 172 System.err.println("Fatal error: " + e); 175 e.printStackTrace(); 176 } 177 } 178 } 179 180 public Command createCommand(String [] args, int index, GlobalOptions gopt, String workDir) { 181 CommitCommand command = new CommitCommand(); 182 command.setBuilder(null); 183 final String getOptString = command.getOptString(); 184 GetOpt go = new GetOpt(args, getOptString); 185 int ch = -1; 186 go.optIndexSet(index); 187 boolean usagePrint = false; 188 while ((ch = go.getopt()) != go.optEOF) { 189 boolean ok = command.setCVSCommand((char)ch, go.optArgGet()); 190 if (!ok) { 191 usagePrint = true; 192 } 193 } 194 if (usagePrint) { 195 throw new IllegalArgumentException (getUsage()); 196 } 197 198 int fileArgsIndex = go.optIndexGet(); 199 200 File[] fileArgs = null; 201 202 if (fileArgsIndex < args.length) { 204 fileArgs = new File[args.length - fileArgsIndex]; 205 if (workDir == null) { 207 workDir = System.getProperty("user.dir"); 208 } 209 File workingDir = new File(workDir); 210 for (int i = fileArgsIndex; i < args.length; i++) { 211 fileArgs[i - fileArgsIndex] = new File(workingDir, args[i]); 212 } 213 command.setFiles(fileArgs); 214 } 215 216 if (command.getMessage() == null && command.getLogMessageFromFile() == null) { 219 String message = createMessage(fileArgs, gopt); 220 if (message == null) { 221 throw new IllegalArgumentException (java.util.ResourceBundle.getBundle(commit.class.getPackage().getName()+".Bundle").getString("commit.messageNotSpecified")); 222 } 223 command.setMessage(message); 224 } 225 226 return command; 227 } 228 229 } | Popular Tags |