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(String name) {
133 commandName = name;
134 }
135
136
137
138
139
140
141 protected boolean requiresRepository() {
142 return true;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public void initRaw(final Repository repository, final String gitDir,
163 InputStream input, OutputStream output, OutputStream error) {
164 this.ins = input;
165 this.outs = output;
166 this.errs = error;
167 init(repository, gitDir);
168 }
169
170
171
172
173
174
175
176
177
178
179 protected void init(Repository repository, String gitDir) {
180 try {
181 final String outputEncoding = repository != null ? repository
182 .getConfig().getString("i18n", null, "logOutputEncoding") : null;
183 if (ins == null)
184 ins = new FileInputStream(FileDescriptor.in);
185 if (outs == null)
186 outs = new FileOutputStream(FileDescriptor.out);
187 if (errs == null)
188 errs = new FileOutputStream(FileDescriptor.err);
189 BufferedWriter outbufw;
190 if (outputEncoding != null)
191 outbufw = new BufferedWriter(new OutputStreamWriter(outs,
192 outputEncoding));
193 else
194 outbufw = new BufferedWriter(new OutputStreamWriter(outs));
195 outw = new ThrowingPrintWriter(outbufw);
196 BufferedWriter errbufw;
197 if (outputEncoding != null)
198 errbufw = new BufferedWriter(new OutputStreamWriter(errs,
199 outputEncoding));
200 else
201 errbufw = new BufferedWriter(new OutputStreamWriter(errs));
202 errw = new ThrowingPrintWriter(errbufw);
203 } catch (IOException e) {
204 throw die(CLIText.get().cannotCreateOutputStream);
205 }
206
207 if (repository != null && repository.getDirectory() != null) {
208 db = repository;
209 gitdir = repository.getDirectory().getAbsolutePath();
210 } else {
211 db = repository;
212 gitdir = gitDir;
213 }
214 }
215
216
217
218
219
220
221
222
223
224
225
226 public final void execute(String[] args) throws Exception {
227 parseArguments(args);
228 run();
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 protected void parseArguments(String[] args) throws IOException {
243 final CmdLineParser clp = new CmdLineParser(this);
244 help = containsHelp(args);
245 try {
246 clp.parseArgument(args);
247 } catch (CmdLineException err) {
248 this.errw.println(CLIText.fatalError(err.getMessage()));
249 if (help) {
250 printUsage("", clp);
251 }
252 throw die(true, err);
253 }
254
255 if (help) {
256 printUsage("", clp);
257 throw new TerminatedByHelpException();
258 }
259
260 argWalk = clp.getRevWalkGently();
261 }
262
263
264
265
266
267
268
269
270 public void printUsageAndExit(CmdLineParser clp) throws IOException {
271 printUsageAndExit("", clp);
272 }
273
274
275
276
277
278
279
280
281
282
283 public void printUsageAndExit(String message, CmdLineParser clp) throws IOException {
284 printUsage(message, clp);
285 throw die(true);
286 }
287
288
289
290
291
292
293
294
295
296
297
298 protected void printUsage(String message, CmdLineParser clp)
299 throws IOException {
300 errw.println(message);
301 errw.print("jgit ");
302 errw.print(commandName);
303 clp.printSingleLineUsage(errw, getResourceBundle());
304 errw.println();
305
306 errw.println();
307 clp.printUsage(errw, getResourceBundle());
308 errw.println();
309
310 errw.flush();
311 }
312
313
314
315
316
317
318
319 public ThrowingPrintWriter getErrorWriter() {
320 return errw;
321 }
322
323
324
325
326
327
328
329 public ThrowingPrintWriter getOutputWriter() {
330 return outw;
331 }
332
333
334
335
336
337
338
339 protected ResourceBundle getResourceBundle() {
340 return CLIText.get().resourceBundle();
341 }
342
343
344
345
346
347
348
349
350
351
352
353 protected abstract void run() throws Exception;
354
355
356
357
358
359
360 public Repository getRepository() {
361 return db;
362 }
363
364 ObjectId resolve(String s) throws IOException {
365 final ObjectId r = db.resolve(s);
366 if (r == null)
367 throw die(MessageFormat.format(CLIText.get().notARevision, s));
368 return r;
369 }
370
371
372
373
374
375
376
377
378 protected static Die die(String why) {
379 return new Die(why);
380 }
381
382
383
384
385
386
387
388
389
390
391 protected static Die die(String why, Throwable cause) {
392 return new Die(why, cause);
393 }
394
395
396
397
398
399
400
401
402
403
404 protected static Die die(boolean aborted) {
405 return new Die(aborted);
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419 protected static Die die(boolean aborted, Throwable cause) {
420 return new Die(aborted, cause);
421 }
422
423 String abbreviateRef(String dst, boolean abbreviateRemote) {
424 if (dst.startsWith(R_HEADS))
425 dst = dst.substring(R_HEADS.length());
426 else if (dst.startsWith(R_TAGS))
427 dst = dst.substring(R_TAGS.length());
428 else if (abbreviateRemote && dst.startsWith(R_REMOTES))
429 dst = dst.substring(R_REMOTES.length());
430 return dst;
431 }
432
433
434
435
436
437
438
439
440
441 public static boolean containsHelp(String[] args) {
442 for (String str : args) {
443 if (str.equals("-h") || str.equals("--help")) {
444 return true;
445 }
446 }
447 return false;
448 }
449
450
451
452
453
454
455 public static class TerminatedByHelpException extends Die {
456 private static final long serialVersionUID = 1L;
457
458
459
460
461 public TerminatedByHelpException() {
462 super(true);
463 }
464
465 }
466 }