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 package org.eclipse.jgit.pgm.opt;
45
46 import java.lang.reflect.Field;
47 import java.util.ArrayList;
48
49 import org.kohsuke.args4j.Argument;
50 import org.kohsuke.args4j.CmdLineException;
51 import org.kohsuke.args4j.IllegalAnnotationError;
52 import org.kohsuke.args4j.NamedOptionDef;
53 import org.kohsuke.args4j.Option;
54 import org.kohsuke.args4j.OptionDef;
55 import org.kohsuke.args4j.spi.OptionHandler;
56 import org.kohsuke.args4j.spi.Setter;
57 import org.eclipse.jgit.lib.ObjectId;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.pgm.TextBuiltin;
60 import org.eclipse.jgit.pgm.internal.CLIText;
61 import org.eclipse.jgit.revwalk.RevCommit;
62 import org.eclipse.jgit.revwalk.RevTree;
63 import org.eclipse.jgit.revwalk.RevWalk;
64 import org.eclipse.jgit.transport.RefSpec;
65 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
66
67
68
69
70
71
72
73
74
75 public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
76 static {
77 registerHandler(AbstractTreeIterator.class,
78 AbstractTreeIteratorHandler.class);
79 registerHandler(ObjectId.class, ObjectIdHandler.class);
80 registerHandler(RefSpec.class, RefSpecHandler.class);
81 registerHandler(RevCommit.class, RevCommitHandler.class);
82 registerHandler(RevTree.class, RevTreeHandler.class);
83 }
84
85 private final Repository db;
86
87 private RevWalk walk;
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public CmdLineParser(final Object bean) {
102 this(bean, null);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public CmdLineParser(final Object bean, Repository repo) {
119 super(bean);
120 if (repo == null && bean instanceof TextBuiltin)
121 repo = ((TextBuiltin) bean).getRepository();
122 this.db = repo;
123 }
124
125 @Override
126 public void parseArgument(final String... args) throws CmdLineException {
127 final ArrayList<String> tmp = new ArrayList<String>(args.length);
128 for (int argi = 0; argi < args.length; argi++) {
129 final String str = args[argi];
130 if (str.equals("--")) {
131 while (argi < args.length)
132 tmp.add(args[argi++]);
133 break;
134 }
135
136 if (str.startsWith("--")) {
137 final int eq = str.indexOf('=');
138 if (eq > 0) {
139 tmp.add(str.substring(0, eq));
140 tmp.add(str.substring(eq + 1));
141 continue;
142 }
143 }
144
145 tmp.add(str);
146 }
147
148 super.parseArgument(tmp.toArray(new String[tmp.size()]));
149 }
150
151
152
153
154
155
156 public Repository getRepository() {
157 if (db == null)
158 throw new IllegalStateException(CLIText.get().noGitRepositoryConfigured);
159 return db;
160 }
161
162
163
164
165
166
167 public RevWalk getRevWalk() {
168 if (walk == null)
169 walk = new RevWalk(getRepository());
170 return walk;
171 }
172
173
174
175
176
177
178
179
180 public RevWalk getRevWalkGently() {
181 return walk;
182 }
183
184 static class MyOptionDef extends OptionDef {
185
186 public MyOptionDef(OptionDef o) {
187 super(o.usage(), o.metaVar(), o.required(), o.handler(), o
188 .isMultiValued());
189 }
190
191 @Override
192 public String toString() {
193 if (metaVar() == null)
194 return "ARG";
195 try {
196 Field field = CLIText.class.getField(metaVar());
197 String ret = field.get(CLIText.get()).toString();
198 return ret;
199 } catch (Exception e) {
200 e.printStackTrace(System.err);
201 return metaVar();
202 }
203 }
204 }
205
206 @Override
207 protected OptionHandler createOptionHandler(OptionDef o, Setter setter) {
208 if (o instanceof NamedOptionDef)
209 return super.createOptionHandler(o, setter);
210 else
211 return super.createOptionHandler(new MyOptionDef(o), setter);
212
213 }
214 }