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