1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.pgm.opt;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.text.MessageFormat;
17
18 import org.eclipse.jgit.dircache.DirCache;
19 import org.eclipse.jgit.dircache.DirCacheIterator;
20 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
21 import org.eclipse.jgit.errors.MissingObjectException;
22 import org.eclipse.jgit.lib.ObjectId;
23 import org.eclipse.jgit.lib.ObjectReader;
24 import org.eclipse.jgit.pgm.internal.CLIText;
25 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
26 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
27 import org.eclipse.jgit.treewalk.FileTreeIterator;
28 import org.eclipse.jgit.treewalk.WorkingTreeOptions;
29 import org.eclipse.jgit.util.FS;
30 import org.kohsuke.args4j.CmdLineException;
31 import org.kohsuke.args4j.CmdLineParser;
32 import org.kohsuke.args4j.OptionDef;
33 import org.kohsuke.args4j.spi.OptionHandler;
34 import org.kohsuke.args4j.spi.Parameters;
35 import org.kohsuke.args4j.spi.Setter;
36
37
38
39
40
41
42
43 public class AbstractTreeIteratorHandler extends
44 OptionHandler<AbstractTreeIterator> {
45 private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public AbstractTreeIteratorHandler(final CmdLineParser parser,
60 final OptionDef option,
61 final Setter<? super AbstractTreeIterator> setter) {
62 super(parser, option, setter);
63 clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
64 }
65
66
67 @Override
68 public int parseArguments(Parameters params) throws CmdLineException {
69 final String name = params.getParameter(0);
70
71 if (new File(name).isDirectory()) {
72 setter.addValue(new FileTreeIterator(
73 new File(name),
74 FS.DETECTED,
75 clp.getRepository().getConfig().get(WorkingTreeOptions.KEY)));
76 return 1;
77 }
78
79 if (new File(name).isFile()) {
80 final DirCache dirc;
81 try {
82 dirc = DirCache.read(new File(name), FS.DETECTED);
83 } catch (IOException e) {
84 throw new CmdLineException(clp, MessageFormat.format(CLIText.get().notAnIndexFile, name), e);
85 }
86 setter.addValue(new DirCacheIterator(dirc));
87 return 1;
88 }
89
90 final ObjectId id;
91 try {
92 id = clp.getRepository().resolve(name);
93 } catch (IOException e) {
94 throw new CmdLineException(clp, CLIText.format(e.getMessage()));
95 }
96 if (id == null)
97 throw new CmdLineException(clp,
98 CLIText.format(CLIText.get().notATree), name);
99
100 final CanonicalTreeParser p = new CanonicalTreeParser();
101 try (ObjectReader curs = clp.getRepository().newObjectReader()) {
102 p.reset(curs, clp.getRevWalk().parseTree(id));
103 } catch (MissingObjectException | IncorrectObjectTypeException e) {
104 CmdLineException cle = new CmdLineException(clp,
105 CLIText.format(CLIText.get().notATree), name);
106 cle.initCause(e);
107 throw cle;
108 } catch (IOException e) {
109 throw new CmdLineException(clp,
110 CLIText.format(CLIText.get().cannotReadBecause), name,
111 e.getMessage());
112 }
113
114 setter.addValue(p);
115 return 1;
116 }
117
118
119 @Override
120 public String getDefaultMetaVariable() {
121 return CLIText.get().metaVar_treeish;
122 }
123 }