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.

RewriteGenerator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.revwalk;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. /**
  48. * Replaces a RevCommit's parents until not colored with REWRITE.
  49. * <p>
  50. * Before a RevCommit is returned to the caller its parents are updated to
  51. * create a dense DAG. Instead of reporting the actual parents as recorded when
  52. * the commit was created the returned commit will reflect the next closest
  53. * commit that matched the revision walker's filters.
  54. * <p>
  55. * This generator is the second phase of a path limited revision walk and
  56. * assumes it is receiving RevCommits from {@link RewriteTreeFilter},
  57. * after they have been fully buffered by {@link AbstractRevQueue}. The full
  58. * buffering is necessary to allow the simple loop used within our own
  59. * {@link #rewrite(RevCommit)} to pull completely through a strand of
  60. * {@link RevWalk#REWRITE} colored commits and come up with a simplification
  61. * that makes the DAG dense. Not fully buffering the commits first would cause
  62. * this loop to abort early, due to commits not being parsed and colored
  63. * correctly.
  64. *
  65. * @see RewriteTreeFilter
  66. */
  67. class RewriteGenerator extends Generator {
  68. private static final int REWRITE = RevWalk.REWRITE;
  69. /** For {@link #cleanup(RevCommit[])} to remove duplicate parents. */
  70. private static final int DUPLICATE = RevWalk.TEMP_MARK;
  71. private final Generator source;
  72. RewriteGenerator(final Generator s) {
  73. source = s;
  74. }
  75. @Override
  76. void shareFreeList(final BlockRevQueue q) {
  77. source.shareFreeList(q);
  78. }
  79. @Override
  80. int outputType() {
  81. return source.outputType() & ~NEEDS_REWRITE;
  82. }
  83. @Override
  84. RevCommit next() throws MissingObjectException,
  85. IncorrectObjectTypeException, IOException {
  86. for (;;) {
  87. final RevCommit c = source.next();
  88. if (c == null)
  89. return null;
  90. boolean rewrote = false;
  91. final RevCommit[] pList = c.parents;
  92. final int nParents = pList.length;
  93. for (int i = 0; i < nParents; i++) {
  94. final RevCommit oldp = pList[i];
  95. final RevCommit newp = rewrite(oldp);
  96. if (oldp != newp) {
  97. pList[i] = newp;
  98. rewrote = true;
  99. }
  100. }
  101. if (rewrote)
  102. c.parents = cleanup(pList);
  103. return c;
  104. }
  105. }
  106. private RevCommit rewrite(RevCommit p) {
  107. for (;;) {
  108. final RevCommit[] pList = p.parents;
  109. if (pList.length > 1) {
  110. // This parent is a merge, so keep it.
  111. //
  112. return p;
  113. }
  114. if ((p.flags & RevWalk.UNINTERESTING) != 0) {
  115. // Retain uninteresting parents. They show where the
  116. // DAG was cut off because it wasn't interesting.
  117. //
  118. return p;
  119. }
  120. if ((p.flags & REWRITE) == 0) {
  121. // This parent was not eligible for rewriting. We
  122. // need to keep it in the DAG.
  123. //
  124. return p;
  125. }
  126. if (pList.length == 0) {
  127. // We can't go back any further, other than to
  128. // just delete the parent entirely.
  129. //
  130. return null;
  131. }
  132. p = pList[0];
  133. }
  134. }
  135. private RevCommit[] cleanup(final RevCommit[] oldList) {
  136. // Remove any duplicate parents caused due to rewrites (e.g. a merge
  137. // with two sides that both simplified back into the merge base).
  138. // We also may have deleted a parent by marking it null.
  139. //
  140. int newCnt = 0;
  141. for (int o = 0; o < oldList.length; o++) {
  142. final RevCommit p = oldList[o];
  143. if (p == null)
  144. continue;
  145. if ((p.flags & DUPLICATE) != 0) {
  146. oldList[o] = null;
  147. continue;
  148. }
  149. p.flags |= DUPLICATE;
  150. newCnt++;
  151. }
  152. if (newCnt == oldList.length) {
  153. for (final RevCommit p : oldList)
  154. p.flags &= ~DUPLICATE;
  155. return oldList;
  156. }
  157. final RevCommit[] newList = new RevCommit[newCnt];
  158. newCnt = 0;
  159. for (final RevCommit p : oldList) {
  160. if (p != null) {
  161. newList[newCnt++] = p;
  162. p.flags &= ~DUPLICATE;
  163. }
  164. }
  165. return newList;
  166. }
  167. }