View Javadoc
1   /*
2    * Copyright (C) 2011, Ketan Padegaonkar <ketanpadegaonkar@gmail.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.api;
11  
12  import java.io.IOException;
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.List;
16  
17  import org.eclipse.jgit.api.errors.GitAPIException;
18  import org.eclipse.jgit.api.errors.JGitInternalException;
19  import org.eclipse.jgit.lib.Constants;
20  import org.eclipse.jgit.lib.Ref;
21  import org.eclipse.jgit.lib.Repository;
22  import org.eclipse.jgit.revwalk.RevWalk;
23  
24  /**
25   * Used to obtain a list of tags.
26   *
27   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html"
28   *      >Git documentation about Tag</a>
29   */
30  public class ListTagCommand extends GitCommand<List<Ref>> {
31  
32  	/**
33  	 * Constructor for ListTagCommand.
34  	 *
35  	 * @param repo
36  	 *            a {@link org.eclipse.jgit.lib.Repository} object.
37  	 */
38  	protected ListTagCommand(Repository repo) {
39  		super(repo);
40  	}
41  
42  	/** {@inheritDoc} */
43  	@Override
44  	public List<Ref> call() throws GitAPIException {
45  		checkCallable();
46  		List<Ref> tags = new ArrayList<>();
47  		try (RevWalklk.html#RevWalk">RevWalk revWalk = new RevWalk(repo)) {
48  			List<Ref> refList = repo.getRefDatabase()
49  					.getRefsByPrefix(Constants.R_TAGS);
50  			for (Ref ref : refList) {
51  				tags.add(ref);
52  			}
53  		} catch (IOException e) {
54  			throw new JGitInternalException(e.getMessage(), e);
55  		}
56  		Collections.sort(tags,
57  				(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) -> o1.getName().compareTo(o2.getName()));
58  		setCallable(false);
59  		return tags;
60  	}
61  
62  }