Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ListBranchCommand.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  4. * Copyright (C) 2014, Robin Stocker <robin@nibor.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.api;
  46. import static org.eclipse.jgit.lib.Constants.HEAD;
  47. import static org.eclipse.jgit.lib.Constants.R_HEADS;
  48. import static org.eclipse.jgit.lib.Constants.R_REMOTES;
  49. import java.io.IOException;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.Collection;
  53. import java.util.Collections;
  54. import java.util.Comparator;
  55. import java.util.List;
  56. import org.eclipse.jgit.api.errors.GitAPIException;
  57. import org.eclipse.jgit.api.errors.JGitInternalException;
  58. import org.eclipse.jgit.api.errors.RefNotFoundException;
  59. import org.eclipse.jgit.internal.JGitText;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.Ref;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.revwalk.RevCommit;
  64. import org.eclipse.jgit.revwalk.RevWalk;
  65. import org.eclipse.jgit.revwalk.RevWalkUtils;
  66. /**
  67. * Used to obtain a list of branches.
  68. * <p>
  69. * In case HEAD is detached (it points directly to a commit), it is also
  70. * returned in the results.
  71. *
  72. * @see <a
  73. * href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
  74. * >Git documentation about Branch</a>
  75. */
  76. public class ListBranchCommand extends GitCommand<List<Ref>> {
  77. private ListMode listMode;
  78. private String containsCommitish;
  79. /**
  80. * The modes available for listing branches (corresponding to the -r and -a
  81. * options)
  82. */
  83. public enum ListMode {
  84. /**
  85. * Corresponds to the -a option (all branches)
  86. */
  87. ALL,
  88. /**
  89. * Corresponds to the -r option (remote branches only)
  90. */
  91. REMOTE;
  92. }
  93. /**
  94. * Constructor for ListBranchCommand.
  95. *
  96. * @param repo
  97. * a {@link org.eclipse.jgit.lib.Repository} object.
  98. */
  99. protected ListBranchCommand(Repository repo) {
  100. super(repo);
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public List<Ref> call() throws GitAPIException {
  105. checkCallable();
  106. List<Ref> resultRefs;
  107. try {
  108. Collection<Ref> refs = new ArrayList<>();
  109. // Also return HEAD if it's detached
  110. Ref head = repo.exactRef(HEAD);
  111. if (head != null && head.getLeaf().getName().equals(HEAD)) {
  112. refs.add(head);
  113. }
  114. if (listMode == null) {
  115. refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS));
  116. } else if (listMode == ListMode.REMOTE) {
  117. refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_REMOTES));
  118. } else {
  119. refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS,
  120. R_REMOTES));
  121. }
  122. resultRefs = new ArrayList<>(filterRefs(refs));
  123. } catch (IOException e) {
  124. throw new JGitInternalException(e.getMessage(), e);
  125. }
  126. Collections.sort(resultRefs, new Comparator<Ref>() {
  127. @Override
  128. public int compare(Ref o1, Ref o2) {
  129. return o1.getName().compareTo(o2.getName());
  130. }
  131. });
  132. setCallable(false);
  133. return resultRefs;
  134. }
  135. private Collection<Ref> filterRefs(Collection<Ref> refs)
  136. throws RefNotFoundException, IOException {
  137. if (containsCommitish == null)
  138. return refs;
  139. try (RevWalk walk = new RevWalk(repo)) {
  140. ObjectId resolved = repo.resolve(containsCommitish);
  141. if (resolved == null)
  142. throw new RefNotFoundException(MessageFormat.format(
  143. JGitText.get().refNotResolved, containsCommitish));
  144. RevCommit containsCommit = walk.parseCommit(resolved);
  145. return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
  146. refs);
  147. }
  148. }
  149. /**
  150. * Set the list mode
  151. *
  152. * @param listMode
  153. * optional: corresponds to the -r/-a options; by default, only
  154. * local branches will be listed
  155. * @return this instance
  156. */
  157. public ListBranchCommand setListMode(ListMode listMode) {
  158. checkCallable();
  159. this.listMode = listMode;
  160. return this;
  161. }
  162. /**
  163. * If this is set, only the branches that contain the specified commit-ish
  164. * as an ancestor are returned.
  165. *
  166. * @param containsCommitish
  167. * a commit ID or ref name
  168. * @return this instance
  169. * @since 3.4
  170. */
  171. public ListBranchCommand setContains(String containsCommitish) {
  172. checkCallable();
  173. this.containsCommitish = containsCommitish;
  174. return this;
  175. }
  176. }