You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListTagCommand.java 1.7KB

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