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 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 org.eclipse.jgit.JGitText;
  47. import org.eclipse.jgit.api.errors.JGitInternalException;
  48. import org.eclipse.jgit.api.errors.NoHeadException;
  49. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.eclipse.jgit.revwalk.RevWalk;
  57. /**
  58. * A class used to execute a {@code Log} command. It has setters for all
  59. * supported options and arguments of this command and a {@link #call()} method
  60. * to finally execute the command. Each instance of this class should only be
  61. * used for one invocation of the command (means: one call to {@link #call()})
  62. * <p>
  63. * This is currently a very basic implementation which takes only one starting
  64. * revision as option.
  65. *
  66. * TODO: add more options (revision ranges, sorting, ...)
  67. *
  68. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
  69. * >Git documentation about Log</a>
  70. */
  71. public class LogCommand extends GitCommand<Iterable<RevCommit>> {
  72. private RevWalk walk;
  73. private boolean startSpecified = false;
  74. /**
  75. * @param repo
  76. */
  77. protected LogCommand(Repository repo) {
  78. super(repo);
  79. walk = new RevWalk(repo);
  80. }
  81. /**
  82. * Executes the {@code Log} command with all the options and parameters
  83. * collected by the setter methods (e.g. {@link #add(AnyObjectId)},
  84. * {@link #not(AnyObjectId)}, ..) of this class. Each instance of this class
  85. * should only be used for one invocation of the command. Don't call this
  86. * method twice on an instance.
  87. *
  88. * @return an iteration over RevCommits
  89. */
  90. public Iterable<RevCommit> call() throws NoHeadException,
  91. JGitInternalException {
  92. checkCallable();
  93. if (!startSpecified) {
  94. try {
  95. ObjectId headId = repo.resolve(Constants.HEAD);
  96. if (headId == null)
  97. throw new NoHeadException(
  98. JGitText.get().noHEADExistsAndNoExplicitStartingRevisionWasSpecified);
  99. add(headId);
  100. } catch (IOException e) {
  101. // all exceptions thrown by add() shouldn't occur and represent
  102. // severe low-level exception which are therefore wrapped
  103. throw new JGitInternalException(
  104. JGitText.get().anExceptionOccurredWhileTryingToAddTheIdOfHEAD,
  105. e);
  106. }
  107. }
  108. setCallable(false);
  109. return walk;
  110. }
  111. /**
  112. * Mark a commit to start graph traversal from.
  113. *
  114. * @see RevWalk#markStart(RevCommit)
  115. * @param start
  116. * @return {@code this}
  117. * @throws MissingObjectException
  118. * the commit supplied is not available from the object
  119. * database. This usually indicates the supplied commit is
  120. * invalid, but the reference was constructed during an earlier
  121. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  122. * @throws IncorrectObjectTypeException
  123. * the object was not parsed yet and it was discovered during
  124. * parsing that it is not actually a commit. This usually
  125. * indicates the caller supplied a non-commit SHA-1 to
  126. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  127. * @throws JGitInternalException
  128. * a low-level exception of JGit has occurred. The original
  129. * exception can be retrieved by calling
  130. * {@link Exception#getCause()}. Expect only
  131. * {@code IOException's} to be wrapped. Subclasses of
  132. * {@link IOException} (e.g. {@link MissingObjectException}) are
  133. * typically not wrapped here but thrown as original exception
  134. */
  135. public LogCommand add(AnyObjectId start) throws MissingObjectException,
  136. IncorrectObjectTypeException, JGitInternalException {
  137. return add(true, start);
  138. }
  139. /**
  140. * Same as {@code --not start}, or {@code ^start}
  141. *
  142. * @param start
  143. * @return {@code this}
  144. * @throws MissingObjectException
  145. * the commit supplied is not available from the object
  146. * database. This usually indicates the supplied commit is
  147. * invalid, but the reference was constructed during an earlier
  148. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  149. * @throws IncorrectObjectTypeException
  150. * the object was not parsed yet and it was discovered during
  151. * parsing that it is not actually a commit. This usually
  152. * indicates the caller supplied a non-commit SHA-1 to
  153. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  154. * @throws JGitInternalException
  155. * a low-level exception of JGit has occurred. The original
  156. * exception can be retrieved by calling
  157. * {@link Exception#getCause()}. Expect only
  158. * {@code IOException's} to be wrapped. Subclasses of
  159. * {@link IOException} (e.g. {@link MissingObjectException}) are
  160. * typically not wrapped here but thrown as original exception
  161. */
  162. public LogCommand not(AnyObjectId start) throws MissingObjectException,
  163. IncorrectObjectTypeException, JGitInternalException {
  164. return add(false, start);
  165. }
  166. /**
  167. * Adds the range {@code since..until}
  168. *
  169. * @param since
  170. * @param until
  171. * @return {@code this}
  172. * @throws MissingObjectException
  173. * the commit supplied is not available from the object
  174. * database. This usually indicates the supplied commit is
  175. * invalid, but the reference was constructed during an earlier
  176. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  177. * @throws IncorrectObjectTypeException
  178. * the object was not parsed yet and it was discovered during
  179. * parsing that it is not actually a commit. This usually
  180. * indicates the caller supplied a non-commit SHA-1 to
  181. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  182. * @throws JGitInternalException
  183. * a low-level exception of JGit has occurred. The original
  184. * exception can be retrieved by calling
  185. * {@link Exception#getCause()}. Expect only
  186. * {@code IOException's} to be wrapped. Subclasses of
  187. * {@link IOException} (e.g. {@link MissingObjectException}) are
  188. * typically not wrapped here but thrown as original exception
  189. */
  190. public LogCommand addRange(AnyObjectId since, AnyObjectId until)
  191. throws MissingObjectException, IncorrectObjectTypeException,
  192. JGitInternalException {
  193. return not(since).add(until);
  194. }
  195. private LogCommand add(boolean include, AnyObjectId start)
  196. throws MissingObjectException, IncorrectObjectTypeException,
  197. JGitInternalException {
  198. checkCallable();
  199. try {
  200. if (include) {
  201. walk.markStart(walk.lookupCommit(start));
  202. startSpecified = true;
  203. } else
  204. walk.markUninteresting(walk.lookupCommit(start));
  205. return this;
  206. } catch (MissingObjectException e) {
  207. throw e;
  208. } catch (IncorrectObjectTypeException e) {
  209. throw e;
  210. } catch (IOException e) {
  211. throw new JGitInternalException(MessageFormat.format(
  212. JGitText.get().exceptionOccuredDuringAddingOfOptionToALogCommand
  213. , start), e);
  214. }
  215. }
  216. }