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.

RevObject.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.CorruptObjectException;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.lib.AnyObjectId;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectLoader;
  52. /** Base object type accessed during revision walking. */
  53. public abstract class RevObject extends ObjectId {
  54. static final int PARSED = 1;
  55. int flags;
  56. RevObject(final AnyObjectId name) {
  57. super(name);
  58. }
  59. void parseHeaders(final RevWalk walk) throws MissingObjectException,
  60. IncorrectObjectTypeException, IOException {
  61. loadCanonical(walk);
  62. flags |= PARSED;
  63. }
  64. void parseBody(final RevWalk walk) throws MissingObjectException,
  65. IncorrectObjectTypeException, IOException {
  66. if ((flags & PARSED) == 0)
  67. parseHeaders(walk);
  68. }
  69. final byte[] loadCanonical(final RevWalk walk) throws IOException,
  70. MissingObjectException, IncorrectObjectTypeException,
  71. CorruptObjectException {
  72. final ObjectLoader ldr = walk.db.openObject(walk.curs, this);
  73. if (ldr == null)
  74. throw new MissingObjectException(this, getType());
  75. final byte[] data = ldr.getCachedBytes();
  76. if (getType() != ldr.getType())
  77. throw new IncorrectObjectTypeException(this, getType());
  78. return data;
  79. }
  80. /**
  81. * Get Git object type. See {@link Constants}.
  82. *
  83. * @return object type
  84. */
  85. public abstract int getType();
  86. /**
  87. * Get the name of this object.
  88. *
  89. * @return unique hash of this object.
  90. */
  91. public final ObjectId getId() {
  92. return this;
  93. }
  94. @Override
  95. public final boolean equals(final AnyObjectId o) {
  96. return this == o;
  97. }
  98. @Override
  99. public final boolean equals(final Object o) {
  100. return this == o;
  101. }
  102. /**
  103. * Test to see if the flag has been set on this object.
  104. *
  105. * @param flag
  106. * the flag to test.
  107. * @return true if the flag has been added to this object; false if not.
  108. */
  109. public final boolean has(final RevFlag flag) {
  110. return (flags & flag.mask) != 0;
  111. }
  112. /**
  113. * Test to see if any flag in the set has been set on this object.
  114. *
  115. * @param set
  116. * the flags to test.
  117. * @return true if any flag in the set has been added to this object; false
  118. * if not.
  119. */
  120. public final boolean hasAny(final RevFlagSet set) {
  121. return (flags & set.mask) != 0;
  122. }
  123. /**
  124. * Test to see if all flags in the set have been set on this object.
  125. *
  126. * @param set
  127. * the flags to test.
  128. * @return true if all flags of the set have been added to this object;
  129. * false if some or none have been added.
  130. */
  131. public final boolean hasAll(final RevFlagSet set) {
  132. return (flags & set.mask) == set.mask;
  133. }
  134. /**
  135. * Add a flag to this object.
  136. * <p>
  137. * If the flag is already set on this object then the method has no effect.
  138. *
  139. * @param flag
  140. * the flag to mark on this object, for later testing.
  141. */
  142. public final void add(final RevFlag flag) {
  143. flags |= flag.mask;
  144. }
  145. /**
  146. * Add a set of flags to this object.
  147. *
  148. * @param set
  149. * the set of flags to mark on this object, for later testing.
  150. */
  151. public final void add(final RevFlagSet set) {
  152. flags |= set.mask;
  153. }
  154. /**
  155. * Remove a flag from this object.
  156. * <p>
  157. * If the flag is not set on this object then the method has no effect.
  158. *
  159. * @param flag
  160. * the flag to remove from this object.
  161. */
  162. public final void remove(final RevFlag flag) {
  163. flags &= ~flag.mask;
  164. }
  165. /**
  166. * Remove a set of flags from this object.
  167. *
  168. * @param set
  169. * the flag to remove from this object.
  170. */
  171. public final void remove(final RevFlagSet set) {
  172. flags &= ~set.mask;
  173. }
  174. @Override
  175. public String toString() {
  176. final StringBuilder s = new StringBuilder();
  177. s.append(Constants.typeString(getType()));
  178. s.append(' ');
  179. s.append(name());
  180. s.append(' ');
  181. appendCoreFlags(s);
  182. return s.toString();
  183. }
  184. /**
  185. * @param s
  186. * buffer to append a debug description of core RevFlags onto.
  187. */
  188. protected void appendCoreFlags(final StringBuilder s) {
  189. s.append((flags & RevWalk.TOPO_DELAY) != 0 ? 'o' : '-');
  190. s.append((flags & RevWalk.TEMP_MARK) != 0 ? 't' : '-');
  191. s.append((flags & RevWalk.REWRITE) != 0 ? 'r' : '-');
  192. s.append((flags & RevWalk.UNINTERESTING) != 0 ? 'u' : '-');
  193. s.append((flags & RevWalk.SEEN) != 0 ? 's' : '-');
  194. s.append((flags & RevWalk.PARSED) != 0 ? 'p' : '-');
  195. }
  196. }