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

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