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.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   * Custom argument handler {@link org.eclipse.jgit.revwalk.RevCommit} from
31   * string values.
32   * <p>
33   * Assumes the parser has been initialized with a Repository.
34   */
35  public class RevCommitHandler extends OptionHandler<RevCommit> {
36  	private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;
37  
38  	/**
39  	 * Create a new handler for the command name.
40  	 * <p>
41  	 * This constructor is used only by args4j.
42  	 *
43  	 * @param parser
44  	 *            a {@link org.kohsuke.args4j.CmdLineParser} object.
45  	 * @param option
46  	 *            a {@link org.kohsuke.args4j.OptionDef} object.
47  	 * @param setter
48  	 *            a {@link org.kohsuke.args4j.spi.Setter} object.
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  	/** {@inheritDoc} */
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("^")) { //$NON-NLS-1$
63  			name = name.substring(1);
64  			interesting = false;
65  		}
66  
67  		final int dot2 = name.indexOf(".."); //$NON-NLS-1$
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 	/** {@inheritDoc} */
120 	@Override
121 	public String getDefaultMetaVariable() {
122 		return CLIText.get().metaVar_commitish;
123 	}
124 }