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

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