1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.pgm.opt;
13
14 import java.io.IOException;
15
16 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
17 import org.eclipse.jgit.errors.MissingObjectException;
18 import org.eclipse.jgit.lib.ObjectId;
19 import org.eclipse.jgit.pgm.internal.CLIText;
20 import org.eclipse.jgit.revwalk.RevCommit;
21 import org.eclipse.jgit.revwalk.RevFlag;
22 import org.kohsuke.args4j.CmdLineException;
23 import org.kohsuke.args4j.CmdLineParser;
24 import org.kohsuke.args4j.OptionDef;
25 import org.kohsuke.args4j.spi.OptionHandler;
26 import org.kohsuke.args4j.spi.Parameters;
27 import org.kohsuke.args4j.spi.Setter;
28
29
30
31
32
33
34
35 public class RevCommitHandler extends OptionHandler<RevCommit> {
36 private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public RevCommitHandler(final CmdLineParser parser, final OptionDef option,
51 final Setter<? super RevCommit> setter) {
52 super(parser, option, setter);
53 clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
54 }
55
56
57 @Override
58 public int parseArguments(Parameters params) throws CmdLineException {
59 String name = params.getParameter(0);
60
61 boolean interesting = true;
62 if (name.startsWith("^")) {
63 name = name.substring(1);
64 interesting = false;
65 }
66
67 final int dot2 = name.indexOf("..");
68 if (dot2 != -1) {
69 if (!option.isMultiValued())
70 throw new CmdLineException(clp,
71 CLIText.format(CLIText.get().onlyOneMetaVarExpectedIn),
72 option.metaVar(), name);
73
74 final String left = name.substring(0, dot2);
75 final String right = name.substring(dot2 + 2);
76 addOne(left, false);
77 addOne(right, true);
78 return 1;
79 }
80
81 addOne(name, interesting);
82 return 1;
83 }
84
85 private void addOne(String name, boolean interesting)
86 throws CmdLineException {
87 final ObjectId id;
88 try {
89 id = clp.getRepository().resolve(name);
90 } catch (IOException e) {
91 throw new CmdLineException(clp, CLIText.format(e.getMessage()));
92 }
93 if (id == null)
94 throw new CmdLineException(clp,
95 CLIText.format(CLIText.get().notACommit), name);
96
97 final RevCommit c;
98 try {
99 c = clp.getRevWalk().parseCommit(id);
100 } catch (MissingObjectException | IncorrectObjectTypeException e) {
101 CmdLineException cle = new CmdLineException(clp,
102 CLIText.format(CLIText.get().notACommit), name);
103 cle.initCause(e);
104 throw cle;
105 } catch (IOException e) {
106 throw new CmdLineException(clp,
107 CLIText.format(CLIText.get().cannotReadBecause), name,
108 e.getMessage());
109 }
110
111 if (interesting)
112 c.remove(RevFlag.UNINTERESTING);
113 else
114 c.add(RevFlag.UNINTERESTING);
115
116 setter.addValue(c);
117 }
118
119
120 @Override
121 public String getDefaultMetaVariable() {
122 return CLIText.get().metaVar_commitish;
123 }
124 }