View Javadoc
1   /*
2    * Copyright (C) 2009, Daniel Cheng (aka SDiZ) <git@sdiz.net>
3    * Copyright (C) 2009, Daniel Cheng (aka SDiZ) <j16sdiz+freenet@gmail.com>
4    * Copyright (C) 2015 Thomas Meyer <thomas@m3y3r.de> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.pgm;
14  
15  import java.io.IOException;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.Ref;
21  import org.eclipse.jgit.pgm.internal.CLIText;
22  import org.eclipse.jgit.pgm.opt.CmdLineParser;
23  import org.kohsuke.args4j.Argument;
24  import org.kohsuke.args4j.CmdLineException;
25  import org.kohsuke.args4j.Option;
26  
27  @Command(usage = "usage_RevParse")
28  class RevParse extends TextBuiltin {
29  	@Option(name = "--all", usage = "usage_RevParseAll")
30  	boolean all;
31  
32  	@Option(name = "--verify", usage = "usage_RevParseVerify")
33  	boolean verify;
34  
35  	@Argument(index = 0, metaVar = "metaVar_commitish")
36  	private List<ObjectId> commits = new ArrayList<>();
37  
38  	/** {@inheritDoc} */
39  	@Override
40  	protected void run() {
41  		try {
42  			if (all) {
43  				for (Ref r : db.getRefDatabase().getRefs()) {
44  					ObjectId objectId = r.getObjectId();
45  					// getRefs skips dangling symrefs, so objectId should never
46  					// be null.
47  					if (objectId == null) {
48  						throw new NullPointerException();
49  					}
50  					outw.println(objectId.name());
51  				}
52  			} else {
53  				if (verify && commits.size() > 1) {
54  					final CmdLineParser clp = new CmdLineParser(this);
55  					throw new CmdLineException(clp,
56  							CLIText.format(CLIText.get().needSingleRevision));
57  				}
58  
59  				for (ObjectId o : commits) {
60  					outw.println(o.name());
61  				}
62  			}
63  		} catch (IOException | CmdLineException e) {
64  			throw die(e.getMessage(), e);
65  		}
66  	}
67  }