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

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