1 /* 2 * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com> 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 package org.eclipse.jgit.util; 44 45 import java.io.IOException; 46 import java.io.InputStream; 47 import java.io.PrintStream; 48 import java.text.MessageFormat; 49 import java.util.concurrent.Callable; 50 51 import org.eclipse.jgit.annotations.Nullable; 52 import org.eclipse.jgit.attributes.Attribute; 53 import org.eclipse.jgit.attributes.Attributes; 54 import org.eclipse.jgit.hooks.PrePushHook; 55 import org.eclipse.jgit.internal.JGitText; 56 import org.eclipse.jgit.lib.ObjectLoader; 57 import org.eclipse.jgit.lib.Repository; 58 import org.eclipse.jgit.revwalk.RevCommit; 59 import org.eclipse.jgit.treewalk.FileTreeIterator; 60 import org.eclipse.jgit.treewalk.TreeWalk; 61 import org.eclipse.jgit.treewalk.filter.PathFilter; 62 63 /** 64 * Represents an optionally present LFS support implementation 65 * 66 * @since 4.11 67 */ 68 public class LfsFactory { 69 70 private static LfsFactory instance = new LfsFactory(); 71 72 /** 73 * Constructor 74 */ 75 protected LfsFactory() { 76 } 77 78 /** 79 * @return the current LFS implementation 80 */ 81 public static LfsFactory getInstance() { 82 return instance; 83 } 84 85 /** 86 * @param instance 87 * register a {@link LfsFactory} instance as the 88 * {@link LfsFactory} implementation to use. 89 */ 90 public static void setInstance(LfsFactory instance) { 91 LfsFactory.instance = instance; 92 } 93 94 /** 95 * @return whether LFS support is available 96 */ 97 public boolean isAvailable() { 98 return false; 99 } 100 101 /** 102 * Apply clean filtering to the given stream, writing the file content to 103 * the LFS storage if required and returning a stream to the LFS pointer 104 * instead. 105 * 106 * @param db 107 * the repository 108 * @param input 109 * the original input 110 * @param length 111 * the expected input stream length 112 * @param attribute 113 * the attribute used to check for LFS enablement (i.e. "merge", 114 * "diff", "filter" from .gitattributes). 115 * @return a stream to the content that should be written to the object 116 * store along with the expected length of the stream. the original 117 * stream is not applicable. 118 * @throws IOException 119 * in case of an error 120 */ 121 public LfsInputStream applyCleanFilter(Repository db, 122 InputStream input, long length, Attribute attribute) 123 throws IOException { 124 return new LfsInputStream(input, length); 125 } 126 127 /** 128 * Apply smudge filtering to a given loader, potentially redirecting it to a 129 * LFS blob which is downloaded on demand. 130 * 131 * @param db 132 * the repository 133 * @param loader 134 * the loader for the blob 135 * @param attribute 136 * the attribute used to check for LFS enablement (i.e. "merge", 137 * "diff", "filter" from .gitattributes). 138 * @return a loader for the actual data of a blob, or the original loader in 139 * case LFS is not applicable. 140 * @throws IOException 141 */ 142 public ObjectLoader applySmudgeFilter(Repository db, 143 ObjectLoader loader, Attribute attribute) throws IOException { 144 return loader; 145 } 146 147 /** 148 * Retrieve a pre-push hook to be applied. 149 * 150 * @param repo 151 * the {@link Repository} the hook is applied to. 152 * @param outputStream 153 * @return a {@link PrePushHook} implementation or <code>null</code> 154 */ 155 public @Nullable PrePushHook getPrePushHook(Repository repo, 156 PrintStream outputStream) { 157 return null; 158 } 159 160 /** 161 * Retrieve an {@link LfsInstallCommand} which can be used to enable LFS 162 * support (if available) either per repository or for the user. 163 * 164 * @return a command to install LFS support. 165 */ 166 public @Nullable LfsInstallCommand getInstallCommand() { 167 return null; 168 } 169 170 /** 171 * @param db 172 * the repository to check 173 * @return whether LFS is enabled for the given repository locally or 174 * globally. 175 */ 176 public boolean isEnabled(Repository db) { 177 return false; 178 } 179 180 /** 181 * @param db 182 * the repository 183 * @param path 184 * the path to find attributes for 185 * @return the {@link Attributes} for the given path. 186 * @throws IOException 187 * in case of an error 188 */ 189 public static Attributes getAttributesForPath(Repository db, String path) 190 throws IOException { 191 try (TreeWalk walk = new TreeWalk(db)) { 192 walk.addTree(new FileTreeIterator(db)); 193 PathFilter f = PathFilter.create(path); 194 walk.setFilter(f); 195 walk.setRecursive(false); 196 Attributes attr = null; 197 while (walk.next()) { 198 if (f.isDone(walk)) { 199 attr = walk.getAttributes(); 200 break; 201 } else if (walk.isSubtree()) { 202 walk.enterSubtree(); 203 } 204 } 205 if (attr == null) { 206 throw new IOException(MessageFormat 207 .format(JGitText.get().noPathAttributesFound, path)); 208 } 209 210 return attr; 211 } 212 } 213 214 /** 215 * Get attributes for given path and commit 216 * 217 * @param db 218 * the repository 219 * @param path 220 * the path to find attributes for 221 * @param commit 222 * the commit to inspect. 223 * @return the {@link Attributes} for the given path. 224 * @throws IOException 225 * in case of an error 226 */ 227 public static Attributes getAttributesForPath(Repository db, String path, 228 RevCommit commit) throws IOException { 229 if (commit == null) { 230 return getAttributesForPath(db, path); 231 } 232 233 try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) { 234 Attributes attr = walk == null ? null : walk.getAttributes(); 235 if (attr == null) { 236 throw new IOException(MessageFormat 237 .format(JGitText.get().noPathAttributesFound, path)); 238 } 239 240 return attr; 241 } 242 } 243 244 /** 245 * Encapsulate a potentially exchanged {@link InputStream} along with the 246 * expected stream content length. 247 */ 248 public static final class LfsInputStream extends InputStream { 249 /** 250 * The actual stream. 251 */ 252 private InputStream stream; 253 254 /** 255 * The expected stream content length. 256 */ 257 private long length; 258 259 /** 260 * Create a new wrapper around a certain stream 261 * 262 * @param stream 263 * the stream to wrap. the stream will be closed on 264 * {@link #close()}. 265 * @param length 266 * the expected length of the stream 267 */ 268 public LfsInputStream(InputStream stream, long length) { 269 this.stream = stream; 270 this.length = length; 271 } 272 273 /** 274 * Create a new wrapper around a temporary buffer. 275 * 276 * @param buffer 277 * the buffer to initialize stream and length from. The 278 * buffer will be destroyed on {@link #close()} 279 * @throws IOException 280 * in case of an error opening the stream to the buffer. 281 */ 282 public LfsInputStream(TemporaryBuffer buffer) throws IOException { 283 this.stream = buffer.openInputStreamWithAutoDestroy(); 284 this.length = buffer.length(); 285 } 286 287 @Override 288 public void close() throws IOException { 289 stream.close(); 290 } 291 292 @Override 293 public int read() throws IOException { 294 return stream.read(); 295 } 296 297 /** 298 * @return the length of the stream 299 */ 300 public long getLength() { 301 return length; 302 } 303 } 304 305 /** 306 * A command to enable LFS. Optionally set a {@link Repository} to enable 307 * locally on the repository only. 308 */ 309 public interface LfsInstallCommand extends Callable<Void> { 310 /** 311 * @param repo 312 * the repository to enable support for. 313 * @return The {@link LfsInstallCommand} for chaining. 314 */ 315 public LfsInstallCommand setRepository(Repository repo); 316 } 317 318 }