1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.eclipse.jgit.pgm;
46
47 import static org.eclipse.jgit.lib.Constants.R_HEADS;
48 import static org.eclipse.jgit.lib.Constants.R_REMOTES;
49 import static org.eclipse.jgit.lib.Constants.R_TAGS;
50
51 import java.io.BufferedWriter;
52 import java.io.FileDescriptor;
53 import java.io.FileInputStream;
54 import java.io.FileOutputStream;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.io.OutputStream;
58 import java.io.OutputStreamWriter;
59 import java.text.MessageFormat;
60 import java.util.ResourceBundle;
61
62 import org.eclipse.jgit.lib.ObjectId;
63 import org.eclipse.jgit.lib.Repository;
64 import org.eclipse.jgit.pgm.internal.CLIText;
65 import org.eclipse.jgit.pgm.opt.CmdLineParser;
66 import org.eclipse.jgit.revwalk.RevWalk;
67 import org.eclipse.jgit.util.io.ThrowingPrintWriter;
68 import org.kohsuke.args4j.CmdLineException;
69 import org.kohsuke.args4j.Option;
70
71
72
73
74
75
76
77
78
79
80
81
82 public abstract class TextBuiltin {
83 private String commandName;
84
85 @Option(name = "--help", usage = "usage_displayThisHelpText", aliases = { "-h" })
86 private boolean help;
87
88
89
90
91
92
93 protected InputStream ins;
94
95
96
97
98
99
100 protected ThrowingPrintWriter outw;
101
102
103
104
105
106
107 protected OutputStream outs;
108
109
110
111
112
113
114 protected ThrowingPrintWriter errw;
115
116
117
118
119
120
121 protected OutputStream errs;
122
123
124 protected Repository db;
125
126
127 protected String gitdir;
128
129
130 protected RevWalk argWalk;
131
132 final void setCommandName(final String name) {
133 commandName = name;
134 }
135
136
137 protected boolean requiresRepository() {
138 return true;
139 }
140
141
142
143
144
145
146
147
148
149
150 protected void init(final Repository repository, final String gitDir) {
151 try {
152 final String outputEncoding = repository != null ? repository
153 .getConfig().getString("i18n", null, "logOutputEncoding") : null;
154 if (ins == null)
155 ins = new FileInputStream(FileDescriptor.in);
156 if (outs == null)
157 outs = new FileOutputStream(FileDescriptor.out);
158 if (errs == null)
159 errs = new FileOutputStream(FileDescriptor.err);
160 BufferedWriter outbufw;
161 if (outputEncoding != null)
162 outbufw = new BufferedWriter(new OutputStreamWriter(outs,
163 outputEncoding));
164 else
165 outbufw = new BufferedWriter(new OutputStreamWriter(outs));
166 outw = new ThrowingPrintWriter(outbufw);
167 BufferedWriter errbufw;
168 if (outputEncoding != null)
169 errbufw = new BufferedWriter(new OutputStreamWriter(errs,
170 outputEncoding));
171 else
172 errbufw = new BufferedWriter(new OutputStreamWriter(errs));
173 errw = new ThrowingPrintWriter(errbufw);
174 } catch (IOException e) {
175 throw die(CLIText.get().cannotCreateOutputStream);
176 }
177
178 if (repository != null && repository.getDirectory() != null) {
179 db = repository;
180 gitdir = repository.getDirectory().getAbsolutePath();
181 } else {
182 db = repository;
183 gitdir = gitDir;
184 }
185 }
186
187
188
189
190
191
192
193
194
195
196
197 public final void execute(String[] args) throws Exception {
198 parseArguments(args);
199 run();
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213 protected void parseArguments(final String[] args) throws IOException {
214 final CmdLineParser clp = new CmdLineParser(this);
215 help = containsHelp(args);
216 try {
217 clp.parseArgument(args);
218 } catch (CmdLineException err) {
219 this.errw.println(CLIText.fatalError(err.getMessage()));
220 if (help) {
221 printUsage("", clp);
222 }
223 throw die(true, err);
224 }
225
226 if (help) {
227 printUsage("", clp);
228 throw new TerminatedByHelpException();
229 }
230
231 argWalk = clp.getRevWalkGently();
232 }
233
234
235
236
237
238
239
240 public void printUsageAndExit(final CmdLineParser clp) throws IOException {
241 printUsageAndExit("", clp);
242 }
243
244
245
246
247
248
249
250
251 public void printUsageAndExit(final String message, final CmdLineParser clp) throws IOException {
252 printUsage(message, clp);
253 throw die(true);
254 }
255
256
257
258
259
260
261
262
263
264 protected void printUsage(final String message, final CmdLineParser clp)
265 throws IOException {
266 errw.println(message);
267 errw.print("jgit ");
268 errw.print(commandName);
269 clp.printSingleLineUsage(errw, getResourceBundle());
270 errw.println();
271
272 errw.println();
273 clp.printUsage(errw, getResourceBundle());
274 errw.println();
275
276 errw.flush();
277 }
278
279
280
281
282
283 public ThrowingPrintWriter getErrorWriter() {
284 return errw;
285 }
286
287
288
289
290
291 protected ResourceBundle getResourceBundle() {
292 return CLIText.get().resourceBundle();
293 }
294
295
296
297
298
299
300
301
302
303
304
305 protected abstract void run() throws Exception;
306
307
308
309
310 public Repository getRepository() {
311 return db;
312 }
313
314 ObjectId resolve(final String s) throws IOException {
315 final ObjectId r = db.resolve(s);
316 if (r == null)
317 throw die(MessageFormat.format(CLIText.get().notARevision, s));
318 return r;
319 }
320
321
322
323
324
325
326 protected static Die die(final String why) {
327 return new Die(why);
328 }
329
330
331
332
333
334
335
336
337 protected static Die die(final String why, final Throwable cause) {
338 return new Die(why, cause);
339 }
340
341
342
343
344
345
346
347 protected static Die die(boolean aborted) {
348 return new Die(aborted);
349 }
350
351
352
353
354
355
356
357
358
359
360 protected static Die die(boolean aborted, final Throwable cause) {
361 return new Die(aborted, cause);
362 }
363
364 String abbreviateRef(String dst, boolean abbreviateRemote) {
365 if (dst.startsWith(R_HEADS))
366 dst = dst.substring(R_HEADS.length());
367 else if (dst.startsWith(R_TAGS))
368 dst = dst.substring(R_TAGS.length());
369 else if (abbreviateRemote && dst.startsWith(R_REMOTES))
370 dst = dst.substring(R_REMOTES.length());
371 return dst;
372 }
373
374
375
376
377
378
379
380 public static boolean containsHelp(String[] args) {
381 for (String str : args) {
382 if (str.equals("-h") || str.equals("--help")) {
383 return true;
384 }
385 }
386 return false;
387 }
388
389
390
391
392
393
394 public static class TerminatedByHelpException extends Die {
395 private static final long serialVersionUID = 1L;
396
397
398
399
400 public TerminatedByHelpException() {
401 super(true);
402 }
403
404 }
405 }