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.

PlotCommit.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2008, 2014 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.revplot;
  44. import org.eclipse.jgit.lib.AnyObjectId;
  45. import org.eclipse.jgit.lib.Ref;
  46. import org.eclipse.jgit.revwalk.RevCommit;
  47. /**
  48. * A commit reference to a commit in the DAG.
  49. *
  50. * @param <L>
  51. * type of lane being used by the plotter.
  52. * @see PlotCommitList
  53. */
  54. public class PlotCommit<L extends PlotLane> extends RevCommit {
  55. static final PlotCommit[] NO_CHILDREN = {};
  56. static final PlotLane[] NO_LANES = {};
  57. static final Ref[] NO_REFS = {};
  58. PlotLane[] forkingOffLanes;
  59. PlotLane[] passingLanes;
  60. PlotLane[] mergingLanes;
  61. PlotLane lane;
  62. PlotCommit[] children;
  63. Ref[] refs;
  64. /**
  65. * Create a new commit.
  66. *
  67. * @param id
  68. * the identity of this commit.
  69. */
  70. protected PlotCommit(final AnyObjectId id) {
  71. super(id);
  72. forkingOffLanes = NO_LANES;
  73. passingLanes = NO_LANES;
  74. mergingLanes = NO_LANES;
  75. children = NO_CHILDREN;
  76. refs = NO_REFS;
  77. }
  78. void addForkingOffLane(final PlotLane f) {
  79. forkingOffLanes = addLane(f, forkingOffLanes);
  80. }
  81. void addPassingLane(final PlotLane c) {
  82. passingLanes = addLane(c, passingLanes);
  83. }
  84. void addMergingLane(final PlotLane m) {
  85. mergingLanes = addLane(m, mergingLanes);
  86. }
  87. private static PlotLane[] addLane(final PlotLane l, PlotLane[] lanes) {
  88. final int cnt = lanes.length;
  89. if (cnt == 0)
  90. lanes = new PlotLane[] { l };
  91. else if (cnt == 1)
  92. lanes = new PlotLane[] { lanes[0], l };
  93. else {
  94. final PlotLane[] n = new PlotLane[cnt + 1];
  95. System.arraycopy(lanes, 0, n, 0, cnt);
  96. n[cnt] = l;
  97. lanes = n;
  98. }
  99. return lanes;
  100. }
  101. void addChild(final PlotCommit c) {
  102. final int cnt = children.length;
  103. if (cnt == 0)
  104. children = new PlotCommit[] { c };
  105. else if (cnt == 1) {
  106. if (!c.getId().equals(children[0].getId()))
  107. children = new PlotCommit[] { children[0], c };
  108. } else {
  109. for (PlotCommit pc : children)
  110. if (c.getId().equals(pc.getId()))
  111. return;
  112. final PlotCommit[] n = new PlotCommit[cnt + 1];
  113. System.arraycopy(children, 0, n, 0, cnt);
  114. n[cnt] = c;
  115. children = n;
  116. }
  117. }
  118. /**
  119. * Get the number of child commits listed in this commit.
  120. *
  121. * @return number of children; always a positive value but can be 0.
  122. */
  123. public final int getChildCount() {
  124. return children.length;
  125. }
  126. /**
  127. * Get the nth child from this commit's child list.
  128. *
  129. * @param nth
  130. * child index to obtain. Must be in the range 0 through
  131. * {@link #getChildCount()}-1.
  132. * @return the specified child.
  133. * @throws ArrayIndexOutOfBoundsException
  134. * an invalid child index was specified.
  135. */
  136. public final PlotCommit getChild(final int nth) {
  137. return children[nth];
  138. }
  139. /**
  140. * Determine if the given commit is a child (descendant) of this commit.
  141. *
  142. * @param c
  143. * the commit to test.
  144. * @return true if the given commit built on top of this commit.
  145. */
  146. public final boolean isChild(final PlotCommit c) {
  147. for (final PlotCommit a : children)
  148. if (a == c)
  149. return true;
  150. return false;
  151. }
  152. /**
  153. * Get the number of refs for this commit.
  154. *
  155. * @return number of refs; always a positive value but can be 0.
  156. */
  157. public final int getRefCount() {
  158. return refs.length;
  159. }
  160. /**
  161. * Get the nth Ref from this commit's ref list.
  162. *
  163. * @param nth
  164. * ref index to obtain. Must be in the range 0 through
  165. * {@link #getRefCount()}-1.
  166. * @return the specified ref.
  167. * @throws ArrayIndexOutOfBoundsException
  168. * an invalid ref index was specified.
  169. */
  170. public final Ref getRef(final int nth) {
  171. return refs[nth];
  172. }
  173. /**
  174. * Obtain the lane this commit has been plotted into.
  175. *
  176. * @return the assigned lane for this commit.
  177. */
  178. @SuppressWarnings("unchecked")
  179. public final L getLane() {
  180. return (L) lane;
  181. }
  182. @Override
  183. public void reset() {
  184. forkingOffLanes = NO_LANES;
  185. passingLanes = NO_LANES;
  186. mergingLanes = NO_LANES;
  187. children = NO_CHILDREN;
  188. lane = null;
  189. super.reset();
  190. }
  191. }