2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
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
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
;
47 * This class represents an entry in a tree, like a blob or another tree.
49 public abstract class TreeEntry
implements Comparable
{
51 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} to visit only modified entries
53 public static final int MODIFIED_ONLY
= 1 << 0;
56 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} to visit only loaded entries
58 public static final int LOADED_ONLY
= 1 << 1;
61 * a flag for {@link TreeEntry#accept(TreeVisitor, int)} obsolete?
63 public static final int CONCURRENT_MODIFICATION
= 1 << 2;
65 private byte[] nameUTF8
;
72 * Construct a named tree entry.
78 protected TreeEntry(final Tree myParent
, final ObjectId myId
,
79 final byte[] myNameUTF8
) {
80 nameUTF8
= myNameUTF8
;
86 * @return parent of this tree.
88 public Tree
getParent() {
95 public void delete() {
96 getParent().removeEntry(this);
101 * Detach this entry from it's parent.
103 public void detachParent() {
107 void attachParent(final Tree 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() {
126 * @return the name of this entry.
128 public String
getName() {
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
);
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
));
151 * @param n The new name
152 * @throws IOException
154 public void rename(final byte[] n
) throws IOException
{
155 final Tree t
= getParent();
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() {
180 * @return SHA-1 of this tree entry (null for new unhashed entries)
182 public ObjectId
getId() {
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)
208 * @return repository relative name of this entry
210 public String
getFullName() {
211 final StringBuffer r
= new StringBuffer();
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
) {
227 if (o
instanceof TreeEntry
)
228 return Tree
.compareNames(nameUTF8
, ((TreeEntry
) o
).nameUTF8
, lastChar(this), lastChar((TreeEntry
)o
));
233 * Helper for accessing tree/blob methods.
236 * @return '/' for Tree entries and NUL for non-treeish objects.
238 final public static int lastChar(TreeEntry treeEntry
) {
239 if (treeEntry
instanceof FileTreeEntry
)
246 * Helper for accessing tree/blob/index methods.
249 * @return '/' for Tree 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)}.
262 * @throws IOException
264 public void accept(final TreeVisitor tv
) throws IOException
{
269 * Visit the members of this TreeEntry.
272 * A visitor object doing the work
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();
291 if (r
.length() > 0) {