View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
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.RevTree;
21  import org.kohsuke.args4j.CmdLineException;
22  import org.kohsuke.args4j.CmdLineParser;
23  import org.kohsuke.args4j.OptionDef;
24  import org.kohsuke.args4j.spi.OptionHandler;
25  import org.kohsuke.args4j.spi.Parameters;
26  import org.kohsuke.args4j.spi.Setter;
27  
28  /**
29   * Custom argument handler {@link org.eclipse.jgit.revwalk.RevTree} from string
30   * values.
31   * <p>
32   * Assumes the parser has been initialized with a Repository.
33   */
34  public class RevTreeHandler extends OptionHandler<RevTree> {
35  	private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
36  
37  	/**
38  	 * Create a new handler for the command name.
39  	 * <p>
40  	 * This constructor is used only by args4j.
41  	 *
42  	 * @param parser
43  	 *            a {@link org.kohsuke.args4j.CmdLineParser} object.
44  	 * @param option
45  	 *            a {@link org.kohsuke.args4j.OptionDef} object.
46  	 * @param setter
47  	 *            a {@link org.kohsuke.args4j.spi.Setter} object.
48  	 */
49  	public RevTreeHandler(final CmdLineParser parser, final OptionDef option,
50  			final Setter<? super RevTree> setter) {
51  		super(parser, option, setter);
52  		clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
53  	}
54  
55  	/** {@inheritDoc} */
56  	@Override
57  	public int parseArguments(Parameters params) throws CmdLineException {
58  		final String name = params.getParameter(0);
59  		final ObjectId id;
60  		try {
61  			id = clp.getRepository().resolve(name);
62  		} catch (IOException e) {
63  			throw new CmdLineException(clp, CLIText.format(e.getMessage()));
64  		}
65  		if (id == null)
66  			throw new CmdLineException(clp,
67  					CLIText.format(CLIText.get().notATree), name);
68  
69  		final RevTree c;
70  		try {
71  			c = clp.getRevWalk().parseTree(id);
72  		} catch (MissingObjectException | IncorrectObjectTypeException e) {
73  			CmdLineException cle = new CmdLineException(clp,
74  					CLIText.format(CLIText.get().notATree), name);
75  			cle.initCause(e);
76  			throw cle;
77  		} catch (IOException e) {
78  			throw new CmdLineException(clp,
79  					CLIText.format(CLIText.get().cannotReadBecause), name,
80  					e.getMessage());
81  		}
82  		setter.addValue(c);
83  		return 1;
84  	}
85  
86  	/** {@inheritDoc} */
87  	@Override
88  	public String getDefaultMetaVariable() {
89  		return CLIText.get().metaVar_treeish;
90  	}
91  }