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