Add option handler for RevTree values
[egit/zawir.git] / org.spearce.jgit.pgm / src / org / spearce / jgit / pgm / opt / RevTreeHandler.java
blobde643365cacf0a35140e3234b72418768a07b40c
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.pgm.opt;
40 import java.io.IOException;
42 import org.kohsuke.args4j.CmdLineException;
43 import org.kohsuke.args4j.CmdLineParser;
44 import org.kohsuke.args4j.OptionDef;
45 import org.kohsuke.args4j.spi.OptionHandler;
46 import org.kohsuke.args4j.spi.Parameters;
47 import org.kohsuke.args4j.spi.Setter;
48 import org.spearce.jgit.errors.IncorrectObjectTypeException;
49 import org.spearce.jgit.errors.MissingObjectException;
50 import org.spearce.jgit.lib.ObjectId;
51 import org.spearce.jgit.revwalk.RevTree;
53 /**
54 * Custom argument handler {@link RevTree} from string values.
55 * <p>
56 * Assumes the parser has been initialized with a Repository.
58 public class RevTreeHandler extends OptionHandler<RevTree> {
59 private final org.spearce.jgit.pgm.opt.CmdLineParser clp;
61 /**
62 * Create a new handler for the command name.
63 * <p>
64 * This constructor is used only by args4j.
66 * @param parser
67 * @param option
68 * @param setter
70 public RevTreeHandler(final CmdLineParser parser, final OptionDef option,
71 final Setter<? super RevTree> setter) {
72 super(parser, option, setter);
73 clp = (org.spearce.jgit.pgm.opt.CmdLineParser) parser;
76 @Override
77 public int parseArguments(final Parameters params) throws CmdLineException {
78 final String name = params.getParameter(0);
79 final ObjectId id;
80 try {
81 id = clp.getRepository().resolve(name);
82 } catch (IOException e) {
83 throw new CmdLineException(e.getMessage());
85 if (id == null)
86 throw new CmdLineException(name + " is not a tree");
88 final RevTree c;
89 try {
90 c = clp.getRevWalk().parseTree(id);
91 } catch (ClassCastException e) {
92 throw new CmdLineException(name + " is not a tree");
93 } catch (MissingObjectException e) {
94 throw new CmdLineException(name + " is not a tree");
95 } catch (IncorrectObjectTypeException e) {
96 throw new CmdLineException(name + " is not a tree");
97 } catch (IOException e) {
98 throw new CmdLineException("cannot read " + name + ": "
99 + e.getMessage());
101 setter.addValue(c);
102 return 1;
105 @Override
106 public String getDefaultMetaVariable() {
107 return "tree-ish";