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.

LogCommand.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. import org.eclipse.jgit.JGitText;
  49. import org.eclipse.jgit.api.errors.JGitInternalException;
  50. import org.eclipse.jgit.api.errors.NoHeadException;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.lib.AnyObjectId;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.revwalk.RevWalk;
  60. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  61. import org.eclipse.jgit.treewalk.filter.PathFilter;
  62. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  63. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  64. /**
  65. * A class used to execute a {@code Log} command. It has setters for all
  66. * supported options and arguments of this command and a {@link #call()} method
  67. * to finally execute the command. Each instance of this class should only be
  68. * used for one invocation of the command (means: one call to {@link #call()})
  69. * <p>
  70. * This is currently a very basic implementation which takes only one starting
  71. * revision as option.
  72. *
  73. * TODO: add more options (revision ranges, sorting, ...)
  74. *
  75. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
  76. * >Git documentation about Log</a>
  77. */
  78. public class LogCommand extends GitCommand<Iterable<RevCommit>> {
  79. private RevWalk walk;
  80. private boolean startSpecified = false;
  81. private final List<PathFilter> pathFilters = new ArrayList<PathFilter>();
  82. /**
  83. * @param repo
  84. */
  85. protected LogCommand(Repository repo) {
  86. super(repo);
  87. walk = new RevWalk(repo);
  88. }
  89. /**
  90. * Executes the {@code Log} command with all the options and parameters
  91. * collected by the setter methods (e.g. {@link #add(AnyObjectId)},
  92. * {@link #not(AnyObjectId)}, ..) of this class. Each instance of this class
  93. * should only be used for one invocation of the command. Don't call this
  94. * method twice on an instance.
  95. *
  96. * @return an iteration over RevCommits
  97. */
  98. public Iterable<RevCommit> call() throws NoHeadException,
  99. JGitInternalException {
  100. checkCallable();
  101. if (pathFilters.size() > 0)
  102. walk.setTreeFilter(AndTreeFilter.create(
  103. PathFilterGroup.create(pathFilters), TreeFilter.ANY_DIFF));
  104. if (!startSpecified) {
  105. try {
  106. ObjectId headId = repo.resolve(Constants.HEAD);
  107. if (headId == null)
  108. throw new NoHeadException(
  109. JGitText.get().noHEADExistsAndNoExplicitStartingRevisionWasSpecified);
  110. add(headId);
  111. } catch (IOException e) {
  112. // all exceptions thrown by add() shouldn't occur and represent
  113. // severe low-level exception which are therefore wrapped
  114. throw new JGitInternalException(
  115. JGitText.get().anExceptionOccurredWhileTryingToAddTheIdOfHEAD,
  116. e);
  117. }
  118. }
  119. setCallable(false);
  120. return walk;
  121. }
  122. /**
  123. * Mark a commit to start graph traversal from.
  124. *
  125. * @see RevWalk#markStart(RevCommit)
  126. * @param start
  127. * @return {@code this}
  128. * @throws MissingObjectException
  129. * the commit supplied is not available from the object
  130. * database. This usually indicates the supplied commit is
  131. * invalid, but the reference was constructed during an earlier
  132. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  133. * @throws IncorrectObjectTypeException
  134. * the object was not parsed yet and it was discovered during
  135. * parsing that it is not actually a commit. This usually
  136. * indicates the caller supplied a non-commit SHA-1 to
  137. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  138. * @throws JGitInternalException
  139. * a low-level exception of JGit has occurred. The original
  140. * exception can be retrieved by calling
  141. * {@link Exception#getCause()}. Expect only
  142. * {@code IOException's} to be wrapped. Subclasses of
  143. * {@link IOException} (e.g. {@link MissingObjectException}) are
  144. * typically not wrapped here but thrown as original exception
  145. */
  146. public LogCommand add(AnyObjectId start) throws MissingObjectException,
  147. IncorrectObjectTypeException, JGitInternalException {
  148. return add(true, start);
  149. }
  150. /**
  151. * Same as {@code --not start}, or {@code ^start}
  152. *
  153. * @param start
  154. * @return {@code this}
  155. * @throws MissingObjectException
  156. * the commit supplied is not available from the object
  157. * database. This usually indicates the supplied commit is
  158. * invalid, but the reference was constructed during an earlier
  159. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  160. * @throws IncorrectObjectTypeException
  161. * the object was not parsed yet and it was discovered during
  162. * parsing that it is not actually a commit. This usually
  163. * indicates the caller supplied a non-commit SHA-1 to
  164. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  165. * @throws JGitInternalException
  166. * a low-level exception of JGit has occurred. The original
  167. * exception can be retrieved by calling
  168. * {@link Exception#getCause()}. Expect only
  169. * {@code IOException's} to be wrapped. Subclasses of
  170. * {@link IOException} (e.g. {@link MissingObjectException}) are
  171. * typically not wrapped here but thrown as original exception
  172. */
  173. public LogCommand not(AnyObjectId start) throws MissingObjectException,
  174. IncorrectObjectTypeException, JGitInternalException {
  175. return add(false, start);
  176. }
  177. /**
  178. * Adds the range {@code since..until}
  179. *
  180. * @param since
  181. * @param until
  182. * @return {@code this}
  183. * @throws MissingObjectException
  184. * the commit supplied is not available from the object
  185. * database. This usually indicates the supplied commit is
  186. * invalid, but the reference was constructed during an earlier
  187. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  188. * @throws IncorrectObjectTypeException
  189. * the object was not parsed yet and it was discovered during
  190. * parsing that it is not actually a commit. This usually
  191. * indicates the caller supplied a non-commit SHA-1 to
  192. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  193. * @throws JGitInternalException
  194. * a low-level exception of JGit has occurred. The original
  195. * exception can be retrieved by calling
  196. * {@link Exception#getCause()}. Expect only
  197. * {@code IOException's} to be wrapped. Subclasses of
  198. * {@link IOException} (e.g. {@link MissingObjectException}) are
  199. * typically not wrapped here but thrown as original exception
  200. */
  201. public LogCommand addRange(AnyObjectId since, AnyObjectId until)
  202. throws MissingObjectException, IncorrectObjectTypeException,
  203. JGitInternalException {
  204. return not(since).add(until);
  205. }
  206. /**
  207. * Add all refs as commits to start the graph traversal from.
  208. *
  209. * @see #add(AnyObjectId)
  210. * @return {@code this}
  211. * @throws IOException
  212. * the references could not be accessed
  213. */
  214. public LogCommand all() throws IOException {
  215. for (Ref ref : getRepository().getAllRefs().values()) {
  216. ObjectId objectId = ref.getPeeledObjectId();
  217. if (objectId == null)
  218. objectId = ref.getObjectId();
  219. add(objectId);
  220. }
  221. return this;
  222. }
  223. /**
  224. * Show only commits that affect any of the specified paths. The path must
  225. * either name a file or a directory exactly. Note that regex expressions or
  226. * wildcards are not supported.
  227. *
  228. * @param path
  229. * a path is relative to the top level of the repository
  230. * @return {@code this}
  231. */
  232. public LogCommand addPath(String path) {
  233. checkCallable();
  234. pathFilters.add(PathFilter.create(path));
  235. return this;
  236. }
  237. private LogCommand add(boolean include, AnyObjectId start)
  238. throws MissingObjectException, IncorrectObjectTypeException,
  239. JGitInternalException {
  240. checkCallable();
  241. try {
  242. if (include) {
  243. walk.markStart(walk.lookupCommit(start));
  244. startSpecified = true;
  245. } else
  246. walk.markUninteresting(walk.lookupCommit(start));
  247. return this;
  248. } catch (MissingObjectException e) {
  249. throw e;
  250. } catch (IncorrectObjectTypeException e) {
  251. throw e;
  252. } catch (IOException e) {
  253. throw new JGitInternalException(MessageFormat.format(
  254. JGitText.get().exceptionOccurredDuringAddingOfOptionToALogCommand
  255. , start), e);
  256. }
  257. }
  258. }