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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.revplot;
  44. import org.eclipse.jgit.lib.AnyObjectId;
  45. import org.eclipse.jgit.revwalk.RevCommit;
  46. import org.eclipse.jgit.lib.Ref;
  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. PlotLane[] passingLanes;
  58. PlotLane lane;
  59. PlotCommit[] children;
  60. Ref[] refs;
  61. /**
  62. * Create a new commit.
  63. *
  64. * @param id
  65. * the identity of this commit.
  66. */
  67. protected PlotCommit(final AnyObjectId id) {
  68. super(id);
  69. passingLanes = NO_LANES;
  70. children = NO_CHILDREN;
  71. }
  72. void addPassingLane(final PlotLane c) {
  73. final int cnt = passingLanes.length;
  74. if (cnt == 0)
  75. passingLanes = new PlotLane[] { c };
  76. else if (cnt == 1)
  77. passingLanes = new PlotLane[] { passingLanes[0], c };
  78. else {
  79. final PlotLane[] n = new PlotLane[cnt + 1];
  80. System.arraycopy(passingLanes, 0, n, 0, cnt);
  81. n[cnt] = c;
  82. passingLanes = n;
  83. }
  84. }
  85. void addChild(final PlotCommit c) {
  86. final int cnt = children.length;
  87. if (cnt == 0)
  88. children = new PlotCommit[] { c };
  89. else if (cnt == 1)
  90. children = new PlotCommit[] { children[0], c };
  91. else {
  92. final PlotCommit[] n = new PlotCommit[cnt + 1];
  93. System.arraycopy(children, 0, n, 0, cnt);
  94. n[cnt] = c;
  95. children = n;
  96. }
  97. }
  98. /**
  99. * Get the number of child commits listed in this commit.
  100. *
  101. * @return number of children; always a positive value but can be 0.
  102. */
  103. public final int getChildCount() {
  104. return children.length;
  105. }
  106. /**
  107. * Get the nth child from this commit's child list.
  108. *
  109. * @param nth
  110. * child index to obtain. Must be in the range 0 through
  111. * {@link #getChildCount()}-1.
  112. * @return the specified child.
  113. * @throws ArrayIndexOutOfBoundsException
  114. * an invalid child index was specified.
  115. */
  116. public final PlotCommit getChild(final int nth) {
  117. return children[nth];
  118. }
  119. /**
  120. * Determine if the given commit is a child (descendant) of this commit.
  121. *
  122. * @param c
  123. * the commit to test.
  124. * @return true if the given commit built on top of this commit.
  125. */
  126. public final boolean isChild(final PlotCommit c) {
  127. for (final PlotCommit a : children)
  128. if (a == c)
  129. return true;
  130. return false;
  131. }
  132. /**
  133. * Get the number of refs for this commit.
  134. *
  135. * @return number of refs; always a positive value but can be 0.
  136. */
  137. public final int getRefCount() {
  138. return refs.length;
  139. }
  140. /**
  141. * Get the nth Ref from this commit's ref list.
  142. *
  143. * @param nth
  144. * ref index to obtain. Must be in the range 0 through
  145. * {@link #getRefCount()}-1.
  146. * @return the specified ref.
  147. * @throws ArrayIndexOutOfBoundsException
  148. * an invalid ref index was specified.
  149. */
  150. public final Ref getRef(final int nth) {
  151. return refs[nth];
  152. }
  153. /**
  154. * Obtain the lane this commit has been plotted into.
  155. *
  156. * @return the assigned lane for this commit.
  157. */
  158. public final L getLane() {
  159. return (L) lane;
  160. }
  161. @Override
  162. public void reset() {
  163. passingLanes = NO_LANES;
  164. children = NO_CHILDREN;
  165. lane = null;
  166. super.reset();
  167. }
  168. }