Encode/decode index and tree entries using UTF-8
[egit/imyousuf.git] / org.spearce.jgit / src / org / spearce / jgit / lib / TreeEntry.java
blobc95863cc392b68f0e16421a8120750c423c44077
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;
43 import org.spearce.jgit.lib.GitIndex.Entry;
44 import org.spearce.jgit.util.RawParseUtils;
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 if (nameUTF8 != null)
130 return RawParseUtils.decode(nameUTF8);
131 return null;
135 * Rename this entry.
137 * @param n The new name
138 * @throws IOException
140 public void rename(final String n) throws IOException {
141 rename(Constants.encode(n));
145 * Rename this entry.
147 * @param n The new name
148 * @throws IOException
150 public void rename(final byte[] n) throws IOException {
151 final Tree t = getParent();
152 if (t != null) {
153 delete();
155 nameUTF8 = n;
156 if (t != null) {
157 t.addEntry(this);
162 * @return true if this entry is new or modified since being loaded.
164 public boolean isModified() {
165 return getId() == null;
169 * Mark this entry as modified.
171 public void setModified() {
172 setId(null);
176 * @return SHA-1 of this tree entry (null for new unhashed entries)
178 public ObjectId getId() {
179 return id;
183 * Set (update) the SHA-1 of this entry. Invalidates the id's of all
184 * entries above this entry as they will have to be recomputed.
186 * @param n SHA-1 for this entry.
188 public void setId(final ObjectId n) {
189 // If we have a parent and our id is being cleared or changed then force
190 // the parent's id to become unset as it depends on our id.
192 final Tree p = getParent();
193 if (p != null && id != n) {
194 if ((id == null && n != null) || (id != null && n == null)
195 || !id.equals(n)) {
196 p.setId(null);
200 id = n;
204 * @return repository relative name of this entry
206 public String getFullName() {
207 final StringBuffer r = new StringBuffer();
208 appendFullName(r);
209 return r.toString();
213 * @return repository relative name of the entry
214 * FIXME better encoding
216 public byte[] getFullNameUTF8() {
217 return getFullName().getBytes();
220 public int compareTo(final Object o) {
221 if (this == o)
222 return 0;
223 if (o instanceof TreeEntry)
224 return Tree.compareNames(nameUTF8, ((TreeEntry) o).nameUTF8, lastChar(this), lastChar((TreeEntry)o));
225 return -1;
229 * Helper for accessing tree/blob methods.
231 * @param treeEntry
232 * @return '/' for Tree entries and NUL for non-treeish objects.
234 final public static int lastChar(TreeEntry treeEntry) {
235 if (treeEntry instanceof FileTreeEntry)
236 return '\0';
237 else
238 return '/';
242 * Helper for accessing tree/blob/index methods.
244 * @param i
245 * @return '/' for Tree entries and NUL for non-treeish objects
247 final public static int lastChar(Entry i) {
248 // FIXME, gitlink etc. Currently Trees cannot appear in the
249 // index so '\0' is always returned, except maybe for submodules
250 // which we do not support yet.
251 return FileMode.TREE.equals(i.getModeBits()) ? '/' : '\0';
255 * See @{link {@link #accept(TreeVisitor, int)}.
257 * @param tv
258 * @throws IOException
260 public void accept(final TreeVisitor tv) throws IOException {
261 accept(tv, 0);
265 * Visit the members of this TreeEntry.
267 * @param tv
268 * A visitor object doing the work
269 * @param flags
270 * Specification for what members to visit. See
271 * {@link #MODIFIED_ONLY}, {@link #LOADED_ONLY},
272 * {@link #CONCURRENT_MODIFICATION}.
273 * @throws IOException
275 public abstract void accept(TreeVisitor tv, int flags) throws IOException;
278 * @return mode (type of object)
280 public abstract FileMode getMode();
282 private void appendFullName(final StringBuffer r) {
283 final TreeEntry p = getParent();
284 final String n = getName();
285 if (p != null) {
286 p.appendFullName(r);
287 if (r.length() > 0) {
288 r.append('/');
291 if (n != null) {
292 r.append(n);