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.

TreeEntry.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2007, 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.lib;
  45. import java.io.IOException;
  46. import org.eclipse.jgit.util.RawParseUtils;
  47. /**
  48. * This class represents an entry in a tree, like a blob or another tree.
  49. *
  50. * @deprecated To look up information about a single path, use
  51. * {@link org.eclipse.jgit.treewalk.TreeWalk#forPath(Repository, String, org.eclipse.jgit.revwalk.RevTree)}.
  52. * To lookup information about multiple paths at once, use a
  53. * {@link org.eclipse.jgit.treewalk.TreeWalk} and obtain the current entry's
  54. * information from its getter methods.
  55. */
  56. @Deprecated
  57. public abstract class TreeEntry implements Comparable {
  58. private byte[] nameUTF8;
  59. private Tree parent;
  60. private ObjectId id;
  61. /**
  62. * Construct a named tree entry.
  63. *
  64. * @param myParent
  65. * @param myId
  66. * @param myNameUTF8
  67. */
  68. protected TreeEntry(final Tree myParent, final ObjectId myId,
  69. final byte[] myNameUTF8) {
  70. nameUTF8 = myNameUTF8;
  71. parent = myParent;
  72. id = myId;
  73. }
  74. /**
  75. * @return parent of this tree.
  76. */
  77. public Tree getParent() {
  78. return parent;
  79. }
  80. /**
  81. * Delete this entry.
  82. */
  83. public void delete() {
  84. getParent().removeEntry(this);
  85. detachParent();
  86. }
  87. /**
  88. * Detach this entry from it's parent.
  89. */
  90. public void detachParent() {
  91. parent = null;
  92. }
  93. void attachParent(final Tree p) {
  94. parent = p;
  95. }
  96. /**
  97. * @return the repository owning this entry.
  98. */
  99. public Repository getRepository() {
  100. return getParent().getRepository();
  101. }
  102. /**
  103. * @return the raw byte name of this entry.
  104. */
  105. public byte[] getNameUTF8() {
  106. return nameUTF8;
  107. }
  108. /**
  109. * @return the name of this entry.
  110. */
  111. public String getName() {
  112. if (nameUTF8 != null)
  113. return RawParseUtils.decode(nameUTF8);
  114. return null;
  115. }
  116. /**
  117. * Rename this entry.
  118. *
  119. * @param n The new name
  120. * @throws IOException
  121. */
  122. public void rename(final String n) throws IOException {
  123. rename(Constants.encode(n));
  124. }
  125. /**
  126. * Rename this entry.
  127. *
  128. * @param n The new name
  129. * @throws IOException
  130. */
  131. public void rename(final byte[] n) throws IOException {
  132. final Tree t = getParent();
  133. if (t != null) {
  134. delete();
  135. }
  136. nameUTF8 = n;
  137. if (t != null) {
  138. t.addEntry(this);
  139. }
  140. }
  141. /**
  142. * @return true if this entry is new or modified since being loaded.
  143. */
  144. public boolean isModified() {
  145. return getId() == null;
  146. }
  147. /**
  148. * Mark this entry as modified.
  149. */
  150. public void setModified() {
  151. setId(null);
  152. }
  153. /**
  154. * @return SHA-1 of this tree entry (null for new unhashed entries)
  155. */
  156. public ObjectId getId() {
  157. return id;
  158. }
  159. /**
  160. * Set (update) the SHA-1 of this entry. Invalidates the id's of all
  161. * entries above this entry as they will have to be recomputed.
  162. *
  163. * @param n SHA-1 for this entry.
  164. */
  165. public void setId(final ObjectId n) {
  166. // If we have a parent and our id is being cleared or changed then force
  167. // the parent's id to become unset as it depends on our id.
  168. //
  169. final Tree p = getParent();
  170. if (p != null && id != n) {
  171. if ((id == null && n != null) || (id != null && n == null)
  172. || !id.equals(n)) {
  173. p.setId(null);
  174. }
  175. }
  176. id = n;
  177. }
  178. /**
  179. * @return repository relative name of this entry
  180. */
  181. public String getFullName() {
  182. final StringBuilder r = new StringBuilder();
  183. appendFullName(r);
  184. return r.toString();
  185. }
  186. /**
  187. * @return repository relative name of the entry
  188. * FIXME better encoding
  189. */
  190. public byte[] getFullNameUTF8() {
  191. return getFullName().getBytes();
  192. }
  193. public int compareTo(final Object o) {
  194. if (this == o)
  195. return 0;
  196. if (o instanceof TreeEntry)
  197. return Tree.compareNames(nameUTF8, ((TreeEntry) o).nameUTF8, lastChar(this), lastChar((TreeEntry)o));
  198. return -1;
  199. }
  200. /**
  201. * Helper for accessing tree/blob methods.
  202. *
  203. * @param treeEntry
  204. * @return '/' for Tree entries and NUL for non-treeish objects.
  205. */
  206. final public static int lastChar(TreeEntry treeEntry) {
  207. if (!(treeEntry instanceof Tree))
  208. return '\0';
  209. else
  210. return '/';
  211. }
  212. /**
  213. * @return mode (type of object)
  214. */
  215. public abstract FileMode getMode();
  216. private void appendFullName(final StringBuilder r) {
  217. final TreeEntry p = getParent();
  218. final String n = getName();
  219. if (p != null) {
  220. p.appendFullName(r);
  221. if (r.length() > 0) {
  222. r.append('/');
  223. }
  224. }
  225. if (n != null) {
  226. r.append(n);
  227. }
  228. }
  229. }