KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > commandLine > command > commit


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

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 /**
31  * Implements the commit command
32  * @author Robert Greig
33  */

34 public class commit extends AbstractCommandProvider {
35
36     public String JavaDoc[] getSynonyms() {
37         return new String JavaDoc[] { "ci", "com", "put" }; // NOI18N
38
}
39     
40     /**
41      * @param editor The editor passed in via -e global option.
42      */

43     private static String JavaDoc getEditorProcess(String JavaDoc editor) {
44         if (editor == null) {
45             if (System.getProperty("os.name").startsWith("Windows")) {
46                 editor = "notepad.exe";
47             }
48             else {
49                 editor = null;//"vi"; - do not use 'vi'. It's not executed correctly in the terminal.
50
}
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                 // Get the template file from the first argument
66
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 JavaDoc 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             // TODO: fix this bit
87
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                     // TODO: don't write out the full path of files
96
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 JavaDoc createMessage(File[] args, GlobalOptions gopt) {
114         File temp = null;
115         BufferedReader reader = null;
116         try {
117             temp = createTempFile(args, gopt.getTempDir());
118             // we now have a temp file with the appropriate text in it. Just
119
// get the appropriate process to edit it.
120
// TODO maybe make this more sophisticated, e.g. the cvs.editor
121
// property allows certain fields to specify arguments, where the
122
// actual filename goes etc.
123
String JavaDoc editorProcess = getEditorProcess(gopt.getEditor());
124             if (editorProcess == null) return null;
125             final Process JavaDoc proc = Runtime.getRuntime().
126                     exec(new String JavaDoc[] { editorProcess, temp.getPath() });
127             int returnCode = -1;
128
129             try {
130                 returnCode = proc.waitFor();
131             }
132             catch (InterruptedException JavaDoc e) {
133                 // So somebody else interrupted us.
134
}
135
136             if (returnCode != 0) {
137                 return null;
138             }
139             else {
140                 // TODO: need to add the bit that tests whether the file
141
// has been changed so that we can bring up the abort etc.
142
// message just like real CVS.
143
reader = new BufferedReader(new FileReader(temp));
144                 String JavaDoc line;
145                 StringBuffer JavaDoc message = new StringBuffer JavaDoc((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             // OK something went wrong so just don't bother returning a
157
// message
158
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 JavaDoc e) {
172                 // we're clearly in real trouble so just dump the
173
// exception to standard err and get out of here
174
System.err.println("Fatal error: " + e);
175                 e.printStackTrace();
176             }
177         }
178     }
179
180     public Command createCommand(String JavaDoc[] args, int index, GlobalOptions gopt, String JavaDoc workDir) {
181         CommitCommand command = new CommitCommand();
182         command.setBuilder(null);
183         final String JavaDoc 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 JavaDoc(getUsage());
196         }
197
198         int fileArgsIndex = go.optIndexGet();
199
200         File[] fileArgs = null;
201
202         // test if we have been passed any file arguments
203
if (fileArgsIndex < args.length) {
204             fileArgs = new File[args.length - fileArgsIndex];
205             // send the arguments as absolute paths
206
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         // now only bring up the editor if the message has not been set using
217
// the -m option
218
if (command.getMessage() == null && command.getLogMessageFromFile() == null) {
219             String JavaDoc message = createMessage(fileArgs, gopt);
220             if (message == null) {
221                 throw new IllegalArgumentException JavaDoc(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