PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java

https://bitbucket.org/cofarrell/jgit
Java | 327 lines | 130 code | 18 blank | 179 comment | 17 complexity | b30679f00682655072396bf9e41738f0 MD5 | raw file
  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.api.errors.GitAPIException;
  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.internal.JGitText;
  54. import org.eclipse.jgit.lib.AnyObjectId;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.revwalk.RevCommit;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.revwalk.filter.AndRevFilter;
  62. import org.eclipse.jgit.revwalk.filter.MaxCountRevFilter;
  63. import org.eclipse.jgit.revwalk.filter.SkipRevFilter;
  64. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  65. import org.eclipse.jgit.treewalk.filter.PathFilter;
  66. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  67. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  68. /**
  69. * A class used to execute a {@code Log} command. It has setters for all
  70. * supported options and arguments of this command and a {@link #call()} method
  71. * to finally execute the command. Each instance of this class should only be
  72. * used for one invocation of the command (means: one call to {@link #call()})
  73. * <p>
  74. * This is currently a very basic implementation which takes only one starting
  75. * revision as option.
  76. *
  77. * TODO: add more options (revision ranges, sorting, ...)
  78. *
  79. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-log.html"
  80. * >Git documentation about Log</a>
  81. */
  82. public class LogCommand extends GitCommand<Iterable<RevCommit>> {
  83. private RevWalk walk;
  84. private boolean startSpecified = false;
  85. private final List<PathFilter> pathFilters = new ArrayList<PathFilter>();
  86. private int maxCount = -1;
  87. private int skip = -1;
  88. /**
  89. * @param repo
  90. */
  91. protected LogCommand(Repository repo) {
  92. super(repo);
  93. walk = new RevWalk(repo);
  94. }
  95. /**
  96. * Executes the {@code Log} command with all the options and parameters
  97. * collected by the setter methods (e.g. {@link #add(AnyObjectId)},
  98. * {@link #not(AnyObjectId)}, ..) of this class. Each instance of this class
  99. * should only be used for one invocation of the command. Don't call this
  100. * method twice on an instance.
  101. *
  102. * @return an iteration over RevCommits
  103. * @throws NoHeadException
  104. * of the references ref cannot be resolved
  105. */
  106. public Iterable<RevCommit> call() throws GitAPIException, NoHeadException {
  107. checkCallable();
  108. if (pathFilters.size() > 0)
  109. walk.setTreeFilter(AndTreeFilter.create(
  110. PathFilterGroup.create(pathFilters), TreeFilter.ANY_DIFF));
  111. if (skip > -1 && maxCount > -1)
  112. walk.setRevFilter(AndRevFilter.create(SkipRevFilter.create(skip),
  113. MaxCountRevFilter.create(maxCount)));
  114. else if (skip > -1)
  115. walk.setRevFilter(SkipRevFilter.create(skip));
  116. else if (maxCount > -1)
  117. walk.setRevFilter(MaxCountRevFilter.create(maxCount));
  118. if (!startSpecified) {
  119. try {
  120. ObjectId headId = repo.resolve(Constants.HEAD);
  121. if (headId == null)
  122. throw new NoHeadException(
  123. JGitText.get().noHEADExistsAndNoExplicitStartingRevisionWasSpecified);
  124. add(headId);
  125. } catch (IOException e) {
  126. // all exceptions thrown by add() shouldn't occur and represent
  127. // severe low-level exception which are therefore wrapped
  128. throw new JGitInternalException(
  129. JGitText.get().anExceptionOccurredWhileTryingToAddTheIdOfHEAD,
  130. e);
  131. }
  132. }
  133. setCallable(false);
  134. return walk;
  135. }
  136. /**
  137. * Mark a commit to start graph traversal from.
  138. *
  139. * @see RevWalk#markStart(RevCommit)
  140. * @param start
  141. * @return {@code this}
  142. * @throws MissingObjectException
  143. * the commit supplied is not available from the object
  144. * database. This usually indicates the supplied commit is
  145. * invalid, but the reference was constructed during an earlier
  146. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  147. * @throws IncorrectObjectTypeException
  148. * the object was not parsed yet and it was discovered during
  149. * parsing that it is not actually a commit. This usually
  150. * indicates the caller supplied a non-commit SHA-1 to
  151. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  152. * @throws JGitInternalException
  153. * a low-level exception of JGit has occurred. The original
  154. * exception can be retrieved by calling
  155. * {@link Exception#getCause()}. Expect only
  156. * {@code IOException's} to be wrapped. Subclasses of
  157. * {@link IOException} (e.g. {@link MissingObjectException}) are
  158. * typically not wrapped here but thrown as original exception
  159. */
  160. public LogCommand add(AnyObjectId start) throws MissingObjectException,
  161. IncorrectObjectTypeException {
  162. return add(true, start);
  163. }
  164. /**
  165. * Same as {@code --not start}, or {@code ^start}
  166. *
  167. * @param start
  168. * @return {@code this}
  169. * @throws MissingObjectException
  170. * the commit supplied is not available from the object
  171. * database. This usually indicates the supplied commit is
  172. * invalid, but the reference was constructed during an earlier
  173. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  174. * @throws IncorrectObjectTypeException
  175. * the object was not parsed yet and it was discovered during
  176. * parsing that it is not actually a commit. This usually
  177. * indicates the caller supplied a non-commit SHA-1 to
  178. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  179. * @throws JGitInternalException
  180. * a low-level exception of JGit has occurred. The original
  181. * exception can be retrieved by calling
  182. * {@link Exception#getCause()}. Expect only
  183. * {@code IOException's} to be wrapped. Subclasses of
  184. * {@link IOException} (e.g. {@link MissingObjectException}) are
  185. * typically not wrapped here but thrown as original exception
  186. */
  187. public LogCommand not(AnyObjectId start) throws MissingObjectException,
  188. IncorrectObjectTypeException {
  189. return add(false, start);
  190. }
  191. /**
  192. * Adds the range {@code since..until}
  193. *
  194. * @param since
  195. * @param until
  196. * @return {@code this}
  197. * @throws MissingObjectException
  198. * the commit supplied is not available from the object
  199. * database. This usually indicates the supplied commit is
  200. * invalid, but the reference was constructed during an earlier
  201. * invocation to {@link RevWalk#lookupCommit(AnyObjectId)}.
  202. * @throws IncorrectObjectTypeException
  203. * the object was not parsed yet and it was discovered during
  204. * parsing that it is not actually a commit. This usually
  205. * indicates the caller supplied a non-commit SHA-1 to
  206. * {@link RevWalk#lookupCommit(AnyObjectId)}.
  207. * @throws JGitInternalException
  208. * a low-level exception of JGit has occurred. The original
  209. * exception can be retrieved by calling
  210. * {@link Exception#getCause()}. Expect only
  211. * {@code IOException's} to be wrapped. Subclasses of
  212. * {@link IOException} (e.g. {@link MissingObjectException}) are
  213. * typically not wrapped here but thrown as original exception
  214. */
  215. public LogCommand addRange(AnyObjectId since, AnyObjectId until)
  216. throws MissingObjectException, IncorrectObjectTypeException {
  217. return not(since).add(until);
  218. }
  219. /**
  220. * Add all refs as commits to start the graph traversal from.
  221. *
  222. * @see #add(AnyObjectId)
  223. * @return {@code this}
  224. * @throws IOException
  225. * the references could not be accessed
  226. */
  227. public LogCommand all() throws IOException {
  228. for (Ref ref : getRepository().getAllRefs().values()) {
  229. if(!ref.isPeeled())
  230. ref = getRepository().peel(ref);
  231. ObjectId objectId = ref.getPeeledObjectId();
  232. if (objectId == null)
  233. objectId = ref.getObjectId();
  234. RevCommit commit = null;
  235. try {
  236. commit = walk.parseCommit(objectId);
  237. } catch (MissingObjectException e) {
  238. // ignore: the ref points to an object that does not exist;
  239. // it should be ignored as traversal starting point.
  240. } catch (IncorrectObjectTypeException e) {
  241. // ignore: the ref points to an object that is not a commit
  242. // (e.g. a tree or a blob);
  243. // it should be ignored as traversal starting point.
  244. }
  245. if (commit != null)
  246. add(commit);
  247. }
  248. return this;
  249. }
  250. /**
  251. * Show only commits that affect any of the specified paths. The path must
  252. * either name a file or a directory exactly. Note that regex expressions or
  253. * wildcards are not supported.
  254. *
  255. * @param path
  256. * a path is relative to the top level of the repository
  257. * @return {@code this}
  258. */
  259. public LogCommand addPath(String path) {
  260. checkCallable();
  261. pathFilters.add(PathFilter.create(path));
  262. return this;
  263. }
  264. /**
  265. * Skip the number of commits before starting to show the commit output.
  266. *
  267. * @param skip
  268. * the number of commits to skip
  269. * @return {@code this}
  270. */
  271. public LogCommand setSkip(int skip) {
  272. checkCallable();
  273. this.skip = skip;
  274. return this;
  275. }
  276. /**
  277. * Limit the number of commits to output.
  278. *
  279. * @param maxCount
  280. * the limit
  281. * @return {@code this}
  282. */
  283. public LogCommand setMaxCount(int maxCount) {
  284. checkCallable();
  285. this.maxCount = maxCount;
  286. return this;
  287. }
  288. private LogCommand add(boolean include, AnyObjectId start)
  289. throws MissingObjectException, IncorrectObjectTypeException,
  290. JGitInternalException {
  291. checkCallable();
  292. try {
  293. if (include) {
  294. walk.markStart(walk.lookupCommit(start));
  295. startSpecified = true;
  296. } else
  297. walk.markUninteresting(walk.lookupCommit(start));
  298. return this;
  299. } catch (MissingObjectException e) {
  300. throw e;
  301. } catch (IncorrectObjectTypeException e) {
  302. throw e;
  303. } catch (IOException e) {
  304. throw new JGitInternalException(MessageFormat.format(
  305. JGitText.get().exceptionOccurredDuringAddingOfOptionToALogCommand
  306. , start), e);
  307. }
  308. }
  309. }