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

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