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 try {
216 clp.parseArgument(args);
217 } catch (CmdLineException err) {
218 if (!help) {
219 this.errw.println(MessageFormat.format(CLIText.get().fatalError, err.getMessage()));
220 throw die(true);
221 }
222 }
223
224 if (help) {
225 printUsageAndExit(clp);
226 }
227
228 argWalk = clp.getRevWalkGently();
229 }
230
231
232
233
234
235
236
237 public void printUsageAndExit(final CmdLineParser clp) throws IOException {
238 printUsageAndExit("", clp);
239 }
240
241
242
243
244
245
246
247
248 public void printUsageAndExit(final String message, final CmdLineParser clp) throws IOException {
249 errw.println(message);
250 errw.print("jgit ");
251 errw.print(commandName);
252 clp.printSingleLineUsage(errw, getResourceBundle());
253 errw.println();
254
255 errw.println();
256 clp.printUsage(errw, getResourceBundle());
257 errw.println();
258
259 errw.flush();
260 throw die(true);
261 }
262
263
264
265
266
267 protected ResourceBundle getResourceBundle() {
268 return CLIText.get().resourceBundle();
269 }
270
271
272
273
274
275
276
277
278
279
280
281 protected abstract void run() throws Exception;
282
283
284
285
286 public Repository getRepository() {
287 return db;
288 }
289
290 ObjectId resolve(final String s) throws IOException {
291 final ObjectId r = db.resolve(s);
292 if (r == null)
293 throw die(MessageFormat.format(CLIText.get().notARevision, s));
294 return r;
295 }
296
297
298
299
300
301
302 protected static Die die(final String why) {
303 return new Die(why);
304 }
305
306
307
308
309
310
311
312
313 protected static Die die(final String why, final Throwable cause) {
314 return new Die(why, cause);
315 }
316
317
318
319
320
321
322
323 protected static Die die(boolean aborted) {
324 return new Die(aborted);
325 }
326
327 String abbreviateRef(String dst, boolean abbreviateRemote) {
328 if (dst.startsWith(R_HEADS))
329 dst = dst.substring(R_HEADS.length());
330 else if (dst.startsWith(R_TAGS))
331 dst = dst.substring(R_TAGS.length());
332 else if (abbreviateRemote && dst.startsWith(R_REMOTES))
333 dst = dst.substring(R_REMOTES.length());
334 return dst;
335 }
336 }