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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.revwalk;
  45. import java.io.IOException;
  46. import java.nio.charset.Charset;
  47. import java.util.ArrayList;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.lib.AnyObjectId;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.MutableObjectId;
  55. import org.eclipse.jgit.lib.ObjectInserter;
  56. import org.eclipse.jgit.lib.ObjectReader;
  57. import org.eclipse.jgit.lib.PersonIdent;
  58. import org.eclipse.jgit.util.RawParseUtils;
  59. /** A commit reference to a commit in the DAG. */
  60. public class RevCommit extends RevObject {
  61. /**
  62. * Parse a commit from its canonical format.
  63. *
  64. * This method constructs a temporary revision pool, parses the commit as
  65. * supplied, and returns it to the caller. Since the commit was built inside
  66. * of a private revision pool its parent pointers will be initialized, but
  67. * will not have their headers loaded.
  68. *
  69. * Applications are discouraged from using this API. Callers usually need
  70. * more than one commit. Use {@link RevWalk#parseCommit(AnyObjectId)} to
  71. * obtain a RevCommit from an existing repository.
  72. *
  73. * @param raw
  74. * the canonical formatted commit to be parsed.
  75. * @return the parsed commit, in an isolated revision pool that is not
  76. * available to the caller.
  77. */
  78. public static RevCommit parse(byte[] raw) {
  79. return parse(new RevWalk((ObjectReader) null), raw);
  80. }
  81. /**
  82. * Parse a commit from its canonical format.
  83. *
  84. * This method inserts the commit directly into the caller supplied revision
  85. * pool, making it appear as though the commit exists in the repository,
  86. * even if it doesn't. The repository under the pool is not affected.
  87. *
  88. * @param rw
  89. * the revision pool to allocate the commit within. The commit's
  90. * tree and parent pointers will be obtained from this pool.
  91. * @param raw
  92. * the canonical formatted commit to be parsed.
  93. * @return the parsed commit, in an isolated revision pool that is not
  94. * available to the caller.
  95. */
  96. public static RevCommit parse(RevWalk rw, byte[] raw) {
  97. ObjectInserter.Formatter fmt = new ObjectInserter.Formatter();
  98. boolean retain = rw.isRetainBody();
  99. rw.setRetainBody(true);
  100. RevCommit r = rw.lookupCommit(fmt.idFor(Constants.OBJ_COMMIT, raw));
  101. r.parseCanonical(rw, raw);
  102. rw.setRetainBody(retain);
  103. return r;
  104. }
  105. static final RevCommit[] NO_PARENTS = {};
  106. private RevTree tree;
  107. RevCommit[] parents;
  108. int commitTime; // An int here for performance, overflows in 2038
  109. int inDegree;
  110. private byte[] buffer;
  111. /**
  112. * Create a new commit reference.
  113. *
  114. * @param id
  115. * object name for the commit.
  116. */
  117. protected RevCommit(final AnyObjectId id) {
  118. super(id);
  119. }
  120. @Override
  121. void parseHeaders(final RevWalk walk) throws MissingObjectException,
  122. IncorrectObjectTypeException, IOException {
  123. parseCanonical(walk, walk.getCachedBytes(this));
  124. }
  125. @Override
  126. void parseBody(final RevWalk walk) throws MissingObjectException,
  127. IncorrectObjectTypeException, IOException {
  128. if (buffer == null) {
  129. buffer = walk.getCachedBytes(this);
  130. if ((flags & PARSED) == 0)
  131. parseCanonical(walk, buffer);
  132. }
  133. }
  134. void parseCanonical(final RevWalk walk, final byte[] raw) {
  135. final MutableObjectId idBuffer = walk.idBuffer;
  136. idBuffer.fromString(raw, 5);
  137. tree = walk.lookupTree(idBuffer);
  138. int ptr = 46;
  139. if (parents == null) {
  140. RevCommit[] pList = new RevCommit[1];
  141. int nParents = 0;
  142. for (;;) {
  143. if (raw[ptr] != 'p')
  144. break;
  145. idBuffer.fromString(raw, ptr + 7);
  146. final RevCommit p = walk.lookupCommit(idBuffer);
  147. if (nParents == 0)
  148. pList[nParents++] = p;
  149. else if (nParents == 1) {
  150. pList = new RevCommit[] { pList[0], p };
  151. nParents = 2;
  152. } else {
  153. if (pList.length <= nParents) {
  154. RevCommit[] old = pList;
  155. pList = new RevCommit[pList.length + 32];
  156. System.arraycopy(old, 0, pList, 0, nParents);
  157. }
  158. pList[nParents++] = p;
  159. }
  160. ptr += 48;
  161. }
  162. if (nParents != pList.length) {
  163. RevCommit[] old = pList;
  164. pList = new RevCommit[nParents];
  165. System.arraycopy(old, 0, pList, 0, nParents);
  166. }
  167. parents = pList;
  168. }
  169. // extract time from "committer "
  170. ptr = RawParseUtils.committer(raw, ptr);
  171. if (ptr > 0) {
  172. ptr = RawParseUtils.nextLF(raw, ptr, '>');
  173. // In 2038 commitTime will overflow unless it is changed to long.
  174. commitTime = RawParseUtils.parseBase10(raw, ptr, null);
  175. }
  176. if (walk.isRetainBody())
  177. buffer = raw;
  178. flags |= PARSED;
  179. }
  180. @Override
  181. public final int getType() {
  182. return Constants.OBJ_COMMIT;
  183. }
  184. static void carryFlags(RevCommit c, final int carry) {
  185. for (;;) {
  186. final RevCommit[] pList = c.parents;
  187. if (pList == null)
  188. return;
  189. final int n = pList.length;
  190. if (n == 0)
  191. return;
  192. for (int i = 1; i < n; i++) {
  193. final RevCommit p = pList[i];
  194. if ((p.flags & carry) == carry)
  195. continue;
  196. p.flags |= carry;
  197. carryFlags(p, carry);
  198. }
  199. c = pList[0];
  200. if ((c.flags & carry) == carry)
  201. return;
  202. c.flags |= carry;
  203. }
  204. }
  205. /**
  206. * Carry a RevFlag set on this commit to its parents.
  207. * <p>
  208. * If this commit is parsed, has parents, and has the supplied flag set on
  209. * it we automatically add it to the parents, grand-parents, and so on until
  210. * an unparsed commit or a commit with no parents is discovered. This
  211. * permits applications to force a flag through the history chain when
  212. * necessary.
  213. *
  214. * @param flag
  215. * the single flag value to carry back onto parents.
  216. */
  217. public void carry(final RevFlag flag) {
  218. final int carry = flags & flag.mask;
  219. if (carry != 0)
  220. carryFlags(this, carry);
  221. }
  222. /**
  223. * Time from the "committer " line of the buffer.
  224. *
  225. * @return time, expressed as seconds since the epoch.
  226. */
  227. public final int getCommitTime() {
  228. return commitTime;
  229. }
  230. /**
  231. * Get a reference to this commit's tree.
  232. *
  233. * @return tree of this commit.
  234. */
  235. public final RevTree getTree() {
  236. return tree;
  237. }
  238. /**
  239. * Get the number of parent commits listed in this commit.
  240. *
  241. * @return number of parents; always a positive value but can be 0.
  242. */
  243. public final int getParentCount() {
  244. return parents.length;
  245. }
  246. /**
  247. * Get the nth parent from this commit's parent list.
  248. *
  249. * @param nth
  250. * parent index to obtain. Must be in the range 0 through
  251. * {@link #getParentCount()}-1.
  252. * @return the specified parent.
  253. * @throws ArrayIndexOutOfBoundsException
  254. * an invalid parent index was specified.
  255. */
  256. public final RevCommit getParent(final int nth) {
  257. return parents[nth];
  258. }
  259. /**
  260. * Obtain an array of all parents (<b>NOTE - THIS IS NOT A COPY</b>).
  261. * <p>
  262. * This method is exposed only to provide very fast, efficient access to
  263. * this commit's parent list. Applications relying on this list should be
  264. * very careful to ensure they do not modify its contents during their use
  265. * of it.
  266. *
  267. * @return the array of parents.
  268. */
  269. public final RevCommit[] getParents() {
  270. return parents;
  271. }
  272. /**
  273. * Obtain the raw unparsed commit body (<b>NOTE - THIS IS NOT A COPY</b>).
  274. * <p>
  275. * This method is exposed only to provide very fast, efficient access to
  276. * this commit's message buffer within a RevFilter. Applications relying on
  277. * this buffer should be very careful to ensure they do not modify its
  278. * contents during their use of it.
  279. *
  280. * @return the raw unparsed commit body. This is <b>NOT A COPY</b>.
  281. * Altering the contents of this buffer may alter the walker's
  282. * knowledge of this commit, and the results it produces.
  283. */
  284. public final byte[] getRawBuffer() {
  285. return buffer;
  286. }
  287. /**
  288. * Parse the author identity from the raw buffer.
  289. * <p>
  290. * This method parses and returns the content of the author line, after
  291. * taking the commit's character set into account and decoding the author
  292. * name and email address. This method is fairly expensive and produces a
  293. * new PersonIdent instance on each invocation. Callers should invoke this
  294. * method only if they are certain they will be outputting the result, and
  295. * should cache the return value for as long as necessary to use all
  296. * information from it.
  297. * <p>
  298. * RevFilter implementations should try to use {@link RawParseUtils} to scan
  299. * the {@link #getRawBuffer()} instead, as this will allow faster evaluation
  300. * of commits.
  301. *
  302. * @return identity of the author (name, email) and the time the commit was
  303. * made by the author; null if no author line was found.
  304. */
  305. public final PersonIdent getAuthorIdent() {
  306. final byte[] raw = buffer;
  307. final int nameB = RawParseUtils.author(raw, 0);
  308. if (nameB < 0)
  309. return null;
  310. return RawParseUtils.parsePersonIdent(raw, nameB);
  311. }
  312. /**
  313. * Parse the committer identity from the raw buffer.
  314. * <p>
  315. * This method parses and returns the content of the committer line, after
  316. * taking the commit's character set into account and decoding the committer
  317. * name and email address. This method is fairly expensive and produces a
  318. * new PersonIdent instance on each invocation. Callers should invoke this
  319. * method only if they are certain they will be outputting the result, and
  320. * should cache the return value for as long as necessary to use all
  321. * information from it.
  322. * <p>
  323. * RevFilter implementations should try to use {@link RawParseUtils} to scan
  324. * the {@link #getRawBuffer()} instead, as this will allow faster evaluation
  325. * of commits.
  326. *
  327. * @return identity of the committer (name, email) and the time the commit
  328. * was made by the committer; null if no committer line was found.
  329. */
  330. public final PersonIdent getCommitterIdent() {
  331. final byte[] raw = buffer;
  332. final int nameB = RawParseUtils.committer(raw, 0);
  333. if (nameB < 0)
  334. return null;
  335. return RawParseUtils.parsePersonIdent(raw, nameB);
  336. }
  337. /**
  338. * Parse the complete commit message and decode it to a string.
  339. * <p>
  340. * This method parses and returns the message portion of the commit buffer,
  341. * after taking the commit's character set into account and decoding the
  342. * buffer using that character set. This method is a fairly expensive
  343. * operation and produces a new string on each invocation.
  344. *
  345. * @return decoded commit message as a string. Never null.
  346. */
  347. public final String getFullMessage() {
  348. final byte[] raw = buffer;
  349. final int msgB = RawParseUtils.commitMessage(raw, 0);
  350. if (msgB < 0)
  351. return "";
  352. final Charset enc = RawParseUtils.parseEncoding(raw);
  353. return RawParseUtils.decode(enc, raw, msgB, raw.length);
  354. }
  355. /**
  356. * Parse the commit message and return the first "line" of it.
  357. * <p>
  358. * The first line is everything up to the first pair of LFs. This is the
  359. * "oneline" format, suitable for output in a single line display.
  360. * <p>
  361. * This method parses and returns the message portion of the commit buffer,
  362. * after taking the commit's character set into account and decoding the
  363. * buffer using that character set. This method is a fairly expensive
  364. * operation and produces a new string on each invocation.
  365. *
  366. * @return decoded commit message as a string. Never null. The returned
  367. * string does not contain any LFs, even if the first paragraph
  368. * spanned multiple lines. Embedded LFs are converted to spaces.
  369. */
  370. public final String getShortMessage() {
  371. final byte[] raw = buffer;
  372. final int msgB = RawParseUtils.commitMessage(raw, 0);
  373. if (msgB < 0)
  374. return "";
  375. final Charset enc = RawParseUtils.parseEncoding(raw);
  376. final int msgE = RawParseUtils.endOfParagraph(raw, msgB);
  377. String str = RawParseUtils.decode(enc, raw, msgB, msgE);
  378. if (hasLF(raw, msgB, msgE))
  379. str = str.replace('\n', ' ');
  380. return str;
  381. }
  382. static boolean hasLF(final byte[] r, int b, final int e) {
  383. while (b < e)
  384. if (r[b++] == '\n')
  385. return true;
  386. return false;
  387. }
  388. /**
  389. * Determine the encoding of the commit message buffer.
  390. * <p>
  391. * Locates the "encoding" header (if present) and then returns the proper
  392. * character set to apply to this buffer to evaluate its contents as
  393. * character data.
  394. * <p>
  395. * If no encoding header is present, {@link Constants#CHARSET} is assumed.
  396. *
  397. * @return the preferred encoding of {@link #getRawBuffer()}.
  398. */
  399. public final Charset getEncoding() {
  400. return RawParseUtils.parseEncoding(buffer);
  401. }
  402. /**
  403. * Parse the footer lines (e.g. "Signed-off-by") for machine processing.
  404. * <p>
  405. * This method splits all of the footer lines out of the last paragraph of
  406. * the commit message, providing each line as a key-value pair, ordered by
  407. * the order of the line's appearance in the commit message itself.
  408. * <p>
  409. * A footer line's key must match the pattern {@code ^[A-Za-z0-9-]+:}, while
  410. * the value is free-form, but must not contain an LF. Very common keys seen
  411. * in the wild are:
  412. * <ul>
  413. * <li>{@code Signed-off-by} (agrees to Developer Certificate of Origin)
  414. * <li>{@code Acked-by} (thinks change looks sane in context)
  415. * <li>{@code Reported-by} (originally found the issue this change fixes)
  416. * <li>{@code Tested-by} (validated change fixes the issue for them)
  417. * <li>{@code CC}, {@code Cc} (copy on all email related to this change)
  418. * <li>{@code Bug} (link to project's bug tracking system)
  419. * </ul>
  420. *
  421. * @return ordered list of footer lines; empty list if no footers found.
  422. */
  423. public final List<FooterLine> getFooterLines() {
  424. final byte[] raw = buffer;
  425. int ptr = raw.length - 1;
  426. while (raw[ptr] == '\n') // trim any trailing LFs, not interesting
  427. ptr--;
  428. final int msgB = RawParseUtils.commitMessage(raw, 0);
  429. final ArrayList<FooterLine> r = new ArrayList<FooterLine>(4);
  430. final Charset enc = getEncoding();
  431. for (;;) {
  432. ptr = RawParseUtils.prevLF(raw, ptr);
  433. if (ptr <= msgB)
  434. break; // Don't parse commit headers as footer lines.
  435. final int keyStart = ptr + 2;
  436. if (raw[keyStart] == '\n')
  437. break; // Stop at first paragraph break, no footers above it.
  438. final int keyEnd = RawParseUtils.endOfFooterLineKey(raw, keyStart);
  439. if (keyEnd < 0)
  440. continue; // Not a well formed footer line, skip it.
  441. // Skip over the ': *' at the end of the key before the value.
  442. //
  443. int valStart = keyEnd + 1;
  444. while (valStart < raw.length && raw[valStart] == ' ')
  445. valStart++;
  446. // Value ends at the LF, and does not include it.
  447. //
  448. int valEnd = RawParseUtils.nextLF(raw, valStart);
  449. if (raw[valEnd - 1] == '\n')
  450. valEnd--;
  451. r.add(new FooterLine(raw, enc, keyStart, keyEnd, valStart, valEnd));
  452. }
  453. Collections.reverse(r);
  454. return r;
  455. }
  456. /**
  457. * Get the values of all footer lines with the given key.
  458. *
  459. * @param keyName
  460. * footer key to find values of, case insensitive.
  461. * @return values of footers with key of {@code keyName}, ordered by their
  462. * order of appearance. Duplicates may be returned if the same
  463. * footer appeared more than once. Empty list if no footers appear
  464. * with the specified key, or there are no footers at all.
  465. * @see #getFooterLines()
  466. */
  467. public final List<String> getFooterLines(final String keyName) {
  468. return getFooterLines(new FooterKey(keyName));
  469. }
  470. /**
  471. * Get the values of all footer lines with the given key.
  472. *
  473. * @param keyName
  474. * footer key to find values of, case insensitive.
  475. * @return values of footers with key of {@code keyName}, ordered by their
  476. * order of appearance. Duplicates may be returned if the same
  477. * footer appeared more than once. Empty list if no footers appear
  478. * with the specified key, or there are no footers at all.
  479. * @see #getFooterLines()
  480. */
  481. public final List<String> getFooterLines(final FooterKey keyName) {
  482. final List<FooterLine> src = getFooterLines();
  483. if (src.isEmpty())
  484. return Collections.emptyList();
  485. final ArrayList<String> r = new ArrayList<String>(src.size());
  486. for (final FooterLine f : src) {
  487. if (f.matches(keyName))
  488. r.add(f.getValue());
  489. }
  490. return r;
  491. }
  492. /**
  493. * Reset this commit to allow another RevWalk with the same instances.
  494. * <p>
  495. * Subclasses <b>must</b> call <code>super.reset()</code> to ensure the
  496. * basic information can be correctly cleared out.
  497. */
  498. public void reset() {
  499. inDegree = 0;
  500. }
  501. final void disposeBody() {
  502. buffer = null;
  503. }
  504. @Override
  505. public String toString() {
  506. final StringBuilder s = new StringBuilder();
  507. s.append(Constants.typeString(getType()));
  508. s.append(' ');
  509. s.append(name());
  510. s.append(' ');
  511. s.append(commitTime);
  512. s.append(' ');
  513. appendCoreFlags(s);
  514. return s.toString();
  515. }
  516. }