Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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