1 /* 2 * Copyright (C) 2017, Google Inc. 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.internal.storage.reftable; 45 46 import static org.eclipse.jgit.lib.RefDatabase.MAX_SYMBOLIC_REF_DEPTH; 47 48 import java.io.ByteArrayOutputStream; 49 import java.io.IOException; 50 import java.util.Collection; 51 52 import org.eclipse.jgit.annotations.Nullable; 53 import org.eclipse.jgit.internal.storage.io.BlockSource; 54 import org.eclipse.jgit.lib.AnyObjectId; 55 import org.eclipse.jgit.lib.Ref; 56 import org.eclipse.jgit.lib.SymbolicRef; 57 58 /** Abstract table of references. */ 59 public abstract class Reftable implements AutoCloseable { 60 /** 61 * @param refs 62 * references to convert into a reftable; may be empty. 63 * @return a reader for the supplied references. 64 */ 65 public static Reftable from(Collection<Ref> refs) { 66 try { 67 ReftableConfig cfg = new ReftableConfig(); 68 cfg.setIndexObjects(false); 69 cfg.setAlignBlocks(false); 70 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 71 new ReftableWriter() 72 .setConfig(cfg) 73 .begin(buf) 74 .sortAndWriteRefs(refs) 75 .finish(); 76 return new ReftableReader(BlockSource.from(buf.toByteArray())); 77 } catch (IOException e) { 78 throw new RuntimeException(e); 79 } 80 } 81 82 /** {@code true} if deletions should be included in results. */ 83 protected boolean includeDeletes; 84 85 /** 86 * @param deletes 87 * if {@code true} deleted references will be returned. If 88 * {@code false} (default behavior), deleted references will be 89 * skipped, and not returned. 90 */ 91 public void setIncludeDeletes(boolean deletes) { 92 includeDeletes = deletes; 93 } 94 95 /** 96 * Seek to the first reference, to iterate in order. 97 * 98 * @return cursor to iterate. 99 * @throws IOException 100 * if references cannot be read. 101 */ 102 public abstract RefCursor allRefs() throws IOException; 103 104 /** 105 * Seek either to a reference, or a reference subtree. 106 * <p> 107 * If {@code refName} ends with {@code "/"} the method will seek to the 108 * subtree of all references starting with {@code refName} as a prefix. If 109 * no references start with this prefix, an empty cursor is returned. 110 * <p> 111 * Otherwise exactly {@code refName} will be looked for. If present, the 112 * returned cursor will iterate exactly one entry. If not found, an empty 113 * cursor is returned. 114 * 115 * @param refName 116 * reference name or subtree to find. 117 * @return cursor to iterate; empty cursor if no references match. 118 * @throws IOException 119 * if references cannot be read. 120 */ 121 public abstract RefCursor seekRef(String refName) throws IOException; 122 123 /** 124 * Match references pointing to a specific object. 125 * 126 * @param id 127 * object to find. 128 * @return cursor to iterate; empty cursor if no references match. 129 * @throws IOException 130 * if references cannot be read. 131 */ 132 public abstract RefCursor byObjectId(AnyObjectId id) throws IOException; 133 134 /** 135 * Seek reader to read log records. 136 * 137 * @return cursor to iterate; empty cursor if no logs are present. 138 * @throws IOException 139 * if logs cannot be read. 140 */ 141 public abstract LogCursor allLogs() throws IOException; 142 143 /** 144 * Read a single reference's log. 145 * 146 * @param refName 147 * exact name of the reference whose log to read. 148 * @return cursor to iterate; empty cursor if no logs match. 149 * @throws IOException 150 * if logs cannot be read. 151 */ 152 public LogCursor seekLog(String refName) throws IOException { 153 return seekLog(refName, Long.MAX_VALUE); 154 } 155 156 /** 157 * Seek to an update index in a reference's log. 158 * 159 * @param refName 160 * exact name of the reference whose log to read. 161 * @param updateIndex 162 * most recent index to return first in the log cursor. Log 163 * records at or before {@code updateIndex} will be returned. 164 * @return cursor to iterate; empty cursor if no logs match. 165 * @throws IOException 166 * if logs cannot be read. 167 */ 168 public abstract LogCursor seekLog(String refName, long updateIndex) 169 throws IOException; 170 171 /** 172 * Lookup a reference, or null if not found. 173 * 174 * @param refName 175 * reference name to find. 176 * @return the reference, or {@code null} if not found. 177 * @throws IOException 178 * if references cannot be read. 179 */ 180 @Nullable 181 public Ref exactRef(String refName) throws IOException { 182 try (RefCursor rc = seekRef(refName)) { 183 return rc.next() ? rc.getRef() : null; 184 } 185 } 186 187 /** 188 * Test if a reference or reference subtree exists. 189 * <p> 190 * If {@code refName} ends with {@code "/"}, the method tests if any 191 * reference starts with {@code refName} as a prefix. 192 * <p> 193 * Otherwise, the method checks if {@code refName} exists. 194 * 195 * @param refName 196 * reference name or subtree to find. 197 * @return {@code true} if the reference exists, or at least one reference 198 * exists in the subtree. 199 * @throws IOException 200 * if references cannot be read. 201 */ 202 public boolean hasRef(String refName) throws IOException { 203 try (RefCursor rc = seekRef(refName)) { 204 return rc.next(); 205 } 206 } 207 208 /** 209 * Test if any reference directly refers to the object. 210 * 211 * @param id 212 * ObjectId to find. 213 * @return {@code true} if any reference exists directly referencing 214 * {@code id}, or a annotated tag that peels to {@code id}. 215 * @throws IOException 216 * if references cannot be read. 217 */ 218 public boolean hasId(AnyObjectId id) throws IOException { 219 try (RefCursor rc = byObjectId(id)) { 220 return rc.next(); 221 } 222 } 223 224 /** 225 * Resolve a symbolic reference to populate its value. 226 * 227 * @param symref 228 * reference to resolve. 229 * @return resolved {@code symref}, or {@code null}. 230 * @throws IOException 231 * if references cannot be read. 232 */ 233 @Nullable 234 public Ref resolve(Ref symref) throws IOException { 235 return resolve(symref, 0); 236 } 237 238 private Ref resolve(Ref ref, int depth) throws IOException { 239 if (!ref.isSymbolic()) { 240 return ref; 241 } 242 243 Ref dst = ref.getTarget(); 244 if (MAX_SYMBOLIC_REF_DEPTH <= depth) { 245 return null; // claim it doesn't exist 246 } 247 248 dst = exactRef(dst.getName()); 249 if (dst == null) { 250 return ref; 251 } 252 253 dst = resolve(dst, depth + 1); 254 if (dst == null) { 255 return null; // claim it doesn't exist 256 } 257 return new SymbolicRef(ref.getName(), dst); 258 } 259 260 @Override 261 public abstract void close() throws IOException; 262 }