View Javadoc
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> 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  package org.eclipse.jgit.api;
13  
14  import static org.eclipse.jgit.lib.Constants.HEAD;
15  import static org.eclipse.jgit.lib.Constants.R_HEADS;
16  import static org.eclipse.jgit.lib.Constants.R_REMOTES;
17  
18  import java.io.IOException;
19  import java.text.MessageFormat;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.eclipse.jgit.api.errors.GitAPIException;
26  import org.eclipse.jgit.api.errors.JGitInternalException;
27  import org.eclipse.jgit.api.errors.RefNotFoundException;
28  import org.eclipse.jgit.internal.JGitText;
29  import org.eclipse.jgit.lib.ObjectId;
30  import org.eclipse.jgit.lib.Ref;
31  import org.eclipse.jgit.lib.Repository;
32  import org.eclipse.jgit.revwalk.RevCommit;
33  import org.eclipse.jgit.revwalk.RevWalk;
34  import org.eclipse.jgit.revwalk.RevWalkUtils;
35  
36  /**
37   * Used to obtain a list of branches.
38   * <p>
39   * In case HEAD is detached (it points directly to a commit), it is also
40   * returned in the results.
41   *
42   * @see <a
43   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html"
44   *      >Git documentation about Branch</a>
45   */
46  public class ListBranchCommand extends GitCommand<List<Ref>> {
47  	private ListMode listMode;
48  
49  	private String containsCommitish;
50  
51  	/**
52  	 * The modes available for listing branches (corresponding to the -r and -a
53  	 * options)
54  	 */
55  	public enum ListMode {
56  		/**
57  		 * Corresponds to the -a option (all branches)
58  		 */
59  		ALL,
60  		/**
61  		 * Corresponds to the -r option (remote branches only)
62  		 */
63  		REMOTE;
64  	}
65  
66  	/**
67  	 * Constructor for ListBranchCommand.
68  	 *
69  	 * @param repo
70  	 *            a {@link org.eclipse.jgit.lib.Repository} object.
71  	 */
72  	protected ListBranchCommand(Repository repo) {
73  		super(repo);
74  	}
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	public List<Ref> call() throws GitAPIException {
79  		checkCallable();
80  		List<Ref> resultRefs;
81  		try {
82  			Collection<Ref> refs = new ArrayList<>();
83  
84  			// Also return HEAD if it's detached
85  			Ref head = repo.exactRef(HEAD);
86  			if (head != null && head.getLeaf().getName().equals(HEAD)) {
87  				refs.add(head);
88  			}
89  
90  			if (listMode == null) {
91  				refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS));
92  			} else if (listMode == ListMode.REMOTE) {
93  				refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_REMOTES));
94  			} else {
95  				refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS,
96  						R_REMOTES));
97  			}
98  			resultRefs = new ArrayList<>(filterRefs(refs));
99  		} catch (IOException e) {
100 			throw new JGitInternalException(e.getMessage(), e);
101 		}
102 
103 		Collections.sort(resultRefs,
104 				(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) -> o1.getName().compareTo(o2.getName()));
105 		setCallable(false);
106 		return resultRefs;
107 	}
108 
109 	private Collection<Ref> filterRefs(Collection<Ref> refs)
110 			throws RefNotFoundException, IOException {
111 		if (containsCommitish == null)
112 			return refs;
113 
114 		try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(repo)) {
115 			ObjectId resolved = repo.resolve(containsCommitish);
116 			if (resolved == null)
117 				throw new RefNotFoundException(MessageFormat.format(
118 						JGitText.get().refNotResolved, containsCommitish));
119 
120 			RevCommit containsCommit = walk.parseCommit(resolved);
121 			return RevWalkUtils.findBranchesReachableFrom(containsCommit, walk,
122 					refs);
123 		}
124 	}
125 
126 	/**
127 	 * Set the list mode
128 	 *
129 	 * @param listMode
130 	 *            optional: corresponds to the -r/-a options; by default, only
131 	 *            local branches will be listed
132 	 * @return this instance
133 	 */
134 	public ListBranchCommand setListMode(ListMode listMode) {
135 		checkCallable();
136 		this.listMode = listMode;
137 		return this;
138 	}
139 
140 	/**
141 	 * If this is set, only the branches that contain the specified commit-ish
142 	 * as an ancestor are returned.
143 	 *
144 	 * @param containsCommitish
145 	 *            a commit ID or ref name
146 	 * @return this instance
147 	 * @since 3.4
148 	 */
149 	public ListBranchCommand setContains(String containsCommitish) {
150 		checkCallable();
151 		this.containsCommitish = containsCommitish;
152 		return this;
153 	}
154 }