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

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