View Javadoc
1   /*
2    * Copyright (C) 2013, Matthias Sohn <matthias.sohn@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.pgm;
11  
12  import java.io.IOException;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import org.eclipse.jgit.api.DescribeCommand;
17  import org.eclipse.jgit.api.Git;
18  import org.eclipse.jgit.api.errors.GitAPIException;
19  import org.eclipse.jgit.api.errors.RefNotFoundException;
20  import org.eclipse.jgit.errors.InvalidPatternException;
21  import org.eclipse.jgit.lib.ObjectId;
22  import org.eclipse.jgit.pgm.internal.CLIText;
23  import org.kohsuke.args4j.Argument;
24  import org.kohsuke.args4j.Option;
25  
26  @Command(common = true, usage = "usage_Describe")
27  class Describe extends TextBuiltin {
28  
29  	@Argument(index = 0, metaVar = "metaVar_treeish")
30  	private ObjectId tree;
31  
32  	@Option(name = "--long", usage = "usage_LongFormat")
33  	private boolean longDesc;
34  
35  	@Option(name = "--tags", usage = "usage_UseTags")
36  	private boolean useTags;
37  
38  	@Option(name = "--always", usage = "usage_AlwaysFallback")
39  	private boolean always;
40  
41  	@Option(name = "--match", usage = "usage_Match", metaVar = "metaVar_pattern")
42  	private List<String> patterns = new ArrayList<>();
43  
44  	/** {@inheritDoc} */
45  	@Override
46  	protected void run() {
47  		try (Gitit.html#Git">Git git = new Git(db)) {
48  			DescribeCommand cmd = git.describe();
49  			if (tree != null) {
50  				cmd.setTarget(tree);
51  			}
52  			cmd.setLong(longDesc);
53  			cmd.setTags(useTags);
54  			cmd.setAlways(always);
55  			cmd.setMatch(patterns.toArray(new String[0]));
56  			String result = null;
57  			try {
58  				result = cmd.call();
59  			} catch (RefNotFoundException e) {
60  				throw die(CLIText.get().noNamesFound, e);
61  			}
62  			if (result == null) {
63  				throw die(CLIText.get().noNamesFound);
64  			}
65  
66  			outw.println(result);
67  		} catch (IOException | InvalidPatternException | GitAPIException e) {
68  			throw die(e.getMessage(), e);
69  		}
70  	}
71  }