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

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