Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TreeEntry.java 6.3KB

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