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.opt;
46
47 import java.io.File;
48 import java.io.IOException;
49 import java.text.MessageFormat;
50
51 import org.kohsuke.args4j.CmdLineException;
52 import org.kohsuke.args4j.CmdLineParser;
53 import org.kohsuke.args4j.OptionDef;
54 import org.kohsuke.args4j.spi.OptionHandler;
55 import org.kohsuke.args4j.spi.Parameters;
56 import org.kohsuke.args4j.spi.Setter;
57 import org.eclipse.jgit.dircache.DirCache;
58 import org.eclipse.jgit.dircache.DirCacheIterator;
59 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
60 import org.eclipse.jgit.errors.MissingObjectException;
61 import org.eclipse.jgit.lib.ObjectId;
62 import org.eclipse.jgit.lib.ObjectReader;
63 import org.eclipse.jgit.pgm.internal.CLIText;
64 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
65 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
66 import org.eclipse.jgit.treewalk.FileTreeIterator;
67 import org.eclipse.jgit.treewalk.WorkingTreeOptions;
68 import org.eclipse.jgit.util.FS;
69
70
71
72
73
74
75 public class AbstractTreeIteratorHandler extends
76 OptionHandler<AbstractTreeIterator> {
77 private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
78
79
80
81
82
83
84
85
86
87
88 public AbstractTreeIteratorHandler(final CmdLineParser parser,
89 final OptionDef option,
90 final Setter<? super AbstractTreeIterator> setter) {
91 super(parser, option, setter);
92 clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
93 }
94
95 @Override
96 public int parseArguments(final Parameters params) throws CmdLineException {
97 final String name = params.getParameter(0);
98
99 if (new File(name).isDirectory()) {
100 setter.addValue(new FileTreeIterator(
101 new File(name),
102 FS.DETECTED,
103 clp.getRepository().getConfig().get(WorkingTreeOptions.KEY)));
104 return 1;
105 }
106
107 if (new File(name).isFile()) {
108 final DirCache dirc;
109 try {
110 dirc = DirCache.read(new File(name), FS.DETECTED);
111 } catch (IOException e) {
112 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notAnIndexFile, name), e);
113 }
114 setter.addValue(new DirCacheIterator(dirc));
115 return 1;
116 }
117
118 final ObjectId id;
119 try {
120 id = clp.getRepository().resolve(name);
121 } catch (IOException e) {
122 throw new CmdLineException(clp, e.getMessage());
123 }
124 if (id == null)
125 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notATree, name));
126
127 final CanonicalTreeParser p = new CanonicalTreeParser();
128 try (ObjectReader curs = clp.getRepository().newObjectReader()) {
129 p.reset(curs, clp.getRevWalk().parseTree(id));
130 } catch (MissingObjectException e) {
131 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notATree, name));
132 } catch (IncorrectObjectTypeException e) {
133 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notATree, name));
134 } catch (IOException e) {
135 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().cannotReadBecause, name, e.getMessage()));
136 }
137
138 setter.addValue(p);
139 return 1;
140 }
141
142 @Override
143 public String getDefaultMetaVariable() {
144 return CLIText.get().metaVar_treeish;
145 }
146 }