Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / TreeEntry.java
blob3cb6126abcd49c521003cfcdb115b619f6aeb745
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.IOException;
42 import java.io.UnsupportedEncodingException;
44 import org.spearce.jgit.lib.GitIndex.Entry;
46 /**
47 * This class represents an entry in a tree, like a blob or another tree.
49 public abstract class TreeEntry implements Comparable {
50 /**
51 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} to visit only modified entries
53 public static final int MODIFIED_ONLY = 1 << 0;
55 /**
56 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} to visit only loaded entries
58 public static final int LOADED_ONLY = 1 << 1;
60 /**
61 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} obsolete?
63 public static final int CONCURRENT_MODIFICATION = 1 << 2;
65 private byte[] nameUTF8;
67 private Tree parent;
69 private ObjectId id;
71 /**
72 * Construct a named tree entry.
74 * @param myParent
75 * @param myId
76 * @param myNameUTF8
78 protected TreeEntry(final Tree myParent, final ObjectId myId,
79 final byte[] myNameUTF8) {
80 nameUTF8 = myNameUTF8;
81 parent = myParent;
82 id = myId;
85 /**
86 * @return parent of this tree.
88 public Tree getParent() {
89 return parent;
92 /**
93 * Delete this entry.
95 public void delete() {
96 getParent().removeEntry(this);
97 detachParent();
101 * Detach this entry from it's parent.
103 public void detachParent() {
104 parent = null;
107 void attachParent(final Tree p) {
108 parent = p;
112 * @return the repository owning this entry.
114 public Repository getRepository() {
115 return getParent().getRepository();
119 * @return the raw byte name of this entry.
121 public byte[] getNameUTF8() {
122 return nameUTF8;
126 * @return the name of this entry.
128 public String getName() {
129 try {
130 return nameUTF8 != null ? new String(nameUTF8,
131 Constants.CHARACTER_ENCODING) : null;
132 } catch (UnsupportedEncodingException uee) {
133 throw new RuntimeException("JVM doesn't support "
134 + Constants.CHARACTER_ENCODING, uee);
139 * Rename this entry.
141 * @param n The new name
142 * @throws IOException
144 public void rename(final String n) throws IOException {
145 rename(n.getBytes(Constants.CHARACTER_ENCODING));
149 * Rename this entry.
151 * @param n The new name
152 * @throws IOException
154 public void rename(final byte[] n) throws IOException {
155 final Tree t = getParent();
156 if (t != null) {
157 delete();
159 nameUTF8 = n;
160 if (t != null) {
161 t.addEntry(this);
166 * @return true if this entry is new or modified since being loaded.
168 public boolean isModified() {
169 return getId() == null;
173 * Mark this entry as modified.
175 public void setModified() {
176 setId(null);
180 * @return SHA-1 of this tree entry (null for new unhashed entries)
182 public ObjectId getId() {
183 return id;
187 * Set (update) the SHA-1 of this entry. Invalidates the id's of all
188 * entries above this entry as they will have to be recomputed.
190 * @param n SHA-1 for this entry.
192 public void setId(final ObjectId n) {
193 // If we have a parent and our id is being cleared or changed then force
194 // the parent's id to become unset as it depends on our id.
196 final Tree p = getParent();
197 if (p != null && id != n) {
198 if ((id == null && n != null) || (id != null && n == null)
199 || !id.equals(n)) {
200 p.setId(null);
204 id = n;
208 * @return repository relative name of this entry
210 public String getFullName() {
211 final StringBuffer r = new StringBuffer();
212 appendFullName(r);
213 return r.toString();
217 * @return repository relative name of the entry
218 * FIXME better encoding
220 public byte[] getFullNameUTF8() {
221 return getFullName().getBytes();
224 public int compareTo(final Object o) {
225 if (this == o)
226 return 0;
227 if (o instanceof TreeEntry)
228 return Tree.compareNames(nameUTF8, ((TreeEntry) o).nameUTF8, lastChar(this), lastChar((TreeEntry)o));
229 return -1;
233 * Helper for accessing tree/blob methods.
235 * @param treeEntry
236 * @return '/' for Tree entries and NUL for non-treeish objects.
238 final public static int lastChar(TreeEntry treeEntry) {
239 if (treeEntry instanceof FileTreeEntry)
240 return '\0';
241 else
242 return '/';
246 * Helper for accessing tree/blob/index methods.
248 * @param i
249 * @return '/' for Tre entries and NUL for non-treeish objects
251 final public static int lastChar(Entry i) {
252 // FIXME, gitlink etc. Currently Trees cannot appear in the
253 // index so '\0' is always returned, except maybe for submodules
254 // which we do not support yet.
255 return FileMode.TREE.equals(i.getModeBits()) ? '/' : '\0';
259 * See @{link {@link #accept(TreeVisitor, int)}.
261 * @param tv
262 * @throws IOException
264 public void accept(final TreeVisitor tv) throws IOException {
265 accept(tv, 0);
269 * Visit the members of this TreeEntry.
271 * @param tv
272 * A visitor object doing the work
273 * @param flags
274 * Specification for what members to visit. See
275 * {@link #MODIFIED_ONLY}, {@link #LOADED_ONLY},
276 * {@link #CONCURRENT_MODIFICATION}.
277 * @throws IOException
279 public abstract void accept(TreeVisitor tv, int flags) throws IOException;
282 * @return mode (type of object)
284 public abstract FileMode getMode();
286 private void appendFullName(final StringBuffer r) {
287 final TreeEntry p = getParent();
288 final String n = getName();
289 if (p != null) {
290 p.appendFullName(r);
291 if (r.length() > 0) {
292 r.append('/');
295 if (n != null) {
296 r.append(n);